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