1 4 package com.tc.aspectwerkz.transform.inlining; 5 6 7 import com.tc.aspectwerkz.definition.AspectDefinition; 8 import com.tc.aspectwerkz.exception.DefinitionException; 9 import com.tc.aspectwerkz.transform.inlining.model.AopAllianceAspectModel; 10 import com.tc.aspectwerkz.transform.inlining.model.AspectWerkzAspectModel; 11 import com.tc.aspectwerkz.transform.inlining.model.SpringAspectModel; 12 import com.tc.aspectwerkz.transform.inlining.spi.AspectModel; 13 import com.tc.aspectwerkz.util.ContextClassLoader; 14 import com.tc.aspectwerkz.reflect.ClassInfo; 15 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.StringTokenizer ; 20 21 26 public class AspectModelManager { 27 28 public static final String ASPECT_MODELS_VM_OPTION = "aspectwerkz.extension.aspectmodels"; 29 private static final String DELIMITER = ":"; 30 31 34 private static List ASPECT_MODELS = new ArrayList (1); 35 36 static { 37 ASPECT_MODELS.add(new AspectWerkzAspectModel()); 38 ASPECT_MODELS.add(new AopAllianceAspectModel()); 39 ASPECT_MODELS.add(new SpringAspectModel()); 40 registerCustomAspectModels(System.getProperty(ASPECT_MODELS_VM_OPTION, null)); 41 42 } 43 44 49 public static AspectModel[] getModels() { 50 return (AspectModel[]) ASPECT_MODELS.toArray(new AspectModel[]{}); 51 } 52 53 59 public static AspectModel getModelFor(String type) { 60 for (Iterator iterator = ASPECT_MODELS.iterator(); iterator.hasNext();) { 61 AspectModel aspectModel = (AspectModel) iterator.next(); 62 if (aspectModel.getAspectModelType().equals(type)) { 63 return aspectModel; 64 } 65 } 66 return null; 67 } 68 69 76 public static void defineAspect(final ClassInfo aspectClassInfo, 77 final AspectDefinition aspectDef, 78 final ClassLoader loader) { 79 for (Iterator iterator = ASPECT_MODELS.iterator(); iterator.hasNext();) { 80 AspectModel aspectModel = (AspectModel) iterator.next(); 81 aspectModel.defineAspect(aspectClassInfo, aspectDef, loader); 82 } 83 } 84 85 90 public static void registerCustomAspectModels(final String aspectModels) { 91 if (aspectModels != null) { 92 StringTokenizer tokenizer = new StringTokenizer (aspectModels, DELIMITER); 93 while (tokenizer.hasMoreTokens()) { 94 final String className = tokenizer.nextToken(); 95 try { 96 final Class modelClass = ContextClassLoader.forName(className); 97 ASPECT_MODELS.add((AspectModel) modelClass.newInstance()); 98 } catch (ClassNotFoundException e) { 99 throw new DefinitionException( 100 "aspect model implementation class not found [" + 101 className + "]: " + e.toString() 102 ); 103 } catch (Exception e) { 104 throw new DefinitionException( 105 "aspect model implementation class could not be instantiated [" + 106 className + 107 "] - make sure it has a default no argument constructor: " + 108 e.toString() 109 ); 110 } 111 } 112 } 113 } 114 } 115 | Popular Tags |