Search in sources :

Example 6 with SessionBean

use of org.apache.openejb.jee.SessionBean in project tomee by apache.

the class CheckDependsOn method validate.

public void validate(final AppModule appModule) {
    module = appModule;
    final LinkResolver<Bean> app = new LinkResolver<Bean>();
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final Resolver<Bean> resolver = new Resolver(app, new LinkResolver<Bean>());
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            final Bean b = new Bean(bean, ejbModule, ejbModule.getModuleUri(), resolver);
            resolver.module.add(ejbModule.getModuleUri(), bean.getEjbName(), b);
            resolver.app.add(ejbModule.getModuleUri(), bean.getEjbName(), b);
        }
    }
    for (final Bean bean : app.values()) {
        final EnterpriseBean enterpriseBean = bean.bean;
        if (!(enterpriseBean instanceof SessionBean)) {
            continue;
        }
        final SessionBean sessionBean = (SessionBean) enterpriseBean;
        if (sessionBean.getSessionType() != SessionType.SINGLETON) {
            continue;
        }
        for (final String ejbName : sessionBean.getDependsOn()) {
            final Bean referee = bean.resolveLink(ejbName);
            if (referee == null) {
                bean.module.getValidation().fail(enterpriseBean.getEjbName(), "dependsOn.noSuchEjb", ejbName);
            } else {
                bean.dependsOn.add(referee);
            }
        }
    }
    try {
        References.sort(new ArrayList<Bean>(app.values()), new References.Visitor<Bean>() {

            public String getName(final Bean t) {
                return t.getId();
            }

            public Set<String> getReferences(final Bean t) {
                final LinkedHashSet<String> refs = new LinkedHashSet<String>();
                for (final Bean bean : t.dependsOn) {
                    refs.add(bean.getId());
                }
                return refs;
            }
        });
    } catch (final CircularReferencesException e) {
        for (final List<Bean> circuit : e.getCircuits()) {
            final List<String> ejbNames = new ArrayList<String>(circuit.size());
            for (final Bean bean : circuit) {
                ejbNames.add(bean.bean.getEjbName());
            }
            fail("EAR", "dependsOn.circuit", Join.join(" -> ", ejbNames), ejbNames.get(0));
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) LinkResolver(org.apache.openejb.util.LinkResolver) EjbModule(org.apache.openejb.config.EjbModule) SessionBean(org.apache.openejb.jee.SessionBean) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) SessionBean(org.apache.openejb.jee.SessionBean) LinkResolver(org.apache.openejb.util.LinkResolver) References(org.apache.openejb.util.References) CircularReferencesException(org.apache.openejb.util.CircularReferencesException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with SessionBean

use of org.apache.openejb.jee.SessionBean in project tomee by apache.

the class CheckMethods method check_postCreateMethodsAreImplemented.

public boolean check_postCreateMethodsAreImplemented(final RemoteBean b, final Class bean, final Class home) {
    boolean result = true;
    if (b instanceof SessionBean) {
        return true;
    }
    final Method[] homeMethods = home.getMethods();
    final Method[] beanMethods = bean.getMethods();
    for (int i = 0; i < homeMethods.length; i++) {
        if (!homeMethods[i].getName().startsWith("create")) {
            continue;
        }
        final Method create = homeMethods[i];
        final StringBuilder ejbPostCreateName = new StringBuilder(create.getName());
        ejbPostCreateName.replace(0, 1, "ejbPostC");
        try {
            bean.getMethod(ejbPostCreateName.toString(), create.getParameterTypes());
        } catch (final NoSuchMethodException e) {
            result = false;
            final String paramString = getParameters(create);
            fail(b, "no.ejb.post.create", b.getEjbClass(), ejbPostCreateName.toString(), paramString);
        }
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) SessionBean(org.apache.openejb.jee.SessionBean)

Example 8 with SessionBean

use of org.apache.openejb.jee.SessionBean in project tomee by apache.

the class CheckMethods method check_createMethodsAreImplemented.

public boolean check_createMethodsAreImplemented(final RemoteBean b, final Class bean, final Class home) {
    boolean result = true;
    final Method[] homeMethods = home.getMethods();
    for (int i = 0; i < homeMethods.length; i++) {
        if (!homeMethods[i].getName().startsWith("create")) {
            continue;
        }
        final Method create = homeMethods[i];
        final StringBuilder ejbCreateName = new StringBuilder(create.getName());
        ejbCreateName.replace(0, 1, "ejbC");
        try {
            if (javax.ejb.EnterpriseBean.class.isAssignableFrom(bean)) {
                bean.getMethod(ejbCreateName.toString(), create.getParameterTypes());
            }
        // TODO: else { /* Check for Init method in pojo session bean class */ }
        } catch (final NoSuchMethodException e) {
            result = false;
            final String paramString = getParameters(create);
            if (b instanceof EntityBean) {
                final EntityBean entity = (EntityBean) b;
                fail(b, "entity.no.ejb.create", b.getEjbClass(), entity.getPrimKeyClass(), ejbCreateName.toString(), paramString);
            } else {
                if (b instanceof SessionBean) {
                    final SessionBean sb = (SessionBean) b;
                    // Under EJB 3.1, it is not required that a stateless session bean have an ejbCreate method, even when it has a home interface
                    if (!sb.getSessionType().equals(SessionType.STATELESS)) {
                        fail(b, "session.no.ejb.create", b.getEjbClass(), ejbCreateName.toString(), paramString);
                    }
                }
            }
        }
    }
    return result;
}
Also used : EntityBean(org.apache.openejb.jee.EntityBean) Method(java.lang.reflect.Method) SessionBean(org.apache.openejb.jee.SessionBean)

Example 9 with SessionBean

use of org.apache.openejb.jee.SessionBean in project tomee by apache.

the class CheckPersistenceRefs method validate.

public void validate(final EjbModule ejbModule) {
    for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
        if (bean instanceof SessionBean) {
            final SessionBean sessionBean = (SessionBean) bean;
            if (sessionBean.getSessionType() == null) {
                // skipping since we don't know here what is the type
                continue;
            }
        }
        final String beanType = getType(bean);
        if (beanType.equals("Stateful")) {
            // skip statefuls and Comp ManagedBean
            continue;
        }
        for (final PersistenceContextRef ref : bean.getPersistenceContextRef()) {
            if (isExtented(ref)) {
                String refName = ref.getName();
                final String prefix = bean.getEjbClass() + "/";
                if (refName.startsWith(prefix)) {
                    refName = refName.substring(prefix.length());
                }
                fail(bean, "persistenceContextExtented.nonStateful", refName, beanType);
            }
        }
    }
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) PersistenceContextRef(org.apache.openejb.jee.PersistenceContextRef) SessionBean(org.apache.openejb.jee.SessionBean)

Example 10 with SessionBean

use of org.apache.openejb.jee.SessionBean in project tomee by apache.

the class CheckRestMethodArePublic method validate.

@Override
public void validate(final AppModule appModule) {
    // valid standalone classes
    final Collection<String> standAloneClasses = new ArrayList<String>();
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
        for (final EjbModule ejb : appModule.getEjbModules()) {
            Thread.currentThread().setContextClassLoader(ejb.getClassLoader());
            for (final EnterpriseBean bean : ejb.getEjbJar().getEnterpriseBeans()) {
                if (bean instanceof SessionBean && ((SessionBean) bean).isRestService()) {
                    standAloneClasses.add(bean.getEjbClass());
                    valid(ejb.getValidation(), ejb.getClassLoader(), bean.getEjbClass());
                }
            }
        }
        for (final WebModule web : appModule.getWebModules()) {
            Thread.currentThread().setContextClassLoader(web.getClassLoader());
            // build the list of classes to validate
            final Collection<String> classes = new ArrayList<String>();
            classes.addAll(web.getRestClasses());
            classes.addAll(web.getEjbRestServices());
            for (final String app : web.getRestApplications()) {
                final Class<?> clazz;
                try {
                    clazz = web.getClassLoader().loadClass(app);
                } catch (final ClassNotFoundException e) {
                    // managed elsewhere, here we just check methods
                    continue;
                }
                final Application appInstance;
                try {
                    appInstance = (Application) clazz.newInstance();
                } catch (final Exception e) {
                    // managed elsewhere
                    continue;
                }
                try {
                    for (final Class<?> rsClass : appInstance.getClasses()) {
                        classes.add(rsClass.getName());
                    }
                /* don't do it or ensure you have cdi activated! + CXF will catch it later
                        for (final Object rsSingleton : appInstance.getSingletons()) {
                            classes.add(rsSingleton.getClass().getName());
                        }
                        */
                } catch (final RuntimeException npe) {
                    if (appInstance == null) {
                        throw npe;
                    }
                // if app relies on cdi it is null here
                }
            }
            // try to avoid to valid twice the same classes
            final Iterator<String> it = classes.iterator();
            while (it.hasNext()) {
                final String current = it.next();
                if (standAloneClasses.contains(current)) {
                    it.remove();
                }
            }
            // valid
            for (final String classname : classes) {
                valid(web.getValidation(), web.getClassLoader(), classname);
            }
            classes.clear();
        }
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
    }
    standAloneClasses.clear();
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) ArrayList(java.util.ArrayList) EjbModule(org.apache.openejb.config.EjbModule) WebModule(org.apache.openejb.config.WebModule) SessionBean(org.apache.openejb.jee.SessionBean) Application(javax.ws.rs.core.Application)

Aggregations

SessionBean (org.apache.openejb.jee.SessionBean)22 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)12 EjbJar (org.apache.openejb.jee.EjbJar)10 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)8 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)7 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)4 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 PortInfo (org.apache.openejb.assembler.classic.PortInfo)3 EjbModule (org.apache.openejb.config.EjbModule)3 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 Beans (org.apache.openejb.jee.Beans)2 EntityBean (org.apache.openejb.jee.EntityBean)2 Interceptor (org.apache.openejb.jee.Interceptor)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1