KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > verifier > strategy > AbstractEJB2xVerifier


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.verifier.strategy;
23
24 // $Id: AbstractEJB2xVerifier.java 41248 2006-02-17 08:31:12Z starksm $
25

26 import org.jboss.metadata.EntityMetaData;
27 import org.jboss.metadata.QueryMetaData;
28 import org.jboss.util.Classes;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * Abstract EJB 2.x bean verifier.
37  *
38  * @author Thomas.Diesler@jboss.org
39  * @since 08-Feb-2005
40  */

41
42 public abstract class AbstractEJB2xVerifier extends AbstractVerifier
43 {
44    protected EJBVerifier11 cmp1XVerifier;
45
46    // The classes for an EJB
47
protected Class JavaDoc bean;
48    protected Class JavaDoc home;
49    protected Class JavaDoc remote;
50    protected Class JavaDoc localHome;
51    protected Class JavaDoc local;
52    protected Class JavaDoc serviceEndpointInterface;
53
54    /*
55     * Constructor
56     */

57    public AbstractEJB2xVerifier(VerificationContext context)
58    {
59       super(context);
60       cmp1XVerifier = new EJBVerifier11(context);
61    }
62
63    /**
64     * Check whether the given method is a create(...) method
65     */

66    public boolean isCreateMethod(Method JavaDoc m)
67    {
68       return m.getName().startsWith(CREATE_METHOD);
69    }
70
71    public boolean isEjbCreateMethod(Method JavaDoc m)
72    {
73       return m.getName().startsWith(EJB_CREATE_METHOD);
74    }
75
76    public boolean isEjbRemoveMethod(Method JavaDoc m)
77    {
78       return m.getName().startsWith(EJB_REMOVE_METHOD);
79    }
80
81    public boolean isEjbSelectMethod(Method JavaDoc m)
82    {
83       return m.getName().startsWith(EJB_SELECT_METHOD);
84    }
85
86    public boolean isEjbHomeMethod(Method JavaDoc m)
87    {
88       return m.getName().startsWith(EJB_HOME_METHOD);
89    }
90
91    /** Finds java.rmi.Remote interface from the class
92     */

93    public boolean hasRemoteInterface(Class JavaDoc c)
94    {
95       return isAssignableFrom("java.rmi.Remote", c);
96    }
97
98    /**
99     * Return all ejbSelect methods
100     */

101    public Iterator JavaDoc getEjbSelectMethods(Class JavaDoc c)
102    {
103       List JavaDoc selects = new LinkedList JavaDoc();
104       Method JavaDoc[] method = c.getMethods();
105
106       for (int i = 0; i < method.length; ++i)
107       {
108          if (isEjbSelectMethod(method[i]))
109          {
110             selects.add(method[i]);
111          }
112       }
113
114       return selects.iterator();
115    }
116
117    /**
118     * Searches for an instance of an ejbRemove method from the class
119     */

120    public boolean hasEJBRemoveMethod(Class JavaDoc c)
121    {
122       Method JavaDoc[] method = c.getMethods();
123       for (int i = 0; i < method.length; ++i)
124       {
125          if (isEjbRemoveMethod(method[i]))
126             return true;
127       }
128
129       return false;
130    }
131
132    /**
133     * Returns the ejbRemove(...) methods of a bean
134     */

135    public Iterator JavaDoc getEJBRemoveMethods(Class JavaDoc c)
136    {
137       List JavaDoc ejbRemoves = new LinkedList JavaDoc();
138       Method JavaDoc[] method = c.getMethods();
139
140       for (int i = 0; i < method.length; ++i)
141       {
142          if (isEjbRemoveMethod(method[i]))
143             ejbRemoves.add(method[i]);
144       }
145
146       return ejbRemoves.iterator();
147    }
148
149    /**
150     * Home methods are any method on the home interface which is
151     * neither a create or find method.
152     */

153    public Iterator JavaDoc getHomeMethods(Class JavaDoc c)
154    {
155       List JavaDoc homes = new LinkedList JavaDoc();
156       Method JavaDoc[] method = c.getMethods();
157
158       for (int i = 0; i < method.length; ++i)
159       {
160          if (!isCreateMethod(method[i]) && !isFinderMethod(method[i]))
161             homes.add(method[i]);
162       }
163
164       return homes.iterator();
165    }
166
167    public Iterator JavaDoc getEjbHomeMethods(Class JavaDoc c)
168    {
169       List JavaDoc homes = new LinkedList JavaDoc();
170       Method JavaDoc[] method = c.getMethods();
171
172       for (int i = 0; i < method.length; ++i)
173       {
174          if (isEjbHomeMethod(method[i]))
175             homes.add(method[i]);
176       }
177
178       return homes.iterator();
179    }
180
181    /**
182     * Check whether there is a matching &lt;query&gt; Element defined
183     * for the Method m
184     *
185     * @param m Method to check, should be either a Finder or a Select
186     * @param e EntityMetaData
187     *
188     * @return <code>true</code> if a matching &lt;query&gt; Element
189     * was located.
190     */

191    protected boolean hasMatchingQuery(Method JavaDoc m, EntityMetaData e)
192    {
193       boolean result = false;
194
195       Iterator JavaDoc qIt = e.getQueries();
196       while (qIt.hasNext())
197       {
198          QueryMetaData qmd = (QueryMetaData)qIt.next();
199
200          // matching names
201
if (!qmd.getMethodName().equals(m.getName()))
202          {
203             continue;
204          }
205
206          Class JavaDoc[] methodParameter = m.getParameterTypes();
207          Class JavaDoc[] queryParameter = null;
208
209          try
210          {
211             queryParameter = Classes.convertToJavaClasses(qmd.getMethodParams(), classloader);
212          }
213          catch (ClassNotFoundException JavaDoc cnfe)
214          {
215             // FIXME: this should be handled differently, especially
216
// there shouldn't be a DeploymentException being
217
// thrown ...
218
continue;
219          }
220
221          // number of parameters has to match
222
if (methodParameter.length != queryParameter.length)
223          {
224             continue;
225          }
226
227          // walk the parameter list and compare for equality
228
boolean parametersMatch = true;
229          for (int i = 0; i < methodParameter.length; i++)
230          {
231             if (!methodParameter[i].equals(queryParameter[i]))
232             {
233                parametersMatch = false;
234                break;
235             }
236          }
237
238          if (parametersMatch)
239          {
240             result = true;
241             break;
242          }
243       }
244
245       return result;
246    }
247 }
248
Popular Tags