00001
00028 package org.objectweb.jonas_ejb.deployment.api;
00029
00030 import java.lang.reflect.Method;
00031 import java.util.HashSet;
00032 import java.util.Iterator;
00033 import java.util.List;
00034
00035 import org.objectweb.jonas_ejb.deployment.xml.MethodParams;
00036 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00037
00044 public class MethodDesc {
00045
00049 public static final int TX_NULL = -1;
00050 public static final int TX_NOT_SET = 0;
00051 public static final int TX_NOT_SUPPORTED = 1;
00052 public static final int TX_REQUIRED = 2;
00053 public static final int TX_SUPPORTS = 3;
00054 public static final int TX_REQUIRES_NEW = 4;
00055 public static final int TX_MANDATORY = 5;
00056 public static final int TX_NEVER = 6;
00057
00058
00059 private static final String[] ATTR = {
00060 "TX_NOT_SET",
00061 "TX_NOT_SUPPORTED",
00062 "TX_REQUIRED",
00063 "TX_SUPPORTS",
00064 "TX_REQUIRES_NEW",
00065 "TX_MANDATORY",
00066 "TX_NEVER"
00067 };
00068
00072 protected static final String TX_STR_DEFAULT_VALUE = "Supports";
00073
00077 protected static final String TX_STR_DEFAULT_VALUE_4_MDB = "NotSupported";
00078
00082 private int txAttribute = TX_NOT_SET;
00083 private int txAttributeStatus = APPLY_TO_BEAN;
00084
00085
00086 private HashSet roleName = new HashSet();
00087
00088 public static final int APPLY_TO_NOTHING = 0;
00089 public static final int APPLY_TO_BEAN = 1;
00090 public static final int APPLY_TO_CLASS = 2;
00091 public static final int APPLY_TO_BEAN_METHOD_NAME = 3;
00092 public static final int APPLY_TO_CLASS_METHOD_NAME = 4;
00093 public static final int APPLY_TO_BEAN_METHOD = 5;
00094 public static final int APPLY_TO_CLASS_METHOD = 6;
00095
00096 protected static final String[] APPLY_TO = {
00097 "APPLY_TO_NOTHING",
00098 "APPLY_TO_BEAN",
00099 "APPLY_TO_CLASS",
00100 "APPLY_TO_BEAN_METHOD_NAME",
00101 "APPLY_TO_CLASS_METHOD_NAME",
00102 "APPLY_TO_BEAN_METHOD",
00103 "APPLY_TO_CLASS_METHOD"
00104 };
00105
00106 private Method meth;
00107 private int index;
00108
00109 protected BeanDesc beanDesc;
00110
00111 private boolean isFinder = false;
00112 private boolean isEjbSelect = false;
00113 private boolean isFindByPrimaryKey = false;
00114
00118 private boolean excluded = false;
00119
00123 public MethodDesc(BeanDesc beanDesc, Method meth, int index) {
00124 this.meth = meth;
00125 this.index = index;
00126 this.beanDesc = beanDesc;
00127 isFindByPrimaryKey = MethodDesc.isFindByPrimaryKey(meth);
00128 isFinder = isFinder(meth);
00129 isEjbSelect = isEjbSelect(meth);
00130 }
00131
00135 public int getIndex() {
00136 return index;
00137 }
00138
00139 public void setIndex(int idx) {
00140 index = idx;
00141 }
00142
00147 public boolean isFinder() {
00148 return isFinder;
00149 }
00150
00155 public boolean isFindByPrimaryKey() {
00156 return isFindByPrimaryKey;
00157 }
00158
00159
00164 public boolean isEjbSelect() {
00165 return isEjbSelect;
00166 }
00167
00168
00174 void overwriteTxAttribute(String transAttribute, int status) throws DeploymentDescException {
00175
00176 if (status < this.txAttributeStatus) {
00177 return;
00178 }
00179 setTxAttribute(transAttribute);
00180 txAttributeStatus = status;
00181 }
00182
00186 void setTxAttribute(String transAttribute) throws DeploymentDescException {
00187 if (transAttribute.equals("NotSupported")) {
00188 txAttribute = TX_NOT_SUPPORTED;
00189 } else if (transAttribute.equals("Required")) {
00190 txAttribute = TX_REQUIRED;
00191 } else if (transAttribute.equals("Supports")) {
00192 txAttribute = TX_SUPPORTS;
00193 } else if (transAttribute.equals("RequiresNew")) {
00194 txAttribute = TX_REQUIRES_NEW;
00195 } else if (transAttribute.equals("Mandatory")) {
00196 txAttribute = TX_MANDATORY;
00197 } else if (transAttribute.equals("Never")) {
00198 txAttribute = TX_NEVER;
00199 } else {
00200 throw new DeploymentDescException(transAttribute
00201 + " is not a valid trans-attribute value");
00202 }
00203 }
00204
00209 void addRoleName (String rn) {
00210 roleName.add(rn);
00211 }
00212
00217 public int matchPattern(Class pclass, String mName, MethodParams patternMethodParams) {
00218 return matchPattern(getMethod(), pclass, mName, patternMethodParams);
00219 }
00220
00225 public static int matchPattern(java.lang.reflect.Method meth,
00226 Class pclass,
00227 String mName,
00228 MethodParams patternMethodParams) {
00229
00230
00231 if (pclass != null && !pclass.isAssignableFrom(meth.getDeclaringClass())) {
00232 return APPLY_TO_NOTHING;
00233 }
00234
00235
00236 if (mName.equals("*")) {
00237 return (pclass == null) ? APPLY_TO_BEAN : APPLY_TO_CLASS;
00238 }
00239
00240
00241 if (!mName.equals(meth.getName())) {
00242 return APPLY_TO_NOTHING;
00243 }
00244
00245
00246 if (patternMethodParams == null) {
00247 return (pclass == null) ? APPLY_TO_BEAN_METHOD_NAME : APPLY_TO_CLASS_METHOD_NAME;
00248 }
00249
00250 Class pars[] = meth.getParameterTypes();
00251 List pattPars = patternMethodParams.getMethodParamList();
00252
00253 if (pars.length != pattPars.size()) {
00254 return APPLY_TO_NOTHING;
00255 }
00256 Iterator i = pattPars.iterator();
00257 for (int ii = 0; ii < pars.length; ii++) {
00258 String cName = (String) i.next();
00259 if (!getClassName(pars[ii]).equals(cName)) {
00260 return APPLY_TO_NOTHING;
00261 }
00262 }
00263 return (pclass == null) ? APPLY_TO_BEAN_METHOD : APPLY_TO_CLASS_METHOD;
00264 }
00265
00271 private static String getClassName(Class c) {
00272 String name;
00273 if (c.isArray()) {
00274 name = getClassName(c.getComponentType()) + "[]";
00275 } else {
00276 name = c.getName();
00277 }
00278 return (name);
00279 }
00280
00281
00282
00288 public int getTxAttribute() {
00289 return txAttribute;
00290 }
00291
00298 public int getTxAttributeStatus() {
00299 return txAttributeStatus;
00300 }
00301
00306 public static String getTxAttributeName(int value) {
00307 if ((value < 0) || (value > ATTR.length)) {
00308 throw new Error(value + " is not a valid TxAttribute");
00309 }
00310 return ATTR[value];
00311 }
00312
00317 public String getTxAttributeName() {
00318 return ATTR[txAttribute];
00319 }
00320
00325 public String[] getRoleName () {
00326 if (roleName.isEmpty()) {
00327 return new String[0];
00328 }
00329 Object[] o = roleName.toArray();
00330 String[] rn = new String[o.length];
00331 for (int i = 0; i < rn.length; i++) {
00332 rn[i] = (String) o[i];
00333 }
00334 return rn;
00335 }
00336
00342 public static String methodElementToString(org.objectweb.jonas_ejb.deployment.xml.Method m) {
00343 return methodElementToString(m.getMethodIntf(), m.getMethodName(), m.getMethodParams());
00344 }
00345
00349 protected static String methodElementToString(String intf, String name, MethodParams params) {
00350 String s = new String();
00351 if (intf != null) {
00352 s = s.concat(intf + ".");
00353 }
00354 s = s.concat(name);
00355 if (params != null) {
00356 s = s.concat("(");
00357 for (Iterator i = params.getMethodParamList().iterator(); i.hasNext();) {
00358 s = s.concat((String) i.next());
00359 if (i.hasNext()) {
00360 s = s.concat(",");
00361 }
00362 }
00363 s = s.concat(")");
00364 }
00365 return (s);
00366 }
00367
00371 public static String toString(Method m) {
00372 StringBuffer ret = new StringBuffer();
00373 ret.append(m.getDeclaringClass().getName());
00374 ret.append('.');
00375 ret.append(m.getName());
00376 ret.append('(');
00377 Class[] params = m.getParameterTypes();
00378 for (int i = 0; i < params.length; i++) {
00379 ret.append(getClassName(params[i]));
00380 if (i == params.length - 1) {
00381 break;
00382 }
00383 ret.append(',');
00384 }
00385 ret.append(')');
00386 return ret.toString();
00387 }
00388
00392 public Method getMethod() {
00393 return meth;
00394 }
00395
00399 public BeanDesc getBeanDesc() {
00400 return beanDesc;
00401 }
00402
00406 public static boolean isFinder(Method meth) {
00407 return (meth.getName().startsWith("find")
00408 && (javax.ejb.EJBHome.class.isAssignableFrom(meth.getDeclaringClass())
00409 || javax.ejb.EJBLocalHome.class.isAssignableFrom(meth.getDeclaringClass())));
00410 }
00411
00415 public static boolean isFindByPrimaryKey(Method meth) {
00416 return (meth.getName().equals("findByPrimaryKey")
00417 && (javax.ejb.EJBHome.class.isAssignableFrom(meth.getDeclaringClass())
00418 || javax.ejb.EJBLocalHome.class.isAssignableFrom(meth.getDeclaringClass())));
00419 }
00420
00424 public static boolean isEjbSelect(Method meth) {
00425 return (meth.getName().startsWith("ejbSelect")
00426 && javax.ejb.EntityBean.class.isAssignableFrom(meth.getDeclaringClass()));
00427 }
00428
00433 public String toString() {
00434 StringBuffer ret = new StringBuffer();
00435 ret.append("\ngetTxAttribute() = " + ATTR[getTxAttribute()]);
00436 ret.append("\ngetTxAttributeStatus() = " + APPLY_TO[getTxAttributeStatus()]);
00437 ret.append("\nMethodIndex = " + index);
00438 ret.append("\nmeth = " + toString(meth));
00439 ret.append("\nisFinder = " + isFinder);
00440 ret.append("\nisEjbSelect = " + isEjbSelect);
00441 ret.append("\nisFindByPrimaryKey = " + isFindByPrimaryKey);
00442 if (!roleName.isEmpty()) {
00443 ret.append("\ngetRoleName() = [");
00444 String [] rn = getRoleName();
00445 for (int i = 0; i < rn.length - 1; i++) {
00446 ret.append (rn[i] + ", ");
00447 }
00448 ret.append(rn[rn.length - 1] + "]");
00449 ret.append("\n");
00450 }
00451 return ret.toString();
00452 }
00453
00457 public boolean isExcluded() {
00458 return excluded;
00459 }
00464 public void setExcluded(boolean excluded) {
00465 this.excluded = excluded;
00466 }
00467 }
00468
00469
00470
00471
00472
00473