KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > server > BaseServerConfig


1 /* JBoss, Home of Professional Open Source
2 * Copyright 2005, JBoss Inc., and individual contributors as indicated
3 * by the @authors tag. See the copyright.txt in the distribution for a
4 * full listing of individual contributors.
5 *
6 * This is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this software; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20 */

21 package org.jboss.system.server;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import org.jboss.util.NestedRuntimeException;
30 import org.jboss.util.Null;
31 import org.jboss.util.Primitives;
32 import org.jboss.util.platform.Java;
33
34 /**
35  * A container for the basic configuration elements required to create
36  * a Server instance.
37  *
38  * <p>MalformedURLException are rethrown as NestedRuntimeExceptions, so that
39  * code that needs to access these values does not have to directly
40  * worry about problems with lazy construction of final URL values.
41  *
42  * <p>Most values are determined durring first call to getter. All values
43  * when determined will have equivilent system properties set.
44  *
45  * <p>Clients are not meant to use this class directly. Instead use
46  * {@link ServerConfigLocator} to get an instance of {@link ServerConfig}
47  * and then use it to get the server's configuration bits.
48  *
49  * @jmx:mbean name="jboss.system:type=ServerConfig"
50  *
51  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
52  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
53  * @author Scott.Stark@jboss.org
54  * @version <tt>$Revision: 41281 $</tt>
55  */

56
57
58 public class BaseServerConfig
59    implements ServerConfig
60 {
61    /** The configuration properties to pull data from. */
62    private Properties JavaDoc props;
63
64    private File JavaDoc homeDir;
65    private URL JavaDoc homeURL;
66    private URL JavaDoc libraryURL;
67
68    /**
69     * The base URL where patch files will be loaded from. This is
70     * typed as an Object to allow its value to contain Null.VALUE
71     * or a URL. If value is Null.VALUE then we have determined
72     * that there is no user configuration for this value and it will
73     * be passed back as null to the requesting client.
74     */

75    private Object JavaDoc patchURL;
76
77    private String JavaDoc serverName;
78    private File JavaDoc serverBaseDir;
79    private File JavaDoc serverHomeDir;
80    private File JavaDoc serverLogDir;
81    private File JavaDoc serverTempDir;
82    private File JavaDoc serverDataDir;
83    private URL JavaDoc serverBaseURL;
84    private URL JavaDoc serverHomeURL;
85    private URL JavaDoc serverLibraryURL;
86    private URL JavaDoc serverConfigURL;
87
88    /** Exit on shutdown flag. */
89    private Boolean JavaDoc exitOnShutdown;
90    private Boolean JavaDoc blockingShutdown;
91    private Boolean JavaDoc requireJBossURLStreamHandlerFactory;
92    private Boolean JavaDoc platformMBeanServer;
93
94    private String JavaDoc rootDeployableFilename;
95
96    /**
97     * Construct a new <tt>ServerConfigImpl</tt> instance.
98     *
99     * @param props Configuration properties.
100     *
101     * @throws Exception Missing or invalid configuration.
102     */

103    public BaseServerConfig(final Properties JavaDoc props) throws Exception JavaDoc
104    {
105       this.props = props;
106
107       // Must have HOME_DIR
108
homeDir = getFile(ServerConfig.HOME_DIR);
109       if (homeDir == null)
110          throw new Exception JavaDoc("Missing configuration value for: " + ServerConfig.HOME_DIR);
111       System.setProperty(ServerConfig.HOME_DIR, homeDir.toString());
112       // Setup the SERVER_HOME_DIR system property
113
getServerHomeDir();
114    }
115
116    /** Breakout the initialization of URLs from the constructor as we need
117     * the ServerConfig.HOME_DIR set for log setup, but we cannot create any
118     * file URLs prior to the
119     */

120    public void initURLs()
121       throws MalformedURLException JavaDoc
122    {
123       // If not set then default to homeDir
124
homeURL = getURL(ServerConfig.HOME_URL);
125       if (homeURL == null)
126          homeURL = homeDir.toURL();
127       System.setProperty(ServerConfig.HOME_URL, homeURL.toString());
128    }
129
130    /////////////////////////////////////////////////////////////////////////
131
// Typed Access //
132
/////////////////////////////////////////////////////////////////////////
133

134    /**
135     * Get the local home directory which the server is running from.
136     *
137     * @jmx:managed-attribute
138     */

139    public File JavaDoc getHomeDir()
140    {
141       return homeDir;
142    }
143
144    /**
145     * Get the home URL which the server is running from.
146     *
147     * @jmx:managed-attribute
148     */

149    public URL JavaDoc getHomeURL()
150    {
151       return homeURL;
152    }
153
154    /**
155     * Get the home URL which the server is running from.
156     *
157     * @jmx:managed-attribute
158     */

159    public URL JavaDoc getLibraryURL()
160    {
161       if (libraryURL == null)
162       {
163          try
164          {
165             libraryURL = getURL(ServerConfig.LIBRARY_URL);
166             if (libraryURL == null)
167             {
168                libraryURL = new URL JavaDoc(homeURL, ServerConfig.LIBRARY_URL_SUFFIX);
169             }
170             System.setProperty(ServerConfig.LIBRARY_URL, libraryURL.toString());
171          }
172          catch (MalformedURLException JavaDoc e)
173          {
174             throw new NestedRuntimeException(e);
175          }
176       }
177       return libraryURL;
178    }
179
180    /**
181     * Get the patch URL for the server.
182     *
183     * @jmx:managed-attribute
184     */

185    public URL JavaDoc getPatchURL()
186    {
187       if (patchURL == null)
188       {
189          try
190          {
191             patchURL = getURL(ServerConfig.PATCH_URL);
192             if (patchURL == null)
193             {
194                patchURL = Null.VALUE;
195             }
196             else
197             {
198                System.setProperty(ServerConfig.PATCH_URL, patchURL.toString());
199             }
200          }
201          catch (MalformedURLException JavaDoc e)
202          {
203             throw new NestedRuntimeException(e);
204          }
205       }
206
207       if (patchURL == Null.VALUE)
208          return null;
209
210       return (URL JavaDoc) patchURL;
211    }
212
213    /**
214     * Get the name of the server.
215     *
216     * @jmx:managed-attribute
217     */

218    public String JavaDoc getServerName()
219    {
220       if (serverName == null)
221       {
222          serverName = props.getProperty(ServerConfig.SERVER_NAME, ServerConfig.DEFAULT_SERVER_NAME);
223          System.setProperty(ServerConfig.SERVER_NAME, serverName);
224       }
225       return serverName;
226    }
227
228    /**
229     * Get the base directory for calculating server home directories.
230     *
231     * @jmx:managed-attribute
232     */

233    public File JavaDoc getServerBaseDir()
234    {
235       if (serverBaseDir == null)
236       {
237          serverBaseDir = getFile(ServerConfig.SERVER_BASE_DIR);
238          if (serverBaseDir == null)
239          {
240             serverBaseDir = new File JavaDoc(homeDir, ServerConfig.SERVER_BASE_DIR_SUFFIX);
241             System.setProperty(ServerConfig.SERVER_BASE_DIR, serverBaseDir.toString());
242          }
243       }
244       return serverBaseDir;
245    }
246
247    /**
248     * Get the server home directory.
249     *
250     * @jmx:managed-attribute
251     */

252    public File JavaDoc getServerHomeDir()
253    {
254       if (serverHomeDir == null)
255       {
256          serverHomeDir = getFile(ServerConfig.SERVER_HOME_DIR);
257          if (serverHomeDir == null)
258          {
259             serverHomeDir = new File JavaDoc(getServerBaseDir(), getServerName());
260             System.setProperty(ServerConfig.SERVER_HOME_DIR, serverHomeDir.toString());
261          }
262       }
263       return serverHomeDir;
264    }
265
266    /**
267     * Get the directory where temporary files will be stored. The associated
268     * ServerConfig.SERVER_LOG_DIR system property needs to be set before
269     * the logging framework is used.
270     *
271     * @see ServerConfig.SERVER_LOG_DIR
272     * @return the writable temp directory
273     */

274    public File JavaDoc getServerLogDir()
275    {
276       if (serverLogDir == null)
277       {
278          serverLogDir = getFile(ServerConfig.SERVER_LOG_DIR);
279          if (serverLogDir == null)
280          {
281             serverLogDir = new File JavaDoc(getServerHomeDir(), ServerConfig.SERVER_LOG_DIR_SUFFIX);
282             System.setProperty(ServerConfig.SERVER_LOG_DIR, serverLogDir.toString());
283          }
284       }
285       return serverLogDir;
286    }
287
288    /**
289     * Get the directory where temporary files will be stored.
290     *
291     * @jmx:managed-attribute
292     * @return the writable temp directory
293     */

294    public File JavaDoc getServerTempDir()
295    {
296       if (serverTempDir == null)
297       {
298          serverTempDir = getFile(ServerConfig.SERVER_TEMP_DIR);
299          if (serverTempDir == null)
300          {
301             serverTempDir = new File JavaDoc(getServerHomeDir(), ServerConfig.SERVER_TEMP_DIR_SUFFIX);
302             System.setProperty(ServerConfig.SERVER_TEMP_DIR, serverTempDir.toString());
303          }
304       }
305       return serverTempDir;
306    }
307
308    /**
309     * Get the directory where local data will be stored.
310     *
311     * @jmx:managed-attribute
312     * @return the data directory
313     */

314    public File JavaDoc getServerDataDir()
315    {
316       if (serverDataDir == null)
317       {
318          serverDataDir = getFile(ServerConfig.SERVER_DATA_DIR);
319          if (serverDataDir == null)
320          {
321             serverDataDir = new File JavaDoc(getServerHomeDir(), ServerConfig.SERVER_DATA_DIR_SUFFIX);
322             System.setProperty(ServerConfig.SERVER_DATA_DIR, serverDataDir.toString());
323          }
324       }
325       return serverDataDir;
326    }
327
328    /**
329     * Get the native dir for unpacking
330     *
331     * @jmx:managed-attribute
332     * @return the directory
333     */

334    public File JavaDoc getServerNativeDir()
335    {
336       String JavaDoc fileName = System.getProperty(NATIVE_DIR_PROPERTY);
337       if (fileName != null)
338          return new File JavaDoc(fileName);
339       return new File JavaDoc(getServerTempDir(), "native");
340    }
341
342    /**
343     * Get the temporary deployment dir for unpacking
344     *
345     * @jmx:managed-attribute
346     * @return the directory
347     */

348    public File JavaDoc getServerTempDeployDir()
349    {
350       return new File JavaDoc(getServerTempDir(), "deploy");
351    }
352
353    /**
354     * Get the base directory for calculating server home URLs.
355     *
356     * @jmx:managed-attribute
357     */

358    public URL JavaDoc getServerBaseURL()
359    {
360       if (serverBaseURL == null)
361       {
362          try
363          {
364             serverBaseURL = getURL(ServerConfig.SERVER_BASE_URL);
365             if (serverBaseURL == null)
366             {
367                serverBaseURL = new URL JavaDoc(homeURL, ServerConfig.SERVER_BASE_URL_SUFFIX);
368             }
369             System.setProperty(ServerConfig.SERVER_BASE_URL, serverBaseURL.toString());
370          }
371          catch (MalformedURLException JavaDoc e)
372          {
373             throw new NestedRuntimeException(e);
374          }
375       }
376       return serverBaseURL;
377    }
378
379    /**
380     * Get the server home URL.
381     *
382     * @jmx:managed-attribute
383     */

384    public URL JavaDoc getServerHomeURL()
385    {
386       if (serverHomeURL == null)
387       {
388          try
389          {
390             serverHomeURL = getURL(ServerConfig.SERVER_HOME_URL);
391             if (serverHomeURL == null)
392             {
393                serverHomeURL = new URL JavaDoc(getServerBaseURL(), getServerName() + "/");
394             }
395             System.setProperty(ServerConfig.SERVER_HOME_URL, serverHomeURL.toString());
396          }
397          catch (MalformedURLException JavaDoc e)
398          {
399             throw new NestedRuntimeException(e);
400          }
401       }
402       return serverHomeURL;
403    }
404
405    /**
406     * Get the server library URL.
407     *
408     * @jmx:managed-attribute
409     */

410    public URL JavaDoc getServerLibraryURL()
411    {
412       if (serverLibraryURL == null)
413       {
414          try
415          {
416             serverLibraryURL = getURL(ServerConfig.SERVER_LIBRARY_URL);
417             if (serverLibraryURL == null)
418             {
419                serverLibraryURL = new URL JavaDoc(getServerHomeURL(), ServerConfig.LIBRARY_URL_SUFFIX);
420             }
421             System.setProperty(ServerConfig.SERVER_LIBRARY_URL, serverLibraryURL.toString());
422          }
423          catch (MalformedURLException JavaDoc e)
424          {
425             throw new NestedRuntimeException(e);
426          }
427       }
428       return serverLibraryURL;
429    }
430
431    /**
432     * Get the server configuration URL.
433     *
434     * @jmx:managed-attribute
435     */

436    public URL JavaDoc getServerConfigURL()
437    {
438       if (serverConfigURL == null)
439       {
440          try
441          {
442             serverConfigURL = getURL(ServerConfig.SERVER_CONFIG_URL);
443             if (serverConfigURL == null)
444             {
445                serverConfigURL = new URL JavaDoc(getServerHomeURL(), ServerConfig.SERVER_CONFIG_URL_SUFFIX);
446             }
447             System.setProperty(ServerConfig.SERVER_CONFIG_URL, serverConfigURL.toString());
448          }
449          catch (MalformedURLException JavaDoc e)
450          {
451             throw new NestedRuntimeException(e);
452          }
453       }
454       return serverConfigURL;
455    }
456
457    /**
458     * Get the current value of the flag that indicates if we are
459     * using the platform MBeanServer as the main jboss server.
460     * Both the {@link ServerConfig.PLATFORM_MBEANSERVER}
461     * property must be set, and the jvm must be jdk1.5+
462     *
463     * @return true if jboss runs on the jvm platfrom MBeanServer
464     *
465     * @jmx:managed-attribute
466     */

467    public boolean getPlatformMBeanServer()
468    {
469       if (platformMBeanServer == null)
470       {
471          if (Java.isCompatible(Java.VERSION_1_5))
472          {
473             // get whatever the user has specified or the default
474
String JavaDoc value = props.getProperty(ServerConfig.PLATFORM_MBEANSERVER,
475                (new Boolean JavaDoc(ServerConfig.DEFAULT_PLATFORM_MBEANSERVER)).toString());
476             
477             // treat empty string as true
478
value = "".equals(value) ? "true" : value;
479             
480             // true or false
481
platformMBeanServer = new Boolean JavaDoc(value);
482          }
483          else
484          {
485             // negative :)
486
platformMBeanServer = Boolean.FALSE;
487          }
488       }
489       return platformMBeanServer.booleanValue();
490    }
491    
492    /**
493     * Enable or disable exiting the JVM when {@link Server#shutdown} is called.
494     * If enabled, then shutdown calls {@link Server#exit}. If disabled, then
495     * only the shutdown hook will be run.
496     *
497     * @param flag True to enable calling exit on shutdown.
498     *
499     * @jmx:managed-attribute
500     */

501    public void setExitOnShutdown(final boolean flag)
502    {
503       exitOnShutdown = Primitives.valueOf(flag);
504    }
505
506    /**
507     * Get the current value of the exit on shutdown flag.
508     *
509     * @return The current value of the exit on shutdown flag.
510     *
511     * @jmx:managed-attribute
512     */

513    public boolean getExitOnShutdown()
514    {
515       if (exitOnShutdown == null)
516       {
517          String JavaDoc value = props.getProperty(ServerConfig.EXIT_ON_SHUTDOWN, null);
518          if (value == null)
519          {
520             exitOnShutdown = Primitives.valueOf(ServerConfig.DEFAULT_EXIT_ON_SHUTDOWN);
521          }
522          else
523          {
524             exitOnShutdown = new Boolean JavaDoc(value);
525          }
526       }
527       return exitOnShutdown.booleanValue();
528    }
529
530    /**
531     * Enable or disable blocking when {@link Server#shutdown} is
532     * called. If enabled, then shutdown will be called in the current
533     * thread. If disabled, then the shutdown hook will be run
534     * ansynchronously in a separate thread.
535     *
536     * @param flag True to enable blocking shutdown.
537     *
538     * @jmx:managed-attribute
539     */

540    public void setBlockingShutdown(final boolean flag)
541    {
542       blockingShutdown = Primitives.valueOf(flag);
543    }
544
545    /**
546     * Get the current value of the blocking shutdown flag.
547     *
548     * @return The current value of the blocking shutdown flag.
549     *
550     * @jmx:managed-attribute
551     */

552    public boolean getBlockingShutdown()
553    {
554       if (blockingShutdown == null)
555       {
556          String JavaDoc value = props.getProperty(ServerConfig.BLOCKING_SHUTDOWN, null);
557          if (value == null)
558          {
559             blockingShutdown = Primitives.valueOf(ServerConfig.DEFAULT_BLOCKING_SHUTDOWN);
560          }
561          else
562          {
563             blockingShutdown = new Boolean JavaDoc(value);
564          }
565       }
566       return blockingShutdown.booleanValue();
567    }
568
569
570    /**
571     * Set the RequireJBossURLStreamHandlerFactory flag. if false,
572     * exceptions when setting the URLStreamHandlerFactory will be
573     * logged and ignored.
574     *
575     * @param flag True to enable blocking shutdown.
576     *
577     * @jmx:managed-attribute
578     */

579    public void setRequireJBossURLStreamHandlerFactory(final boolean flag)
580    {
581       requireJBossURLStreamHandlerFactory = Primitives.valueOf(flag);
582    }
583
584    /**
585     * Get the current value of the requireJBossURLStreamHandlerFactory flag.
586     *
587     * @return The current value of the requireJBossURLStreamHandlerFactory flag.
588     *
589     * @jmx:managed-attribute
590     */

591    public boolean getRequireJBossURLStreamHandlerFactory()
592    {
593       if (requireJBossURLStreamHandlerFactory == null)
594       {
595          String JavaDoc value = props.getProperty(ServerConfig.REQUIRE_JBOSS_URL_STREAM_HANDLER_FACTORY, null);
596          if (value == null)
597          {
598             requireJBossURLStreamHandlerFactory = Primitives.valueOf(ServerConfig.DEFAULT_REQUIRE_JBOSS_URL_STREAM_HANDLER_FACTORY);
599          }
600          else
601          {
602             requireJBossURLStreamHandlerFactory = new Boolean JavaDoc(value);
603          }
604       }
605       return requireJBossURLStreamHandlerFactory.booleanValue();
606    }
607
608    /**
609     * Set the filename of the root deployable that will be used to finalize
610     * the bootstrap process.
611     *
612     * @param filename The filename of the root deployable.
613     *
614     * @jmx:managed-attribute
615     */

616    public void setRootDeploymentFilename(final String JavaDoc filename)
617    {
618       this.rootDeployableFilename = filename;
619    }
620
621    /**
622     * Get the filename of the root deployable that will be used to finalize
623     * the bootstrap process.
624     *
625     * @return The filename of the root deployable.
626     *
627     * @jmx:managed-attribute
628     */

629    public String JavaDoc getRootDeploymentFilename()
630    {
631       if (rootDeployableFilename == null)
632       {
633          rootDeployableFilename = props.getProperty(ServerConfig.ROOT_DEPLOYMENT_FILENAME,
634                ServerConfig.DEFAULT_ROOT_DEPLOYMENT_FILENAME);
635       }
636
637       return rootDeployableFilename;
638    }
639
640    /**
641     * Get a URL from configuration.
642     */

643    private URL JavaDoc getURL(final String JavaDoc name) throws MalformedURLException JavaDoc
644    {
645       String JavaDoc value = props.getProperty(name, null);
646       if (value != null)
647       {
648          if (!value.endsWith("/")) value += "/";
649          return new URL JavaDoc(value);
650       }
651
652       return null;
653    }
654
655    /**
656     * Get a File from configuration.
657     * @return the CanonicalFile form for the given name.
658     */

659    private File JavaDoc getFile(final String JavaDoc name)
660    {
661       String JavaDoc value = props.getProperty(name, null);
662       if (value != null)
663       {
664          try
665          {
666             File JavaDoc f = new File JavaDoc(value);
667             return f.getCanonicalFile();
668          }
669          catch(IOException JavaDoc e)
670          {
671             return new File JavaDoc(value);
672          }
673       }
674
675       return null;
676    }
677 }
678
679
Popular Tags