KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > inlining > deployer > DeploymentHandle


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.transform.inlining.deployer;
5
6 import com.tc.aspectwerkz.definition.AdviceDefinition;
7 import com.tc.aspectwerkz.definition.AspectDefinition;
8 import com.tc.aspectwerkz.definition.SystemDefinition;
9 import com.tc.aspectwerkz.definition.SystemDefinitionContainer;
10 import com.tc.aspectwerkz.expression.ExpressionInfo;
11 import com.tc.aspectwerkz.util.UuidGenerator;
12
13 import java.lang.ref.WeakReference JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * Universal Unique IDentifier (UUID) for a deployment event.
20  * <p/>
21  * Can be stored by the user to allow access to a specific deployment event.
22  * <p/>
23  * Visibility for all methods are package private, user should only use it as a handle.
24  *
25  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
26  */

27 public final class DeploymentHandle {
28
29   private final String JavaDoc UUID;
30   private final Map JavaDoc m_definitionChangeElements = new HashMap JavaDoc();
31   private final WeakReference JavaDoc m_loaderRef;
32   private final WeakReference JavaDoc m_classRef;
33
34   /**
35    * Creates a new handle.
36    *
37    * @param clazz the class of the entity being deployed
38    */

39   DeploymentHandle(final Class JavaDoc clazz, final ClassLoader JavaDoc loader) {
40     if (clazz == null) {
41       throw new IllegalArgumentException JavaDoc("class can not be null");
42     }
43     if (loader == null) {
44       throw new IllegalArgumentException JavaDoc("loader can not be null");
45     }
46     //TODO this Uuid is very slow at 1st invocation. Should we use the other one ?
47
UUID = UuidGenerator.generate(clazz);
48     m_loaderRef = new WeakReference JavaDoc(loader);
49     m_classRef = new WeakReference JavaDoc(clazz);
50   }
51
52   void registerDefinitionChange(final AdviceDefinition adviceDef, final ExpressionInfo oldExpression) {
53     m_definitionChangeElements.put(
54             adviceDef.getQualifiedName(),
55             new DefinitionChangeElement(adviceDef, oldExpression)
56     );
57   }
58
59   Class JavaDoc getAspectClass() {
60     return (Class JavaDoc) m_classRef.get();
61   }
62
63   Map JavaDoc getDefintionChangeElements() {
64     return m_definitionChangeElements;
65   }
66
67   void revertChanges() {
68     final ClassLoader JavaDoc loader = (ClassLoader JavaDoc) m_loaderRef.get();
69     // hotdeployment is done thru the virtual system, so reverts changes as well
70
SystemDefinition systemDef = SystemDefinitionContainer.getVirtualDefinitionFor(loader);
71     for (Iterator JavaDoc it2 = systemDef.getAspectDefinitions().iterator(); it2.hasNext();) {
72       AspectDefinition aspectDef = (AspectDefinition) it2.next();
73       for (Iterator JavaDoc it3 = aspectDef.getAfterAdviceDefinitions().iterator(); it3.hasNext();) {
74         AdviceDefinition adviceDef = (AdviceDefinition) it3.next();
75         DefinitionChangeElement changeElement =
76                 (DefinitionChangeElement) m_definitionChangeElements.get(adviceDef.getQualifiedName());
77         if (changeElement != null) {
78           changeElement.getAdviceDef().setExpressionInfo(changeElement.getOldExpression());
79         }
80       }
81     }
82   }
83
84   public String JavaDoc toString() {
85     return new StringBuffer JavaDoc().append("DeploymentHandle [").
86             append(UUID.toString()).append(',').
87             append(((Class JavaDoc) m_classRef.get()).getName()).append(',').
88             append(m_loaderRef.get()).append(']').toString();
89   }
90
91   public int hashCode() {
92     return UUID.hashCode();
93   }
94
95   public boolean equals(Object JavaDoc o) {
96     return ((DeploymentHandle) o).UUID.equals(UUID);
97   }
98
99   /**
100    * Holds the definition change of one advice.
101    *
102    * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
103    */

104   static class DefinitionChangeElement {
105     private final AdviceDefinition m_adviceDef;
106     private final ExpressionInfo m_oldExpression;
107
108     public DefinitionChangeElement(final AdviceDefinition adviceDef, final ExpressionInfo oldExpression) {
109       m_adviceDef = adviceDef;
110       m_oldExpression = oldExpression;
111     }
112
113     public ExpressionInfo getOldExpression() {
114       return m_oldExpression;
115     }
116
117     public AdviceDefinition getAdviceDef() {
118       return m_adviceDef;
119     }
120   }
121 }
122
Popular Tags