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 java.util.Map;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.Set;
14  import java.lang.ref.WeakReference;
15  
16  import org.codehaus.aspectwerkz.util.UuidGenerator;
17  import org.codehaus.aspectwerkz.definition.AdviceDefinition;
18  import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
19  import org.codehaus.aspectwerkz.definition.SystemDefinition;
20  import org.codehaus.aspectwerkz.definition.AspectDefinition;
21  import org.codehaus.aspectwerkz.expression.ExpressionInfo;
22  
23  /***
24   * Universal Unique IDentifier (UUID) for a deployment event.
25   * <p/>
26   * Can be stored by the user to allow access to a specific deployment event.
27   * <p/>
28   * Visibility for all methods are package private, user should only use it as a handle.
29   *
30   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
31   */
32  public final class DeploymentHandle {
33  
34      private final String UUID;
35      private final Map m_definitionChangeElements = new HashMap();
36      private final WeakReference m_loaderRef;
37      private final WeakReference m_classRef;
38  
39      /***
40       * Creates a new handle.
41       *
42       * @param clazz the class of the entity being deployed
43       */
44      DeploymentHandle(final Class clazz, final ClassLoader loader) {
45          if (clazz == null) {
46              throw new IllegalArgumentException("class can not be null");
47          }
48          if (loader == null) {
49              throw new IllegalArgumentException("loader can not be null");
50          }
51          UUID = UuidGenerator.generate(clazz);
52          m_loaderRef = new WeakReference(loader);
53          m_classRef = new WeakReference(clazz);
54      }
55  
56      void registerDefinitionChange(final AdviceDefinition adviceDef, final ExpressionInfo oldExpression) {
57          m_definitionChangeElements.put(
58                  adviceDef.getQualifiedName(),
59                  new DefinitionChangeElement(adviceDef, oldExpression)
60          );
61      }
62  
63      Class getAspectClass() {
64          return (Class) m_classRef.get();
65      }
66  
67      Map getDefintionChangeElements() {
68          return m_definitionChangeElements;
69      }
70  
71      void revertChanges() {
72          final ClassLoader loader = (ClassLoader) m_loaderRef.get();
73          // hotdeployment is done thru the virtual system, so reverts changes as well
74          SystemDefinition systemDef = SystemDefinitionContainer.getVirtualDefinitionAt(loader);
75          for (Iterator it2 = systemDef.getAspectDefinitions().iterator(); it2.hasNext();) {
76              AspectDefinition aspectDef = (AspectDefinition) it2.next();
77              for (Iterator it3 = aspectDef.getAfterAdviceDefinitions().iterator(); it3.hasNext();) {
78                  AdviceDefinition adviceDef = (AdviceDefinition) it3.next();
79                  DefinitionChangeElement changeElement =
80                          (DefinitionChangeElement) m_definitionChangeElements.get(adviceDef.getQualifiedName());
81                  if (changeElement != null) {
82                      changeElement.getAdviceDef().setExpressionInfo(changeElement.getOldExpression());
83                  }
84              }
85          }
86      }
87  
88      public String toString() {
89          return new StringBuffer().append("DeploymentHandle [").
90                  append(UUID.toString()).append(',').
91                  append(((Class) m_classRef.get()).getName()).append(',').
92                  append((ClassLoader) m_loaderRef.get()).append(']').toString();
93      }
94  
95      public int hashCode() {
96          return UUID.hashCode();
97      }
98  
99      public boolean equals(Object o) {
100         return ((DeploymentHandle) o).UUID.equals(UUID);
101     }
102 
103     /***
104      * Holds the definition change of one advice.
105      *
106      * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
107      */
108     static class DefinitionChangeElement {
109         private final AdviceDefinition m_adviceDef;
110         private final ExpressionInfo m_oldExpression;
111 
112         public DefinitionChangeElement(final AdviceDefinition adviceDef, final ExpressionInfo oldExpression) {
113             m_adviceDef = adviceDef;
114             m_oldExpression = oldExpression;
115         }
116 
117         public ExpressionInfo getOldExpression() {
118             return m_oldExpression;
119         }
120 
121         public AdviceDefinition getAdviceDef() {
122             return m_adviceDef;
123         }
124     }
125 }