package org.jboss.verifier.strategy;
import org.jboss.metadata.EntityMetaData;
import org.jboss.metadata.SessionMetaData;
import org.jboss.verifier.Section;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
public class EJBVerifier11 extends AbstractVerifier
{
public EJBVerifier11(VerificationContext context)
{
super(context);
}
public String getMessageBundle()
{
return "EJB11Messages.properties";
}
public void checkSession(SessionMetaData session)
{
boolean beanVerified = false;
boolean homeVerified = false;
boolean remoteVerified = false;
beanVerified = verifySessionBean(session);
homeVerified = verifySessionHome(session);
remoteVerified = verifySessionRemote(session);
if (beanVerified && homeVerified && remoteVerified)
{
fireBeanVerifiedEvent(session);
}
}
public void checkEntity(EntityMetaData entity)
{
boolean pkVerified = false;
boolean beanVerified = false;
boolean homeVerified = false;
boolean remoteVerified = false;
beanVerified = verifyEntityBean(entity);
homeVerified = verifyEntityHome(entity);
remoteVerified = verifyEntityRemote(entity);
pkVerified = verifyPrimaryKey(entity);
if (beanVerified && homeVerified && remoteVerified && pkVerified)
{
fireBeanVerifiedEvent(entity);
}
}
public boolean isCreateMethod(Method m)
{
return m.getName().equals(CREATE_METHOD);
}
public boolean isEjbCreateMethod(Method m)
{
return m.getName().equals(EJB_CREATE_METHOD);
}
private boolean verifySessionHome(SessionMetaData session)
{
boolean status = true;
String name = session.getHome();
if (name == null)
return false;
try
{
Class home = classloader.loadClass(name);
if (session.isStateless())
{
if (!hasDefaultCreateMethod(home))
{
fireSpecViolationEvent(session, new Section("6.8.a"));
status = false;
}
else
{
Method create = getDefaultCreateMethod(home);
if (!hasRemoteReturnType(session, create))
{
fireSpecViolationEvent(session, create, new Section("6.8.b"));
;
status = false;
}
if (hasMoreThanOneCreateMethods(home))
{
fireSpecViolationEvent(session, new Section("6.8.c"));
status = false;
}
}
}
if (!hasEJBHomeInterface(home))
{
fireSpecViolationEvent(session, new Section("6.10.6.a"));
status = false;
}
Iterator it = Arrays.asList(home.getMethods()).iterator();
while (it.hasNext())
{
Method method = (Method)it.next();
if (!hasLegalRMIIIOPArguments(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.6.b"));
status = false;
}
if (!hasLegalRMIIIOPReturnType(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.6.c"));
status = false;
}
if (!throwsRemoteException(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.6.d"));
status = false;
}
}
if (!hasCreateMethod(home))
{
fireSpecViolationEvent(session, new Section("6.10.6.e"));
status = false;
}
Iterator createMethods = getCreateMethods(home);
try
{
String beanClass = session.getEjbClass();
Class bean = classloader.loadClass(beanClass);
while (createMethods.hasNext())
{
Method create = (Method)createMethods.next();
if (!hasMatchingEJBCreate(bean, create))
{
fireSpecViolationEvent(session, create, new Section("6.10.6.f"));
status = false;
}
if (!hasRemoteReturnType(session, create))
{
fireSpecViolationEvent(session, create, new Section("6.10.6.g"));
status = false;
}
if (hasMatchingEJBCreate(bean, create))
{
Method ejbCreate = getMatchingEJBCreate(bean, create);
if (!hasMatchingExceptions(ejbCreate, create))
{
fireSpecViolationEvent(session, create, new Section("6.10.6.h"));
status = false;
}
}
if (!throwsCreateException(create))
{
fireSpecViolationEvent(session, create, new Section("6.10.6.i"));
status = false;
}
}
}
catch (ClassNotFoundException ignored)
{
}
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(session, new Section("16.2.c"));
status = false;
}
return status;
}
private boolean verifySessionRemote(SessionMetaData session)
{
boolean status = true;
String name = session.getRemote();
if (name == null)
return false;
try
{
Class remote = classloader.loadClass(name);
if (!hasEJBObjectInterface(remote))
{
fireSpecViolationEvent(session, new Section("6.10.5.a"));
status = false;
}
Iterator it = Arrays.asList(remote.getMethods()).iterator();
while (it.hasNext())
{
Method method = (Method)it.next();
if (!hasLegalRMIIIOPArguments(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.5.b"));
status = false;
}
if (!hasLegalRMIIIOPReturnType(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.5.c"));
status = false;
}
if (!throwsRemoteException(method))
{
fireSpecViolationEvent(session, method, new Section("6.10.5.d"));
status = false;
}
}
String beanName = session.getEjbClass();
Class bean = classloader.loadClass(beanName);
Iterator iterator = Arrays.asList(remote.getDeclaredMethods()).iterator();
while (iterator.hasNext())
{
Method remoteMethod = (Method)iterator.next();
if (!hasMatchingMethod(bean, remoteMethod))
{
fireSpecViolationEvent(session, remoteMethod, new Section("6.10.5.e"));
status = false;
}
if (hasMatchingMethod(bean, remoteMethod))
{
try
{
Method beanMethod = bean.getMethod(remoteMethod.getName(), remoteMethod.getParameterTypes());
if (!hasMatchingReturnType(remoteMethod, beanMethod))
{
fireSpecViolationEvent(session, remoteMethod, new Section("6.10.5.f"));
status = false;
}
if (!hasMatchingExceptions(beanMethod, remoteMethod))
{
fireSpecViolationEvent(session, remoteMethod, new Section("6.10.5.g"));
status = false;
}
}
catch (NoSuchMethodException ignored)
{
}
}
} }
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(session, new Section("16.2.d"));
status = false;
}
return status;
}
private boolean verifySessionBean(SessionMetaData session)
{
boolean status = true;
String name = session.getEjbClass();
try
{
Class bean = classloader.loadClass(name);
if (!hasSessionBeanInterface(bean))
{
fireSpecViolationEvent(session, new Section("6.5.1"));
status = false;
}
if (hasSessionSynchronizationInterface(bean))
{
if (session.isStateless())
{
fireSpecViolationEvent(session, new Section("6.5.3.a"));
status = false;
}
if (session.isBeanManagedTx())
{
fireSpecViolationEvent(session, new Section("6.5.3.b"));
status = false;
}
}
if (!hasEJBCreateMethod(bean, true))
{
fireSpecViolationEvent(session, new Section("6.5.5"));
status = false;
}
if (hasSessionSynchronizationInterface(bean)
&& session.isBeanManagedTx())
{
fireSpecViolationEvent(session, new Section("6.6.1"));
status = false;
}
if (!isPublic(bean))
{
fireSpecViolationEvent(session, new Section("6.10.2.a"));
status = false;
}
if (isFinal(bean))
{
fireSpecViolationEvent(session, new Section("6.10.2.b"));
status = false;
}
if (isAbstract(bean))
{
fireSpecViolationEvent(session, new Section("6.10.2.c"));
status = false;
}
if (!hasDefaultConstructor(bean))
{
fireSpecViolationEvent(session, new Section("6.10.2.d"));
status = false;
}
if (hasFinalizer(bean))
{
fireSpecViolationEvent(session, new Section("6.10.2.e"));
status = false;
}
if (hasEJBCreateMethod(bean, true))
{
Iterator it = getEJBCreateMethods(bean);
while (it.hasNext())
{
Method ejbCreate = (Method)it.next();
if (!isPublic(ejbCreate))
{
fireSpecViolationEvent(session, ejbCreate, new Section("6.10.3.a"));
status = false;
}
if ((isFinal(ejbCreate)) || (isStatic(ejbCreate)))
{
fireSpecViolationEvent(session, ejbCreate, new Section("6.10.3.b"));
status = false;
}
if (!hasVoidReturnType(ejbCreate))
{
fireSpecViolationEvent(session, ejbCreate, new Section("6.10.3.c"));
status = false;
}
if (!hasLegalRMIIIOPArguments(ejbCreate))
{
fireSpecViolationEvent(session, ejbCreate, new Section("6.10.3.d"));
status = false;
}
} }
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(session, new Section("16.2.b"));
status = false;
}
return status;
}
private boolean verifyEntityHome(EntityMetaData entity)
{
boolean status = true;
String name = entity.getHome();
if (name == null)
return false;
try
{
Class home = classloader.loadClass(name);
if (!hasEJBHomeInterface(home))
{
fireSpecViolationEvent(entity, new Section("9.2.8.a"));
status = false;
}
Iterator homeMethods = Arrays.asList(home.getMethods()).iterator();
while (homeMethods.hasNext())
{
Method method = (Method)homeMethods.next();
if (!hasLegalRMIIIOPArguments(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.8.b"));
status = false;
}
if (!hasLegalRMIIIOPReturnType(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.8.c"));
status = false;
}
if (!throwsRemoteException(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.8.d"));
status = false;
}
}
homeMethods = Arrays.asList(home.getMethods()).iterator();
while (homeMethods.hasNext())
{
Method method = (Method)homeMethods.next();
if (method.getDeclaringClass().getName().equals(EJB_HOME_INTERFACE))
continue;
if (!(isCreateMethod(method) || isFinderMethod(method)))
{
fireSpecViolationEvent(entity, method, new Section("9.2.8.e"));
status = false;
}
}
try
{
String beanClass = entity.getEjbClass();
Class bean = classloader.loadClass(beanClass);
Iterator createMethods = getCreateMethods(home);
while (createMethods.hasNext())
{
Method create = (Method)createMethods.next();
if (!hasMatchingEJBCreate(bean, create))
{
fireSpecViolationEvent(entity, create, new Section("9.2.8.f"));
status = false;
}
if (!hasRemoteReturnType(entity, create))
{
fireSpecViolationEvent(entity, create, new Section("9.2.8.g"));
status = false;
}
if (hasMatchingEJBCreate(bean, create)
&& hasMatchingEJBPostCreate(bean, create))
{
Method ejbCreate = getMatchingEJBCreate(bean, create);
Method ejbPostCreate = getMatchingEJBPostCreate(bean, create);
if (!(hasMatchingExceptions(ejbCreate, create)
&& hasMatchingExceptions(ejbPostCreate, create)))
{
fireSpecViolationEvent(entity, create, new Section("9.2.8.h"));
status = false;
}
}
if (!throwsCreateException(create))
{
fireSpecViolationEvent(entity, create, new Section("9.2.8.i"));
status = false;
}
}
}
catch (ClassNotFoundException ignored)
{
}
try
{
String beanClass = entity.getEjbClass();
Class bean = classloader.loadClass(beanClass);
Iterator finderMethods = getFinderMethods(home);
while (finderMethods.hasNext())
{
Method finder = (Method)finderMethods.next();
if ((entity.isBMP()) && (!hasMatchingEJBFind(bean, finder)))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.8.j"));
status = false;
}
if (!(hasRemoteReturnType(entity, finder)
|| isMultiObjectFinder(finder)))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.8.k"));
status = false;
}
if ((entity.isBMP()) && (hasMatchingEJBFind(bean, finder)))
{
Method ejbFind = getMatchingEJBFind(bean, finder);
if (!(hasMatchingExceptions(ejbFind, finder)))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.8.l"));
status = false;
}
}
if (!throwsFinderException(finder))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.8.m"));
status = false;
}
}
}
catch (ClassNotFoundException ignored)
{
}
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(entity, new Section("16.2.c"));
status = false;
}
return status;
}
private boolean verifyEntityRemote(EntityMetaData entity)
{
boolean status = true;
String name = entity.getRemote();
if (name == null)
return false;
try
{
Class remote = classloader.loadClass(name);
if (!hasEJBObjectInterface(remote))
{
fireSpecViolationEvent(entity, new Section("9.2.7.a"));
status = false;
}
Iterator remoteMethods = Arrays.asList(remote.getMethods()).iterator();
while (remoteMethods.hasNext())
{
Method method = (Method)remoteMethods.next();
if (!hasLegalRMIIIOPArguments(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.b"));
status = false;
}
if (!hasLegalRMIIIOPReturnType(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.c"));
status = false;
}
if (!hasLegalRMIIIOPExceptionTypes(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.h"));
status = false;
}
if (!throwsRemoteException(method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.d"));
status = false;
}
}
try
{
String beanClass = entity.getEjbClass();
Class bean = classloader.loadClass(beanClass);
remoteMethods = Arrays.asList(remote.getMethods()).iterator();
while (remoteMethods.hasNext())
{
Method method = (Method)remoteMethods.next();
if (method.getDeclaringClass().getName().equals(EJB_OBJECT_INTERFACE))
continue;
if (!hasMatchingMethod(bean, method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.e"));
status = false;
}
if (hasMatchingMethod(bean, method))
{
try
{
Method beanMethod = bean.getMethod(method.getName(), method.getParameterTypes());
if (!hasMatchingReturnType(beanMethod, method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.f"));
status = false;
}
if (!hasMatchingExceptions(beanMethod, method))
{
fireSpecViolationEvent(entity, method, new Section("9.2.7.g"));
status = false;
}
}
catch (NoSuchMethodException ignored)
{
}
}
}
}
catch (ClassNotFoundException ignored)
{
}
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(entity, new Section("16.2.d"));
status = false;
}
return status;
}
private boolean verifyEntityBean(EntityMetaData entity)
{
boolean status = true;
String name = entity.getEjbClass();
try
{
Class bean = classloader.loadClass(name);
if (!hasEntityBeanInterface(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.a"));
status = false;
}
if (!isPublic(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.b"));
status = false;
}
if (isAbstract(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.c"));
status = false;
}
if (isFinal(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.d"));
status = false;
}
if (!hasDefaultConstructor(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.e"));
status = false;
}
if (hasFinalizer(bean))
{
fireSpecViolationEvent(entity, new Section("9.2.2.f"));
status = false;
}
if (hasEJBCreateMethod(bean, false))
{
Iterator it = getEJBCreateMethods(bean);
while (it.hasNext())
{
Method ejbCreate = (Method)it.next();
if (!isPublic(ejbCreate))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.3.a"));
status = false;
}
if ((isFinal(ejbCreate)) || (isStatic(ejbCreate)))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.3.b"));
status = false;
}
if (!hasPrimaryKeyReturnType(entity, ejbCreate))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.3.c"));
status = false;
}
if (!hasLegalRMIIIOPArguments(ejbCreate))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.3.d"));
status = false;
}
if (!hasLegalRMIIIOPReturnType(ejbCreate))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.3.e"));
status = false;
}
}
}
if (hasEJBCreateMethod(bean, false))
{
Iterator it = getEJBCreateMethods(bean);
while (it.hasNext())
{
Method ejbCreate = (Method)it.next();
if (!hasMatchingEJBPostCreate(bean, ejbCreate))
{
fireSpecViolationEvent(entity, ejbCreate, new Section("9.2.4.a"));
status = false;
}
if (hasMatchingEJBPostCreate(bean, ejbCreate))
{
Method ejbPostCreate = getMatchingEJBPostCreate(bean, ejbCreate);
if (!isPublic(ejbPostCreate))
{
fireSpecViolationEvent(entity, ejbPostCreate, new Section("9.2.4.b"));
status = false;
}
if (isStatic(ejbPostCreate))
{
fireSpecViolationEvent(entity, ejbPostCreate, new Section("9.2.4.c"));
status = false;
}
if (isFinal(ejbPostCreate))
{
fireSpecViolationEvent(entity, ejbPostCreate, new Section("9.2.4.d"));
status = false;
}
if (!hasVoidReturnType(ejbPostCreate))
{
fireSpecViolationEvent(entity, ejbPostCreate, new Section("9.2.4.e"));
status = false;
}
}
}
}
if (entity.isBMP() && (!hasEJBFindByPrimaryKey(bean)))
{
fireSpecViolationEvent(entity, new Section("9.2.5.a"));
status = false;
}
if (hasEJBFindByPrimaryKey(bean))
{
Method ejbFindByPrimaryKey = getEJBFindByPrimaryKey(bean);
if (!hasPrimaryKeyReturnType(entity, ejbFindByPrimaryKey))
{
fireSpecViolationEvent(entity, ejbFindByPrimaryKey, new Section("9.2.5.b"));
status = false;
}
if (!isSingleObjectFinder(entity, ejbFindByPrimaryKey))
{
fireSpecViolationEvent(entity, ejbFindByPrimaryKey, new Section("9.2.5.c"));
status = false;
}
}
if (hasFinderMethod(bean))
{
Iterator it = getEJBFindMethods(bean);
while (it.hasNext())
{
Method finder = (Method)it.next();
if (!isPublic(finder))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.5.d"));
status = false;
}
if (isFinal(finder))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.5.e"));
status = false;
}
if (isStatic(finder))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.5.f"));
status = false;
}
if (!hasLegalRMIIIOPArguments(finder))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.5.g"));
status = false;
}
if (!(isSingleObjectFinder(entity, finder)
|| isMultiObjectFinder(finder)))
{
fireSpecViolationEvent(entity, finder, new Section("9.2.5.h"));
status = false;
}
}
}
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(entity, new Section("16.2.b"));
status = false;
}
return status;
}
private boolean verifyPrimaryKey(EntityMetaData entity)
{
boolean status = true;
if (entity.getPrimaryKeyClass() == null
|| entity.getPrimaryKeyClass().length() == 0)
{
fireSpecViolationEvent(entity, new Section("16.5.a"));
return false; }
if (entity.getPrimKeyField() == null
|| entity.getPrimKeyField().length() == 0)
{
Class cls = null;
try
{
cls = classloader.loadClass(entity.getPrimaryKeyClass());
if (entity.isCMP())
{
if (!isPublic(cls))
{
fireSpecViolationEvent(entity, new Section("9.4.7.2.a"));
status = false;
}
if (!isAllFieldsPublic(cls))
{
fireSpecViolationEvent(entity, new Section("9.4.7.2.b"));
status = false;
}
if (!hasANonStaticField(cls))
{
fireSpecViolationEvent(entity, new Section("9.4.7.2.c"));
status = false;
}
}
if (!cls.getName().equals("java.lang.Object"))
{
Object one, two;
try
{
one = cls.newInstance();
two = cls.newInstance();
try
{
if (!one.equals(two))
{
fireSpecViolationEvent(entity, new Section("9.2.9.b"));
status = false;
}
}
catch (NullPointerException e)
{
} try
{
if (one.hashCode() != two.hashCode())
{
fireSpecViolationEvent(entity, new Section("9.2.9.c"));
status = false;
}
}
catch (NullPointerException e)
{
} }
catch (IllegalAccessException e)
{
}
catch (InstantiationException e)
{
}
}
}
catch (ClassNotFoundException e)
{
fireSpecViolationEvent(entity, new Section("16.2.e"));
status = false; }
}
else
{
if (entity.isBMP())
{
fireSpecViolationEvent(entity, new Section("9.4.7.1.a"));
status = false;
}
try
{
Class fieldClass = classloader.loadClass(entity.getEjbClass());
Field field = null;
try
{
field = fieldClass.getField(entity.getPrimKeyField());
if (!entity.getPrimaryKeyClass().equals(field.getType().getName()))
{
fireSpecViolationEvent(entity, new Section("9.4.7.1.c"));
status = false;
}
Iterator it = entity.getCMPFields();
boolean found = false;
while (it.hasNext())
{
String fieldName = (String)it.next();
if (fieldName.equals(entity.getPrimKeyField()))
{
found = true;
break;
}
}
if (!found)
{
fireSpecViolationEvent(entity, new Section("9.4.7.1.d"));
status = false;
}
}
catch (NoSuchFieldException e)
{
fireSpecViolationEvent(entity, new Section("9.4.7.1.b"));
status = false;
}
}
catch (ClassNotFoundException e)
{
} }
return status;
}
}