1 8 package org.codehaus.aspectwerkz.transform.inlining; 9 10 import java.util.StringTokenizer ; 11 12 import org.codehaus.aspectwerkz.util.ContextClassLoader; 13 import org.codehaus.aspectwerkz.util.ContextClassLoader; 14 import org.codehaus.aspectwerkz.transform.inlining.spi.AspectModel; 15 import org.codehaus.aspectwerkz.definition.AspectDefinition; 16 import org.codehaus.aspectwerkz.reflect.ClassInfo; 17 import org.codehaus.aspectwerkz.exception.DefinitionException; 18 19 24 public class AspectModelManager { 25 26 public static final String ASPECT_MODELS_VM_OPTION = "aspectwerkz.extension.aspectmodels"; 27 private static final String DELIMITER = ":"; 28 29 32 private static AspectModel[] ASPECT_MODELS = new AspectModel[]{}; 33 34 static { 35 registerAspectModels(System.getProperty(ASPECT_MODELS_VM_OPTION, null)); 36 } 37 38 43 public static AspectModel[] getModels() { 44 return ASPECT_MODELS; 45 } 46 47 53 public static AspectModel getModelFor(String type) { 54 for (int i = 0; i < ASPECT_MODELS.length; i++) { 55 AspectModel aspectModel = ASPECT_MODELS[i]; 56 if (aspectModel.getAspectModelType().equals(type)) { 57 return aspectModel; 58 } 59 } 60 return null; 61 } 62 63 70 public static void defineAspect(final ClassInfo aspectClassInfo, 71 final AspectDefinition aspectDef, 72 final ClassLoader loader) { 73 for (int i = 0; i < ASPECT_MODELS.length; i++) { 74 ASPECT_MODELS[i].defineAspect(aspectClassInfo, aspectDef, loader); 75 } 76 } 77 78 83 private static void registerAspectModels(final String aspectModels) { 84 if (aspectModels != null) { 85 StringTokenizer tokenizer = new StringTokenizer (aspectModels, DELIMITER); 86 ASPECT_MODELS = new AspectModel[tokenizer.countTokens()]; 87 for (int i = 0; i < ASPECT_MODELS.length; i++) { 88 final String className = tokenizer.nextToken(); 89 try { 90 final Class modelClass = ContextClassLoader.forName(className); 91 ASPECT_MODELS[i] = (AspectModel) modelClass.newInstance(); 92 } catch (ClassNotFoundException e) { 93 throw new DefinitionException( 94 "aspect model implementation class not found [" + 95 className + "]: " + e.toString() 96 ); 97 } catch (Exception e) { 98 throw new DefinitionException( 99 "aspect model implementation class could not be instantiated [" + 100 className + 101 "] - make sure it has a default no argument constructor: " + 102 e.toString() 103 ); 104 } 105 } 106 } 107 } 108 } 109 | Popular Tags |