KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > modules > boot > MuleBootstrap


1 /*
2  * $Id: MuleBootstrap.java 4219 2006-12-09 10:15:14Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.modules.boot;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLClassLoader JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.mule.MuleServer;
22 import org.mule.util.ClassUtils;
23 import org.mule.util.SystemUtils;
24 import org.tanukisoftware.wrapper.WrapperManager;
25 import org.tanukisoftware.wrapper.WrapperSimpleApp;
26
27 /**
28  * Determine which is the main class to run and delegate control to the Java Service
29  * Wrapper. <p/> MuleBootstrap class is responsible for constructing Mule's classpath
30  * from the Mule home folder.
31  */

32 public class MuleBootstrap
33 {
34
35     /**
36      * Do not instantiate MuleBootstrap.
37      */

38     private MuleBootstrap()
39     {
40         super();
41     }
42
43     /**
44      * Entry point.
45      *
46      * @param args command-line arguments
47      * @throws Exception in case of any fatal problem
48      */

49     public static void main(String JavaDoc args[]) throws Exception JavaDoc
50     {
51         // Make sure MULE_HOME is set.
52
File JavaDoc muleHome = null;
53
54         String JavaDoc muleHomeVar = System.getProperty("mule.home");
55         // Note: we can't use StringUtils.isBlank() here because we don't have that library yet.
56
if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%")) {
57             muleHome = new File JavaDoc(muleHomeVar).getCanonicalFile();
58         }
59         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory()) {
60             throw new IllegalArgumentException JavaDoc(
61                 "Either MULE_HOME is not set or does not contain a valid directory.");
62         }
63
64         File JavaDoc muleBase = null;
65
66         String JavaDoc muleBaseVar = System.getProperty("mule.base");
67         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%")) {
68             muleBase = new File JavaDoc(muleBaseVar).getCanonicalFile();
69         } else {
70             muleBase = muleHome;
71         }
72
73         // Build up a list of libraries from $MULE_HOME/lib/* and add them to the classpath.
74
DefaultMuleClassPathConfig classPath = new DefaultMuleClassPathConfig(muleHome, muleBase);
75         addLibrariesToClasspath(classPath.getURLs());
76
77         // If the license ack file isn't on the classpath, we need to
78
// display the EULA and make sure the user accepts it before continuing
79
ClassLoader JavaDoc sys = ClassLoader.getSystemClassLoader();
80         if (ClassUtils.getResource("META-INF/mule/license.props", MuleBootstrap.class) == null) {
81             LicenseHandler licenseHandler = new LicenseHandler(muleHome, muleBase);
82             // If the user didn't accept the license, then we have to exit
83
// Exiting this way insures that the wrapper won't try again
84
// (by default it'll try to start 3 times)
85
if (!licenseHandler.getAcceptance())
86             {
87                 WrapperManager.stop(-1);
88             }
89         }
90
91         // One-time download to get libraries not included in the Mule distribution due
92
// to silly licensing restrictions.
93
//
94
// Now we will download these libraries to MULE_BASE/lib/user. In
95
// a standard installation, MULE_BASE will be MULE_HOME.
96
if (!ClassUtils.isClassOnPath("javax.activation.DataSource", MuleBootstrap.class)) {
97             LibraryDownloader downloader = new LibraryDownloader(muleBase);
98             addLibrariesToClasspath(downloader.downloadLibraries());
99         }
100
101         // the core jar has been added dynamically, this construct will run with
102
// a new Mule classpath now
103
String JavaDoc mainClassName = SystemUtils.getCommandLineOption("-main", args);
104
105         if (mainClassName == null)
106         {
107             mainClassName = MuleServer.class.getName();
108         }
109
110         // Add the main class name as the first argument to the Wrapper.
111
String JavaDoc[] appArgs = new String JavaDoc[args.length + 1];
112         appArgs[0] = mainClassName;
113         System.arraycopy(args, 0, appArgs, 1, args.length);
114
115         // Call the wrapper
116
WrapperSimpleApp.main(appArgs);
117     }
118
119     private static void addLibrariesToClasspath(List JavaDoc urls)
120         throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
121
122         ClassLoader JavaDoc sys = ClassLoader.getSystemClassLoader();
123         if (!(sys instanceof URLClassLoader JavaDoc))
124         {
125             throw new IllegalArgumentException JavaDoc(
126                 "PANIC: Mule has been started with an unsupported classloader: " + sys.getClass().getName()
127                                 + ". " + "Please report this error to user<at>mule<dot>codehaus<dot>org");
128         }
129
130         // system classloader is in this case the one that launched the application,
131
// which is usually something like a JDK-vendor proprietary AppClassLoader
132
URLClassLoader JavaDoc sysCl = (URLClassLoader JavaDoc)sys;
133
134         /*
135          * IMPORTANT NOTE: The more 'natural' way would be to create a custom
136          * URLClassLoader and configure it, but then there's a chicken-and-egg
137          * problem, as all classes MuleBootstrap depends on would have been loaded by
138          * a parent classloader, and not ours. There's no straightforward way to
139          * change this, and is documented in a Sun's classloader guide. The solution
140          * would've involved overriding the ClassLoader.findClass() method and
141          * modifying the semantics to be child-first, but that way we are calling for
142          * trouble. Hacking the primordial classloader is a bit brutal, but works
143          * perfectly in case of running from the command-line as a standalone app.
144          * All Mule embedding options then delegate the classpath config to the
145          * embedder (a developer embedding Mule in the app), thus classloaders are
146          * not modified in those scenarios.
147          */

148
149         // get a Method ref from the normal class, but invoke on a proprietary parent
150
// object,
151
// as this method is usually protected in those classloaders
152
Class JavaDoc refClass = URLClassLoader JavaDoc.class;
153         Method JavaDoc methodAddUrl = refClass.getDeclaredMethod("addURL", new Class JavaDoc[]{URL JavaDoc.class});
154         methodAddUrl.setAccessible(true);
155         for (Iterator JavaDoc it = urls.iterator(); it.hasNext();)
156         {
157             URL JavaDoc url = (URL JavaDoc)it.next();
158             // System.out.println("Adding: " + url.toExternalForm());
159
methodAddUrl.invoke(sysCl, new Object JavaDoc[]{url});
160         }
161     }
162 }
163
Popular Tags