1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2007 - 2009 Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.libraries.base.boot;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.base.config.ExtendedConfiguration;
import org.pentaho.reporting.libraries.base.config.ExtendedConfigurationWrapper;
import org.pentaho.reporting.libraries.base.config.HierarchicalConfiguration;
import org.pentaho.reporting.libraries.base.config.PropertyFileConfiguration;
import org.pentaho.reporting.libraries.base.config.SystemPropertyConfiguration;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
import org.pentaho.reporting.libraries.base.versioning.DependencyInformation;
import org.pentaho.reporting.libraries.base.versioning.ProjectInformation;
/**
* The common base for all Boot classes.
* <p/>
* This initializes the subsystem and all dependent subsystems. Implementors of this class have to provide a public
* static getInstance() method which returns a singleton instance of the booter implementation.
* <p/>
* Further creation of Boot object should be prevented using protected or private constructors in that class, or proper
* singleton behaviour cannot be guaranteed.
*
* @author Thomas Morgner
*/
public abstract class AbstractBoot implements SubSystem
{
/**
* The logger for this class.
*/
private static final Log LOGGER = LogFactory.getLog(AbstractBoot.class);
/**
* The configuration wrapper around the plain configuration.
*/
private ExtendedConfigurationWrapper extWrapper;
/**
* A packageManager instance of the package manager.
*/
private PackageManager packageManager;
/**
* Global configuration.
*/
private Configuration globalConfig;
/**
* A flag indicating whether the booting is currenly in progress.
*/
private boolean bootInProgress;
/**
* A flag indicating whether the booting is complete.
*/
private boolean bootDone;
/**
* The reason why booting failed for easier debugging or logging.
*/
private Exception bootFailed;
/**
* Default constructor.
*/
protected AbstractBoot()
{
}
/**
* Returns the packageManager instance of the package manager.
*
* @return The package manager.
*/
public synchronized PackageManager getPackageManager()
{
if (this.packageManager == null)
{
this.packageManager = new PackageManager(this);
}
return this.packageManager;
}
/**
* Returns the global configuration.
*
* @return The global configuration.
*/
public synchronized Configuration getGlobalConfig()
{
if (this.globalConfig == null)
{
this.globalConfig = loadConfiguration();
}
return this.globalConfig;
}
/**
* Checks, whether the booting is in progress.
*
* @return true, if the booting is in progress, false otherwise.
*/
public final synchronized boolean isBootInProgress()
{
return this.bootInProgress;
}
/**
* Checks, whether the booting is complete.
*
* @return true, if the booting is complete, false otherwise.
*/
public final synchronized boolean isBootDone()
{
return this.bootDone;
}
/**
* Loads the configuration. This will be called exactly once.
*
* @return The configuration.
*/
protected abstract Configuration loadConfiguration();
/**
* Starts the boot process. The boot process is synchronized and will block if parallel booting is not finished yet.
* Any failure in booting will set the <code>bootFailed</code> property to true. If booting is finished, the
* <code>bootDone</code> property is set to true.
*/
public final void start()
{
synchronized (this)
{
if (isBootDone())
{
return;
}
if (isBootFailed())
{
LOGGER.error(getClass() + " failed to boot: " + bootFailed.getMessage());
}
while (isBootInProgress())
{
try
{
wait();
}
catch (InterruptedException e)
{
// ignore ..
}
}
if (isBootDone())
{
notifyAll();
return;
}
this.bootInProgress = true;
}
try
{
// boot dependent libraries ...
final ProjectInformation info = getProjectInfo();
if (info != null)
{
performBootDependencies(info.getLibraries());
performBootDependencies(info.getOptionalLibraries());
}
performBoot();
if (info != null)
{
LOGGER.info(info.getName() + ' ' + info.getVersion() + " started.");
}
else
{
LOGGER.info(getClass() + " started.");
}
}
catch (Exception e)
{
LOGGER.error(getClass() + " failed to boot: ", e);
this.bootFailed = e;
}
finally
{
synchronized (this)
{
this.bootInProgress = false;
this.bootDone = true;
notifyAll();
}
}
}
/**
* Boots all dependent libraries. The dependencies must be initialized properly before the booting of this
* library or application can start. If any of the dependencies fails to initialize properly, the whole
* boot-process will be aborted.
*
* @param childs the array of dependencies, never null.
*/
private void performBootDependencies(final DependencyInformation[] childs)
{
if (childs == null)
{
return;
}
for (int i = 0; i < childs.length; i++)
{
final DependencyInformation child = childs[i];
if (child instanceof ProjectInformation == false)
{
continue;
}
final ProjectInformation projectInformation = (ProjectInformation) child;
final AbstractBoot boot = loadBooter(projectInformation.getBootClass());
if (boot != null)
{
// but we're waiting until the booting is complete ...
synchronized (boot)
{
boot.start();
while (boot.isBootDone() == false &&
boot.isBootFailed() == false)
{
try
{
boot.wait();
}
catch (InterruptedException e)
{
// ignore it ..
}
}
if (boot.isBootFailed())
{
this.bootFailed = boot.getBootFailureReason();
LOGGER.error("Dependent project failed to boot up: " +
projectInformation.getBootClass() + " failed to boot: ", this.bootFailed);
return;
}
}
}
}
}
/**
* Checks whether the booting failed. If booting failed, the reason for the failure (the Exception that caused
* the error) is stored as property <code>bootFailureReason</code>.
*
* @return true, if booting failed, false otherwise.
*/
public boolean isBootFailed()
{
return this.bootFailed != null;
}
/**
* Returns the failure reason for the boot process. This method returns null, if booting was successful.
*
* @return the failure reason.
*/
public Exception getBootFailureReason()
{
return bootFailed;
}
/**
* Performs the boot.
*/
protected abstract void performBoot();
/**
* Returns the project info.
*
* @return The project info.
*/
protected abstract ProjectInformation getProjectInfo();
/**
* Loads the specified booter implementation.
*
* @param classname the class name.
* @return The boot class.
*/
protected AbstractBoot loadBooter(final String classname)
{
return loadBooter(classname, getClass());
}
/**
* Loads the specified booter-class.
*
* @param classname the classname of the booter class.
* @param source the source-class from where to get the classloader.
* @return the instantiated booter or null, if no booter could be loaded.
*/
public static AbstractBoot loadBooter(final String classname, final Class source)
{
if (classname == null)
{
return null;
}
if (source == null)
{
throw new NullPointerException();
}
try
{
final ClassLoader loader = ObjectUtilities.getClassLoader(source);
final Class c = Class.forName(classname, false, loader);
final Method m = c.getMethod("getInstance", (Class[]) null);
return (AbstractBoot) m.invoke(null, (Object[]) null);
}
catch (Exception e)
{
LOGGER.info("Unable to boot dependent class: " + classname);
return null;
}
}
/**
* Creates a default configuration setup, which loads its settings from the static configuration (defaults provided by
* the developers of the library) and the user configuration (settings provided by the deployer). The deployer's
* settings override the developer's settings.
* <p/>
* If the parameter <code>addSysProps</code> is set to true, the system properties will be added as third
* configuration layer. The system properties configuration allows to override all other settings.
*
* @param staticConfig the resource name of the developers configuration
* @param userConfig the resource name of the deployers configuration
* @param addSysProps a flag defining whether to include the system properties into the configuration.
* @param source the classloader source to load resources from.
* @return the configured Configuration instance.
*/
protected Configuration createDefaultHierarchicalConfiguration
(final String staticConfig,
final String userConfig,
final boolean addSysProps,
final Class source)
{
if (source == null)
{
throw new NullPointerException("SourceClass must not be null.");
}
final HierarchicalConfiguration globalConfig = new HierarchicalConfiguration(getClass());
if (staticConfig != null)
{
final PropertyFileConfiguration rootProperty = new PropertyFileConfiguration();
rootProperty.load(staticConfig, source);
globalConfig.insertConfiguration(rootProperty);
globalConfig.insertConfiguration(getPackageManager().getPackageConfiguration());
}
if (userConfig != null)
{
final String userConfigStripped;
if (userConfig.charAt(0) == '/')
{
userConfigStripped = userConfig.substring(1);
}
else
{
userConfigStripped = userConfig;
}
try
{
final Enumeration userConfigs = ObjectUtilities.getClassLoader(source).getResources(userConfigStripped);
final ArrayList configs = new ArrayList();
while (userConfigs.hasMoreElements())
{
final URL url = (URL) userConfigs.nextElement();
try
{
final PropertyFileConfiguration baseProperty = new PropertyFileConfiguration();
final InputStream in = url.openStream();
try
{
baseProperty.load(in);
}
finally
{
in.close();
}
configs.add(baseProperty);
}
catch (IOException ioe)
{
LOGGER.warn("Failed to load the user configuration at " + url, ioe);
}
}
for (int i = configs.size() - 1; i >= 0; i--)
{
final PropertyFileConfiguration baseProperty = (PropertyFileConfiguration) configs.get(i);
globalConfig.insertConfiguration(baseProperty);
}
}
catch (IOException e)
{
LOGGER.warn("Failed to lookup the user configurations.", e);
}
}
if (addSysProps)
{
final SystemPropertyConfiguration systemConfig = new SystemPropertyConfiguration();
globalConfig.insertConfiguration(systemConfig);
}
return globalConfig;
}
/**
* Returns the global configuration as extended configuration.
*
* @return the extended configuration.
*/
public synchronized ExtendedConfiguration getExtendedConfig()
{
if (extWrapper == null)
{
extWrapper = new ExtendedConfigurationWrapper(getGlobalConfig());
}
return extWrapper;
}
}
|