KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > base > AbstractBoot


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.19 2006/12/11 12:02:27 taqua 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
45 package org.jfree.base;
46
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.lang.reflect.Method JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Enumeration JavaDoc;
53
54 import org.jfree.base.config.HierarchicalConfiguration;
55 import org.jfree.base.config.PropertyFileConfiguration;
56 import org.jfree.base.config.SystemPropertyConfiguration;
57 import org.jfree.base.modules.PackageManager;
58 import org.jfree.base.modules.SubSystem;
59 import org.jfree.util.Configuration;
60 import org.jfree.util.ExtendedConfiguration;
61 import org.jfree.util.ExtendedConfigurationWrapper;
62 import org.jfree.util.Log;
63 import org.jfree.util.ObjectUtilities;
64
65 /**
66  * The common base for all Boot classes.
67  * <p>
68  * This initializes the subsystem and all dependent subsystems.
69  * Implementors of this class have to provide a public static
70  * getInstance() method which returns a singleton instance of the
71  * booter implementation.
72  * <p>
73  * Further creation of Boot object should be prevented using
74  * protected or private constructors in that class, or proper
75  * initialzation cannot be guaranteed.
76  *
77  * @author Thomas Morgner
78  */

79 public abstract class AbstractBoot implements SubSystem {
80
81     /** The configuration wrapper around the plain configuration. */
82     private ExtendedConfigurationWrapper extWrapper;
83
84     /** A packageManager instance of the package manager. */
85     private PackageManager packageManager;
86
87     /** Global configuration. */
88     private Configuration globalConfig;
89
90     /** A flag indicating whether the booting is currenly in progress. */
91     private boolean bootInProgress;
92
93     /** A flag indicating whether the booting is complete. */
94     private boolean bootDone;
95
96     /**
97      * Default constructor.
98      */

99     protected AbstractBoot() {
100     }
101
102     /**
103      * Returns the packageManager instance of the package manager.
104      *
105      * @return The package manager.
106      */

107     public synchronized PackageManager getPackageManager() {
108         if (this.packageManager == null) {
109             this.packageManager = PackageManager.createInstance(this);
110         }
111         return this.packageManager;
112     }
113
114     /**
115      * Returns the global configuration.
116      *
117      * @return The global configuration.
118      */

119     public synchronized Configuration getGlobalConfig() {
120         if (this.globalConfig == null) {
121             this.globalConfig = loadConfiguration();
122         }
123         return this.globalConfig;
124     }
125
126     /**
127      * Checks, whether the booting is in progress.
128      *
129      * @return true, if the booting is in progress, false otherwise.
130      */

131     public final synchronized boolean isBootInProgress() {
132         return this.bootInProgress;
133     }
134
135     /**
136      * Checks, whether the booting is complete.
137      *
138      * @return true, if the booting is complete, false otherwise.
139      */

140     public final synchronized boolean isBootDone() {
141         return this.bootDone;
142     }
143
144     /**
145      * Loads the configuration. This will be called exactly once.
146      *
147      * @return The configuration.
148      */

149     protected abstract Configuration loadConfiguration();
150
151     /**
152      * Starts the boot process.
153      */

154     public final void start() {
155
156         synchronized (this) {
157             if (isBootDone()) {
158                 return;
159             }
160             while (isBootInProgress()) {
161               try {
162                 wait();
163               }
164               catch (InterruptedException JavaDoc e) {
165                 // ignore ..
166
}
167             }
168             if (isBootDone()) {
169                 return;
170             }
171             this.bootInProgress = true;
172         }
173
174         // boot dependent libraries ...
175
final BootableProjectInfo info = getProjectInfo();
176         if (info != null) {
177             final BootableProjectInfo[] childs = info.getDependencies();
178             for (int i = 0; i < childs.length; i++) {
179                 final AbstractBoot boot = loadBooter(childs[i].getBootClass());
180                 if (boot != null) {
181                     // but we're waiting until the booting is complete ...
182
synchronized(boot) {
183                       boot.start();
184                       while (boot.isBootDone() == false) {
185                         try {
186                           boot.wait();
187                         }
188                         catch (InterruptedException JavaDoc e) {
189                           // ignore it ..
190
}
191                       }
192                     }
193                 }
194             }
195         }
196
197         performBoot();
198         if (info != null)
199         {
200           Log.info (info.getName() + " " + info.getVersion() + " started.");
201         }
202         else
203         {
204           Log.info (getClass() + " started.");
205         }
206
207         synchronized (this) {
208             this.bootInProgress = false;
209             this.bootDone = true;
210             notifyAll();
211         }
212     }
213
214     /**
215      * Performs the boot.
216      */

217     protected abstract void performBoot();
218
219     /**
220      * Returns the project info.
221      *
222      * @return The project info.
223      */

224     protected abstract BootableProjectInfo getProjectInfo();
225
226     /**
227      * Loads the specified booter implementation.
228      *
229      * @param classname the class name.
230      *
231      * @return The boot class.
232      */

233     protected AbstractBoot loadBooter(final String JavaDoc classname) {
234         if (classname == null) {
235             return null;
236         }
237         try {
238             final Class JavaDoc c = ObjectUtilities.getClassLoader(
239                     getClass()).loadClass(classname);
240             final Method JavaDoc m = c.getMethod("getInstance", (Class JavaDoc[]) null);
241             return (AbstractBoot) m.invoke(null, (Object JavaDoc[]) null);
242         }
243         catch (Exception JavaDoc e) {
244             Log.info ("Unable to boot dependent class: " + classname);
245             return null;
246         }
247     }
248
249     /**
250      * Creates a default configuration setup, which loads its settings from
251      * the static configuration (defaults provided by the developers of the
252      * library) and the user configuration (settings provided by the deployer).
253      * The deployer's settings override the developer's settings.
254      *
255      * If the parameter <code>addSysProps</code> is set to true, the system
256      * properties will be added as third configuration layer. The system
257      * properties configuration allows to override all other settings.
258      *
259      * @param staticConfig the resource name of the developers configuration
260      * @param userConfig the resource name of the deployers configuration
261      * @param addSysProps a flag defining whether to include the system
262      * properties into the configuration.
263      * @return the configured Configuration instance.
264      */

265     protected Configuration createDefaultHierarchicalConfiguration
266         (final String JavaDoc staticConfig, final String JavaDoc userConfig,
267                 final boolean addSysProps)
268     {
269         final HierarchicalConfiguration globalConfig
270             = new HierarchicalConfiguration();
271
272         if (staticConfig != null) {
273           final PropertyFileConfiguration rootProperty
274               = new PropertyFileConfiguration();
275           rootProperty.load(staticConfig);
276           globalConfig.insertConfiguration(rootProperty);
277           globalConfig.insertConfiguration(
278                   getPackageManager().getPackageConfiguration());
279         }
280         if (userConfig != null) {
281           String JavaDoc userConfigStripped;
282           if (userConfig.startsWith("/")) {
283             userConfigStripped = userConfig.substring(1);
284           }
285           else {
286             userConfigStripped = userConfig;
287           }
288           try {
289             final Enumeration JavaDoc userConfigs = ObjectUtilities.getClassLoader
290                             (getClass()).getResources(userConfigStripped);
291             final ArrayList JavaDoc configs = new ArrayList JavaDoc();
292             while (userConfigs.hasMoreElements()) {
293               final URL JavaDoc url = (URL JavaDoc) userConfigs.nextElement();
294               try {
295                 final PropertyFileConfiguration baseProperty =
296                         new PropertyFileConfiguration();
297                 final InputStream JavaDoc in = url.openStream();
298                 baseProperty.load(in);
299                 in.close();
300                 configs.add(baseProperty);
301               }
302               catch(IOException JavaDoc ioe) {
303                 Log.warn ("Failed to load the user configuration at " + url, ioe);
304               }
305             }
306
307             for (int i = configs.size() - 1; i >= 0; i--) {
308               final PropertyFileConfiguration baseProperty =
309                       (PropertyFileConfiguration) configs.get(i);
310               globalConfig.insertConfiguration(baseProperty);
311             }
312           }
313           catch (IOException JavaDoc e) {
314             Log.warn ("Failed to lookup the user configurations.", e);
315           }
316         }
317         final SystemPropertyConfiguration systemConfig
318             = new SystemPropertyConfiguration();
319         globalConfig.insertConfiguration(systemConfig);
320         return globalConfig;
321     }
322
323     /**
324      * Returns the global configuration as extended configuration.
325      *
326      * @return the extended configuration.
327      */

328     public synchronized ExtendedConfiguration getExtendedConfig ()
329     {
330       if (extWrapper == null) {
331           extWrapper = new ExtendedConfigurationWrapper(getGlobalConfig());
332       }
333       return extWrapper;
334     }
335 }
336
Popular Tags