01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.aspect;
05:
06: import java.lang.reflect.Constructor;
07:
08: import com.tc.aspectwerkz.DeploymentModel;
09: import com.tc.aspectwerkz.exception.DefinitionException;
10:
11: /**
12: * Abstract base class for the mixin container implementations.
13: *
14: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15: */
16: public abstract class AbstractMixinFactory implements MixinFactory {
17:
18: protected final Class m_mixinClass;
19: protected final DeploymentModel m_deploymentModel;
20: protected Constructor m_defaultConstructor;
21: protected Constructor m_perClassConstructor;
22: protected Constructor m_perInstanceConstructor;
23:
24: /**
25: * Creates a new mixin factory.
26: *
27: * @param mixinClass
28: * @param deploymentModel
29: */
30: public AbstractMixinFactory(final Class mixinClass,
31: 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
37: .getConstructor(new Class[] { Class.class });
38: } else if (m_deploymentModel
39: .equals(DeploymentModel.PER_INSTANCE)) {
40: m_perInstanceConstructor = m_mixinClass
41: .getConstructor(new Class[] { Object.class });
42: } else if (m_deploymentModel
43: .equals(DeploymentModel.PER_JVM)) {
44: m_defaultConstructor = m_mixinClass
45: .getConstructor(new Class[] {});
46: } else {
47: throw new DefinitionException("deployment model for ["
48: + m_mixinClass.getName()
49: + "] is not supported [" + m_deploymentModel
50: + "]");
51: }
52: } catch (NoSuchMethodException e1) {
53: try {
54: m_defaultConstructor = m_mixinClass
55: .getConstructor(new Class[] {});
56: } catch (NoSuchMethodException e2) {
57: throw new DefinitionException(
58: "mixin ["
59: + m_mixinClass.getName()
60: + "] does not have a constructor that matches with its deployment model or a non-argument default constructor");
61: }
62: }
63: }
64:
65: /**
66: * Creates a new perJVM mixin instance.
67: *
68: * @return the mixin instance
69: */
70: public abstract Object mixinOf();
71:
72: /**
73: * Creates a new perClass mixin instance.
74: *
75: * @param klass
76: * @return the mixin instance
77: */
78: public abstract Object mixinOf(Class klass);
79:
80: /**
81: * Creates a new perInstance mixin instance.
82: *
83: * @param instance
84: * @return the mixin instance
85: */
86: public abstract Object mixinOf(Object instance);
87: }
|