0001: /**
0002: * JOnAS: Java(TM) Open Application Server
0003: * Copyright (C) 1999-2004 Bull S.A.
0004: * Contact: jonas-team@objectweb.org
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
0019: * USA
0020: *
0021: * --------------------------------------------------------------------------
0022: * $Id: VcMethod.java 8121 2006-03-14 07:58:28Z durieuxp $
0023: * --------------------------------------------------------------------------
0024: */package org.objectweb.jonas_ejb.genic;
0025:
0026: import java.lang.reflect.Method;
0027: import java.util.ArrayList;
0028: import java.util.Iterator;
0029: import java.util.List;
0030: import java.util.StringTokenizer;
0031: import java.util.Vector;
0032:
0033: import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
0034: import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc;
0035: import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc;
0036: import org.objectweb.jonas_ejb.deployment.api.FieldJdbcDesc;
0037: import org.objectweb.jonas_ejb.deployment.api.MethodCmp2Desc;
0038: import org.objectweb.jonas_ejb.deployment.api.MethodDesc;
0039: import org.objectweb.jonas_ejb.deployment.api.MethodJdbcCmp1Desc;
0040: import org.objectweb.jonas_ejb.lib.BeanNaming;
0041: import org.objectweb.jonas_ejb.lib.JavaType;
0042: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
0043: import org.objectweb.jorm.type.api.PType;
0044:
0045: /**
0046: * This class is the "Velocity context" for a interface method or a bean method used in the Velocity Templates.
0047: * @author Helene Joanin (Bull) : Initial developer
0048: * @author Santiago Gala (sgala@hisitech.com) - 00/09/14 - Parameters can be followed by a number in the WHERE clause
0049: * @author Joe Gittings has proposed to code method signature for security
0050: * in order to avoid same signature for inherited methods.
0051: */
0052:
0053: public class VcMethod {
0054:
0055: /**
0056: * home.create method
0057: */
0058: static final byte METHOD_CREATE = 0;
0059: /**
0060: * bean.ebjCreate method
0061: */
0062: static final byte METHOD_EJB_CREATE = 1;
0063: /**
0064: * bean.ejbPostCreate method
0065: */
0066: static final byte METHOD_EJB_POST_CREATE = 2;
0067: /**
0068: * EJBObject.remove() method
0069: */
0070: static final byte METHOD_REMOVE_THIS = 10;
0071: /**
0072: * home.remove(primarykey) method
0073: */
0074: static final byte METHOD_REMOVE_PK = 11;
0075: /**
0076: * home.remove(handle) method
0077: */
0078: static final byte METHOD_REMOVE_HANDLE = 12;
0079: /**
0080: * bean.ejbRemove() method
0081: */
0082: static final byte METHOD_EJB_REMOVE = 13;
0083: /**
0084: * home.findByPrimaryKey(pk) method
0085: */
0086: static final byte METHOD_FINDER_BY_PK = 20;
0087: /**
0088: * home.findXxx() method
0089: */
0090: static final byte METHOD_FINDER_ALL = 21;
0091: /**
0092: * home.findXxx() method that returns a simple object
0093: */
0094: static final byte METHOD_FINDER_SIMPLE = 22;
0095: /**
0096: * home.findXxx() method that returns an Enumeration
0097: */
0098: static final byte METHOD_FINDER_ENUM = 23;
0099: /**
0100: * home.findXxx() method that returns a Collection
0101: */
0102: static final byte METHOD_FINDER_COL = 24;
0103: /**
0104: * bean.ejbSelectXxx() method that returns a simple object
0105: */
0106: static final byte METHOD_EJB_SELECT_SIMPLE = 30;
0107: /**
0108: * bean.ejbSelectXxx() method that returns a Collection
0109: */
0110: static final byte METHOD_EJB_SELECT_COL = 31;
0111: /**
0112: * bean.ejbSelectXxx() method that returns a Set
0113: */
0114: static final byte METHOD_EJB_SELECT_SET = 32;
0115: /**
0116: * bean.ejbLoad() method
0117: */
0118: static final byte METHOD_EJB_LOAD = 40;
0119: /**
0120: * bean.ejbStore() method
0121: */
0122: static final byte METHOD_EJB_STORE = 41;
0123: /**
0124: * bean.setEntityContext() method
0125: */
0126: static final byte METHOD_EJB_SET_ENTITY_CONTEXT = 42;
0127: /**
0128: * bean.ejbActivate() method
0129: */
0130: static final byte METHOD_EJB_ACTIVATE = 43;
0131:
0132: /**
0133: * kind of the method ie home.create() or bean.ejbCreate, or ...
0134: */
0135: private byte mKind = -1;
0136:
0137: /**
0138: * name of the method
0139: */
0140: private String mName = null;
0141:
0142: /**
0143: * name of the method with the firt letter capitalized
0144: */
0145: private String mCapName = null;
0146:
0147: /**
0148: * string representation of the formal parameters of the method (for ex: "int p1, String p2");
0149: */
0150: private StringBuffer mFormalParameters = null;
0151:
0152: /**
0153: * string representation of the actual parameters of the method (for ex: "p1, p2")
0154: */
0155: private StringBuffer mActualParameters = null;
0156:
0157: /**
0158: * Array of objects of the parameters
0159: */
0160: private StringBuffer mArrayObjectParameters = null;
0161:
0162: /**
0163: * string representation of the exception list
0164: */
0165: private StringBuffer mExceptionList = null;
0166:
0167: /**
0168: * string representation of the security signature method
0169: */
0170: private String mSecuritySignature;
0171:
0172: /**
0173: * transactional attribute of the method
0174: */
0175: private int mTxAttribute = MethodDesc.TX_NULL;
0176:
0177: /**
0178: * name of the return type of the method
0179: */
0180: private String mReturnType;
0181:
0182: /**
0183: * default return value
0184: */
0185: private String mDefaultValue;
0186:
0187: /**
0188: * default wrapper type for the return type
0189: */
0190: private String mWrapperType;
0191:
0192: /**
0193: * java.lang.reflect.Method, just for explicit error message
0194: */
0195: private Method mMethod = null;
0196:
0197: /**
0198: * index of the method. Each method is identify with an unuique index.
0199: */
0200: private int methodIndex = -1;
0201:
0202: /**
0203: * BeanDesc of the associated bean to the method
0204: */
0205: private BeanDesc beanDesc = null;
0206:
0207: /*
0208: * For CMP1 only
0209: */
0210:
0211: /**
0212: * string representation of the SQL statement associated to the method
0213: */
0214: private String mSqlStmt = null;
0215:
0216: /**
0217: * VcParamWhere list of the finder method
0218: */
0219: private ArrayList mParamWhereList = null;
0220:
0221: /*
0222: * For CMP2 only
0223: */
0224:
0225: /**
0226: * true is this is a finder/ejbSelect method that return a remote bean
0227: * (Only used in CMP2 for finder and ejbSelect methods)
0228: */
0229:
0230: private boolean isEjbqlReturnRemoteBean = false;
0231: /**
0232: * true is this is a finder/ejbSelect method that return a local bean
0233: * (Only used in CMP2 for finder and ejbSelect methods)
0234: */
0235:
0236: private boolean isEjbqlReturnLocalBean = false;
0237: /**
0238: * Medor Field representation of the result of the finder/ejbSelect method
0239: * (Only used in CMP2 for finder and ejbSelect methods)
0240: */
0241:
0242: private org.objectweb.medor.api.Field resFieldOfEjbql = null;
0243: /**
0244: * VcParam list of the finder/ejbSelect method
0245: * (Only used in CMP2 for finder and ejbSelect methods)
0246: */
0247: private ArrayList mParamList = null;
0248:
0249: /**
0250: * Class list of the parameters of the method
0251: */
0252: private Class[] parameterTypes = null;
0253: /**
0254: * true if the method throws javax.ejb.CreateException
0255: */
0256: private boolean canThrowCreate = false;
0257:
0258: /**
0259: * VcMethod constructor
0260: * @param method java.lang.reflect.Method method description
0261: * @param methodDesc method description
0262: * @param dd bean description of the associated bean
0263: */
0264: VcMethod(Method method, MethodDesc methodDesc, BeanDesc dd) {
0265:
0266: if (methodDesc != null) {
0267: methodIndex = methodDesc.getIndex();
0268: }
0269: beanDesc = dd;
0270: /*
0271: * The 'method' may be
0272: * - a home interface's method
0273: * - a local home interface's method
0274: * - a remote interface's method
0275: * - a local interface's method
0276: * - a bean's method as
0277: * - a beans's ejbSelect() method,
0278: * setEntityContext(), ejbActivate(), ejbCreate(), ejbLoad(), ejbStore(), ejbRemove().
0279: * - a beans's ejbSelect() method,
0280: * In the last case, the 'methodDesc' is null in case of
0281: * setEntityContext(), ejbActivate(), ejbLoad() and ejbStore().
0282: */
0283:
0284: Class declClass = method.getDeclaringClass();
0285: Class[] params = method.getParameterTypes();
0286: parameterTypes = params;
0287: Class[] exceptions = method.getExceptionTypes();
0288: mMethod = method;
0289:
0290: // Name and CapName
0291: mName = method.getName();
0292: mCapName = BeanNaming.firstToUpperCase(mName);
0293:
0294: // FormalParameters and ActualParameters
0295: mFormalParameters = new StringBuffer();
0296: mActualParameters = new StringBuffer();
0297: mArrayObjectParameters = new StringBuffer();
0298: String var = null;
0299: for (int p = 1; p <= params.length; p++) {
0300: var = "p" + p;
0301: mFormalParameters.append(JavaType.getName(params[p - 1])
0302: + " " + var);
0303: mActualParameters.append(var);
0304: mArrayObjectParameters.append(JavaType.toStringObject(var,
0305: params[p - 1]));
0306: if (p < params.length) {
0307: mFormalParameters.append(", ");
0308: mActualParameters.append(", ");
0309: mArrayObjectParameters.append(", ");
0310: }
0311: }
0312:
0313: // ExceptionList
0314: mExceptionList = new StringBuffer();
0315: for (int e = 0; e < exceptions.length; e++) {
0316: Class ecl = exceptions[e];
0317: if (javax.ejb.CreateException.class.isAssignableFrom(ecl)) {
0318: canThrowCreate = true;
0319: }
0320: mExceptionList.append(JavaType.getName(ecl));
0321: if (e < exceptions.length - 1) {
0322: mExceptionList.append(", ");
0323: }
0324: }
0325:
0326: // SecuritySignature
0327: // No methodDesc for bean's methods as ejbLoad(), ejbStore(), ...
0328: if (methodDesc != null) {
0329: mSecuritySignature = "";
0330: if (methodDesc.getRoleName().length > 0
0331: || methodDesc.isExcluded()) {
0332: mSecuritySignature = BeanNaming.getSignature(dd
0333: .getEjbName(), method);
0334: }
0335: }
0336:
0337: // TxAttribute
0338: // No methodDesc for bean's methods as ejbLoad(), ejbStore(), ...
0339: if (methodDesc != null) {
0340: mTxAttribute = methodDesc.getTxAttribute();
0341: }
0342:
0343: // ReturnType
0344: mReturnType = JavaType.getName(method.getReturnType());
0345: if (!mReturnType.equals("void")) {
0346: mDefaultValue = JavaType.getDefaultValue(method
0347: .getReturnType());
0348: mWrapperType = JavaType.getWrapperType(method
0349: .getReturnType());
0350: }
0351:
0352: // isFinderXXX
0353: boolean isFinder = false;
0354:
0355: if (mName.startsWith("find")
0356: && (javax.ejb.EJBHome.class.isAssignableFrom(declClass) || javax.ejb.EJBLocalHome.class
0357: .isAssignableFrom(declClass))) {
0358: isFinder = true;
0359: }
0360: if (isFinder) {
0361: if ("findByPrimaryKey".equals(mName)) {
0362: mKind = METHOD_FINDER_BY_PK;
0363: } else {
0364: if (java.util.Enumeration.class.equals(method
0365: .getReturnType())) {
0366: mKind = METHOD_FINDER_ENUM;
0367: } else if (java.util.Collection.class.equals(method
0368: .getReturnType())) {
0369: mKind = METHOD_FINDER_COL;
0370: } else {
0371: mKind = METHOD_FINDER_SIMPLE;
0372: }
0373: }
0374: }
0375:
0376: // isCreate
0377: if (mName.startsWith("create")
0378: && (javax.ejb.EJBHome.class.isAssignableFrom(declClass) || javax.ejb.EJBLocalHome.class
0379: .isAssignableFrom(declClass))) {
0380: mKind = METHOD_CREATE;
0381: }
0382:
0383: // isEjbCreate
0384: if (mName.startsWith("ejbCreate")
0385: && javax.ejb.EnterpriseBean.class
0386: .isAssignableFrom(declClass)) {
0387: mKind = METHOD_EJB_CREATE;
0388: }
0389:
0390: // isEjbPostCreate
0391: if (mName.startsWith("ejbPostCreate")
0392: && javax.ejb.EnterpriseBean.class
0393: .isAssignableFrom(declClass)) {
0394: mKind = METHOD_EJB_POST_CREATE;
0395: }
0396:
0397: // isRemoveXXX
0398: boolean isRemove = "remove".equals(mName);
0399: if (isRemove) {
0400: if (declClass.equals(javax.ejb.EJBObject.class)
0401: || declClass.equals(javax.ejb.EJBLocalObject.class)) {
0402: mKind = METHOD_REMOVE_THIS;
0403: } else if (declClass.equals(javax.ejb.EJBHome.class)
0404: || declClass.equals(javax.ejb.EJBLocalHome.class)) {
0405: if (params[0].equals(javax.ejb.Handle.class)) {
0406: mKind = METHOD_REMOVE_HANDLE;
0407: } else {
0408: mKind = METHOD_REMOVE_PK;
0409: }
0410: }
0411: }
0412:
0413: // isEjbRemove, isEjbLoad, isEjbStore
0414: if (javax.ejb.EnterpriseBean.class.isAssignableFrom(declClass)
0415: && (params.length == 0)) {
0416: if ("ejbRemove".equals(mName)) {
0417: mKind = METHOD_EJB_REMOVE;
0418: } else if ("ejbLoad".equals(mName)) {
0419: mKind = METHOD_EJB_LOAD;
0420: } else if ("ejbStore".equals(mName)) {
0421: mKind = METHOD_EJB_STORE;
0422: }
0423: }
0424:
0425: // isEjbSetEntityContext
0426: if (javax.ejb.EnterpriseBean.class.isAssignableFrom(declClass)) {
0427: if ("setEntityContext".equals(mName)
0428: && (params.length == 1)
0429: && javax.ejb.EntityContext.class.equals(params[0])) {
0430: mKind = METHOD_EJB_SET_ENTITY_CONTEXT;
0431: }
0432: }
0433:
0434: // isEjbActivate
0435: if (javax.ejb.EnterpriseBean.class.isAssignableFrom(declClass)) {
0436: if ("ejbActivate".equals(mName) && (params.length == 0)) {
0437: mKind = METHOD_EJB_ACTIVATE;
0438: }
0439: }
0440:
0441: if (dd instanceof EntityJdbcCmp1Desc) {
0442: // For Entity CMP 1
0443: // SqlStmt, and ParamsWhere in case of FinderSimple, FinderEnum or FinderCol.
0444: EntityJdbcCmp1Desc edd = (EntityJdbcCmp1Desc) dd;
0445: StringBuffer lf = new StringBuffer();
0446: StringBuffer lv = new StringBuffer();
0447: boolean firstLf = true;
0448: boolean firstLv = true;
0449: if (mKind == METHOD_EJB_CREATE) {
0450: for (Iterator i = edd.getCmpFieldDescIterator(); i
0451: .hasNext();) {
0452: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0453: if (firstLv) {
0454: firstLv = false;
0455: } else {
0456: lf.append(", ");
0457: lv.append(", ");
0458: }
0459: lf.append(fd.getJdbcFieldName());
0460: lv.append("?");
0461: }
0462: mSqlStmt = "insert into " + edd.getJdbcTableName()
0463: + " (" + lf + ") values (" + lv + ")";
0464: } else if (mKind == METHOD_FINDER_BY_PK) {
0465: for (Iterator i = edd.getCmpFieldDescIterator(); i
0466: .hasNext();) {
0467: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0468: if (fd.isPrimaryKey()) {
0469: if (firstLv) {
0470: firstLv = false;
0471: } else {
0472: lf.append(", ");
0473: lv.append(" and ");
0474: }
0475: lf.append(fd.getJdbcFieldName());
0476: lv.append(fd.getJdbcFieldName() + "=?");
0477: }
0478: }
0479:
0480: mSqlStmt = "select " + lf + " from "
0481: + edd.getJdbcTableName() + " where " + lv;
0482: } else if ((mKind == METHOD_FINDER_SIMPLE)
0483: || (mKind == METHOD_FINDER_ENUM)
0484: || (mKind == METHOD_FINDER_COL)) {
0485: if (!((MethodJdbcCmp1Desc) methodDesc).hasWhereClause()) {
0486: throw new Error(
0487: "No WHERE clause defined for the finder method '"
0488: + method.toString() + "'");
0489: }
0490: ArrayList posParams = new ArrayList();
0491: String clauseWhere = parseWhere(
0492: ((MethodJdbcCmp1Desc) methodDesc)
0493: .getWhereClause(), posParams);
0494: for (Iterator i = edd.getCmpFieldDescIterator(); i
0495: .hasNext();) {
0496: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0497: if (fd.isPrimaryKey()) {
0498: if (firstLf) {
0499: firstLf = false;
0500: } else {
0501: lf.append(", ");
0502: }
0503: lf.append(fd.getJdbcFieldName());
0504: }
0505: }
0506:
0507: mSqlStmt = "select " + lf + " from "
0508: + edd.getJdbcTableName() + " " + clauseWhere;
0509: mParamWhereList = new ArrayList();
0510: for (int i = 0; i < posParams.size(); i++) {
0511: int pos = ((Integer) posParams.get(i)).intValue();
0512: if (pos >= params.length) {
0513: throw new Error(
0514: "Wrong parameters number between the method definition and the WHERE clause of the method '"
0515: + method + "'");
0516: }
0517: VcParamWhere vpw = new VcParamWhere(params[pos],
0518: pos + 1);
0519: mParamWhereList.add(vpw);
0520: }
0521: } else if (mKind == METHOD_EJB_REMOVE) {
0522: for (Iterator i = edd.getCmpFieldDescIterator(); i
0523: .hasNext();) {
0524: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0525: if (fd.isPrimaryKey()) {
0526: if (firstLv) {
0527: firstLv = false;
0528: } else {
0529: lv.append(" and ");
0530: }
0531: lv.append(fd.getJdbcFieldName() + "=?");
0532: }
0533: }
0534:
0535: mSqlStmt = "delete from " + edd.getJdbcTableName()
0536: + " where " + lv;
0537: } else if (mKind == METHOD_EJB_LOAD) {
0538: for (Iterator i = edd.getCmpFieldDescIterator(); i
0539: .hasNext();) {
0540: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0541: if (firstLf) {
0542: firstLf = false;
0543: } else {
0544: lf.append(", ");
0545: }
0546: lf.append(fd.getJdbcFieldName());
0547: if (fd.isPrimaryKey()) {
0548: if (firstLv) {
0549: firstLv = false;
0550: } else {
0551: lv.append(" and ");
0552: }
0553: lv.append(fd.getJdbcFieldName() + "=?");
0554: }
0555: }
0556:
0557: mSqlStmt = "select " + lf + " from "
0558: + edd.getJdbcTableName() + " where " + lv;
0559: } else if (mKind == METHOD_EJB_STORE) {
0560: for (Iterator i = edd.getCmpFieldDescIterator(); i
0561: .hasNext();) {
0562: FieldJdbcDesc fd = (FieldJdbcDesc) i.next();
0563: if (fd.isPrimaryKey()) {
0564: if (firstLv) {
0565: firstLv = false;
0566: } else {
0567: lv.append(" and ");
0568: }
0569: lv.append(fd.getJdbcFieldName() + "=?");
0570: } else {
0571: if (firstLf) {
0572: firstLf = false;
0573: } else {
0574: lf.append(", ");
0575: }
0576: lf.append(fd.getJdbcFieldName() + "=?");
0577: }
0578: }
0579: mSqlStmt = "update " + edd.getJdbcTableName() + " set "
0580: + lf + " where " + lv;
0581: }
0582: } else if (dd instanceof EntityJdbcCmp2Desc) {
0583: // ejbSelect() methods
0584: boolean isEjbSelect = false;
0585: if (mName.startsWith("ejbSelect")
0586: && javax.ejb.EntityBean.class
0587: .isAssignableFrom(declClass)) {
0588: isEjbSelect = true;
0589: }
0590: if (isEjbSelect) {
0591: if (java.util.Collection.class.equals(method
0592: .getReturnType())) {
0593: mKind = METHOD_EJB_SELECT_COL;
0594: } else if (java.util.Set.class.equals(method
0595: .getReturnType())) {
0596: mKind = METHOD_EJB_SELECT_SET;
0597: } else {
0598: mKind = METHOD_EJB_SELECT_SIMPLE;
0599: }
0600: }
0601: if (isFinder || isEjbSelect) {
0602: mParamList = new ArrayList();
0603: for (int p = 0; p < params.length; p++) {
0604: mParamList.add(new VcParam(params[p], dd));
0605: }
0606: if (isEjbSelect) {
0607: try {
0608: resFieldOfEjbql = ((MethodCmp2Desc) methodDesc)
0609: .getQueryTreeHolder(null).getResField();
0610: if (resFieldOfEjbql.getType().getTypeCode() == PType.TYPECODE_REFERENCE) {
0611: if (((MethodCmp2Desc) methodDesc)
0612: .isResultTypeMappingRemote()) {
0613: isEjbqlReturnRemoteBean = true;
0614: } else {
0615: isEjbqlReturnLocalBean = true;
0616: }
0617: }
0618: } catch (DeploymentDescException e) {
0619: throw new Error(
0620: "Error while the EJB-QL parsing of '"
0621: + method.toString() + "': "
0622: + e.getMessage());
0623: }
0624: } else if (!isFinderByPk()) {
0625: // finder method other than findByPrimaryKey: parse the EJBQL query to check the query
0626: try {
0627: ((MethodCmp2Desc) methodDesc)
0628: .getQueryTreeHolder(null).getResField();
0629: } catch (DeploymentDescException ignore) {
0630: GenIC
0631: .warning("Warning: Error while the EJB-QL parsing of '"
0632: + method.toString()
0633: + "': "
0634: + ignore.getMessage());
0635: }
0636: }
0637: }
0638:
0639: }
0640:
0641: // System.out.print("VcMethod: "+method.toString());
0642: // System.out.println(toString());
0643: }
0644:
0645: /**
0646: * @return the string representation of the java default value for the cmp field (ie "0 "for int, "null" for object ...)
0647: */
0648: public String getDefaultValue() {
0649: return mDefaultValue;
0650: }
0651:
0652: /**
0653: * Parses Strings with syntax "WHERE summary like ?<nnn>"
0654: * where <nnn> is a decimal integer that indexes (starting by 0)
0655: * the parameters in the findByXXX method for CMP1.
0656: * @param fragment
0657: * @param pos
0658: * @return
0659: * @author: Santiago Gala (sgala@hisitech.com)
0660: */
0661: private String parseWhere(String fragment, List pos) {
0662: StringTokenizer parseWhere = new StringTokenizer(fragment,
0663: "?\n", true);
0664: int numpos = pos.size();
0665: String newWhere = "";
0666: String tok;
0667: while (parseWhere.hasMoreTokens()) {
0668: tok = parseWhere.nextToken();
0669: String numVar = "";
0670: if (Character.isDigit(tok.charAt(0))) {
0671: for (int i = 0; tok.length() > i
0672: && Character.isDigit(tok.charAt(i)); i++) {
0673: numVar += tok.charAt(i);
0674: }
0675: if (numVar.length() > 0) {
0676: newWhere += tok.substring(numVar.length(), tok
0677: .length());
0678: pos.set(numpos - 1, (Object) new Integer(Integer
0679: .parseInt(numVar) - 1));
0680: }
0681: } else {
0682: newWhere += tok;
0683: if (tok.charAt(0) == '?') {
0684: pos.add(new Integer(numpos++));
0685: }
0686: }
0687: }
0688: return newWhere;
0689: }
0690:
0691: /**
0692: * @return Return the name of the method
0693: */
0694: public String getName() {
0695: return mName;
0696: }
0697:
0698: /**
0699: * @return Return the name of the method with the first letter capitalized
0700: */
0701: public String getCapName() {
0702: return mCapName;
0703: }
0704:
0705: /**
0706: * @return Return the string representation of the formal parameters list (ie "int p1, String p2")
0707: */
0708: public String getFormalParameters() {
0709: return mFormalParameters.toString();
0710: }
0711:
0712: /**
0713: * @return Return the string representation of the actual parameters list (ie "p1, p2")
0714: */
0715: public String getActualParameters() {
0716: return mActualParameters.toString();
0717: }
0718:
0719: /**
0720: * @return Return the string representation of the exception list of the method
0721: */
0722: public String getExceptionList() {
0723: return mExceptionList.toString();
0724: }
0725:
0726: /**
0727: * @return Return true if the method throws the javax.ejb.createException exception
0728: */
0729: public boolean canThrowCreate() {
0730: return canThrowCreate;
0731: }
0732:
0733: /**
0734: * @return Return the string representation of the security signature of the method
0735: */
0736: public String getSecuritySignature() {
0737: if (mSecuritySignature == null) {
0738: throw new Error(
0739: "VcMethod.getSecuritySignature() not available for the method '"
0740: + mMethod.toString() + "'");
0741: }
0742: return mSecuritySignature;
0743: }
0744:
0745: /**
0746: * @return Return the transactional attribute associated to the method
0747: */
0748: public int getTxAttribute() {
0749: if (mTxAttribute == MethodDesc.TX_NULL) {
0750: throw new Error(
0751: "VcMethod.getTxAttribute() not available for the method '"
0752: + mMethod.toString() + "'");
0753: }
0754: return mTxAttribute;
0755: }
0756:
0757: /**
0758: * @return Return the name of the return type of the method
0759: */
0760: public String getReturnType() {
0761: return mReturnType;
0762: }
0763:
0764: /**
0765: * @return the string representation of the wrapper type for the field
0766: */
0767: public String getWrapperType() {
0768: return mWrapperType;
0769: }
0770:
0771: /**
0772: * @return Return false because we are not able to know if a finder method is a FinderAll method !!
0773: */
0774: public boolean isFinderAll() {
0775: // How to known a finder is a finderAll !?
0776: return false;
0777: }
0778:
0779: /**
0780: * @return Return true if the method is the findByPrimaryKey() method
0781: */
0782: public boolean isFinderByPk() {
0783: return (mKind == METHOD_FINDER_BY_PK);
0784: }
0785:
0786: /**
0787: * @return Return true if the method is a finder method that returns a unique object
0788: */
0789: public boolean isFinderSimple() {
0790: return (mKind == METHOD_FINDER_SIMPLE);
0791: }
0792:
0793: /**
0794: * @return Return true if the method is a finder method that returns a Enumeration
0795: */
0796: public boolean isFinderEnum() {
0797: return (mKind == METHOD_FINDER_ENUM);
0798: }
0799:
0800: /**
0801: * @return Return true if the method is a finder method that returns a Collection
0802: */
0803: public boolean isFinderCol() {
0804: return (mKind == METHOD_FINDER_COL);
0805: }
0806:
0807: /**
0808: * @return Return true if the method is an ejbSelect method that returns a unique object
0809: */
0810: public boolean isEjbSelectSimple() {
0811: return (mKind == METHOD_EJB_SELECT_SIMPLE);
0812: }
0813:
0814: /**
0815: * @return Return true if the method is an ejbSelect method that returns a Set
0816: */
0817: public boolean isEjbSelectSet() {
0818: return (mKind == METHOD_EJB_SELECT_SET);
0819: }
0820:
0821: /**
0822: * @return Return true if the method is an ejbSelect method that returns a Collection
0823: */
0824: public boolean isEjbSelectCol() {
0825: return (mKind == METHOD_EJB_SELECT_COL);
0826: }
0827:
0828: /**
0829: * @return Return true if the method is a finder/select method that return a remote bean
0830: */
0831: public boolean isEjbqlReturnRemoteBean() {
0832: return isEjbqlReturnRemoteBean;
0833: }
0834:
0835: /**
0836: * @return Return true if the method is a finder/select method that return a local bean
0837: */
0838: public boolean isEjbqlReturnLocalBean() {
0839: return isEjbqlReturnLocalBean;
0840: }
0841:
0842: /**
0843: * @return Return true if the method is home.create()
0844: */
0845: public boolean isCreate() {
0846: return (mKind == METHOD_CREATE);
0847: }
0848:
0849: /**
0850: * @return Return true if the method is bean.ejbCreate()
0851: */
0852: public boolean isEjbCreate() {
0853: return (mKind == METHOD_EJB_CREATE);
0854: }
0855:
0856: /**
0857: * @return Return true if the method is bean.ejbPostCreate()
0858: */
0859: public boolean isEjbPostCreate() {
0860: return (mKind == METHOD_EJB_POST_CREATE);
0861: }
0862:
0863: /**
0864: * @return Return true if the method is EJBObject.remove() method
0865: */
0866: public boolean isRemoveThis() {
0867: return (mKind == METHOD_REMOVE_THIS);
0868: }
0869:
0870: /**
0871: * @return Return true if the method is home.remove(primarykey)
0872: */
0873: public boolean isRemovePk() {
0874: return (mKind == METHOD_REMOVE_PK);
0875: }
0876:
0877: /**
0878: * @return Return true if the method is home.remove(handle);
0879: */
0880: public boolean isRemoveHandle() {
0881: return (mKind == METHOD_REMOVE_HANDLE);
0882: }
0883:
0884: /**
0885: * @return Return true if the method is bean.ejbRemove()
0886: */
0887: public boolean isEjbRemove() {
0888: return (mKind == METHOD_EJB_REMOVE);
0889: }
0890:
0891: /**
0892: * @return Return true if the method is bean.ejbLoad()
0893: */
0894: public boolean isEjbLoad() {
0895: return (mKind == METHOD_EJB_LOAD);
0896: }
0897:
0898: /**
0899: * @return Return true if the method is bean.ejbStore()
0900: */
0901: public boolean isEjbStore() {
0902: return (mKind == METHOD_EJB_STORE);
0903: }
0904:
0905: /**
0906: * @return Return true if the method is bean.setEntityContext()
0907: */
0908: public boolean isEjbSetEntityContext() {
0909: return (mKind == METHOD_EJB_SET_ENTITY_CONTEXT);
0910: }
0911:
0912: /**
0913: * @return Return true if the method is bean.ejbActivate()
0914: */
0915: public boolean isEjbActivate() {
0916: return (mKind == METHOD_EJB_ACTIVATE);
0917: }
0918:
0919: /**
0920: * Only for CMP1 finder methods
0921: * @return Return the string representation of the SQL statement associated to the method
0922: */
0923: public String getSqlStmt() {
0924: if (mSqlStmt == null) {
0925: throw new Error(
0926: "VcMethod.getSqlStmt() not available for the method '"
0927: + mMethod.toString() + "'");
0928: }
0929: return mSqlStmt;
0930: }
0931:
0932: /**
0933: * @return Return the Class array of the parameters of the method
0934: */
0935: public Class[] getParameterTypes() {
0936: return parameterTypes;
0937: }
0938:
0939: /**
0940: * @return Return the number of the parameters of the method
0941: */
0942: public int getParametersNumber() {
0943: return parameterTypes.length;
0944: }
0945:
0946: /**
0947: * Only for CMP1 finder methods
0948: * @return Return the VcParamWhere list of the method
0949: */
0950: public Vector getParamWhereList() {
0951: if (mParamWhereList == null) {
0952: throw new Error(
0953: "VcMethod.getParamWhereList() not available for the method '"
0954: + mMethod.toString() + "'");
0955: }
0956: return (new Vector(mParamWhereList));
0957: }
0958:
0959: /**
0960: * @return Return the VcParam list of the method
0961: */
0962: public Vector getParamList() {
0963: if (mParamList == null) {
0964: throw new Error(
0965: "VcMethod.getParamList() not available for the method '"
0966: + mMethod.toString() + "'");
0967: }
0968: return (new Vector(mParamList));
0969: }
0970:
0971: /**
0972: * @return Return the index of the method. This is a unique index for each method of a bean.
0973: */
0974: public int getMethodIndex() {
0975: return methodIndex;
0976: }
0977:
0978: /**
0979: * Only for CMP2 finder/select methods
0980: * @return Return the Medor field of tuple result of an Finder/Select method
0981: */
0982: public org.objectweb.medor.api.Field getResFieldOfEjbql() {
0983: return resFieldOfEjbql;
0984: }
0985:
0986: /**
0987: * Only for CMP2 finder/select methods
0988: * @return Return the Medor get method name of the tuple result of an ejbSelect method
0989: */
0990: public String getTupleGetter() {
0991: PType type = resFieldOfEjbql.getType();
0992: switch (type.getTypeCode()) {
0993: case PType.TYPECODE_BOOLEAN:
0994: return "getBoolean";
0995: case PType.TYPECODE_BYTE:
0996: return "getByte";
0997: case PType.TYPECODE_CHAR:
0998: return "getChar";
0999: case PType.TYPECODE_DATE:
1000: return "getDate";
1001: case PType.TYPECODE_DOUBLE:
1002: return "getDouble";
1003: case PType.TYPECODE_FLOAT:
1004: return "getFloat";
1005: case PType.TYPECODE_INT:
1006: return "getInt";
1007: case PType.TYPECODE_LONG:
1008: return "getLong";
1009: case PType.TYPECODE_SERIALIZED:
1010: return "getObject";
1011: case PType.TYPECODE_SHORT:
1012: return "getShort";
1013: case PType.TYPECODE_STRING:
1014: return "getString";
1015: case PType.TYPECODE_BIGDECIMAL:
1016: case PType.TYPECODE_REFERENCE:
1017: case PType.TYPECODE_BYTEARRAY:
1018: case PType.TYPECODE_CHARARRAY:
1019: case PType.TYPECODE_OBJBOOLEAN:
1020: case PType.TYPECODE_OBJBYTE:
1021: case PType.TYPECODE_OBJCHAR:
1022: case PType.TYPECODE_OBJDOUBLE:
1023: case PType.TYPECODE_OBJFLOAT:
1024: case PType.TYPECODE_OBJINT:
1025: case PType.TYPECODE_OBJLONG:
1026: case PType.TYPECODE_OBJSHORT:
1027: default:
1028: return "getObject";
1029: }
1030: }
1031:
1032: /**
1033: * Only for CMP2 finder/select methods
1034: * @return Return true is the return type of the Medor getter method of the tuple result of an ejbSelect method is a primitive type
1035: */
1036: public boolean isTupleGetterPrimitive() {
1037: PType type = resFieldOfEjbql.getType();
1038: switch (type.getTypeCode()) {
1039: case PType.TYPECODE_BOOLEAN:
1040: case PType.TYPECODE_BYTE:
1041: case PType.TYPECODE_CHAR:
1042: case PType.TYPECODE_DOUBLE:
1043: case PType.TYPECODE_FLOAT:
1044: case PType.TYPECODE_INT:
1045: case PType.TYPECODE_LONG:
1046: case PType.TYPECODE_SHORT:
1047: return true;
1048: default:
1049: return false;
1050: }
1051: }
1052:
1053: /**
1054: * Only for CMP2 finder/select methos
1055: * @return Return the associated class of the Medor getter method of the tuple result of an ejbSelect method when it's a primtive type
1056: */
1057: public String getTupleGetterObjectClass() {
1058: PType type = resFieldOfEjbql.getType();
1059: switch (type.getTypeCode()) {
1060: case PType.TYPECODE_BOOLEAN:
1061: return "Boolean";
1062: case PType.TYPECODE_BYTE:
1063: return "Byte";
1064: case PType.TYPECODE_CHAR:
1065: return "Char";
1066: case PType.TYPECODE_DOUBLE:
1067: return "Double";
1068: case PType.TYPECODE_FLOAT:
1069: return "Float";
1070: case PType.TYPECODE_INT:
1071: return "Integer";
1072: case PType.TYPECODE_LONG:
1073: return "Long";
1074: case PType.TYPECODE_SHORT:
1075: return "Short";
1076: default:
1077: return "Object";
1078: }
1079: }
1080:
1081: /**
1082: * Split a dot separated path into tokens
1083: * @param path input path to split
1084: * @return Return the string array of the splited token
1085: */
1086: String[] splitPath(String path) {
1087: StringTokenizer st = new StringTokenizer(path, ".");
1088: String[] ret = new String[st.countTokens()];
1089: for (int i = 0; i < ret.length; i++) {
1090: ret[i] = st.nextToken();
1091: }
1092: return ret;
1093: }
1094:
1095: /**
1096: * Gets an empty array if no parameters and return an array of objects of the parameters if any
1097: * @return array of objects
1098: */
1099: public String getArrayObjectParameters() {
1100: if (mArrayObjectParameters.length() == 0) {
1101: return "new Object[0]";
1102: } else {
1103: return "new Object[] {" + mArrayObjectParameters.toString()
1104: + "}";
1105: }
1106: }
1107:
1108: /**
1109: * @return Return a string representation of the VcMethod object for debug use
1110: */
1111: public String toString() {
1112: StringBuffer ret = new StringBuffer();
1113: ret.append("\n Name = " + getName());
1114: ret.append("\n CapName = " + getCapName());
1115: ret.append("\n FormalParameters = "
1116: + getFormalParameters());
1117: ret.append("\n ActualParameters = "
1118: + getActualParameters());
1119: ret.append("\n ExceptionList = "
1120: + getExceptionList());
1121: if (mSecuritySignature != null) {
1122: ret.append("\n SecuritySignature = "
1123: + getSecuritySignature());
1124: }
1125: if (mTxAttribute != MethodDesc.TX_NULL) {
1126: ret.append("\n TxAttribute = "
1127: + getTxAttribute());
1128: }
1129: ret.append("\n ReturnType = " + getReturnType());
1130: ret.append("\n isFinderByPk = " + isFinderByPk());
1131: ret.append("\n isFinderSimple = " + isFinderSimple());
1132: ret.append("\n isFinderEnum = " + isFinderEnum());
1133: ret.append("\n isFinderCol = " + isFinderCol());
1134: ret.append("\n isCreate = " + isCreate());
1135: ret.append("\n isEjbCreate = " + isEjbCreate());
1136: ret.append("\n isRemoveThis = " + isRemoveThis());
1137: ret.append("\n isRemovePk = " + isRemovePk());
1138: ret.append("\n isRemoveHandle = " + isRemoveHandle());
1139: ret.append("\n isEjbRemove = " + isEjbRemove());
1140: ret.append("\n isEjbLoad = " + isEjbLoad());
1141: ret.append("\n isEjbStore = " + isEjbStore());
1142: ret.append("\n isEjbSetEntityContext = "
1143: + isEjbSetEntityContext());
1144: ret.append("\n isEjbActivate = " + isEjbActivate());
1145: ret.append("\n isEjbSelectSimple = "
1146: + isEjbSelectSimple());
1147: ret.append("\n isEjbSelectSet = " + isEjbSelectSet());
1148: ret.append("\n isEjbSelectCol = " + isEjbSelectCol());
1149: if (mSqlStmt != null) {
1150: ret.append("\n SqlStmt = " + getSqlStmt());
1151: }
1152: if (mParamWhereList != null) {
1153: ret.append("\n ParamWhereList = "
1154: + getParamWhereList());
1155: }
1156: return (ret.toString());
1157: }
1158:
1159: }
|