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.expression;
005:
006: import com.tc.aspectwerkz.exception.DefinitionException;
007:
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.WeakHashMap;
011:
012: /**
013: * The expression namespace as well as a repository for the namespaces. <p/>A namespace is usually defined by the name
014: * of the class defining the expression.
015: *
016: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
017: */
018: public final class ExpressionNamespace {
019: /**
020: * Namespace container.
021: */
022: private static final Map s_namespaces = new WeakHashMap();
023:
024: /**
025: * Map with all the expressions in the namespace, [name:expression] pairs.
026: */
027: private final Map m_expressions = new HashMap();
028:
029: /**
030: * The namespace.
031: */
032: private final String m_namespace;
033:
034: /**
035: * Creates a new expression namespace.
036: *
037: * @param namespace
038: */
039: private ExpressionNamespace(final String namespace) {
040: m_namespace = namespace;
041: }
042:
043: /**
044: * Returns the expression namespace for a specific namespace.
045: *
046: * @param namespace the expression namespace
047: * @return the expression namespace abstraction
048: */
049: public static synchronized ExpressionNamespace getNamespace(
050: final String namespace) {
051: if (!s_namespaces.containsKey(namespace)) {
052: s_namespaces.put(namespace, new ExpressionNamespace(
053: namespace));
054: }
055: return (ExpressionNamespace) s_namespaces.get(namespace);
056: }
057:
058: /**
059: * Adds an expression info to the namespace.
060: *
061: * @param name the name mapped to the expression
062: * @param expressionInfo the expression info to add
063: */
064: public void addExpressionInfo(final String name,
065: final ExpressionInfo expressionInfo) {
066: m_expressions.put(name, expressionInfo);
067: }
068:
069: /**
070: * Returns the expression info with a specific name or null if it could not be found.
071: *
072: * @param name the name of the expression
073: * @return the expression info
074: */
075: public ExpressionInfo getExpressionInfoOrNull(final String name) {
076: int index = name.lastIndexOf('.');
077: if (index != -1) {
078: // stay in the same CflowStack
079: //TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
080: return getNamespace(name.substring(0, index))
081: .getExpressionInfoOrNull(
082: name.substring(index + 1, name.length()));
083: } else {
084: final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions
085: .get(name));
086: // if (expressionInfo == null) {
087: // throw new DefinitionException(
088: // new StringBuffer().
089: // append("could not resolve reference to pointcut [").
090: // append(name).
091: // append("] in namespace [").
092: // append(m_namespace).
093: // append("]").toString()
094: // );
095: // }
096: return expressionInfo;
097: }
098: }
099:
100: /**
101: * Returns the expression info with a specific name or throw an exception if it could not be found.
102: *
103: * @param name the name of the expression
104: * @return the expression info
105: */
106: public ExpressionInfo getExpressionInfo(final String name) {
107: int index = name.lastIndexOf('.');
108: if (index != -1) {
109: // stay in the same CflowStack
110: //TODO: allow for lookup in other CflowStack providing they are in the same hierarchy
111: return getNamespace(name.substring(0, index))
112: .getExpressionInfo(
113: name.substring(index + 1, name.length()));
114: } else {
115: final ExpressionInfo expressionInfo = ((ExpressionInfo) m_expressions
116: .get(name));
117: if (expressionInfo == null) {
118: throw new DefinitionException(
119: new StringBuffer()
120: .append(
121: "could not resolve reference to pointcut [")
122: .append(name)
123: .append("] in namespace [").append(
124: m_namespace).append("]")
125: .toString());
126: }
127: return expressionInfo;
128: }
129: }
130:
131: /**
132: * Returns the expression with a specific name.
133: *
134: * @param name the name of the expression
135: * @return the expression
136: */
137: public ExpressionVisitor getExpression(final String name) {
138: return getExpressionInfo(name).getExpression();
139: }
140:
141: /**
142: * Returns the advised class expression with a specific name.
143: *
144: * @param name the name of the expression
145: * @return the expression
146: */
147: public AdvisedClassFilterExpressionVisitor getAdvisedClassExpression(
148: final String name) {
149: return getExpressionInfo(name)
150: .getAdvisedClassFilterExpression();
151: }
152:
153: /**
154: * Returns the name of the namespace.
155: *
156: * @return the name of the namespace
157: */
158: public String getName() {
159: return m_namespace;
160: }
161: }
|