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;
005:
006: import com.tc.aspectwerkz.perx.PerObjectAspect;
007:
008: /**
009: * Enum containing the different deployment model types.
010: * <p/>
011: * Note: equals does not check for pointcut equality for perthis/pertarget but
012: * does only checks for types
013: *
014: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
015: */
016: public class DeploymentModel {
017:
018: public static final DeploymentModel PER_JVM = new DeploymentModel(
019: "perJVM");
020: public static final DeploymentModel PER_CLASS = new DeploymentModel(
021: "perClass");
022: public static final DeploymentModel PER_INSTANCE = new DeploymentModel(
023: "perInstance");
024:
025: public static final DeploymentModel PER_TARGET = new DeploymentModel(
026: "perTarget");
027: public static final DeploymentModel PER_THIS = new DeploymentModel(
028: "perThis");
029: public static final DeploymentModel PER_CFLOW = new DeploymentModel(
030: "perCflow");
031: public static final DeploymentModel PER_CFLOWBELOW = new DeploymentModel(
032: "perCflowbelow");
033:
034: private static final String THIS_POINTCUT = "this("
035: + PerObjectAspect.ADVICE_ARGUMENT_NAME + ")";
036: private static final String TARGET_POINTCUT = "target("
037: + PerObjectAspect.ADVICE_ARGUMENT_NAME + ")";
038:
039: protected final String m_name;
040:
041: DeploymentModel(String name) {
042: m_name = name;
043: }
044:
045: public String toString() {
046: return m_name;
047: }
048:
049: public boolean equals(Object o) {
050: if (this == o) {
051: return true;
052: }
053: if (!(o instanceof DeploymentModel)) {
054: return false;
055: }
056: final DeploymentModel adviceType = (DeploymentModel) o;
057: if ((m_name != null) ? (!m_name.equals(adviceType.m_name))
058: : (adviceType.m_name != null)) {
059: return false;
060: }
061: return true;
062: }
063:
064: public int hashCode() {
065: return ((m_name != null) ? m_name.hashCode() : 0);
066: }
067:
068: public static DeploymentModel getDeploymentModelFor(
069: final String deploymentModelAsString) {
070: if (deploymentModelAsString == null
071: || deploymentModelAsString.equals("")) {
072: return PER_JVM; // default is PER_JVM
073: }
074: if (deploymentModelAsString
075: .equalsIgnoreCase(PER_JVM.toString())) {
076: return PER_JVM;
077: } else if (deploymentModelAsString.equalsIgnoreCase(PER_CLASS
078: .toString())) {
079: return PER_CLASS;
080: } else if (deploymentModelAsString
081: .equalsIgnoreCase(PER_INSTANCE.toString())) {
082: return PER_INSTANCE;
083: } else if (deploymentModelAsString.equalsIgnoreCase(PER_CFLOW
084: .toString())) {
085: return PER_CFLOW;
086: } else if (deploymentModelAsString
087: .equalsIgnoreCase(PER_CFLOWBELOW.toString())) {
088: return PER_CFLOWBELOW;
089: } else if (deploymentModelAsString.equalsIgnoreCase(PER_THIS
090: .toString())) {
091: return PER_THIS;
092: } else if (deploymentModelAsString.equalsIgnoreCase(PER_TARGET
093: .toString())) {
094: return PER_TARGET;
095: // below support for more advanced schemes.
096: } else if (deploymentModelAsString.toLowerCase().startsWith(
097: PER_THIS.m_name.toLowerCase())) {
098: return new PointcutControlledDeploymentModel(
099: PER_THIS.m_name, getDeploymentExpression(
100: deploymentModelAsString, THIS_POINTCUT));
101: } else if (deploymentModelAsString.toLowerCase().startsWith(
102: PER_TARGET.m_name.toLowerCase())) {
103: return new PointcutControlledDeploymentModel(
104: PER_TARGET.m_name, getDeploymentExpression(
105: deploymentModelAsString, TARGET_POINTCUT));
106: } else {
107: System.out
108: .println("AW::WARNING - no such deployment model ["
109: + deploymentModelAsString
110: + "] using default (perJVM)");
111: return PER_JVM; // falling back to default - PER_JVM
112: }
113: }
114:
115: /**
116: * @param deploymentModelAsString
117: * @return
118: */
119: private static String getDeploymentExpression(
120: String deploymentModelAsString, final String pointcut) {
121: int startIndex = deploymentModelAsString.indexOf('(');
122: int endIndex = deploymentModelAsString.lastIndexOf(')');
123:
124: if (startIndex == -1 || endIndex == -1
125: || startIndex >= endIndex) {
126: System.out
127: .println("AW::ERROR - wrong deployment model definition ["
128: + deploymentModelAsString + "]");
129:
130: return "";
131: }
132:
133: return deploymentModelAsString.substring(startIndex + 1,
134: endIndex).trim()
135: + " && " + pointcut;
136: }
137:
138: /**
139: * perthis.. pertarget.. deployment model depends on a pointcut expression
140: */
141: public static final class PointcutControlledDeploymentModel extends
142: DeploymentModel {
143:
144: private String m_expression;
145:
146: PointcutControlledDeploymentModel(String name, String expression) {
147: super (name);
148: m_expression = expression;
149: }
150:
151: public String getDeploymentExpression() {
152: return m_expression;
153: }
154:
155: public String toString() {
156: // returns only the name, not the expression
157: return m_name;
158: }
159:
160: public boolean equals(Object o) {
161: return super .equals(o);
162: }
163:
164: public int hashCode() {
165: return super.hashCode();
166: }
167: }
168: }
|