View Javadoc

1   /*
2    * $Id: MuleBootstrapUtils.java 11957 2008-06-04 14:23:25Z dirk.olmes $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.boot;
12  
13  import java.io.File;
14  import java.lang.reflect.InvocationTargetException;
15  import java.lang.reflect.Method;
16  import java.net.URL;
17  import java.net.URLClassLoader;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  public final class MuleBootstrapUtils
22  {
23      private static final String MULE_LIB_FILENAME = "lib" + File.separator + "mule";
24      private static final String MULE_HOME = System.getProperty("mule.home");
25      
26      public static final String MULE_LOCAL_JAR_FILENAME = "mule-local-install.jar";
27  
28      private MuleBootstrapUtils()
29      {
30          // utility class only
31      }
32      
33      public static File getMuleHomeFile()
34      {
35          return new File(MULE_HOME);
36      }
37      
38      public static File getMuleLibDir()
39      {   
40          return new File(MULE_HOME + File.separator + MULE_LIB_FILENAME);
41      }
42      
43      public static File getMuleLocalJarFile()
44      {
45          return new File(getMuleLibDir(), MULE_LOCAL_JAR_FILENAME);
46      }
47      
48      public static void addLocalJarFilesToClasspath(File muleHome, File muleBase) throws Exception
49      {
50          DefaultMuleClassPathConfig classPath = new DefaultMuleClassPathConfig(muleHome, muleBase);
51          addLibrariesToClasspath(classPath.getURLs());
52      }
53      
54      public static void addLibrariesToClasspath(List urls) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
55      {
56          ClassLoader sys = ClassLoader.getSystemClassLoader();
57          if (!(sys instanceof URLClassLoader))
58          {
59              throw new IllegalArgumentException(
60                  "PANIC: Mule has been started with an unsupported classloader: " + sys.getClass().getName()
61                                  + ". " + "Please report this error to user<at>mule<dot>codehaus<dot>org");
62          }
63      
64          // system classloader is in this case the one that launched the application,
65          // which is usually something like a JDK-vendor proprietary AppClassLoader
66          URLClassLoader sysCl = (URLClassLoader) sys;
67      
68          /*
69           * IMPORTANT NOTE: The more 'natural' way would be to create a custom
70           * URLClassLoader and configure it, but then there's a chicken-and-egg
71           * problem, as all classes MuleBootstrap depends on would have been loaded by
72           * a parent classloader, and not ours. There's no straightforward way to
73           * change this, and is documented in a Sun's classloader guide. The solution
74           * would've involved overriding the ClassLoader.findClass() method and
75           * modifying the semantics to be child-first, but that way we are calling for
76           * trouble. Hacking the primordial classloader is a bit brutal, but works
77           * perfectly in case of running from the command-line as a standalone app.
78           * All Mule embedding options then delegate the classpath config to the
79           * embedder (a developer embedding Mule in the app), thus classloaders are
80           * not modified in those scenarios.
81           */
82      
83          // get a Method ref from the normal class, but invoke on a proprietary parent
84          // object,
85          // as this method is usually protected in those classloaders
86          Class refClass = URLClassLoader.class;
87          Method methodAddUrl = refClass.getDeclaredMethod("addURL", new Class[]{URL.class});
88          methodAddUrl.setAccessible(true);
89          for (Iterator it = urls.iterator(); it.hasNext();)
90          {
91              URL url = (URL) it.next();
92              methodAddUrl.invoke(sysCl, new Object[]{url});
93          }
94      }
95      
96      public static class ProxyInfo
97      {
98          String host;
99          String port;
100         String username;
101         String password;
102         
103         public ProxyInfo(String host, String port)
104         {
105             this(host, port, null, null);
106         }
107         
108         public ProxyInfo(String host, String port, String username, String password)
109         {
110             this.host = host;
111             this.port = port;
112             this.username = username;
113             this.password = password;
114         }
115     }
116 }