Search in sources :

Example 1 with AnnotationFinder

use of org.apache.openejb.util.AnnotationFinder in project tomee by apache.

the class DeploymentLoader method checkAnnotations.

private Class<? extends DeploymentModule> checkAnnotations(final URL urls, final ClassLoader classLoader, final boolean scanPotentialEjbModules, final boolean scanPotentialClientModules) {
    Class<? extends DeploymentModule> cls = null;
    if (scanPotentialEjbModules || scanPotentialClientModules) {
        final AnnotationFinder classFinder = new AnnotationFinder(classLoader, urls);
        final Set<Class<? extends DeploymentModule>> otherTypes = new LinkedHashSet<>();
        final AnnotationFinder.Filter filter = new AnnotationFinder.Filter() {

            final String packageName = LocalClient.class.getName().replace("LocalClient", "");

            @Override
            public boolean accept(final String annotationName) {
                if (scanPotentialClientModules && annotationName.startsWith(packageName)) {
                    if (LocalClient.class.getName().equals(annotationName)) {
                        otherTypes.add(ClientModule.class);
                    }
                    if (RemoteClient.class.getName().equals(annotationName)) {
                        otherTypes.add(ClientModule.class);
                    }
                } else if (scanPotentialEjbModules) {
                    if (annotationName.startsWith("javax.ejb.")) {
                        if ("javax.ejb.Stateful".equals(annotationName)) {
                            return true;
                        }
                        if ("javax.ejb.Stateless".equals(annotationName)) {
                            return true;
                        }
                        if ("javax.ejb.Singleton".equals(annotationName)) {
                            return true;
                        }
                        if ("javax.ejb.MessageDriven".equals(annotationName)) {
                            return true;
                        }
                    } else if (scanManagedBeans && "javax.annotation.ManagedBean".equals(annotationName)) {
                        return true;
                    }
                }
                return false;
            }
        };
        if (classFinder.find(filter)) {
            cls = EjbModule.class;
            // if it is a war just throw an error
            try {
                if (LOGGER.isWarningEnabled()) {
                    final File ar = URLs.toFile(urls);
                    if (!ar.isDirectory() && !ar.getName().endsWith("ar")) {
                        // guess no archive extension, check it is not a hidden war
                        try (JarFile war = new JarFile(ar)) {
                            final ZipEntry entry = war.getEntry("WEB-INF/");
                            if (entry != null) {
                                LOGGER.warning("you deployed " + urls.toExternalForm() + ", it seems it is a war with no extension, please rename it");
                            }
                        }
                    }
                }
            } catch (final Exception ignored) {
            // no-op
            }
        }
        if (otherTypes.size() > 0) {
            // We may want some ordering/sorting if we add more type scanning
            cls = otherTypes.iterator().next();
        }
    }
    return cls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ZipEntry(java.util.zip.ZipEntry) LocalClient(org.apache.openejb.api.LocalClient) JarFile(java.util.jar.JarFile) OpenEJBException(org.apache.openejb.OpenEJBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Filter(org.apache.xbean.finder.filter.Filter) RemoteClient(org.apache.openejb.api.RemoteClient) JarFile(java.util.jar.JarFile) File(java.io.File) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) AnnotationFinder(org.apache.openejb.util.AnnotationFinder)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 LocalClient (org.apache.openejb.api.LocalClient)1 RemoteClient (org.apache.openejb.api.RemoteClient)1 AnnotationFinder (org.apache.openejb.util.AnnotationFinder)1 IAnnotationFinder (org.apache.xbean.finder.IAnnotationFinder)1 Filter (org.apache.xbean.finder.filter.Filter)1