1 25 26 27 28 package org.objectweb.jonas_ejb.deployment.api; 29 30 import java.lang.reflect.Method ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.objectweb.jonas_ejb.deployment.xml.MethodParams; 36 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 37 38 44 public class MethodDesc { 45 46 49 public static final int TX_NULL = -1; 50 public static final int TX_NOT_SET = 0; 51 public static final int TX_NOT_SUPPORTED = 1; 52 public static final int TX_REQUIRED = 2; 53 public static final int TX_SUPPORTS = 3; 54 public static final int TX_REQUIRES_NEW = 4; 55 public static final int TX_MANDATORY = 5; 56 public static final int TX_NEVER = 6; 57 58 59 private static final String [] ATTR = { 60 "TX_NOT_SET", 61 "TX_NOT_SUPPORTED", 62 "TX_REQUIRED", 63 "TX_SUPPORTS", 64 "TX_REQUIRES_NEW", 65 "TX_MANDATORY", 66 "TX_NEVER" 67 }; 68 69 72 protected static final String TX_STR_DEFAULT_VALUE = "Supports"; 73 74 77 protected static final String TX_STR_DEFAULT_VALUE_4_MDB = "NotSupported"; 78 79 82 private int txAttribute = TX_NOT_SET; 83 private int txAttributeStatus = APPLY_TO_BEAN; 84 85 private HashSet roleName = new HashSet (); 87 88 public static final int APPLY_TO_NOTHING = 0; 89 public static final int APPLY_TO_BEAN = 1; 90 public static final int APPLY_TO_CLASS = 2; 91 public static final int APPLY_TO_BEAN_METHOD_NAME = 3; 92 public static final int APPLY_TO_CLASS_METHOD_NAME = 4; 93 public static final int APPLY_TO_BEAN_METHOD = 5; 94 public static final int APPLY_TO_CLASS_METHOD = 6; 95 96 protected static final String [] APPLY_TO = { 97 "APPLY_TO_NOTHING", 98 "APPLY_TO_BEAN", 99 "APPLY_TO_CLASS", 100 "APPLY_TO_BEAN_METHOD_NAME", 101 "APPLY_TO_CLASS_METHOD_NAME", 102 "APPLY_TO_BEAN_METHOD", 103 "APPLY_TO_CLASS_METHOD" 104 }; 105 106 private Method meth; 107 private Class classDef; 108 private int index; 109 110 protected BeanDesc beanDesc; 111 112 private boolean isFinder = false; 113 private boolean isEjbSelect = false; 114 private boolean isFindByPrimaryKey = false; 115 116 119 private boolean excluded = false; 120 121 124 public MethodDesc(BeanDesc beanDesc, Method meth, Class clDef, int index) { 125 this.meth = meth; 126 this.classDef = clDef; 127 this.index = index; 128 this.beanDesc = beanDesc; 129 isFindByPrimaryKey = MethodDesc.isFindByPrimaryKey(meth); 130 isFinder = isFinder(meth); 131 isEjbSelect = isEjbSelect(meth); 132 } 133 134 137 public int getIndex() { 138 return index; 139 } 140 141 public void setIndex(int idx) { 142 index = idx; 143 } 144 145 149 public boolean isFinder() { 150 return isFinder; 151 } 152 153 157 public boolean isFindByPrimaryKey() { 158 return isFindByPrimaryKey; 159 } 160 161 162 166 public boolean isEjbSelect() { 167 return isEjbSelect; 168 } 169 170 171 176 void overwriteTxAttribute(String transAttribute, int status) throws DeploymentDescException { 177 if (status < this.txAttributeStatus) { 179 return; 180 } 181 setTxAttribute(transAttribute); 182 txAttributeStatus = status; 183 } 184 185 188 void setTxAttribute(String transAttribute) throws DeploymentDescException { 189 if (transAttribute.equals("NotSupported")) { 190 txAttribute = TX_NOT_SUPPORTED; 191 } else if (transAttribute.equals("Required")) { 192 txAttribute = TX_REQUIRED; 193 } else if (transAttribute.equals("Supports")) { 194 txAttribute = TX_SUPPORTS; 195 } else if (transAttribute.equals("RequiresNew")) { 196 txAttribute = TX_REQUIRES_NEW; 197 } else if (transAttribute.equals("Mandatory")) { 198 txAttribute = TX_MANDATORY; 199 } else if (transAttribute.equals("Never")) { 200 txAttribute = TX_NEVER; 201 } else { 202 throw new DeploymentDescException(transAttribute 203 + " is not a valid trans-attribute value"); 204 } 205 } 206 207 211 void addRoleName (String rn) { 212 roleName.add(rn); 213 } 214 215 219 public int matchPattern(Class pclass, String mName, MethodParams patternMethodParams) { 220 return matchPattern(getMethod(), classDef, pclass, mName, patternMethodParams); 221 } 222 223 227 public static int matchPattern(java.lang.reflect.Method meth, 228 Class classMeth, 229 Class pclass, 230 String mName, 231 MethodParams patternMethodParams) { 232 233 if (pclass != null && !pclass.isAssignableFrom(classMeth)) { 235 return APPLY_TO_NOTHING; 236 } 237 238 if (mName.equals("*")) { 240 return (pclass == null) ? APPLY_TO_BEAN : APPLY_TO_CLASS; 241 } 242 243 if (!mName.equals(meth.getName())) { 245 return APPLY_TO_NOTHING; 246 } 247 248 if (patternMethodParams == null) { 250 return (pclass == null) ? APPLY_TO_BEAN_METHOD_NAME : APPLY_TO_CLASS_METHOD_NAME; 251 } 252 253 Class pars[] = meth.getParameterTypes(); 254 List pattPars = patternMethodParams.getMethodParamList(); 255 if (pars.length != pattPars.size()) { 257 return APPLY_TO_NOTHING; 258 } 259 Iterator i = pattPars.iterator(); 260 for (int ii = 0; ii < pars.length; ii++) { 261 String cName = (String ) i.next(); 262 if (!getClassName(pars[ii]).equals(cName)) { 263 return APPLY_TO_NOTHING; 264 } 265 } 266 return (pclass == null) ? APPLY_TO_BEAN_METHOD : APPLY_TO_CLASS_METHOD; 267 } 268 269 274 private static String getClassName(Class c) { 275 String name; 276 if (c.isArray()) { 277 name = getClassName(c.getComponentType()) + "[]"; 278 } else { 279 name = c.getName(); 280 } 281 return (name); 282 } 283 284 285 286 291 public int getTxAttribute() { 292 return txAttribute; 293 } 294 295 301 public int getTxAttributeStatus() { 302 return txAttributeStatus; 303 } 304 305 309 public static String getTxAttributeName(int value) { 310 if ((value < 0) || (value > ATTR.length)) { 311 throw new Error (value + " is not a valid TxAttribute"); 312 } 313 return ATTR[value]; 314 } 315 316 320 public String getTxAttributeName() { 321 return ATTR[txAttribute]; 322 } 323 324 328 public String [] getRoleName () { 329 if (roleName.isEmpty()) { 330 return new String [0]; 331 } 332 Object [] o = roleName.toArray(); 333 String [] rn = new String [o.length]; 334 for (int i = 0; i < rn.length; i++) { 335 rn[i] = (String ) o[i]; 336 } 337 return rn; 338 } 339 340 345 public static String methodElementToString(org.objectweb.jonas_ejb.deployment.xml.Method m) { 346 return methodElementToString(m.getMethodIntf(), m.getMethodName(), m.getMethodParams()); 347 } 348 349 352 protected static String methodElementToString(String intf, String name, MethodParams params) { 353 String s = new String (); 354 if (intf != null) { 355 s = s.concat(intf + "."); 356 } 357 s = s.concat(name); 358 if (params != null) { 359 s = s.concat("("); 360 for (Iterator i = params.getMethodParamList().iterator(); i.hasNext();) { 361 s = s.concat((String ) i.next()); 362 if (i.hasNext()) { 363 s = s.concat(","); 364 } 365 } 366 s = s.concat(")"); 367 } 368 return (s); 369 } 370 371 374 public static String toString(Method m) { 375 StringBuffer ret = new StringBuffer (); 376 ret.append(m.getDeclaringClass().getName()); 377 ret.append('.'); 378 ret.append(m.getName()); 379 ret.append('('); 380 Class [] params = m.getParameterTypes(); 381 for (int i = 0; i < params.length; i++) { 382 ret.append(getClassName(params[i])); 383 if (i == params.length - 1) { 384 break; 385 } 386 ret.append(','); 387 } 388 ret.append(')'); 389 return ret.toString(); 390 } 391 392 395 public Method getMethod() { 396 return meth; 397 } 398 399 402 public BeanDesc getBeanDesc() { 403 return beanDesc; 404 } 405 406 409 public static boolean isFinder(Method meth) { 410 return (meth.getName().startsWith("find") 411 && (javax.ejb.EJBHome .class.isAssignableFrom(meth.getDeclaringClass()) 412 || javax.ejb.EJBLocalHome .class.isAssignableFrom(meth.getDeclaringClass()))); 413 } 414 415 418 public static boolean isFindByPrimaryKey(Method meth) { 419 return (meth.getName().equals("findByPrimaryKey") 420 && (javax.ejb.EJBHome .class.isAssignableFrom(meth.getDeclaringClass()) 421 || javax.ejb.EJBLocalHome .class.isAssignableFrom(meth.getDeclaringClass()))); 422 } 423 424 427 public static boolean isEjbSelect(Method meth) { 428 return (meth.getName().startsWith("ejbSelect") 429 && javax.ejb.EntityBean .class.isAssignableFrom(meth.getDeclaringClass())); 430 } 431 432 436 public String toString() { 437 StringBuffer ret = new StringBuffer (); 438 ret.append("\ngetTxAttribute() = " + ATTR[getTxAttribute()]); 439 ret.append("\ngetTxAttributeStatus() = " + APPLY_TO[getTxAttributeStatus()]); 440 ret.append("\nMethodIndex = " + index); 441 ret.append("\nmeth = " + toString(meth)); 442 ret.append("\nisFinder = " + isFinder); 443 ret.append("\nisEjbSelect = " + isEjbSelect); 444 ret.append("\nisFindByPrimaryKey = " + isFindByPrimaryKey); 445 if (!roleName.isEmpty()) { 446 ret.append("\ngetRoleName() = ["); 447 String [] rn = getRoleName(); 448 for (int i = 0; i < rn.length - 1; i++) { 449 ret.append (rn[i] + ", "); 450 } 451 ret.append(rn[rn.length - 1] + "]"); 452 ret.append("\n"); 453 } 454 return ret.toString(); 455 } 456 457 460 public boolean isExcluded() { 461 return excluded; 462 } 463 467 public void setExcluded(boolean excluded) { 468 this.excluded = excluded; 469 } 470 } 471 472 473 474 475 476 477 | Popular Tags |