KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 package org.jboss.system.server;
23
24 // $Id: ServerLoader.java 58346 2006-11-14 17:27:00Z kabir.khan@jboss.com $
25

26 import java.io.File JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 /**
36  * A helper class to load a JBoss server instance.
37  *
38  * <p>Basic usage is something like this:
39  * <pre>
40  * // setup the basic server config properties
41  * Properties props = new Properties(System.getProperties());
42  * props.put(ServerConfig.SERVER_LIBRARY_URL, "http://myserver.com/myjboss/lib/");
43  * // set some more properties
44  *
45  * // create a new loader to do the dirty work
46  * ServerLoader loader = new ServerLoader(props);
47  *
48  * // add the jaxp & jmx library to use
49  * loader.addLibrary("crimson.jar");
50  * loader.addLibrary("jboss-jmx-core.jar");
51  *
52  * // load and initialize the server instance
53  * ClassLoader parent = Thread.currentThread().getContextClassLoader();
54  * Server server = loader.load(parent);
55  * server.init(props);
56  *
57  * // start up the server
58  * server.start();
59  *
60  * // go make some coffee, drink a beer or play GTA3
61  * // ...
62  *
63  * // shutdown and go to sleep
64  * server.shutdown();
65  * </pre>
66  * @version <tt>$Revision: 58346 $</tt>
67  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
68  * @author <a HREF="mailto:adrian.brock@happeningtimes.com">Adrian Brock</a>
69  * @author Scott.Stark@jboss.org
70  * @author Thomas.Diesler@jboss.org
71  */

72 public class ServerLoader
73 {
74    /**
75     * The default list of boot libraries. Does not include
76     * the JAXP or JMX impl, users of this class should add the
77     * proper libraries.
78     * TODO: use vfs to list the root directory
79     */

80    public static final String JavaDoc DEFAULT_BOOT_LIBRARY_LIST =
81       "log4j-boot.jar,jboss-common-core.jar,jboss-logging-spi.jar,jboss-logging-log4j.jar,jboss-logging-jdk.jar,jboss-system.jar,jboss-xml-binding.jar,javassist.jar,jboss-aop-mc-int-boot.jar,jboss-container.jar,jboss-dependency.jar,jboss-microcontainer.jar,jboss-deployers.jar,dom4j.jar,jboss-j2se.jar,jboss-mbeans.jar,jboss-jmx.jar,jboss-system-jmx.jar";
82
83    /** The default server type. */
84    public static final String JavaDoc DEFAULT_SERVER_TYPE = "org.jboss.system.server.profileservice.ServerImpl";
85
86    /**
87     * Configuration properties.
88     */

89    protected Properties JavaDoc props;
90
91    /**
92     * The URL where libraries are read from.
93     */

94    protected URL JavaDoc libraryURL;
95
96    /**
97     * A list of extra URLs to add to the classpath when loading
98     * the server.
99     */

100    protected List JavaDoc extraClasspath = new LinkedList JavaDoc();
101
102    /**
103     * Construct a <tt>ServerLoader</tt>.
104     *
105     * @param props Configuration properties.
106     *
107     * @throws Exception Invalid configuration
108     */

109    public ServerLoader(final Properties JavaDoc props) throws Exception JavaDoc
110    {
111       if (props == null)
112          throw new IllegalArgumentException JavaDoc("props is null");
113
114       this.props = props;
115
116       // must have HOME_URL, or we can't continue
117
URL JavaDoc homeURL = getURL(ServerConfig.HOME_URL);
118       if (homeURL == null)
119       {
120          throw new Exception JavaDoc("Missing configuration value for: "
121             + ServerConfig.HOME_URL);
122       }
123
124       libraryURL = getURL(ServerConfig.LIBRARY_URL);
125       if (libraryURL == null)
126       {
127          // need library url to make boot urls list
128
libraryURL = new URL JavaDoc(homeURL, ServerConfig.LIBRARY_URL_SUFFIX);
129       }
130
131       // If the home URL begins with http add the webav and httpclient jars
132
if( homeURL.getProtocol().startsWith("http") == true )
133       {
134          this.addLibrary("webdavlib.jar");
135          this.addLibrary("commons-httpclient.jar");
136          this.addLibrary("commons-logging.jar");
137       }
138    }
139
140    /**
141     * Add an extra library to the end of list of libraries
142     * which will be loaded from the library URL when loading
143     * the Server class.
144     *
145     * @param filename A filename (no directory parts)
146     *
147     * @throws MalformedURLException Could not generate URL from library URL + filename
148     */

149    public void addLibrary(final String JavaDoc filename) throws MalformedURLException JavaDoc
150    {
151       if (filename == null)
152          throw new IllegalArgumentException JavaDoc("filename is null");
153
154       URL JavaDoc jarURL = new URL JavaDoc(libraryURL, filename);
155       extraClasspath.add(jarURL);
156    }
157
158    /**
159     * Add a list of comma seperated library file names.
160     *
161     * @param filenames A list of comma seperated filenames (with no directory parts)
162     *
163     * @throws MalformedURLException Could not generate URL from library URL + filename
164     */

165    public void addLibraries(final String JavaDoc filenames) throws MalformedURLException JavaDoc
166    {
167       if (filenames == null)
168          throw new IllegalArgumentException JavaDoc("filenames is null");
169
170       StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(filenames, ",");
171       while (stok.hasMoreElements())
172       {
173          addLibrary(stok.nextToken().trim());
174       }
175    }
176
177    /**
178     * Add an extra URL to the classpath used to load the server.
179     *
180     * @param url A URL to add to the classpath.
181     */

182    public void addURL(final URL JavaDoc url)
183    {
184       if (url == null)
185          throw new IllegalArgumentException JavaDoc("url is null");
186
187       extraClasspath.add(url);
188    }
189
190    /**
191     * Add the jars from the lib/endorsed dir if it exists.
192     * Note, the path must exist locally for this to work.
193     * @throws MalformedURLException Could not generate URL from library URL + filename
194     */

195    public void addEndorsedJars() throws MalformedURLException JavaDoc
196    {
197       File JavaDoc endorsedDir = new File JavaDoc(libraryURL.getPath() + "/endorsed");
198       if (endorsedDir.exists())
199       {
200          String JavaDoc [] list = endorsedDir.list();
201          for (int i = 0; list != null && i < list.length; i++)
202          {
203             String JavaDoc jarname = list[i];
204             addLibrary("endorsed/" + jarname);
205          }
206       }
207    }
208
209    /**
210     * Get a URL from configuration or system properties.
211     */

212    protected URL JavaDoc getURL(final String JavaDoc name) throws MalformedURLException JavaDoc
213    {
214       String JavaDoc value = props.getProperty(name, null);
215       if (value != null)
216       {
217          if (!value.endsWith("/")) value += "/";
218          return new URL JavaDoc(value);
219       }
220       return null;
221    }
222
223    /**
224     * Returns an array of URLs which will be used to load the
225     * core system and construct a new Server object instance.
226     */

227    protected URL JavaDoc[] getBootClasspath() throws MalformedURLException JavaDoc
228    {
229       List JavaDoc list = new LinkedList JavaDoc();
230
231       // prepend users classpath to allow for overrides
232
list.addAll(extraClasspath);
233
234       String JavaDoc value = props.getProperty(ServerConfig.BOOT_LIBRARY_LIST, DEFAULT_BOOT_LIBRARY_LIST);
235
236       StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ",");
237       while (stok.hasMoreElements())
238       {
239          URL JavaDoc url = new URL JavaDoc(libraryURL, stok.nextToken().trim());
240          list.add(url);
241       }
242
243       return (URL JavaDoc[]) list.toArray(new URL JavaDoc[list.size()]);
244    }
245
246    /**
247     * Load a {@link Server} instance.
248     *
249     * @parent The parent of any class loader created during boot.
250     * @return An uninitialized (and unstarted) Server instance.
251     *
252     * @throws Exception Failed to load or create Server instance.
253     */

254    public Server load(final ClassLoader JavaDoc parent) throws Exception JavaDoc
255    {
256       Server server;
257       ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
258
259       try
260       {
261          // get the boot lib list
262
URL JavaDoc[] urls = getBootClasspath();
263          URLClassLoader JavaDoc classLoader = new NoAnnotationURLClassLoader(urls, parent);
264          Thread.currentThread().setContextClassLoader(classLoader);
265
266          // construct a new Server instance
267
String JavaDoc typename = props.getProperty(ServerConfig.SERVER_TYPE, DEFAULT_SERVER_TYPE);
268          server = createServer(typename, classLoader);
269       }
270       finally
271       {
272          Thread.currentThread().setContextClassLoader(oldCL);
273       }
274
275       // thats all folks, have fun
276
return server;
277    }
278
279    /**
280     * Construct a new instance of Server, loading all required classes from
281     * the given ClassLoader.
282     * @param typename - the fqcn of the Server implementation
283     * @param loader - the ClassLoader to load typename with
284     */

285    protected Server createServer(final String JavaDoc typename, final ClassLoader JavaDoc loader)
286       throws Exception JavaDoc
287    {
288       // load the class first
289
Class JavaDoc type = loader.loadClass(typename);
290
291       // and then create a new instance
292
Server server = (Server) type.newInstance();
293       return server;
294    }
295 }
296
Popular Tags