KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Constructor JavaDoc;
7
8
9 import com.tc.aspectwerkz.DeploymentModel;
10 import com.tc.aspectwerkz.exception.DefinitionException;
11
12 /**
13  * Abstract base class for the mixin container implementations.
14  *
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  */

17 public abstract class AbstractMixinFactory implements MixinFactory {
18
19   protected final Class JavaDoc m_mixinClass;
20   protected final DeploymentModel m_deploymentModel;
21   protected Constructor JavaDoc m_defaultConstructor;
22   protected Constructor JavaDoc m_perClassConstructor;
23   protected Constructor JavaDoc m_perInstanceConstructor;
24
25   /**
26    * Creates a new mixin factory.
27    *
28    * @param mixinClass
29    * @param deploymentModel
30    */

31   public AbstractMixinFactory(final Class JavaDoc mixinClass, final DeploymentModel deploymentModel) {
32     m_mixinClass = mixinClass;
33     m_deploymentModel = deploymentModel;
34     try {
35       if (m_deploymentModel.equals(DeploymentModel.PER_CLASS)) {
36         m_perClassConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{Class JavaDoc.class});
37       } else if (m_deploymentModel.equals(DeploymentModel.PER_INSTANCE)) {
38         m_perInstanceConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{Object JavaDoc.class});
39       } else if (m_deploymentModel.equals(DeploymentModel.PER_JVM)) {
40         m_defaultConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{});
41       } else {
42         throw new DefinitionException(
43                 "deployment model for [" + m_mixinClass.getName() + "] is not supported [" +
44                         m_deploymentModel + "]"
45         );
46       }
47     } catch (NoSuchMethodException JavaDoc e1) {
48       try {
49         m_defaultConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{});
50       } catch (NoSuchMethodException JavaDoc e2) {
51         throw new DefinitionException(
52                 "mixin [" + m_mixinClass.getName() +
53                         "] does not have a constructor that matches with its deployment model or a non-argument default constructor"
54         );
55       }
56     }
57   }
58
59   /**
60    * Creates a new perJVM mixin instance.
61    *
62    * @return the mixin instance
63    */

64   public abstract Object JavaDoc mixinOf();
65
66   /**
67    * Creates a new perClass mixin instance.
68    *
69    * @param klass
70    * @return the mixin instance
71    */

72   public abstract Object JavaDoc mixinOf(Class JavaDoc klass);
73
74   /**
75    * Creates a new perInstance mixin instance.
76    *
77    * @param instance
78    * @return the mixin instance
79    */

80   public abstract Object JavaDoc mixinOf(Object JavaDoc instance);
81 }
Popular Tags