001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.transform.inlining.deployer;
008:
009: import java.util.Map;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.Set;
013: import java.lang.ref.WeakReference;
014:
015: import org.codehaus.aspectwerkz.util.UuidGenerator;
016: import org.codehaus.aspectwerkz.definition.AdviceDefinition;
017: import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
018: import org.codehaus.aspectwerkz.definition.SystemDefinition;
019: import org.codehaus.aspectwerkz.definition.AspectDefinition;
020: import org.codehaus.aspectwerkz.expression.ExpressionInfo;
021:
022: /**
023: * Universal Unique IDentifier (UUID) for a deployment event.
024: * <p/>
025: * Can be stored by the user to allow access to a specific deployment event.
026: * <p/>
027: * Visibility for all methods are package private, user should only use it as a handle.
028: *
029: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
030: */
031: public final class DeploymentHandle {
032:
033: private final String UUID;
034: private final Map m_definitionChangeElements = new HashMap();
035: private final WeakReference m_loaderRef;
036: private final WeakReference m_classRef;
037:
038: /**
039: * Creates a new handle.
040: *
041: * @param clazz the class of the entity being deployed
042: */
043: DeploymentHandle(final Class clazz, final ClassLoader loader) {
044: if (clazz == null) {
045: throw new IllegalArgumentException("class can not be null");
046: }
047: if (loader == null) {
048: throw new IllegalArgumentException("loader can not be null");
049: }
050: UUID = UuidGenerator.generate(clazz);
051: m_loaderRef = new WeakReference(loader);
052: m_classRef = new WeakReference(clazz);
053: }
054:
055: void registerDefinitionChange(final AdviceDefinition adviceDef,
056: final ExpressionInfo oldExpression) {
057: m_definitionChangeElements.put(adviceDef.getQualifiedName(),
058: new DefinitionChangeElement(adviceDef, oldExpression));
059: }
060:
061: Class getAspectClass() {
062: return (Class) m_classRef.get();
063: }
064:
065: Map getDefintionChangeElements() {
066: return m_definitionChangeElements;
067: }
068:
069: void revertChanges() {
070: final ClassLoader loader = (ClassLoader) m_loaderRef.get();
071: // hotdeployment is done thru the virtual system, so reverts changes as well
072: SystemDefinition systemDef = SystemDefinitionContainer
073: .getVirtualDefinitionAt(loader);
074: for (Iterator it2 = systemDef.getAspectDefinitions().iterator(); it2
075: .hasNext();) {
076: AspectDefinition aspectDef = (AspectDefinition) it2.next();
077: for (Iterator it3 = aspectDef.getAfterAdviceDefinitions()
078: .iterator(); it3.hasNext();) {
079: AdviceDefinition adviceDef = (AdviceDefinition) it3
080: .next();
081: DefinitionChangeElement changeElement = (DefinitionChangeElement) m_definitionChangeElements
082: .get(adviceDef.getQualifiedName());
083: if (changeElement != null) {
084: changeElement.getAdviceDef().setExpressionInfo(
085: changeElement.getOldExpression());
086: }
087: }
088: }
089: }
090:
091: public String toString() {
092: return new StringBuffer().append("DeploymentHandle [").append(
093: UUID.toString()).append(',').append(
094: ((Class) m_classRef.get()).getName()).append(',')
095: .append((ClassLoader) m_loaderRef.get()).append(']')
096: .toString();
097: }
098:
099: public int hashCode() {
100: return UUID.hashCode();
101: }
102:
103: public boolean equals(Object o) {
104: return ((DeploymentHandle) o).UUID.equals(UUID);
105: }
106:
107: /**
108: * Holds the definition change of one advice.
109: *
110: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
111: */
112: static class DefinitionChangeElement {
113: private final AdviceDefinition m_adviceDef;
114: private final ExpressionInfo m_oldExpression;
115:
116: public DefinitionChangeElement(
117: final AdviceDefinition adviceDef,
118: final ExpressionInfo oldExpression) {
119: m_adviceDef = adviceDef;
120: m_oldExpression = oldExpression;
121: }
122:
123: public ExpressionInfo getOldExpression() {
124: return m_oldExpression;
125: }
126:
127: public AdviceDefinition getAdviceDef() {
128: return m_adviceDef;
129: }
130: }
131: }
|