KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > DeploymentModel


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;
5
6 import com.tc.aspectwerkz.perx.PerObjectAspect;
7
8 /**
9  * Enum containing the different deployment model types.
10  * <p/>
11  * Note: equals does not check for pointcut equality for perthis/pertarget but
12  * does only checks for types
13  *
14  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15  */

16 public class DeploymentModel {
17
18   public static final DeploymentModel PER_JVM = new DeploymentModel("perJVM");
19   public static final DeploymentModel PER_CLASS = new DeploymentModel("perClass");
20   public static final DeploymentModel PER_INSTANCE = new DeploymentModel("perInstance");
21
22   public static final DeploymentModel PER_TARGET = new DeploymentModel("perTarget");
23   public static final DeploymentModel PER_THIS = new DeploymentModel("perThis");
24   public static final DeploymentModel PER_CFLOW = new DeploymentModel("perCflow");
25   public static final DeploymentModel PER_CFLOWBELOW = new DeploymentModel("perCflowbelow");
26
27   private static final String JavaDoc THIS_POINTCUT = "this(" + PerObjectAspect.ADVICE_ARGUMENT_NAME + ")";
28   private static final String JavaDoc TARGET_POINTCUT = "target(" + PerObjectAspect.ADVICE_ARGUMENT_NAME + ")";
29
30   protected final String JavaDoc m_name;
31
32   private DeploymentModel(String JavaDoc name) {
33     m_name = name;
34   }
35
36   public String JavaDoc toString() {
37     return m_name;
38   }
39
40   public boolean equals(Object JavaDoc o) {
41     if (this == o) {
42       return true;
43     }
44     if (!(o instanceof DeploymentModel)) {
45       return false;
46     }
47     final DeploymentModel adviceType = (DeploymentModel) o;
48     if ((m_name != null) ? (!m_name.equals(adviceType.m_name)) : (adviceType.m_name != null)) {
49       return false;
50     }
51     return true;
52   }
53
54   public int hashCode() {
55     return ((m_name != null) ? m_name.hashCode() : 0);
56   }
57
58   public static DeploymentModel getDeploymentModelFor(final String JavaDoc deploymentModelAsString) {
59     if (deploymentModelAsString == null || deploymentModelAsString.equals("")) {
60       return PER_JVM; // default is PER_JVM
61
}
62     if (deploymentModelAsString.equalsIgnoreCase(PER_JVM.toString())) {
63       return PER_JVM;
64     } else if (deploymentModelAsString.equalsIgnoreCase(PER_CLASS.toString())) {
65       return PER_CLASS;
66     } else if (deploymentModelAsString.equalsIgnoreCase(PER_INSTANCE.toString())) {
67       return PER_INSTANCE;
68     } else if (deploymentModelAsString.equalsIgnoreCase(PER_CFLOW.toString())) {
69       return PER_CFLOW;
70     } else if (deploymentModelAsString.equalsIgnoreCase(PER_CFLOWBELOW.toString())) {
71       return PER_CFLOWBELOW;
72     } else if (deploymentModelAsString.equalsIgnoreCase(PER_THIS.toString())) {
73       return PER_THIS;
74     } else if (deploymentModelAsString.equalsIgnoreCase(PER_TARGET.toString())) {
75       return PER_TARGET;
76       // below support for more advanced schemes.
77
} else if (deploymentModelAsString.toLowerCase().startsWith(PER_THIS.m_name.toLowerCase())) {
78       return new PointcutControlledDeploymentModel(PER_THIS,
79               getDeploymentExpression(deploymentModelAsString, THIS_POINTCUT)
80       );
81     } else if (deploymentModelAsString.toLowerCase().startsWith(PER_TARGET.m_name.toLowerCase())) {
82       return new PointcutControlledDeploymentModel(PER_TARGET,
83               getDeploymentExpression(deploymentModelAsString, TARGET_POINTCUT)
84       );
85     } else {
86       System.out.println(
87               "AW::WARNING - no such deployment model [" + deploymentModelAsString + "] using default (perJVM)"
88       );
89       return PER_JVM; // falling back to default - PER_JVM
90
}
91   }
92
93   /**
94    * @param deploymentModelAsString
95    * @return
96    */

97   private static String JavaDoc getDeploymentExpression(String JavaDoc deploymentModelAsString,
98                                                 final String JavaDoc pointcut) {
99     int startIndex = deploymentModelAsString.indexOf('(');
100     int endIndex = deploymentModelAsString.lastIndexOf(')');
101
102     if (startIndex == -1 || endIndex == -1 || startIndex >= endIndex) {
103       System.out.println(
104               "AW::ERROR - wrong deployment model definition [" + deploymentModelAsString + "]");
105
106       return "";
107     }
108
109     return deploymentModelAsString.substring(startIndex + 1, endIndex).trim()
110             + " && "
111             + pointcut;
112   }
113
114   /**
115    * perthis.. pertarget.. deployment model depends on a pointcut expression
116    */

117   public static final class PointcutControlledDeploymentModel extends DeploymentModel {
118
119     private String JavaDoc m_expression;
120
121     private PointcutControlledDeploymentModel(DeploymentModel deploymentModel, String JavaDoc expression) {
122       super(deploymentModel.m_name);
123       m_expression = expression;
124     }
125
126     public String JavaDoc getDeploymentExpression() {
127       return m_expression;
128     }
129
130     public String JavaDoc toString() {
131       // returns only the name, not the expression
132
return m_name;
133     }
134
135     public boolean equals(Object JavaDoc o) {
136       return super.equals(o);
137     }
138
139     public int hashCode() {
140       return super.hashCode();
141     }
142   }
143 }
Popular Tags