View Javadoc

1   /*
2    * $Id
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.modules.boot;
12  
13  import org.mule.MuleServer;
14  import org.mule.util.ClassUtils;
15  import org.mule.util.SystemUtils;
16  
17  import java.lang.reflect.Method;
18  import java.net.URL;
19  
20  import org.tanukisoftware.wrapper.WrapperSimpleApp;
21  
22  /**
23   * JRockit VM has problems with MuleBootstrap's approach. Namely, for jars loaded
24   * dynamically into a system classloader (launcher) it can't resolve static method
25   * calls. At the same time, inspecting the dynamically loaded class reports all methods
26   * as available. The class implements all such calls via reflection as a workaround.
27   * Sun's VM had no problems with the original approach, and doesn't have any with this
28   * workaround either.
29   */
30  public class ReflectionHelper
31  {
32  
33      /** Do not instantiate ReflectionHelper. */
34      private ReflectionHelper()
35      {
36          // no-op
37      }
38  
39      /**
40       * Wrap {@link ClassUtils#getResource(String, Class)}.
41       * @param resourceName The name of the resource to load
42       * @param callingClass The Class object of the calling object
43       */
44      public static URL getResource(final String resourceName, final Class callingClass) throws Exception
45      {
46          Class clazz = ClassUtils.class;
47          Method m = clazz.getMethod("getResource", new Class[] {String.class, Class.class});
48          Object result = m.invoke(null, new Object[] { resourceName, callingClass});
49          return (URL) result;
50      }
51  
52      /**
53       * Wrap {@link ClassUtils#isClassOnPath(String, Class)}.
54       * @param className The class name to look for
55       * @param currentClass the calling class
56       * @return true if the class is on the path
57       */
58      public static boolean isClassOnPath(String className, Class currentClass) throws Exception
59      {
60          Class clazz = ClassUtils.class;
61          Method m = clazz.getMethod("isClassOnPath", new Class[] {String.class, Class.class});
62          Object result = m.invoke(null, new Object[] { className, currentClass});
63          return ((Boolean) result).booleanValue();
64      }
65  
66      /**
67       * Workaround for JRockit unable to access a public static field value.
68       * @return value of {@link MuleServer#CLI_OPTIONS}
69       */
70      public static String[][] getCliOptions() throws Exception
71      {
72          return (String[][]) MuleServer.class.getField("CLI_OPTIONS").get(null);
73      }
74  
75      /**
76       * Wrap {@link SystemUtils#getCommandLineOption(String, String[], String[][])}.
77       */
78      public static String getCommandLineOption(String option, String args[], String opts[][]) throws Exception
79      {
80          Class clazz = SystemUtils.class;
81          Method m = clazz.getMethod("getCommandLineOption", new Class[] {String.class, String[].class, String[][].class});
82          Object result = m.invoke(null, new Object[] { option, args, opts});
83          return (String) result;
84      }
85  
86      /**
87       * Wrap {@link WrapperSimpleApp#main(String[])}.
88       */
89      public static void wrapperMain(String[] args) throws Exception
90      {
91          Class clazz = WrapperSimpleApp.class;
92          Method m = clazz.getMethod("main", new Class[] {String[].class});
93          m.invoke(null, new Object[] {args});
94      }
95  
96      /**
97       * Wrap {@link WrapperSimpleApp#stop(int)}.
98       */
99      public static void wrapperStop(int exitCode) throws Exception
100     {
101         Class clazz = WrapperSimpleApp.class;
102         Method m = clazz.getMethod("stop", new Class[] {int.class});
103         m.invoke(null, new Object[] {new Integer(exitCode)});
104     }
105 
106 }