001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.verifier.strategy;
023:
024: // $Id: AbstractEJB2xVerifier.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
025:
026: import org.jboss.metadata.EntityMetaData;
027: import org.jboss.metadata.QueryMetaData;
028: import org.jboss.util.Classes;
029:
030: import java.lang.reflect.Method;
031: import java.util.Iterator;
032: import java.util.LinkedList;
033: import java.util.List;
034:
035: /**
036: * Abstract EJB 2.x bean verifier.
037: *
038: * @author Thomas.Diesler@jboss.org
039: * @since 08-Feb-2005
040: */
041:
042: public abstract class AbstractEJB2xVerifier extends AbstractVerifier {
043: protected EJBVerifier11 cmp1XVerifier;
044:
045: // The classes for an EJB
046: protected Class bean;
047: protected Class home;
048: protected Class remote;
049: protected Class localHome;
050: protected Class local;
051: protected Class serviceEndpointInterface;
052:
053: /*
054: * Constructor
055: */
056: public AbstractEJB2xVerifier(VerificationContext context) {
057: super (context);
058: cmp1XVerifier = new EJBVerifier11(context);
059: }
060:
061: /**
062: * Check whether the given method is a create(...) method
063: */
064: public boolean isCreateMethod(Method m) {
065: return m.getName().startsWith(CREATE_METHOD);
066: }
067:
068: public boolean isEjbCreateMethod(Method m) {
069: return m.getName().startsWith(EJB_CREATE_METHOD);
070: }
071:
072: public boolean isEjbRemoveMethod(Method m) {
073: return m.getName().startsWith(EJB_REMOVE_METHOD);
074: }
075:
076: public boolean isEjbSelectMethod(Method m) {
077: return m.getName().startsWith(EJB_SELECT_METHOD);
078: }
079:
080: public boolean isEjbHomeMethod(Method m) {
081: return m.getName().startsWith(EJB_HOME_METHOD);
082: }
083:
084: /** Finds java.rmi.Remote interface from the class
085: */
086: public boolean hasRemoteInterface(Class c) {
087: return isAssignableFrom("java.rmi.Remote", c);
088: }
089:
090: /**
091: * Return all ejbSelect methods
092: */
093: public Iterator getEjbSelectMethods(Class c) {
094: List selects = new LinkedList();
095: Method[] method = c.getMethods();
096:
097: for (int i = 0; i < method.length; ++i) {
098: if (isEjbSelectMethod(method[i])) {
099: selects.add(method[i]);
100: }
101: }
102:
103: return selects.iterator();
104: }
105:
106: /**
107: * Searches for an instance of an ejbRemove method from the class
108: */
109: public boolean hasEJBRemoveMethod(Class c) {
110: Method[] method = c.getMethods();
111: for (int i = 0; i < method.length; ++i) {
112: if (isEjbRemoveMethod(method[i]))
113: return true;
114: }
115:
116: return false;
117: }
118:
119: /**
120: * Returns the ejbRemove(...) methods of a bean
121: */
122: public Iterator getEJBRemoveMethods(Class c) {
123: List ejbRemoves = new LinkedList();
124: Method[] method = c.getMethods();
125:
126: for (int i = 0; i < method.length; ++i) {
127: if (isEjbRemoveMethod(method[i]))
128: ejbRemoves.add(method[i]);
129: }
130:
131: return ejbRemoves.iterator();
132: }
133:
134: /**
135: * Home methods are any method on the home interface which is
136: * neither a create or find method.
137: */
138: public Iterator getHomeMethods(Class c) {
139: List homes = new LinkedList();
140: Method[] method = c.getMethods();
141:
142: for (int i = 0; i < method.length; ++i) {
143: if (!isCreateMethod(method[i])
144: && !isFinderMethod(method[i]))
145: homes.add(method[i]);
146: }
147:
148: return homes.iterator();
149: }
150:
151: public Iterator getEjbHomeMethods(Class c) {
152: List homes = new LinkedList();
153: Method[] method = c.getMethods();
154:
155: for (int i = 0; i < method.length; ++i) {
156: if (isEjbHomeMethod(method[i]))
157: homes.add(method[i]);
158: }
159:
160: return homes.iterator();
161: }
162:
163: /**
164: * Check whether there is a matching <query> Element defined
165: * for the Method m
166: *
167: * @param m Method to check, should be either a Finder or a Select
168: * @param e EntityMetaData
169: *
170: * @return <code>true</code> if a matching <query> Element
171: * was located.
172: */
173: protected boolean hasMatchingQuery(Method m, EntityMetaData e) {
174: boolean result = false;
175:
176: Iterator qIt = e.getQueries();
177: while (qIt.hasNext()) {
178: QueryMetaData qmd = (QueryMetaData) qIt.next();
179:
180: // matching names
181: if (!qmd.getMethodName().equals(m.getName())) {
182: continue;
183: }
184:
185: Class[] methodParameter = m.getParameterTypes();
186: Class[] queryParameter = null;
187:
188: try {
189: queryParameter = Classes.convertToJavaClasses(qmd
190: .getMethodParams(), classloader);
191: } catch (ClassNotFoundException cnfe) {
192: // FIXME: this should be handled differently, especially
193: // there shouldn't be a DeploymentException being
194: // thrown ...
195: continue;
196: }
197:
198: // number of parameters has to match
199: if (methodParameter.length != queryParameter.length) {
200: continue;
201: }
202:
203: // walk the parameter list and compare for equality
204: boolean parametersMatch = true;
205: for (int i = 0; i < methodParameter.length; i++) {
206: if (!methodParameter[i].equals(queryParameter[i])) {
207: parametersMatch = false;
208: break;
209: }
210: }
211:
212: if (parametersMatch) {
213: result = true;
214: break;
215: }
216: }
217:
218: return result;
219: }
220: }
|