AbstractBoot.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -----------------
  28. * AbstractBoot.java
  29. * -----------------
  30. * (C)opyright 2004, 2005, by Thomas Morgner and Contributors.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: AbstractBoot.java,v 1.21 2008/09/10 09:22:57 mungady Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 07-Jun-2004 : Added source headers (DG);
  40. * 18-Aug-2005 : Added casts to suppress compiler warnings, as suggested in
  41. * patch 1260622 (DG);
  42. *
  43. */
  44. package org.jfree.base;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.lang.reflect.Method;
  48. import java.net.URL;
  49. import java.util.ArrayList;
  50. import java.util.Enumeration;
  51. import org.jfree.base.config.HierarchicalConfiguration;
  52. import org.jfree.base.config.PropertyFileConfiguration;
  53. import org.jfree.base.config.SystemPropertyConfiguration;
  54. import org.jfree.base.modules.PackageManager;
  55. import org.jfree.base.modules.SubSystem;
  56. import org.jfree.util.Configuration;
  57. import org.jfree.util.ExtendedConfiguration;
  58. import org.jfree.util.ExtendedConfigurationWrapper;
  59. import org.jfree.util.Log;
  60. import org.jfree.util.ObjectUtilities;
  61. /**
  62. * The common base for all Boot classes.
  63. * <p>
  64. * This initializes the subsystem and all dependent subsystems.
  65. * Implementors of this class have to provide a public static
  66. * getInstance() method which returns a singleton instance of the
  67. * booter implementation.
  68. * <p>
  69. * Further creation of Boot object should be prevented using
  70. * protected or private constructors in that class, or proper
  71. * initialzation cannot be guaranteed.
  72. *
  73. * @author Thomas Morgner
  74. */
  75. public abstract class AbstractBoot implements SubSystem {
  76. /** The configuration wrapper around the plain configuration. */
  77. private ExtendedConfigurationWrapper extWrapper;
  78. /** A packageManager instance of the package manager. */
  79. private PackageManager packageManager;
  80. /** Global configuration. */
  81. private Configuration globalConfig;
  82. /** A flag indicating whether the booting is currenly in progress. */
  83. private boolean bootInProgress;
  84. /** A flag indicating whether the booting is complete. */
  85. private boolean bootDone;
  86. /**
  87. * Default constructor.
  88. */
  89. protected AbstractBoot() {
  90. }
  91. /**
  92. * Returns the packageManager instance of the package manager.
  93. *
  94. * @return The package manager.
  95. */
  96. public synchronized PackageManager getPackageManager() {
  97. if (this.packageManager == null) {
  98. this.packageManager = PackageManager.createInstance(this);
  99. }
  100. return this.packageManager;
  101. }
  102. /**
  103. * Returns the global configuration.
  104. *
  105. * @return The global configuration.
  106. */
  107. public synchronized Configuration getGlobalConfig() {
  108. if (this.globalConfig == null) {
  109. this.globalConfig = loadConfiguration();
  110. }
  111. return this.globalConfig;
  112. }
  113. /**
  114. * Checks, whether the booting is in progress.
  115. *
  116. * @return true, if the booting is in progress, false otherwise.
  117. */
  118. public final synchronized boolean isBootInProgress() {
  119. return this.bootInProgress;
  120. }
  121. /**
  122. * Checks, whether the booting is complete.
  123. *
  124. * @return true, if the booting is complete, false otherwise.
  125. */
  126. public final synchronized boolean isBootDone() {
  127. return this.bootDone;
  128. }
  129. /**
  130. * Loads the configuration. This will be called exactly once.
  131. *
  132. * @return The configuration.
  133. */
  134. protected abstract Configuration loadConfiguration();
  135. /**
  136. * Starts the boot process.
  137. */
  138. public final void start() {
  139. synchronized (this) {
  140. if (isBootDone()) {
  141. return;
  142. }
  143. while (isBootInProgress()) {
  144. try {
  145. wait();
  146. }
  147. catch (InterruptedException e) {
  148. // ignore ..
  149. }
  150. }
  151. if (isBootDone()) {
  152. return;
  153. }
  154. this.bootInProgress = true;
  155. }
  156. // boot dependent libraries ...
  157. final BootableProjectInfo info = getProjectInfo();
  158. if (info != null) {
  159. final BootableProjectInfo[] childs = info.getDependencies();
  160. for (int i = 0; i < childs.length; i++) {
  161. final AbstractBoot boot = loadBooter(childs[i].getBootClass());
  162. if (boot != null) {
  163. // but we're waiting until the booting is complete ...
  164. synchronized(boot) {
  165. boot.start();
  166. while (boot.isBootDone() == false) {
  167. try {
  168. boot.wait();
  169. }
  170. catch (InterruptedException e) {
  171. // ignore it ..
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. performBoot();
  179. if (info != null)
  180. {
  181. Log.info (info.getName() + " " + info.getVersion() + " started.");
  182. }
  183. else
  184. {
  185. Log.info (getClass() + " started.");
  186. }
  187. synchronized (this) {
  188. this.bootInProgress = false;
  189. this.bootDone = true;
  190. notifyAll();
  191. }
  192. }
  193. /**
  194. * Performs the boot.
  195. */
  196. protected abstract void performBoot();
  197. /**
  198. * Returns the project info.
  199. *
  200. * @return The project info.
  201. */
  202. protected abstract BootableProjectInfo getProjectInfo();
  203. /**
  204. * Loads the specified booter implementation.
  205. *
  206. * @param classname the class name.
  207. *
  208. * @return The boot class.
  209. */
  210. protected AbstractBoot loadBooter(final String classname) {
  211. if (classname == null) {
  212. return null;
  213. }
  214. try {
  215. final Class c = ObjectUtilities.getClassLoader(
  216. getClass()).loadClass(classname);
  217. final Method m = c.getMethod("getInstance", (Class[]) null);
  218. return (AbstractBoot) m.invoke(null, (Object[]) null);
  219. }
  220. catch (Exception e) {
  221. Log.info ("Unable to boot dependent class: " + classname);
  222. return null;
  223. }
  224. }
  225. /**
  226. * Creates a default configuration setup, which loads its settings from
  227. * the static configuration (defaults provided by the developers of the
  228. * library) and the user configuration (settings provided by the deployer).
  229. * The deployer's settings override the developer's settings.
  230. *
  231. * If the parameter <code>addSysProps</code> is set to true, the system
  232. * properties will be added as third configuration layer. The system
  233. * properties configuration allows to override all other settings.
  234. *
  235. * @param staticConfig the resource name of the developers configuration
  236. * @param userConfig the resource name of the deployers configuration
  237. * @param addSysProps a flag defining whether to include the system
  238. * properties into the configuration.
  239. * @return the configured Configuration instance.
  240. */
  241. protected Configuration createDefaultHierarchicalConfiguration
  242. (final String staticConfig, final String userConfig,
  243. final boolean addSysProps)
  244. {
  245. return createDefaultHierarchicalConfiguration
  246. (staticConfig, userConfig, addSysProps, PropertyFileConfiguration.class);
  247. }
  248. /**
  249. * Creates a default hierarchical configuration.
  250. *
  251. * @param staticConfig the static configuration.
  252. * @param userConfig the user configuration.
  253. * @param addSysProps additional system properties.
  254. * @param source the source.
  255. *
  256. * @return The configuration.
  257. */
  258. protected Configuration createDefaultHierarchicalConfiguration
  259. (final String staticConfig, final String userConfig,
  260. final boolean addSysProps, final Class source)
  261. {
  262. final HierarchicalConfiguration globalConfig
  263. = new HierarchicalConfiguration();
  264. if (staticConfig != null) {
  265. final PropertyFileConfiguration rootProperty
  266. = new PropertyFileConfiguration();
  267. rootProperty.load(staticConfig, getClass());
  268. globalConfig.insertConfiguration(rootProperty);
  269. globalConfig.insertConfiguration(
  270. getPackageManager().getPackageConfiguration());
  271. }
  272. if (userConfig != null) {
  273. String userConfigStripped;
  274. if (userConfig.startsWith("/")) {
  275. userConfigStripped = userConfig.substring(1);
  276. }
  277. else {
  278. userConfigStripped = userConfig;
  279. }
  280. try {
  281. final Enumeration userConfigs = ObjectUtilities.getClassLoader
  282. (getClass()).getResources(userConfigStripped);
  283. final ArrayList configs = new ArrayList();
  284. while (userConfigs.hasMoreElements()) {
  285. final URL url = (URL) userConfigs.nextElement();
  286. try {
  287. final PropertyFileConfiguration baseProperty =
  288. new PropertyFileConfiguration();
  289. final InputStream in = url.openStream();
  290. baseProperty.load(in);
  291. in.close();
  292. configs.add(baseProperty);
  293. }
  294. catch(IOException ioe) {
  295. Log.warn ("Failed to load the user configuration at " + url, ioe);
  296. }
  297. }
  298. for (int i = configs.size() - 1; i >= 0; i--) {
  299. final PropertyFileConfiguration baseProperty =
  300. (PropertyFileConfiguration) configs.get(i);
  301. globalConfig.insertConfiguration(baseProperty);
  302. }
  303. }
  304. catch (IOException e) {
  305. Log.warn ("Failed to lookup the user configurations.", e);
  306. }
  307. }
  308. if (addSysProps) {
  309. final SystemPropertyConfiguration systemConfig
  310. = new SystemPropertyConfiguration();
  311. globalConfig.insertConfiguration(systemConfig);
  312. }
  313. return globalConfig;
  314. }
  315. /**
  316. * Returns the global configuration as extended configuration.
  317. *
  318. * @return the extended configuration.
  319. */
  320. public synchronized ExtendedConfiguration getExtendedConfig ()
  321. {
  322. if (this.extWrapper == null) {
  323. this.extWrapper = new ExtendedConfigurationWrapper(getGlobalConfig());
  324. }
  325. return this.extWrapper;
  326. }
  327. }