KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > aspect > DefaultMixinFactory


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.aspect;
5
6
7 import com.tc.aspectwerkz.DeploymentModel;
8 import com.tc.aspectwerkz.exception.DefinitionException;
9 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
10
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.WeakHashMap JavaDoc;
14
15 /**
16  * Abstract base class for the mixin factory implementations.
17  *
18  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
20  */

21 public class DefaultMixinFactory extends AbstractMixinFactory {
22
23   private static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
24
25   private Object JavaDoc m_perJVM = null;
26
27   private Map JavaDoc m_perClassMixins = new WeakHashMap JavaDoc();
28
29   private Map JavaDoc m_perInstanceMixins = new WeakHashMap JavaDoc();
30
31   /**
32    * Creates a new default mixin factory.
33    *
34    * @param mixinClass
35    * @param deploymentModel
36    */

37   public DefaultMixinFactory(final Class JavaDoc mixinClass, final DeploymentModel deploymentModel) {
38     super(mixinClass, deploymentModel);
39   }
40
41   /**
42    * Creates a new perJVM mixin instance.
43    *
44    * @return the mixin instance
45    */

46   public Object JavaDoc mixinOf() {
47     if (m_perJVM != null) {
48       return m_perJVM;
49     }
50     synchronized (this) {
51       final Object JavaDoc mixin;
52       if (m_deploymentModel == DeploymentModel.PER_JVM) {
53         try {
54           mixin = m_defaultConstructor.newInstance(EMPTY_OBJECT_ARRAY);
55         } catch (InvocationTargetException JavaDoc e) {
56           throw new WrappedRuntimeException(e.getTargetException());
57         } catch (Exception JavaDoc e) {
58           throw new WrappedRuntimeException(e);
59         }
60       } else {
61         throw new DefinitionException(
62                 "Mixins.mixinOf() is can not be invoked for mixin deployed using as " +
63                         m_deploymentModel
64         );
65       }
66       m_perJVM = mixin;
67     }
68     return m_perJVM;
69   }
70
71   /**
72    * Creates a new perClass mixin instance.
73    *
74    * @param klass
75    * @return the mixin instance
76    */

77   public Object JavaDoc mixinOf(final Class JavaDoc klass) {
78     if (m_perClassMixins.containsKey(klass)) {
79       return m_perClassMixins.get(klass);
80     }
81     synchronized (m_perClassMixins) {
82       if (!m_perClassMixins.containsKey(klass)) {
83         final Object JavaDoc mixin;
84         if (m_deploymentModel == DeploymentModel.PER_CLASS) {
85           try {
86             if (m_perClassConstructor != null) {
87               mixin = m_perClassConstructor.newInstance(new Object JavaDoc[]{klass});
88             } else if (m_defaultConstructor != null) {
89               mixin = m_defaultConstructor.newInstance(new Object JavaDoc[]{});
90             } else {
91               throw new DefinitionException(
92                       "no valid constructor found for mixin [" + m_mixinClass.getName() + "]"
93               );
94             }
95           } catch (InvocationTargetException JavaDoc e) {
96             throw new WrappedRuntimeException(e.getTargetException());
97           } catch (Exception JavaDoc e) {
98             throw new WrappedRuntimeException(e);
99           }
100         } else {
101           throw new DefinitionException(
102                   "Mixins.mixinOf(Class) is can not be invoked for mixin deployed using as " +
103                           m_deploymentModel
104           );
105         }
106         m_perClassMixins.put(klass, mixin);
107       }
108       return m_perClassMixins.get(klass);
109     }
110   }
111
112   /**
113    * Creates a new perInstance mixin instance.
114    *
115    * @param instance
116    * @return the mixin instance
117    */

118   public Object JavaDoc mixinOf(final Object JavaDoc instance) {
119     if (m_perInstanceMixins.containsKey(instance)) {
120       return m_perInstanceMixins.get(instance);
121     }
122     synchronized (m_perInstanceMixins) {
123       if (!m_perInstanceMixins.containsKey(instance)) {
124         final Object JavaDoc mixin;
125         if (m_deploymentModel == DeploymentModel.PER_INSTANCE) {
126           try {
127             if (m_perInstanceConstructor != null) {
128               mixin = m_perInstanceConstructor.newInstance(new Object JavaDoc[]{instance});
129             } else if (m_defaultConstructor != null) {
130               mixin = m_defaultConstructor.newInstance(new Object JavaDoc[]{});
131             } else {
132               throw new DefinitionException(
133                       "no valid constructor found for mixin [" + m_mixinClass.getName() + "]"
134               );
135             }
136           } catch (InvocationTargetException JavaDoc e) {
137             throw new WrappedRuntimeException(e.getTargetException());
138           } catch (Exception JavaDoc e) {
139             throw new WrappedRuntimeException(e);
140           }
141         } else {
142           throw new DefinitionException(
143                   "Mixins.mixinOf(Object) is can not be invoked for mixin deployed using as " +
144                           m_deploymentModel
145           );
146         }
147         m_perInstanceMixins.put(instance, mixin);
148       }
149       return m_perInstanceMixins.get(instance);
150     }
151   }
152 }
Popular Tags