001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.transform.inlining.deployer;
005:
006: import com.tc.aspectwerkz.definition.AdviceDefinition;
007: import com.tc.aspectwerkz.definition.AspectDefinition;
008: import com.tc.aspectwerkz.definition.SystemDefinition;
009: import com.tc.aspectwerkz.definition.SystemDefinitionContainer;
010: import com.tc.aspectwerkz.expression.ExpressionInfo;
011: import com.tc.aspectwerkz.util.UuidGenerator;
012:
013: import java.lang.ref.WeakReference;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Map;
017:
018: /**
019: * Universal Unique IDentifier (UUID) for a deployment event.
020: * <p/>
021: * Can be stored by the user to allow access to a specific deployment event.
022: * <p/>
023: * Visibility for all methods are package private, user should only use it as a handle.
024: *
025: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
026: */
027: public final class DeploymentHandle {
028:
029: private final String UUID;
030: private final Map m_definitionChangeElements = new HashMap();
031: private final WeakReference m_loaderRef;
032: private final WeakReference m_classRef;
033:
034: /**
035: * Creates a new handle.
036: *
037: * @param clazz the class of the entity being deployed
038: */
039: DeploymentHandle(final Class clazz, final ClassLoader loader) {
040: if (clazz == null) {
041: throw new IllegalArgumentException("class can not be null");
042: }
043: if (loader == null) {
044: throw new IllegalArgumentException("loader can not be null");
045: }
046: //TODO this Uuid is very slow at 1st invocation. Should we use the other one ?
047: UUID = UuidGenerator.generate(clazz);
048: m_loaderRef = new WeakReference(loader);
049: m_classRef = new WeakReference(clazz);
050: }
051:
052: void registerDefinitionChange(final AdviceDefinition adviceDef,
053: final ExpressionInfo oldExpression) {
054: m_definitionChangeElements.put(adviceDef.getQualifiedName(),
055: new DefinitionChangeElement(adviceDef, oldExpression));
056: }
057:
058: Class getAspectClass() {
059: return (Class) m_classRef.get();
060: }
061:
062: Map getDefintionChangeElements() {
063: return m_definitionChangeElements;
064: }
065:
066: void revertChanges() {
067: final ClassLoader loader = (ClassLoader) m_loaderRef.get();
068: // hotdeployment is done thru the virtual system, so reverts changes as well
069: SystemDefinition systemDef = SystemDefinitionContainer
070: .getVirtualDefinitionFor(loader);
071: for (Iterator it2 = systemDef.getAspectDefinitions().iterator(); it2
072: .hasNext();) {
073: AspectDefinition aspectDef = (AspectDefinition) it2.next();
074: for (Iterator it3 = aspectDef.getAfterAdviceDefinitions()
075: .iterator(); it3.hasNext();) {
076: AdviceDefinition adviceDef = (AdviceDefinition) it3
077: .next();
078: DefinitionChangeElement changeElement = (DefinitionChangeElement) m_definitionChangeElements
079: .get(adviceDef.getQualifiedName());
080: if (changeElement != null) {
081: changeElement.getAdviceDef().setExpressionInfo(
082: changeElement.getOldExpression());
083: }
084: }
085: }
086: }
087:
088: public String toString() {
089: return new StringBuffer().append("DeploymentHandle [").append(
090: UUID.toString()).append(',').append(
091: ((Class) m_classRef.get()).getName()).append(',')
092: .append(m_loaderRef.get()).append(']').toString();
093: }
094:
095: public int hashCode() {
096: return UUID.hashCode();
097: }
098:
099: 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(
113: final AdviceDefinition adviceDef,
114: final ExpressionInfo oldExpression) {
115: m_adviceDef = adviceDef;
116: m_oldExpression = oldExpression;
117: }
118:
119: public ExpressionInfo getOldExpression() {
120: return m_oldExpression;
121: }
122:
123: public AdviceDefinition getAdviceDef() {
124: return m_adviceDef;
125: }
126: }
127: }
|