View Javadoc

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.expression;
9   
10  import org.codehaus.aspectwerkz.exception.DefinitionException;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.WeakHashMap;
15  
16  /***
17   * The expression namespace as well as a repository for the namespaces. <p/>A namespace is usually defined by the name
18   * of the class defining the expression.
19   * 
20   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
21   */
22  public final class ExpressionNamespace {
23      /***
24       * Namespace container.
25       */
26      private static final Map s_namespaces = new WeakHashMap();
27  
28      /***
29       * Map with all the expressions in the namespace, [name:expression] pairs.
30       */
31      private final Map m_expressions = new HashMap();
32  
33      /***
34       * The namespace.
35       */
36      private final String m_namespace;
37  
38      /***
39       * Creates a new expression namespace.
40       * 
41       * @param namespace
42       */
43      private ExpressionNamespace(final String namespace) {
44          m_namespace = namespace;
45      }
46  
47      /***
48       * Returns the expression namespace for a specific namespace.
49       * 
50       * @param namespace the expression namespace
51       * @return the expression namespace abstraction
52       */
53      public static synchronized ExpressionNamespace getNamespace(final String namespace) {
54          if (!s_namespaces.containsKey(namespace)) {
55              s_namespaces.put(namespace, new ExpressionNamespace(namespace));
56          }
57          return (ExpressionNamespace) s_namespaces.get(namespace);
58      }
59  
60      /***
61       * Adds an expression info to the namespace.
62       * 
63       * @param name the name mapped to the expression
64       * @param expressionInfo the expression info to add
65       */
66      public void addExpressionInfo(final String name, final ExpressionInfo expressionInfo) {
67          m_expressions.put(name, expressionInfo);
68      }
69  
70      /***
71       * Returns the expression info with a specific name.
72       * 
73       * @param name the name of the expression
74       * @return the expression info
75       */
76      public ExpressionInfo getExpressionInfo(final String name) {
77          int index = name.lastIndexOf('.');
78          if (index != -1) {
79              // stay in the same AspectSystem
80              //TODO: allow for lookup in other AspectSystem providing they are in the same hierarchy
81              return getNamespace(name.substring(0, index)).getExpressionInfo(name.substring(index + 1, name.length()));
82          } else {
83              return ((ExpressionInfo) m_expressions.get(name));
84          }
85      }
86  
87      /***
88       * Returns the expression with a specific name.
89       * 
90       * @param name the name of the expression
91       * @return the expression
92       */
93      public ExpressionVisitor getExpression(final String name) {
94          return getExpressionInfo(name).getExpression();
95      }
96  
97      /***
98       * Returns the cflow expression with a specific name.
99       * 
100      * @param name the name of the expression
101      * @return the expression
102      */
103     public CflowExpressionVisitor getCflowExpression(final String name) {
104         return getExpressionInfo(name).getCflowExpression();
105     }
106 
107     /***
108      * Returns the runtime cflow expression with a specific name.
109      * 
110      * @param name the name of the expression
111      * @return the expression
112      */
113     public CflowExpressionVisitorRuntime getCflowExpressionRuntime(final String name) {
114         return getExpressionInfo(name).getCflowExpressionRuntime();
115     }
116 
117     /***
118      * Returns the advised class expression with a specific name.
119      * 
120      * @param name the name of the expression
121      * @return the expression
122      */
123     public AdvisedClassFilterExpressionVisitor getAdvisedClassExpression(final String name) {
124         return getExpressionInfo(name).getAdvisedClassFilterExpression();
125     }
126 
127     /***
128      * Returns the advised cflow class expression witha a specific name.
129      * 
130      * @param name the name of the expression
131      * @return the expression
132      */
133     public AdvisedCflowClassFilterExpressionVisitor getAdvisedCflowClassExpression(final String name) {
134         return getExpressionInfo(name).getAdvisedCflowClassFilterExpression();
135     }
136 
137     /***
138      * Returns the name of the namespace.
139      * 
140      * @return the name of the namespace
141      */
142     public String getName() {
143         return m_namespace;
144     }
145 }