View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.transform.inlining.deployer;
9   
10  import org.codehaus.aspectwerkz.util.ContextClassLoader;
11  import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
12  
13  /***
14   * Factory for the different redefiner implementations.
15   *
16   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17   */
18  public class RedefinerFactory {
19      private static final String HOTSWAP_REDEFINER_CLASS_NAME =
20              "org.codehaus.aspectwerkz.extension.hotswap.HotSwapRedefiner";
21  
22      private static final String JVMTI_REDEFINER_CLASS_NAME =
23              "org.codehaus.aspectwerkz.hook.JVMTIRedefiner";
24  
25      /***
26       * Creates a new redefiner instance.
27       * Try first with JDK 5 and failover on Java 1.4 HotSwap (requires native AW module)
28       *
29       *
30       * @return the redefiner instance
31       */
32      public static Redefiner newRedefiner(final Type type) {
33          if (type.equals(Type.HOTSWAP)) {
34              try {
35                  Class redefinerClass = ContextClassLoader.forName(JVMTI_REDEFINER_CLASS_NAME);
36                  return (Redefiner) redefinerClass.newInstance();
37              } catch (Throwable t) {
38                  try {
39                      Class redefinerClass = ContextClassLoader.forName(HOTSWAP_REDEFINER_CLASS_NAME);
40                      return (Redefiner) redefinerClass.newInstance();
41                  } catch (ClassNotFoundException e) {
42                      // TODO this message will be wrong if Java 5 did not started a preMain
43                      throw new WrappedRuntimeException(
44                              "redefiner class [HotSwapRedefiner] could not be found on classpath, make sure you have the aspectwerkz extensions jar file in your classpath",
45                              e
46                      );
47                  } catch (Exception e) {
48                      // TODO this message will be wrong upon Java 5..
49                      throw new WrappedRuntimeException("redefiner class [HotSwapRedefiner] could not be instantiated", e);
50                  }
51              }
52  
53          } else if (type.equals(Type.JVMTI)) {
54              throw new UnsupportedOperationException("JVMTI is not supported yet");
55          } else {
56              throw new UnsupportedOperationException("unknown redefiner type: " + type.toString());
57          }
58      }
59  
60      /***
61       * Type-safe enum for the different redefiner implementations.
62       *
63       * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
64       */
65      public static class Type {
66          public static final Type HOTSWAP = new Type("HOTSWAP");
67          public static final Type JVMTI = new Type("JVMTI");
68  
69          private final String m_name;
70  
71          private Type(String name) {
72              m_name = name;
73          }
74  
75          public String toString() {
76              return m_name;
77          }
78      }
79  }