KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > inlining > AspectModelManager


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 /**
22  * Manages the different aspect model implementations that is registered.
23  *
24  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
25  */

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

34   private static List JavaDoc ASPECT_MODELS = new ArrayList JavaDoc(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   /**
45    * Returns an array with all the aspect models that has been registered.
46    *
47    * @return an array with the aspect models
48    */

49   public static AspectModel[] getModels() {
50     return (AspectModel[]) ASPECT_MODELS.toArray(new AspectModel[]{});
51   }
52
53   /**
54    * Returns the advice model for a specific aspect model type id.
55    *
56    * @param type the aspect model type id
57    * @return the aspect model
58    */

59   public static AspectModel getModelFor(String JavaDoc type) {
60     for (Iterator JavaDoc 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   /**
70    * Let all aspect models try to define the aspect (only one should succeed).
71    *
72    * @param aspectClassInfo
73    * @param aspectDef
74    * @param loader
75    */

76   public static void defineAspect(final ClassInfo aspectClassInfo,
77                                   final AspectDefinition aspectDef,
78                                   final ClassLoader JavaDoc loader) {
79     for (Iterator JavaDoc iterator = ASPECT_MODELS.iterator(); iterator.hasNext();) {
80       AspectModel aspectModel = (AspectModel) iterator.next();
81       aspectModel.defineAspect(aspectClassInfo, aspectDef, loader);
82     }
83   }
84
85   /**
86    * Registers aspect models.
87    *
88    * @param aspectModels the class names of the aspect models to register concatenated and separated with a ':'.
89    */

90   public static void registerCustomAspectModels(final String JavaDoc aspectModels) {
91     if (aspectModels != null) {
92       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(aspectModels, DELIMITER);
93       while (tokenizer.hasMoreTokens()) {
94         final String JavaDoc className = tokenizer.nextToken();
95         try {
96           final Class JavaDoc modelClass = ContextClassLoader.forName(className);
97           ASPECT_MODELS.add((AspectModel) modelClass.newInstance());
98         } catch (ClassNotFoundException JavaDoc e) {
99           throw new DefinitionException(
100                   "aspect model implementation class not found [" +
101                           className + "]: " + e.toString()
102           );
103         } catch (Exception JavaDoc 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