1 4 package com.tc.aspectwerkz.aspect.management; 5 6 7 import com.tc.aspectwerkz.DeploymentModel; 8 import com.tc.aspectwerkz.aspect.container.AspectFactoryManager; 9 import com.tc.aspectwerkz.definition.AspectDefinition; 10 import com.tc.aspectwerkz.definition.SystemDefinition; 11 import com.tc.aspectwerkz.definition.SystemDefinitionContainer; 12 import com.tc.aspectwerkz.util.ContextClassLoader; 13 14 import java.util.*; 15 import java.lang.reflect.Method ; 16 17 26 public class Aspects { 27 28 36 private static String [] getAspectQNameAndAspectClassName(final ClassLoader visibleFrom, final String qName) { 37 AspectDefinition aspectDefinition = lookupAspectDefinition(visibleFrom, qName); 38 return new String []{aspectDefinition.getQualifiedName(), aspectDefinition.getClassName()}; 39 } 40 41 48 public static Object aspectOf(final String qName) { 49 return aspectOf(Thread.currentThread().getContextClassLoader(), qName); 50 } 51 52 60 public static Object aspectOf(final Class aspectClass) { 61 String aspectClassName = aspectClass.getName().replace('/', '.'); 62 return aspectOf(aspectClass.getClassLoader(), aspectClassName); 63 } 64 65 72 public static Object aspectOf(final ClassLoader visibleFrom, final String qName) { 73 String [] qNameAndAspectClassName = getAspectQNameAndAspectClassName(visibleFrom, qName); 74 return aspect$Of(qNameAndAspectClassName[0], qNameAndAspectClassName[1], visibleFrom); 75 } 76 77 85 public static Object aspectOf(final Class aspectClass, final Class targetClass) { 86 String aspectClassName = aspectClass.getName().replace('/', '.'); 87 return aspectOf(aspectClassName, targetClass); 88 } 89 90 97 public static Object aspectOf(final String qName, final Class targetClass) { 98 String [] qNameAndAspectClassName = getAspectQNameAndAspectClassName(targetClass.getClassLoader(), qName); 100 return aspect$Of(qNameAndAspectClassName[0], qNameAndAspectClassName[1], targetClass); 101 } 102 103 111 public static Object aspectOf(final Class aspectClass, final Object targetInstance) { 112 String aspectClassName = aspectClass.getName().replace('/', '.'); 113 return aspectOf(aspectClassName, targetInstance); 114 } 115 116 123 public static Object aspectOf(final String qName, final Object targetInstance) { 124 AspectDefinition aspectDef = lookupAspectDefinition(targetInstance.getClass().getClassLoader(), qName); 126 DeploymentModel deployModel = aspectDef.getDeploymentModel(); 127 String [] qNameAndAspectClassName = getAspectQNameAndAspectClassName( 128 targetInstance.getClass().getClassLoader(), qName); 129 130 if (DeploymentModel.PER_INSTANCE.equals(deployModel) 131 || DeploymentModel.PER_THIS.equals(deployModel) 132 || DeploymentModel.PER_TARGET.equals(deployModel)) { 133 return aspect$Of(qNameAndAspectClassName[0], qNameAndAspectClassName[1], targetInstance); 134 } else { 135 throw new NoAspectBoundException("Cannot retrieve instance level aspect with " 136 + "deployment-scope " 137 + deployModel.toString() 138 + " named ", 139 qName); 140 } 141 } 142 143 150 public static boolean hasAspect(final String qName, final Object targetInstance) { 151 String [] qNameAndAspectClassName = getAspectQNameAndAspectClassName(targetInstance.getClass().getClassLoader(), qName); 152 try { 153 Class factory = ContextClassLoader.forName( 154 targetInstance.getClass().getClassLoader(), 155 AspectFactoryManager.getAspectFactoryClassName(qNameAndAspectClassName[1], qName).replace('/', '.') 156 ); 157 Method m = factory.getMethod("hasAspect", new Class []{Object .class}); 158 Boolean b = (Boolean ) m.invoke(null, new Object []{targetInstance}); 159 return b.booleanValue(); 160 } catch (Throwable t) { 161 return false; 162 } 163 } 164 165 168 public static Object aspect$Of(String qName, String aspectClassName, ClassLoader loader) { 169 try { 170 Class factory = ContextClassLoader.forName( 171 loader, 172 AspectFactoryManager.getAspectFactoryClassName(aspectClassName, qName).replace('/', '.') 173 ); 174 Method m = factory.getMethod("aspectOf", new Class [0]); 175 return m.invoke(null, new Object [0]); 176 } catch (NoAspectBoundException nabe) { 177 throw nabe; 178 } catch (Throwable t) { 179 throw new NoAspectBoundException(t, qName); 180 } 181 } 182 183 public static Object aspect$Of(String qName, String aspectClassName, final Class perClass) { 184 try { 185 Class factory = ContextClassLoader.forName( 186 perClass.getClassLoader(), 187 AspectFactoryManager.getAspectFactoryClassName(aspectClassName, qName).replace('/', '.') 188 ); 189 Method m = factory.getMethod("aspectOf", new Class []{Class .class}); 190 return m.invoke(null, new Object []{perClass}); 191 } catch (NoAspectBoundException nabe) { 192 throw nabe; 193 } catch (Throwable t) { 194 throw new NoAspectBoundException(t, qName); 195 } 196 } 197 198 public static Object aspect$Of(String qName, String aspectClassName, final Object perInstance) { 199 try { 200 ClassLoader loader = perInstance.getClass().getClassLoader(); 201 Class containerClass = ContextClassLoader.forName( 202 loader, 203 AspectFactoryManager.getAspectFactoryClassName(aspectClassName, qName).replace('/', '.') 204 ); 205 Method m = containerClass.getMethod("aspectOf", new Class []{Object .class}); 206 return m.invoke(null, new Object []{perInstance}); 207 } catch (NoAspectBoundException nabe) { 208 throw nabe; 209 } catch (Throwable t) { 210 throw new NoAspectBoundException(t, qName); 211 } 212 } 213 214 216 224 private static AspectDefinition lookupAspectDefinition(final ClassLoader visibleFrom, final String qName) { 225 AspectDefinition aspectDefinition = null; 226 227 Set definitions = SystemDefinitionContainer.getDefinitionsFor(visibleFrom); 228 if (qName.indexOf('/') > 0) { 229 for (Iterator iterator = definitions.iterator(); iterator.hasNext();) { 231 SystemDefinition systemDefinition = (SystemDefinition) iterator.next(); 232 if (!qName.startsWith(systemDefinition.getUuid())) { 233 continue; 234 } 235 for (Iterator iterator1 = systemDefinition.getAspectDefinitions().iterator(); iterator1.hasNext();) { 236 AspectDefinition aspectDef = (AspectDefinition) iterator1.next(); 237 if (qName.equals(aspectDef.getQualifiedName())) { 238 aspectDefinition = aspectDef; 239 break; 240 } 241 } 242 } 243 } else { 244 int found = 0; 247 for (Iterator iterator = definitions.iterator(); iterator.hasNext();) { 248 SystemDefinition systemDefinition = (SystemDefinition) iterator.next(); 249 for (Iterator iterator1 = systemDefinition.getAspectDefinitions().iterator(); iterator1.hasNext();) { 250 AspectDefinition aspectDef = (AspectDefinition) iterator1.next(); 251 if (qName.equals(aspectDef.getClassName())) { 252 aspectDefinition = aspectDef; 253 found++; 254 } 255 } 256 } 257 if (found > 1) { 258 throw new NoAspectBoundException("More than one AspectDefinition found, consider using other API methods", qName); 259 } 260 261 } 262 263 if (aspectDefinition == null) { 264 throw new NoAspectBoundException("Could not find AspectDefinition", qName); 265 } 266 267 return aspectDefinition; 268 } 269 270 273 private Aspects() { 274 } 275 276 } 277 | Popular Tags |