KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > ExpressionNamespace


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.expression;
5
6 import com.tc.aspectwerkz.exception.DefinitionException;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.WeakHashMap JavaDoc;
11
12 /**
13  * The expression namespace as well as a repository for the namespaces. <p/>A namespace is usually defined by the name
14  * of the class defining the expression.
15  *
16  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17  */

18 public final class ExpressionNamespace {
19   /**
20    * Namespace container.
21    */

22   private static final Map JavaDoc s_namespaces = new WeakHashMap JavaDoc();
23
24   /**
25    * Map with all the expressions in the namespace, [name:expression] pairs.
26    */

27   private final Map JavaDoc m_expressions = new HashMap JavaDoc();
28
29   /**
30    * The namespace.
31    */

32   private final String JavaDoc m_namespace;
33
34   /**
35    * Creates a new expression namespace.
36    *
37    * @param namespace
38    */

39   private ExpressionNamespace(final String JavaDoc namespace) {
40     m_namespace = namespace;
41   }
42
43   /**
44    * Returns the expression namespace for a specific namespace.
45    *
46    * @param namespace the expression namespace
47    * @return the expression namespace abstraction
48    */

49   public static synchronized ExpressionNamespace getNamespace(final String JavaDoc namespace) {
50     if (!s_namespaces.containsKey(namespace)) {
51       s_namespaces.put(namespace, new ExpressionNamespace(namespace));
52     }
53     return (ExpressionNamespace) s_namespaces.get(namespace);
54   }
55
56   /**
57    * Adds an expression info to the namespace.
58    *
59    * @param name the name mapped to the expression
60    * @param expressionInfo the expression info to add
61    */

62   public void addExpressionInfo(final String JavaDoc name, final ExpressionInfo expressionInfo) {
63     m_expressions.put(name, expressionInfo);
64   }
65
66   /**
67    * Returns the expression info with a specific name or null if it could not be found.
68    *
69    * @param name the name of the expression
70    * @return the expression info
71    */

72   public ExpressionInfo getExpressionInfoOrNull(final String JavaDoc name) {
73     int index = name.lastIndexOf('.');
74     if (index != -1) {
75       // stay in the same CflowStack
76
//TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
77
return getNamespace(name.substring(0, index)).getExpressionInfoOrNull(
78               name.substring(index + 1, name.length())
79       );
80     } else {
81       final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions.get(name));
82 // if (expressionInfo == null) {
83
// throw new DefinitionException(
84
// new StringBuffer().
85
// append("could not resolve reference to pointcut [").
86
// append(name).
87
// append("] in namespace [").
88
// append(m_namespace).
89
// append("]").toString()
90
// );
91
// }
92
return expressionInfo;
93     }
94   }
95
96   /**
97    * Returns the expression info with a specific name or throw an exception if it could not be found.
98    *
99    * @param name the name of the expression
100    * @return the expression info
101    */

102   public ExpressionInfo getExpressionInfo(final String JavaDoc name) {
103     int index = name.lastIndexOf('.');
104     if (index != -1) {
105       // stay in the same CflowStack
106
//TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
107
return getNamespace(name.substring(0, index)).getExpressionInfo(name.substring(index + 1, name.length()));
108     } else {
109       final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions.get(name));
110       if (expressionInfo == null) {
111         throw new DefinitionException(
112                 new StringBuffer JavaDoc().
113                         append("could not resolve reference to pointcut [").
114                         append(name).
115                         append("] in namespace [").
116                         append(m_namespace).
117                         append("]").toString()
118         );
119       }
120       return expressionInfo;
121     }
122   }
123
124   /**
125    * Returns the expression with a specific name.
126    *
127    * @param name the name of the expression
128    * @return the expression
129    */

130   public ExpressionVisitor getExpression(final String JavaDoc name) {
131     return getExpressionInfo(name).getExpression();
132   }
133
134   /**
135    * Returns the advised class expression with a specific name.
136    *
137    * @param name the name of the expression
138    * @return the expression
139    */

140   public AdvisedClassFilterExpressionVisitor getAdvisedClassExpression(final String JavaDoc name) {
141     return getExpressionInfo(name).getAdvisedClassFilterExpression();
142   }
143
144   /**
145    * Returns the name of the namespace.
146    *
147    * @return the name of the namespace
148    */

149   public String JavaDoc getName() {
150     return m_namespace;
151   }
152 }
Popular Tags