001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.transform.inlining;
005:
006: import com.tc.aspectwerkz.definition.AspectDefinition;
007: import com.tc.aspectwerkz.exception.DefinitionException;
008: import com.tc.aspectwerkz.transform.inlining.model.AopAllianceAspectModel;
009: import com.tc.aspectwerkz.transform.inlining.model.AspectWerkzAspectModel;
010: import com.tc.aspectwerkz.transform.inlining.model.SpringAspectModel;
011: import com.tc.aspectwerkz.transform.inlining.spi.AspectModel;
012: import com.tc.aspectwerkz.util.ContextClassLoader;
013: import com.tc.aspectwerkz.reflect.ClassInfo;
014:
015: import java.util.ArrayList;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.StringTokenizer;
019:
020: /**
021: * Manages the different aspect model implementations that is registered.
022: *
023: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
024: */
025: public class AspectModelManager {
026:
027: public static final String ASPECT_MODELS_VM_OPTION = "aspectwerkz.extension.aspectmodels";
028: private static final String DELIMITER = ":";
029:
030: /**
031: * The aspects models that are registered
032: */
033: private static List ASPECT_MODELS = new ArrayList(1);
034:
035: static {
036: ASPECT_MODELS.add(new AspectWerkzAspectModel());
037: ASPECT_MODELS.add(new AopAllianceAspectModel());
038: ASPECT_MODELS.add(new SpringAspectModel());
039: registerCustomAspectModels(System.getProperty(
040: ASPECT_MODELS_VM_OPTION, null));
041:
042: }
043:
044: /**
045: * Returns an array with all the aspect models that has been registered.
046: *
047: * @return an array with the aspect models
048: */
049: public static AspectModel[] getModels() {
050: return (AspectModel[]) ASPECT_MODELS
051: .toArray(new AspectModel[] {});
052: }
053:
054: /**
055: * Returns the advice model for a specific aspect model type id.
056: *
057: * @param type the aspect model type id
058: * @return the aspect model
059: */
060: public static AspectModel getModelFor(String type) {
061: for (Iterator iterator = ASPECT_MODELS.iterator(); iterator
062: .hasNext();) {
063: AspectModel aspectModel = (AspectModel) iterator.next();
064: if (aspectModel.getAspectModelType().equals(type)) {
065: return aspectModel;
066: }
067: }
068: return null;
069: }
070:
071: /**
072: * Let all aspect models try to define the aspect (only one should succeed).
073: *
074: * @param aspectClassInfo
075: * @param aspectDef
076: * @param loader
077: */
078: public static void defineAspect(final ClassInfo aspectClassInfo,
079: final AspectDefinition aspectDef, final ClassLoader loader) {
080: for (Iterator iterator = ASPECT_MODELS.iterator(); iterator
081: .hasNext();) {
082: AspectModel aspectModel = (AspectModel) iterator.next();
083: aspectModel
084: .defineAspect(aspectClassInfo, aspectDef, loader);
085: }
086: }
087:
088: /**
089: * Registers aspect models.
090: *
091: * @param aspectModels the class names of the aspect models to register concatenated and separated with a ':'.
092: */
093: public static void registerCustomAspectModels(
094: final String aspectModels) {
095: if (aspectModels != null) {
096: StringTokenizer tokenizer = new StringTokenizer(
097: aspectModels, DELIMITER);
098: while (tokenizer.hasMoreTokens()) {
099: final String className = tokenizer.nextToken();
100: try {
101: final Class modelClass = ContextClassLoader
102: .forName(className);
103: ASPECT_MODELS.add(modelClass.newInstance());
104: } catch (ClassNotFoundException e) {
105: throw new DefinitionException(
106: "aspect model implementation class not found ["
107: + className + "]: " + e.toString());
108: } catch (Exception e) {
109: throw new DefinitionException(
110: "aspect model implementation class could not be instantiated ["
111: + className
112: + "] - make sure it has a default no argument constructor: "
113: + e.toString());
114: }
115: }
116: }
117: }
118: }
|