1 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 ; 12 import java.util.Map ; 13 import java.util.WeakHashMap ; 14 15 21 public class DefaultMixinFactory extends AbstractMixinFactory { 22 23 private static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 24 25 private Object m_perJVM = null; 26 27 private Map m_perClassMixins = new WeakHashMap (); 28 29 private Map m_perInstanceMixins = new WeakHashMap (); 30 31 37 public DefaultMixinFactory(final Class mixinClass, final DeploymentModel deploymentModel) { 38 super(mixinClass, deploymentModel); 39 } 40 41 46 public Object mixinOf() { 47 if (m_perJVM != null) { 48 return m_perJVM; 49 } 50 synchronized (this) { 51 final Object mixin; 52 if (m_deploymentModel == DeploymentModel.PER_JVM) { 53 try { 54 mixin = m_defaultConstructor.newInstance(EMPTY_OBJECT_ARRAY); 55 } catch (InvocationTargetException e) { 56 throw new WrappedRuntimeException(e.getTargetException()); 57 } catch (Exception 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 77 public Object mixinOf(final Class 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 mixin; 84 if (m_deploymentModel == DeploymentModel.PER_CLASS) { 85 try { 86 if (m_perClassConstructor != null) { 87 mixin = m_perClassConstructor.newInstance(new Object []{klass}); 88 } else if (m_defaultConstructor != null) { 89 mixin = m_defaultConstructor.newInstance(new Object []{}); 90 } else { 91 throw new DefinitionException( 92 "no valid constructor found for mixin [" + m_mixinClass.getName() + "]" 93 ); 94 } 95 } catch (InvocationTargetException e) { 96 throw new WrappedRuntimeException(e.getTargetException()); 97 } catch (Exception 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 118 public Object mixinOf(final Object 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 mixin; 125 if (m_deploymentModel == DeploymentModel.PER_INSTANCE) { 126 try { 127 if (m_perInstanceConstructor != null) { 128 mixin = m_perInstanceConstructor.newInstance(new Object []{instance}); 129 } else if (m_defaultConstructor != null) { 130 mixin = m_defaultConstructor.newInstance(new Object []{}); 131 } else { 132 throw new DefinitionException( 133 "no valid constructor found for mixin [" + m_mixinClass.getName() + "]" 134 ); 135 } 136 } catch (InvocationTargetException e) { 137 throw new WrappedRuntimeException(e.getTargetException()); 138 } catch (Exception 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 |