001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config.rules;
017:
018: import org.apache.openejb.OpenEJBException;
019: import org.apache.openejb.jee.EnterpriseBean;
020: import org.apache.openejb.jee.RemoteBean;
021: import org.apache.openejb.jee.EntityBean;
022: import org.apache.openejb.jee.SessionBean;
023: import org.apache.openejb.config.EjbModule;
024:
025: import javax.ejb.EJBLocalObject;
026: import java.lang.reflect.Method;
027:
028: public class CheckMethods extends ValidationBase {
029:
030: public void validate(EjbModule ejbModule) {
031:
032: for (EnterpriseBean bean : ejbModule.getEjbJar()
033: .getEnterpriseBeans()) {
034: if (!(bean instanceof RemoteBean))
035: continue;
036: RemoteBean b = (RemoteBean) bean;
037:
038: if (b.getHome() != null) {
039: check_remoteInterfaceMethods(b);
040: check_homeInterfaceMethods(b);
041: }
042: if (b.getLocalHome() != null) {
043: check_localInterfaceMethods(b);
044: check_localHomeInterfaceMethods(b);
045: }
046: }
047: }
048:
049: private void check_localHomeInterfaceMethods(RemoteBean b) {
050: Class home = null;
051: Class bean = null;
052: try {
053: home = loadClass(b.getLocalHome());
054: bean = loadClass(b.getEjbClass());
055: } catch (OpenEJBException e) {
056: return;
057: }
058:
059: if (check_hasCreateMethod(b, bean, home)) {
060: check_createMethodsAreImplemented(b, bean, home);
061: check_postCreateMethodsAreImplemented(b, bean, home);
062: }
063:
064: check_unusedCreateMethods(b, bean, home);
065: }
066:
067: private void check_localInterfaceMethods(RemoteBean b) {
068: Class intrface = null;
069: Class beanClass = null;
070: try {
071: intrface = loadClass(b.getLocal());
072: beanClass = loadClass(b.getEjbClass());
073: } catch (OpenEJBException e) {
074: return;
075: }
076:
077: Method[] interfaceMethods = intrface.getMethods();
078: Method[] beanClassMethods = intrface.getMethods();
079:
080: for (int i = 0; i < interfaceMethods.length; i++) {
081: if (interfaceMethods[i].getDeclaringClass() == EJBLocalObject.class)
082: continue;
083: try {
084: String name = interfaceMethods[i].getName();
085: Class[] params = interfaceMethods[i]
086: .getParameterTypes();
087: Method beanMethod = beanClass.getMethod(name, params);
088: } catch (NoSuchMethodException nsme) {
089:
090: fail(b, "no.busines.method", interfaceMethods[i]
091: .getName(), interfaceMethods[i].toString(),
092: "local", intrface.getName(), beanClass
093: .getName());
094: }
095: }
096:
097: }
098:
099: private void check_remoteInterfaceMethods(RemoteBean b) {
100:
101: Class intrface = null;
102: Class beanClass = null;
103: try {
104: intrface = loadClass(b.getRemote());
105: beanClass = loadClass(b.getEjbClass());
106: } catch (OpenEJBException e) {
107: return;
108: }
109:
110: Method[] interfaceMethods = intrface.getMethods();
111: Method[] beanClassMethods = intrface.getMethods();
112:
113: for (int i = 0; i < interfaceMethods.length; i++) {
114: if (interfaceMethods[i].getDeclaringClass() == javax.ejb.EJBObject.class)
115: continue;
116: try {
117: String name = interfaceMethods[i].getName();
118: Class[] params = interfaceMethods[i]
119: .getParameterTypes();
120: Method beanMethod = beanClass.getMethod(name, params);
121: } catch (NoSuchMethodException nsme) {
122:
123: fail(b, "no.busines.method", interfaceMethods[i]
124: .getName(), interfaceMethods[i].toString(),
125: "remote", intrface.getName(), beanClass
126: .getName());
127:
128: }
129: }
130: }
131:
132: private void check_homeInterfaceMethods(RemoteBean b) {
133: Class home = null;
134: Class bean = null;
135: try {
136: home = loadClass(b.getHome());
137: bean = loadClass(b.getEjbClass());
138: } catch (OpenEJBException e) {
139: return;
140: }
141:
142: if (check_hasCreateMethod(b, bean, home)) {
143: check_createMethodsAreImplemented(b, bean, home);
144: check_postCreateMethodsAreImplemented(b, bean, home);
145: }
146:
147: check_unusedCreateMethods(b, bean, home);
148: }
149:
150: public boolean check_hasCreateMethod(RemoteBean b, Class bean,
151: Class home) {
152:
153: if (b instanceof SessionBean
154: && !javax.ejb.SessionBean.class.isAssignableFrom(bean)) {
155: // This is a pojo-style bean
156: return false;
157: }
158:
159: if (b instanceof EntityBean) {
160: // entity beans are not required to have a create method
161: return false;
162: }
163:
164: Method[] homeMethods = home.getMethods();
165:
166: boolean hasCreateMethod = false;
167:
168: for (int i = 0; i < homeMethods.length && !hasCreateMethod; i++) {
169: hasCreateMethod = homeMethods[i].getName().startsWith(
170: "create");
171: }
172:
173: if (!hasCreateMethod) {
174:
175: fail(b, "no.home.create", b.getHome(), b.getRemote());
176:
177: }
178:
179: return hasCreateMethod;
180: }
181:
182: public boolean check_createMethodsAreImplemented(RemoteBean b,
183: Class bean, Class home) {
184: boolean result = true;
185:
186: Method[] homeMethods = home.getMethods();
187:
188: for (int i = 0; i < homeMethods.length; i++) {
189: if (!homeMethods[i].getName().startsWith("create"))
190: continue;
191:
192: Method create = homeMethods[i];
193:
194: StringBuilder ejbCreateName = new StringBuilder(create
195: .getName());
196: ejbCreateName.replace(0, 1, "ejbC");
197:
198: try {
199: if (EnterpriseBean.class.isAssignableFrom(bean)) {
200: bean.getMethod(ejbCreateName.toString(), create
201: .getParameterTypes());
202: } else {
203: // TODO: Check for Init method in pojo session bean class
204: }
205: } catch (NoSuchMethodException e) {
206: result = false;
207:
208: String paramString = getParameters(create);
209:
210: if (b instanceof EntityBean) {
211: EntityBean entity = (EntityBean) b;
212:
213: fail(b, "entity.no.ejb.create", b.getEjbClass(),
214: entity.getPrimKeyClass(), ejbCreateName
215: .toString(), paramString);
216:
217: } else {
218:
219: fail(b, "session.no.ejb.create", b.getEjbClass(),
220: ejbCreateName.toString(), paramString);
221:
222: }
223: }
224: }
225:
226: return result;
227: }
228:
229: public boolean check_postCreateMethodsAreImplemented(RemoteBean b,
230: Class bean, Class home) {
231: boolean result = true;
232:
233: if (b instanceof SessionBean)
234: return true;
235:
236: Method[] homeMethods = home.getMethods();
237: Method[] beanMethods = bean.getMethods();
238:
239: for (int i = 0; i < homeMethods.length; i++) {
240: if (!homeMethods[i].getName().startsWith("create"))
241: continue;
242: Method create = homeMethods[i];
243: StringBuilder ejbPostCreateName = new StringBuilder(create
244: .getName());
245: ejbPostCreateName.replace(0, 1, "ejbPostC");
246: try {
247: bean.getMethod(ejbPostCreateName.toString(), create
248: .getParameterTypes());
249: } catch (NoSuchMethodException e) {
250: result = false;
251:
252: String paramString = getParameters(create);
253:
254: fail(b, "no.ejb.post.create", b.getEjbClass(),
255: ejbPostCreateName.toString(), paramString);
256:
257: }
258: }
259:
260: return result;
261: }
262:
263: public boolean check_unusedCreateMethods(RemoteBean b, Class bean,
264: Class home) {
265: boolean result = true;
266:
267: Method[] homeMethods = home.getMethods();
268: Method[] beanMethods = bean.getMethods();
269:
270: for (int i = 0; i < homeMethods.length; i++) {
271: if (!beanMethods[i].getName().startsWith("ejbCreate"))
272: continue;
273: Method ejbCreate = beanMethods[i];
274: StringBuilder create = new StringBuilder(ejbCreate
275: .getName());
276: create.replace(0, 4, "c");
277: try {
278: // TODO, don't just check the remote home interface, there is a local too
279: home.getMethod(create.toString(), ejbCreate
280: .getParameterTypes());
281: } catch (NoSuchMethodException e) {
282: result = false;
283:
284: String paramString = getParameters(ejbCreate);
285:
286: warn(b, "unused.ejb.create", b.getEjbClass(), ejbCreate
287: .getName(), create.toString(), paramString,
288: home.getName());
289:
290: }
291: }
292:
293: return result;
294: }
295:
296: /// public void check_findMethods(){
297: /// if(this.componentType == this.BMP_ENTITY ){
298: ///
299: /// String beanMethodName = "ejbF"+method.getName().substring(1);
300: /// beanMethod = beanClass.getMethod(beanMethodName,method.getParameterTypes());
301: /// }
302: /// }
303: ///
304: /// public void check_homeMethods(){
305: /// String beanMethodName = "ejbHome"+method.getName().substring(0,1).toUpperCase()+method.getName().substring(1);
306: /// beanMethod = beanClass.getMethod(beanMethodName,method.getParameterTypes());
307: /// }
308:
309: }
|