KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > AspectContext


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;
5
6 import com.tc.aspectwerkz.definition.AspectDefinition;
7
8 import java.io.ObjectInputStream JavaDoc;
9 import java.lang.ref.WeakReference JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /**
14  * Contains information about and for classes that has been defined as cross-cutting.
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public final class AspectContext {
19   /**
20    * An empty <code>Object</code> array.
21    */

22   public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[]{};
23
24   /**
25    * The name for the cross-cuttable class.
26    */

27   private String JavaDoc m_name;
28
29   /**
30    * The qualified name of the aspect. Stored for serialization purpose.
31    */

32   private String JavaDoc m_qName;
33
34   /**
35    * The aspect class, wrapped in a weak reference since is a key of aspect container referenced by this object.
36    */

37   private transient WeakReference JavaDoc m_aspectClassRef;
38
39   /**
40    * Holds the deployment model.
41    */

42   private DeploymentModel m_deploymentModel;
43
44   /**
45    * Holds the parameters passed to the aspect.
46    */

47   private Map JavaDoc m_parameters = new HashMap JavaDoc();
48
49   /**
50    * Holds the metadata.
51    */

52   private Map JavaDoc m_metaData = new HashMap JavaDoc();
53
54   /**
55    * The UUID for the system.
56    */

57   private String JavaDoc m_uuid;
58
59   /**
60    * The aspect definition.
61    */

62   private transient AspectDefinition m_aspectDefinition;
63
64   /**
65    * The associated object.
66    * Null for perJVM, but present for the class, target, this, instance or thread deployment models.
67    */

68   private transient Object JavaDoc m_associatedObject;
69
70   /**
71    * Creates a new cross-cutting info instance.
72    *
73    * @param uuid
74    * @param aspectClass
75    * @param deploymentModel
76    * @param aspectDef
77    * @param parameters
78    * @param associated instance (null/class/instance/thread)
79    */

80   public AspectContext(final String JavaDoc uuid,
81                        final Class JavaDoc aspectClass,
82                        final String JavaDoc name,
83                        final DeploymentModel deploymentModel,
84                        final AspectDefinition aspectDef,
85                        final Map JavaDoc parameters,
86                        final Object JavaDoc associated) {
87     m_uuid = uuid;
88     m_aspectClassRef = new WeakReference JavaDoc(aspectClass);
89     m_name = name;
90     m_qName = aspectDef.getQualifiedName();
91     m_deploymentModel = deploymentModel;
92     m_aspectDefinition = aspectDef;
93     if (parameters != null) {
94       m_parameters = parameters;
95     }
96     m_associatedObject = associated;
97   }
98
99   /**
100    * Returns the UUID for the system.
101    *
102    * @return the UUID for the system
103    */

104   public String JavaDoc getUuid() {
105     return m_uuid;
106   }
107
108   /**
109    * Returns the name of the aspect.
110    *
111    * @return the name of the aspect
112    */

113   public String JavaDoc getName() {
114     return m_name;
115   }
116
117   /**
118    * Returns the deployment model.
119    *
120    * @return the deployment model
121    */

122   public DeploymentModel getDeploymentModel() {
123     return m_deploymentModel;
124   }
125
126   /**
127    * Returns the cross-cuttable class.
128    *
129    * @return the cross-cuttable class
130    */

131   public Class JavaDoc getAspectClass() {
132     return (Class JavaDoc) m_aspectClassRef.get();
133   }
134
135   /**
136    * Returns the aspect definition.
137    * <p/>
138    * Will return null after deserialization.
139    *
140    * @return the aspect definition
141    */

142   public AspectDefinition getAspectDefinition() {
143     return m_aspectDefinition;
144   }
145
146   /**
147    * Sets a parameter.
148    *
149    * @param name the name of the parameter
150    * @param value the value of the parameter
151    */

152   public void setParameter(final String JavaDoc name, final String JavaDoc value) {
153     m_parameters.put(name, value);
154   }
155
156   /**
157    * Returns the value of a parameter.
158    *
159    * @param name the name of the parameter
160    * @return the value of the parameter or null if not specified
161    */

162   public String JavaDoc getParameter(final String JavaDoc name) {
163     return (String JavaDoc) m_parameters.get(name);
164   }
165
166   /**
167    * Adds metadata.
168    *
169    * @param key the key
170    * @param value the value
171    */

172   public void addMetaData(final Object JavaDoc key, final Object JavaDoc value) {
173     m_metaData.put(key, value);
174   }
175
176   /**
177    * Returns the metadata for a specific key.
178    *
179    * @param key the key
180    * @return the value
181    */

182   public Object JavaDoc getMetaData(final Object JavaDoc key) {
183     return m_metaData.get(key);
184   }
185
186   /**
187    * Returns the associated object with the aspect beeing instantiated. This depend on the aspect deployment model.
188    * It can be a null/class/instance/thread.
189    *
190    * @return the associated object
191    */

192   public Object JavaDoc getAssociatedObject() {
193     return m_associatedObject;
194   }
195
196   /**
197    * Provides custom deserialization.
198    *
199    * @param stream the object input stream containing the serialized object
200    * @throws Exception in case of failure
201    */

202   private void readObject(final ObjectInputStream JavaDoc stream) throws Exception JavaDoc {
203     ObjectInputStream.GetField JavaDoc fields = stream.readFields();
204     m_uuid = (String JavaDoc) fields.get("m_uuid", null);
205     m_name = (String JavaDoc) fields.get("m_name", null);
206     m_qName = (String JavaDoc) fields.get("m_qName", null);
207     Class JavaDoc aspectClass = Class.forName(m_name);
208     m_aspectClassRef = new WeakReference JavaDoc(aspectClass);
209     m_deploymentModel = (DeploymentModel) fields.get("m_deploymentModel", DeploymentModel.PER_JVM);
210     m_parameters = (Map JavaDoc) fields.get("m_parameters", new HashMap JavaDoc());
211     m_metaData = (Map JavaDoc) fields.get("m_metaData", new HashMap JavaDoc());
212
213     //TODO aspectDef from m_qName
214
}
215 }
Popular Tags