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;
005:
006: import com.tc.aspectwerkz.definition.AspectDefinition;
007:
008: import java.io.ObjectInputStream;
009: import java.lang.ref.WeakReference;
010: import java.util.HashMap;
011: import java.util.Map;
012:
013: /**
014: * Contains information about and for classes that has been defined as cross-cutting.
015: *
016: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
017: */
018: public final class AspectContext {
019: /**
020: * An empty <code>Object</code> array.
021: */
022: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[] {};
023:
024: /**
025: * The name for the cross-cuttable class.
026: */
027: private String m_name;
028:
029: /**
030: * The qualified name of the aspect. Stored for serialization purpose.
031: */
032: private String m_qName;
033:
034: /**
035: * The aspect class, wrapped in a weak reference since is a key of aspect container referenced by this object.
036: */
037: private transient WeakReference m_aspectClassRef;
038:
039: /**
040: * Holds the deployment model.
041: */
042: private DeploymentModel m_deploymentModel;
043:
044: /**
045: * Holds the parameters passed to the aspect.
046: */
047: private Map m_parameters = new HashMap();
048:
049: /**
050: * Holds the metadata.
051: */
052: private Map m_metaData = new HashMap();
053:
054: /**
055: * The UUID for the system.
056: */
057: private String m_uuid;
058:
059: /**
060: * The aspect definition.
061: */
062: private transient AspectDefinition m_aspectDefinition;
063:
064: /**
065: * The associated object.
066: * Null for perJVM, but present for the class, target, this, instance or thread deployment models.
067: */
068: private transient Object m_associatedObject;
069:
070: /**
071: * Creates a new cross-cutting info instance.
072: *
073: * @param uuid
074: * @param aspectClass
075: * @param deploymentModel
076: * @param aspectDef
077: * @param parameters
078: * @param associated instance (null/class/instance/thread)
079: */
080: public AspectContext(final String uuid, final Class aspectClass,
081: final String name, final DeploymentModel deploymentModel,
082: final AspectDefinition aspectDef, final Map parameters,
083: final Object associated) {
084: m_uuid = uuid;
085: m_aspectClassRef = new WeakReference(aspectClass);
086: m_name = name;
087: m_qName = aspectDef.getQualifiedName();
088: m_deploymentModel = deploymentModel;
089: m_aspectDefinition = aspectDef;
090: if (parameters != null) {
091: m_parameters = parameters;
092: }
093: m_associatedObject = associated;
094: }
095:
096: /**
097: * Returns the UUID for the system.
098: *
099: * @return the UUID for the system
100: */
101: public String getUuid() {
102: return m_uuid;
103: }
104:
105: /**
106: * Returns the name of the aspect.
107: *
108: * @return the name of the aspect
109: */
110: public String getName() {
111: return m_name;
112: }
113:
114: /**
115: * Returns the deployment model.
116: *
117: * @return the deployment model
118: */
119: public DeploymentModel getDeploymentModel() {
120: return m_deploymentModel;
121: }
122:
123: /**
124: * Returns the cross-cuttable class.
125: *
126: * @return the cross-cuttable class
127: */
128: public Class getAspectClass() {
129: return (Class) m_aspectClassRef.get();
130: }
131:
132: /**
133: * Returns the aspect definition.
134: * <p/>
135: * Will return null after deserialization.
136: *
137: * @return the aspect definition
138: */
139: public AspectDefinition getAspectDefinition() {
140: return m_aspectDefinition;
141: }
142:
143: /**
144: * Sets a parameter.
145: *
146: * @param name the name of the parameter
147: * @param value the value of the parameter
148: */
149: public void setParameter(final String name, final String value) {
150: m_parameters.put(name, value);
151: }
152:
153: /**
154: * Returns the value of a parameter.
155: *
156: * @param name the name of the parameter
157: * @return the value of the parameter or null if not specified
158: */
159: public String getParameter(final String name) {
160: return (String) m_parameters.get(name);
161: }
162:
163: /**
164: * Adds metadata.
165: *
166: * @param key the key
167: * @param value the value
168: */
169: public void addMetaData(final Object key, final Object value) {
170: m_metaData.put(key, value);
171: }
172:
173: /**
174: * Returns the metadata for a specific key.
175: *
176: * @param key the key
177: * @return the value
178: */
179: public Object getMetaData(final Object key) {
180: return m_metaData.get(key);
181: }
182:
183: /**
184: * Returns the associated object with the aspect beeing instantiated. This depend on the aspect deployment model.
185: * It can be a null/class/instance/thread.
186: *
187: * @return the associated object
188: */
189: public Object getAssociatedObject() {
190: return m_associatedObject;
191: }
192:
193: /**
194: * Provides custom deserialization.
195: *
196: * @param stream the object input stream containing the serialized object
197: * @throws Exception in case of failure
198: */
199: private void readObject(final ObjectInputStream stream)
200: throws Exception {
201: ObjectInputStream.GetField fields = stream.readFields();
202: m_uuid = (String) fields.get("m_uuid", null);
203: m_name = (String) fields.get("m_name", null);
204: m_qName = (String) fields.get("m_qName", null);
205: Class aspectClass = Class.forName(m_name);
206: m_aspectClassRef = new WeakReference(aspectClass);
207: m_deploymentModel = (DeploymentModel) fields.get(
208: "m_deploymentModel", DeploymentModel.PER_JVM);
209: m_parameters = (Map) fields.get("m_parameters", new HashMap());
210: m_metaData = (Map) fields.get("m_metaData", new HashMap());
211:
212: //TODO aspectDef from m_qName
213: }
214: }
|