Search in sources :

Example 16 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class TestClient method processSetterInjections.

protected final void processSetterInjections() {
    Object home = null;
    ClassFinder finder = null;
    List<Method> methodList = null;
    finder = new ClassFinder(getClassPath());
    methodList = finder.findAnnotatedMethods(EJB.class);
    for (final Iterator methods = methodList.iterator(); methods.hasNext(); ) {
        final Method method = (Method) methods.next();
        final EJB ejbAnnotation = method.getAnnotation(EJB.class);
        if ((ejbAnnotation.name() != null) && (ejbAnnotation.name() != "") && (ejbAnnotation.beanInterface() != null)) {
            try {
                home = initialContext.lookup(ejbAnnotation.name());
                // home = ejbAnnotation.beanInterface().cast(home;
                home = cast(home, ejbAnnotation.beanInterface());
                method.setAccessible(true);
                method.invoke(this, new Object[] { home });
            } catch (final Exception ex) {
                // TODO - MNour : Needs better exception handling
                ex.printStackTrace();
            }
        }
    }
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) EJB(javax.ejb.EJB)

Example 17 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class ApplicationComposers method validate.

private void validate() {
    final List<Throwable> errors = new ArrayList<>();
    if (isContainer()) {
        final Map<Object, List<Method>> annotatedConfigurationMethods = findAnnotatedMethods(new HashMap<Object, List<Method>>(), Configuration.class);
        {
            int nbProp = 0;
            int nbOpenejb = 0;
            for (final List<Method> list : annotatedConfigurationMethods.values()) {
                for (final Method m : list) {
                    final Class<?> type = m.getReturnType();
                    if (Openejb.class.isAssignableFrom(type) || String.class.equals(type)) {
                        nbOpenejb++;
                    } else if (Properties.class.isAssignableFrom(type)) {
                        nbProp++;
                    }
                // else not supported?
                }
            }
            if (nbProp > 1 || nbOpenejb > 1) {
                final String gripe = "Test class should have no more than one @Configuration method by type (Openejb/String or Properties)";
                errors.add(new Exception(gripe));
            }
        }
        int injectorSize = 0;
        for (final List<Method> m : findAnnotatedMethods(new HashMap<Object, List<Method>>(), org.apache.openejb.junit.MockInjector.class).values()) {
            injectorSize += m.size();
        }
        for (final List<Method> m : findAnnotatedMethods(new HashMap<Object, List<Method>>(), MockInjector.class).values()) {
            injectorSize += m.size();
        }
        if (injectorSize > 1) {
            errors.add(new Exception("Test class should have no more than one @MockInjector method"));
        }
        final List<Method> components = new ArrayList<>();
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), Component.class).values()) {
            components.addAll(l);
        }
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), org.apache.openejb.junit.Component.class).values()) {
            components.addAll(l);
        }
        for (final Method method : components) {
            if (method.getParameterTypes().length > 0) {
                errors.add(new Exception("@Component methods shouldn't take any parameters"));
            }
        }
        for (final ClassFinder finder : testClassFinders.values()) {
            for (final Field field : finder.findAnnotatedFields(RandomPort.class)) {
                final Class<?> type = field.getType();
                if (int.class != type && URL.class != type) {
                    throw new IllegalArgumentException("@RandomPort is only supported for int fields");
                }
            }
        }
    }
    if (isApplication()) {
        final List<Method> descriptors = new ArrayList<>();
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), Descriptors.class).values()) {
            descriptors.addAll(l);
        }
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), org.apache.openejb.junit.Descriptors.class).values()) {
            descriptors.addAll(l);
        }
        for (final Method method : descriptors) {
            final Class<?> returnType = method.getReturnType();
            if (!returnType.equals(WebModule.class) && !returnType.equals(EjbModule.class) && !returnType.equals(WebApp.class) && !returnType.equals(EjbJar.class) && !returnType.equals(AppModule.class)) {
                errors.add(new Exception("@Descriptors can't be used on " + returnType.getName()));
            }
        }
        final List<Method> classes = new ArrayList<>();
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), Classes.class).values()) {
            classes.addAll(l);
        }
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), org.apache.openejb.junit.Classes.class).values()) {
            classes.addAll(l);
        }
        for (final Method method : classes) {
            final Class<?> returnType = method.getReturnType();
            if (!returnType.equals(WebModule.class) && !returnType.equals(EjbModule.class) && !returnType.equals(WebApp.class) && !returnType.equals(EjbJar.class) && !EnterpriseBean.class.isAssignableFrom(returnType)) {
                errors.add(new Exception("@Classes can't be used on a method returning " + returnType));
            }
        }
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), Jars.class).values()) {
            for (final Method method : l) {
                final Class<?> returnType = method.getReturnType();
                if (!returnType.equals(WebModule.class) && !returnType.equals(EjbModule.class) && !returnType.equals(WebApp.class) && !returnType.equals(EjbJar.class) && !EnterpriseBean.class.isAssignableFrom(returnType)) {
                    errors.add(new Exception("@Classes can't be used on a method returning " + returnType));
                }
            }
        }
        int appModules = 0;
        int modules = 0;
        final List<Method> moduleMethods = new ArrayList<>();
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), Module.class).values()) {
            moduleMethods.addAll(l);
        }
        for (final List<Method> l : findAnnotatedMethods(new HashMap<Object, List<Method>>(), org.apache.openejb.junit.Module.class).values()) {
            moduleMethods.addAll(l);
        }
        for (final Method method : moduleMethods) {
            modules++;
            final Class<?> type = method.getReturnType();
            if (Application.class.isAssignableFrom(type)) {
                appModules++;
            } else if (!isValidModuleType(type, MODULE_TYPES)) {
                final String gripe = "@Module method must return " + Join.join(" or ", MODULE_TYPES).replaceAll("(class|interface) ", "");
                errors.add(new Exception(gripe));
            }
        }
        if (appModules > 1) {
            final String gripe = "Test class should have no more than one @Module method that returns " + Application.class.getName();
            errors.add(new Exception(gripe));
        }
        if (modules < 1 && testClass.getAnnotation(Classes.class) == null && testClass.getAnnotation(Default.class) == null) {
            final String gripe = "Test class should have at least one @Module method";
            errors.add(new Exception(gripe));
        }
    }
    if (!errors.isEmpty()) {
        throw new OpenEJBRuntimeException(errors.toString());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EjbModule(org.apache.openejb.config.EjbModule) URL(java.net.URL) Field(java.lang.reflect.Field) ClassFinder(org.apache.xbean.finder.ClassFinder) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) EjbJar(org.apache.openejb.jee.EjbJar) Method(java.lang.reflect.Method) WebModule(org.apache.openejb.config.WebModule) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) DeploymentModule(org.apache.openejb.config.DeploymentModule) WebModule(org.apache.openejb.config.WebModule) PersistenceModule(org.apache.openejb.config.PersistenceModule) NamedModule(org.apache.openejb.jee.NamedModule) EjbModule(org.apache.openejb.config.EjbModule) AppModule(org.apache.openejb.config.AppModule) ConnectorModule(org.apache.openejb.config.ConnectorModule) Application(org.apache.openejb.jee.Application)

Example 18 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class ApplicationComposers method fixFakeClassFinder.

private ClassFinder fixFakeClassFinder(final Object inputTestInstance) {
    // test injections, we faked the instance before having it so ensuring we use the right finder
    ClassFinder testClassFinder = testClassFinders.get(inputTestInstance);
    if (testClassFinder == null) {
        final ApplicationComposers self = this;
        final ClassFinder remove = testClassFinders.remove(self);
        if (remove != null) {
            testClassFinders.put(inputTestInstance, remove);
            testClassFinder = remove;
            afterRunnables.add(new // reset state for next test
            Runnable() {

                @Override
                public void run() {
                    final ClassFinder classFinder = testClassFinders.remove(inputTestInstance);
                    if (classFinder != null) {
                        testClassFinders.put(self, classFinder);
                    }
                }
            });
        }
    }
    return testClassFinder;
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder)

Example 19 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class AnnotationDeployerTest method testModule.

private EjbModule testModule() {
    final EjbJar ejbJar = new EjbJar("test-classes");
    final EjbModule ejbModule = new EjbModule(ejbJar);
    ejbModule.setFinder(new ClassFinder(AnnotationDeployerTest.class, BusinessException.class, Exception.class, GenericInterface.class, InterceptedSLSBean.class, MyMainClass.class, TestLocalBean.class, ValueRequiredException.class));
    return ejbModule;
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder) ResourceException(javax.resource.ResourceException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) ApplicationException(javax.ejb.ApplicationException) NamingException(javax.naming.NamingException) InvalidPropertyException(javax.resource.spi.InvalidPropertyException) EjbJar(org.apache.openejb.jee.EjbJar)

Example 20 with ClassFinder

use of org.apache.xbean.finder.ClassFinder in project tomee by apache.

the class AnnotationDeployerTest method testConnectorModule.

private ConnectorModule testConnectorModule() {
    final Connector connector = new Connector();
    final ConnectorModule connectorModule = new ConnectorModule(connector);
    connectorModule.setFinder(new ClassFinder(TestConnector.class, TestManagedConnectionFactory.class, TestActivation.class, TestAdminObject.class));
    return connectorModule;
}
Also used : Connector(org.apache.openejb.jee.Connector) ClassFinder(org.apache.xbean.finder.ClassFinder)

Aggregations

ClassFinder (org.apache.xbean.finder.ClassFinder)24 Method (java.lang.reflect.Method)10 ArrayList (java.util.ArrayList)10 URL (java.net.URL)8 File (java.io.File)6 HashMap (java.util.HashMap)6 Field (java.lang.reflect.Field)5 List (java.util.List)5 MojoFailureException (org.apache.maven.plugin.MojoFailureException)5 URLClassLoader (java.net.URLClassLoader)4 Map (java.util.Map)4 Arrays.asList (java.util.Arrays.asList)3 LinkedList (java.util.LinkedList)3 Properties (java.util.Properties)3 MalformedURLException (java.net.MalformedURLException)2 Iterator (java.util.Iterator)2 TreeSet (java.util.TreeSet)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 EJB (javax.ejb.EJB)2 NamingException (javax.naming.NamingException)2