KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > transform > inlining > AspectModelManager


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.transform.inlining;
9
10 import java.util.StringTokenizer JavaDoc;
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 /**
20  * Manages the different aspect model implementations that is registered.
21  *
22  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
23  */

24 public class AspectModelManager {
25
26     public static final String JavaDoc ASPECT_MODELS_VM_OPTION = "aspectwerkz.extension.aspectmodels";
27     private static final String JavaDoc DELIMITER = ":";
28
29     /**
30      * The aspects models that are registered
31      */

32     private static AspectModel[] ASPECT_MODELS = new AspectModel[]{};
33
34     static {
35         registerAspectModels(System.getProperty(ASPECT_MODELS_VM_OPTION, null));
36     }
37
38     /**
39      * Returns an array with all the aspect models that has been registered.
40      *
41      * @return an array with the aspect models
42      */

43     public static AspectModel[] getModels() {
44         return ASPECT_MODELS;
45     }
46
47     /**
48      * Returns the advice model for a specific aspect model type id.
49      *
50      * @param type the aspect model type id
51      * @return the aspect model
52      */

53     public static AspectModel getModelFor(String JavaDoc 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     /**
64      * Let all aspect models try to define the aspect (only one will succeed).
65      *
66      * @param aspectClassInfo
67      * @param aspectDef
68      * @param loader
69      */

70     public static void defineAspect(final ClassInfo aspectClassInfo,
71                                     final AspectDefinition aspectDef,
72                                     final ClassLoader JavaDoc loader) {
73         for (int i = 0; i < ASPECT_MODELS.length; i++) {
74             ASPECT_MODELS[i].defineAspect(aspectClassInfo, aspectDef, loader);
75         }
76     }
77
78     /**
79      * Registers aspect models.
80      *
81      * @param aspectModels the class names of the aspect models to register concatenated and separated with a ':'.
82      */

83     private static void registerAspectModels(final String JavaDoc aspectModels) {
84         if (aspectModels != null) {
85             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(aspectModels, DELIMITER);
86             ASPECT_MODELS = new AspectModel[tokenizer.countTokens()];
87             for (int i = 0; i < ASPECT_MODELS.length; i++) {
88                 final String JavaDoc className = tokenizer.nextToken();
89                 try {
90                     final Class JavaDoc modelClass = ContextClassLoader.forName(className);
91                     ASPECT_MODELS[i] = (AspectModel) modelClass.newInstance();
92                 } catch (ClassNotFoundException JavaDoc e) {
93                     throw new DefinitionException(
94                             "aspect model implementation class not found [" +
95                             className + "]: " + e.toString()
96                     );
97                 } catch (Exception JavaDoc 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