Search in sources :

Example 1 with WebApp

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

the class ApplicationComposers method deployApp.

public void deployApp(final Object inputTestInstance) throws Exception {
    final ClassFinder testClassFinder = fixFakeClassFinder(inputTestInstance);
    final ClassLoader loader = testClass.getClassLoader();
    AppModule appModule = new AppModule(loader, testClass.getSimpleName());
    // Add the test case as an @ManagedBean
    final ManagedBean testBean;
    {
        final EjbJar ejbJar = new EjbJar();
        final OpenejbJar openejbJar = new OpenejbJar();
        testBean = ejbJar.addEnterpriseBean(new ManagedBean(testClass.getSimpleName(), testClass.getName(), true));
        testBean.localBean();
        testBean.setTransactionType(TransactionType.BEAN);
        final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(testBean);
        ejbDeployment.setDeploymentId(testClass.getName());
        final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
        ejbModule.getProperties().setProperty("openejb.cdi.activated", "false");
        final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new ClassesArchive(ancestors(testClass)));
        ejbModule.setFinder(finder);
        if (finder.findMetaAnnotatedFields(Inject.class).size() + finder.findMetaAnnotatedMethods(Inject.class).size() > 0) {
            // "activate" cdi to avoid WARNINGs
            ejbModule.setBeans(new Beans());
        }
        appModule.getEjbModules().add(ejbModule);
    }
    final Map<String, URL> additionalDescriptors = descriptorsToMap(testClass.getAnnotation(org.apache.openejb.junit.Descriptors.class));
    final Map<String, URL> additionalDescriptorsNew = descriptorsToMap(testClass.getAnnotation(Descriptors.class));
    additionalDescriptors.putAll(additionalDescriptorsNew);
    Application application = null;
    int webModulesNb = 0;
    final Jars globalJarsAnnotation = testClass.getAnnotation(Jars.class);
    // Invoke the @Module producer methods to build out the AppModule
    int moduleNumber = 0;
    // we dont consider resources.xml to set an app as standalone or not
    int notBusinessModuleNumber = 0;
    final Map<Object, List<Method>> moduleMethods = new HashMap<>();
    findAnnotatedMethods(moduleMethods, Module.class);
    findAnnotatedMethods(moduleMethods, org.apache.openejb.junit.Module.class);
    for (final Map.Entry<Object, List<Method>> methods : moduleMethods.entrySet()) {
        moduleNumber += methods.getValue().size();
        for (final Method method : methods.getValue()) {
            final Object obj = method.invoke(methods.getKey());
            final Jars jarsAnnotation = method.getAnnotation(Jars.class);
            final Classes classesAnnotation = method.getAnnotation(Classes.class);
            final org.apache.openejb.junit.Classes classesAnnotationOld = method.getAnnotation(org.apache.openejb.junit.Classes.class);
            final boolean defaultConfig = method.getAnnotation(Default.class) != null;
            Class<?>[] classes = null;
            String[] excludes = null;
            Class<?>[] cdiInterceptors = null;
            Class<?>[] cdiAlternatives = null;
            Class<?>[] cdiStereotypes = null;
            Class<?>[] cdiDecorators = null;
            boolean cdi = false;
            boolean innerClassesAsBean = false;
            if (classesAnnotation != null) {
                classes = classesAnnotation.value();
                excludes = classesAnnotation.excludes();
                innerClassesAsBean = classesAnnotation.innerClassesAsBean();
                cdiInterceptors = classesAnnotation.cdiInterceptors();
                cdiDecorators = classesAnnotation.cdiDecorators();
                cdiAlternatives = classesAnnotation.cdiAlternatives();
                cdiStereotypes = classesAnnotation.cdiStereotypes();
                cdi = isCdi(classesAnnotation.cdi(), cdiInterceptors, cdiAlternatives, cdiStereotypes, cdiDecorators);
            } else if (classesAnnotationOld != null) {
                classes = classesAnnotationOld.value();
            }
            if (obj instanceof WebApp) {
                // will add the ejbmodule too
                final WebApp webApp = WebApp.class.cast(obj);
                if (webApp.getContextRoot() == null && classesAnnotation != null) {
                    webApp.contextRoot(classesAnnotation.context());
                }
                webModulesNb++;
                addWebApp(appModule, testBean, additionalDescriptors, method.getAnnotation(Descriptors.class), method.getAnnotation(JaxrsProviders.class), webApp, globalJarsAnnotation, jarsAnnotation, classes, excludes, cdiInterceptors, cdiAlternatives, cdiDecorators, cdiStereotypes, cdi, innerClassesAsBean, defaultConfig);
            } else if (obj instanceof WebModule) {
                // will add the ejbmodule too
                webModulesNb++;
                final WebModule webModule = (WebModule) obj;
                webModule.getAltDDs().putAll(additionalDescriptors);
                webModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                final EjbModule ejbModule = DeploymentLoader.addWebModule(webModule, appModule);
                ejbModule.getProperties().put(CdiScanner.OPENEJB_CDI_FILTER_CLASSLOADER, "false");
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                webModule.setFinder(finderFromClasses(webModule, classes, files, excludes));
                ejbModule.setFinder(webModule.getFinder());
            } else if (obj instanceof EjbModule) {
                final EjbModule ejbModule = (EjbModule) obj;
                ejbModule.getAltDDs().putAll(additionalDescriptors);
                ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                ejbModule.initAppModule(appModule);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof EjbJar) {
                final EjbJar ejbJar = (EjbJar) obj;
                setId(ejbJar, method);
                final EjbModule ejbModule = new EjbModule(ejbJar);
                ejbModule.getAltDDs().putAll(additionalDescriptors);
                ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof EnterpriseBean) {
                final EnterpriseBean bean = (EnterpriseBean) obj;
                final EjbJar ejbJar = new EjbJar(method.getName());
                ejbJar.addEnterpriseBean(bean);
                final EjbModule ejbModule = new EjbModule(ejbJar);
                final Beans beans = new Beans();
                beans.addManagedClass(bean.getEjbClass());
                ejbModule.setBeans(beans);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof Application) {
                application = (Application) obj;
                setId(application, method);
            } else if (obj instanceof Connector) {
                final Connector connector = (Connector) obj;
                setId(connector, method);
                appModule.getConnectorModules().add(new ConnectorModule(connector));
            } else if (obj instanceof Persistence) {
                final Persistence persistence = (Persistence) obj;
                appModule.addPersistenceModule(new PersistenceModule(appModule, implicitRootUrl(method.getAnnotation(PersistenceRootUrl.class)), persistence));
                notBusinessModuleNumber++;
            } else if (obj instanceof PersistenceUnit) {
                final PersistenceUnit unit = (PersistenceUnit) obj;
                appModule.addPersistenceModule(new PersistenceModule(appModule, implicitRootUrl(method.getAnnotation(PersistenceRootUrl.class)), new Persistence(unit)));
                notBusinessModuleNumber++;
            } else if (obj instanceof Beans) {
                final Beans beans = (Beans) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setBeans(beans);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(beans, cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof Class[]) {
                final Class[] beans = (Class[]) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder(new ClassesArchive(beans)).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof Class) {
                final Class bean = (Class) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder(new ClassesArchive(bean)).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof IAnnotationFinder) {
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder((IAnnotationFinder) obj);
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof ClassesArchive) {
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder((Archive) obj).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof Resources) {
                final Resources asResources = Resources.class.cast(obj);
                appModule.getResources().addAll(asResources.getResource());
                appModule.getContainers().addAll(asResources.getContainer());
                notBusinessModuleNumber++;
            } else if (obj instanceof AppModule) {
                // we can probably go further here
                final AppModule module = (AppModule) obj;
                module.getAltDDs().putAll(additionalDescriptors);
                module.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                if (module.getWebModules().size() > 0) {
                    webModulesNb++;
                }
                appModule.getEjbModules().addAll(module.getEjbModules());
                appModule.getPersistenceModules().addAll(module.getPersistenceModules());
                appModule.getAdditionalLibMbeans().addAll(module.getAdditionalLibMbeans());
                appModule.getWebModules().addAll(module.getWebModules());
                appModule.getConnectorModules().addAll(module.getConnectorModules());
                appModule.getResources().addAll(module.getResources());
                appModule.getServices().addAll(module.getServices());
                appModule.getPojoConfigurations().putAll(module.getPojoConfigurations());
                appModule.getAdditionalLibraries().addAll(module.getAdditionalLibraries());
                appModule.getAltDDs().putAll(module.getAltDDs());
                appModule.getProperties().putAll(module.getProperties());
            } else {
                moduleNumber--;
            }
        }
    }
    final Classes classClasses = testClass.getAnnotation(Classes.class);
    if (classClasses != null) {
        final WebApp webapp = new WebApp();
        webapp.setContextRoot(classClasses.context());
        addWebApp(appModule, testBean, additionalDescriptors, null, null, webapp, globalJarsAnnotation, null, classClasses.value(), classClasses.excludes(), classClasses.cdiInterceptors(), classClasses.cdiAlternatives(), classClasses.cdiDecorators(), classClasses.cdiStereotypes(), classClasses.cdi(), classClasses.innerClassesAsBean(), testClass.getAnnotation(Default.class) != null);
        webModulesNb++;
        moduleNumber++;
    }
    // Application is final in AppModule, which is fine, so we'll create a new one and move everything
    if (application != null) {
        final AppModule newModule = new AppModule(appModule.getClassLoader(), appModule.getModuleId(), application, false);
        newModule.getClientModules().addAll(appModule.getClientModules());
        newModule.addPersistenceModules(appModule.getPersistenceModules());
        newModule.getEjbModules().addAll(appModule.getEjbModules());
        newModule.getConnectorModules().addAll(appModule.getConnectorModules());
        appModule = newModule;
    }
    // config for the app
    for (final Map.Entry<Object, List<Method>> method : findAnnotatedMethods(new HashMap<>(), ApplicationConfiguration.class).entrySet()) {
        for (final Method m : method.getValue()) {
            final Object o = m.invoke(method.getKey());
            if (Properties.class.isInstance(o)) {
                appModule.getProperties().putAll(Properties.class.cast(o));
            }
        }
    }
    // copy ejb into beans if cdi is activated and init finder
    for (final EjbModule ejb : appModule.getEjbModules()) {
        final EnterpriseBean[] enterpriseBeans = ejb.getEjbJar().getEnterpriseBeans();
        final Beans beans = ejb.getBeans();
        if (beans != null && ejb.getEjbJar() != null) {
            for (final EnterpriseBean bean : enterpriseBeans) {
                boolean found = false;
                for (final List<String> mc : beans.getManagedClasses().values()) {
                    if (mc.contains(bean.getEjbClass())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    beans.addManagedClass(bean.getEjbClass());
                }
            }
        }
    }
    if (moduleNumber - notBusinessModuleNumber == 1 && webModulesNb == 1) {
        appModule.setStandloneWebModule();
    }
    if (webModulesNb > 0 && SystemInstance.get().getComponent(WebAppBuilder.class) == null) {
        SystemInstance.get().setComponent(WebAppBuilder.class, new LightweightWebAppBuilder());
    }
    final Context jndiContext = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
    for (final EnvEntry entry : testBean.getEnvEntry()) {
        // set it in global jndi context since that's "app" entries and otherwise when we are no more in test bean context lookup fails
        final String name = entry.getName();
        final String jndi;
        if (name.startsWith("java:") || name.startsWith("comp/env")) {
            jndi = name;
        } else {
            jndi = "java:comp/env/" + name;
        }
        jndiContext.bind(jndi, entry.getEnvEntryValue());
    }
    appInfo = SystemInstance.get().getComponent(ConfigurationFactory.class).configureApplication(appModule);
    appContext = assembler.createApplication(appInfo);
    if (mockCdiContexts() && appContext.getWebBeansContext() != null) {
        ScopeHelper.startContexts(appContext.getWebBeansContext().getContextsService(), servletContext, session);
    }
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(testClass.getName());
    enrich(inputTestInstance, context);
    JavaSecurityManagers.setSystemProperty(Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    JavaSecurityManagers.setSystemProperty(OPENEJB_APPLICATION_COMPOSER_CONTEXT, appContext.getGlobalJndiContext());
    final List<Field> fields = new ArrayList<>(testClassFinder.findAnnotatedFields(AppResource.class));
    fields.addAll(testClassFinder.findAnnotatedFields(org.apache.openejb.junit.AppResource.class));
    for (final Field field : fields) {
        final Class<?> type = field.getType();
        if (AppModule.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, appModule);
        } else if (Context.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, new InitialContext(new Properties() {

                {
                    setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
                }
            }));
        } else if (ApplicationComposers.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, this);
        } else if (ContextProvider.class.isAssignableFrom(type)) {
            RESTResourceFinder finder = SystemInstance.get().getComponent(RESTResourceFinder.class);
            if (finder == null || !ContextProvider.class.isInstance(finder)) {
                finder = new ContextProvider(finder);
                SystemInstance.get().setComponent(RESTResourceFinder.class, finder);
            }
            field.setAccessible(true);
            field.set(inputTestInstance, finder);
        } else {
            throw new IllegalArgumentException("can't find value for type " + type.getName());
        }
    }
    previous = ThreadContext.enter(new ThreadContext(context, null, Operation.BUSINESS));
    // switch back since next test will use another instance
    testClassFinders.put(this, testClassFinder);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) AppModule(org.apache.openejb.config.AppModule) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) HashMap(java.util.HashMap) EjbModule(org.apache.openejb.config.EjbModule) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) RESTResourceFinder(org.apache.openejb.rest.RESTResourceFinder) Method(java.lang.reflect.Method) WebModule(org.apache.openejb.config.WebModule) ContextProvider(org.apache.openejb.testing.rest.ContextProvider) LinkedList(java.util.LinkedList) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) BeanContext(org.apache.openejb.BeanContext) Collection(java.util.Collection) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Resources(org.apache.openejb.config.sys.Resources) ManagedBean(org.apache.openejb.jee.ManagedBean) Application(org.apache.openejb.jee.Application) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) Connector(org.apache.openejb.jee.Connector) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) Properties(java.util.Properties) URL(java.net.URL) ConnectorModule(org.apache.openejb.config.ConnectorModule) Field(java.lang.reflect.Field) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) ClassFinder(org.apache.xbean.finder.ClassFinder) EjbJar(org.apache.openejb.jee.EjbJar) EnvEntry(org.apache.openejb.jee.EnvEntry) WebContext(org.apache.openejb.core.WebContext) InitialContext(javax.naming.InitialContext) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) FinderFactory(org.apache.openejb.config.FinderFactory) PersistenceModule(org.apache.openejb.config.PersistenceModule) InitialContext(javax.naming.InitialContext) Beans(org.apache.openejb.jee.Beans) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) WebApp(org.apache.openejb.jee.WebApp)

Example 2 with WebApp

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

the class DontFailOnUnusedTagTest method run.

@Test
public void run() throws Exception {
    // this test just validates we passthrough on unknown elements (tomcat will fail/warn)
    try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("webxml31.xml")) {
        final WebApp web = Sxc.unmarshalJavaee(new WebApp$JAXB(), is);
        assertNotNull(web.getAbsoluteOrdering());
    }
}
Also used : InputStream(java.io.InputStream) WebApp(org.apache.openejb.jee.WebApp) WebApp$JAXB(org.apache.openejb.jee.WebApp$JAXB) Test(org.junit.Test)

Example 3 with WebApp

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

the class SunConversion method convertModule.

public void convertModule(final WebModule webModule) {
    if (webModule == null) {
        return;
    }
    final WebApp webApp = webModule.getWebApp();
    if (webApp == null) {
        return;
    }
    final SunWebApp sunWebApp = getSunWebApp(webModule);
    if (sunWebApp == null) {
        return;
    }
    if (sunWebApp.getContextRoot() != null) {
        webModule.setContextRoot(sunWebApp.getContextRoot());
    }
    // map ejb-refs
    final Map<String, JndiReference> refMap = new TreeMap<>();
    refMap.putAll(webApp.getEjbRefMap());
    refMap.putAll(webApp.getEjbLocalRefMap());
    // map ejb-ref jndi name declaration to deploymentId
    for (final EjbRef ref : sunWebApp.getEjbRef()) {
        if (ref.getJndiName() != null) {
            final String refName = ref.getEjbRefName();
            JndiReference ejbRef = refMap.get(refName);
            if (ejbRef == null) {
                ejbRef = new org.apache.openejb.jee.EjbRef();
                ejbRef.setName(refName);
                refMap.put(refName, ejbRef);
                webApp.getEjbRef().add((org.apache.openejb.jee.EjbRef) ejbRef);
            }
            ejbRef.setMappedName(ref.getJndiName());
        }
    }
    // map resource-env-refs and message-destination-refs
    final Map<String, JndiReference> resEnvMap = new TreeMap<>();
    resEnvMap.putAll(webApp.getResourceRefMap());
    resEnvMap.putAll(webApp.getResourceEnvRefMap());
    resEnvMap.putAll(webApp.getMessageDestinationRefMap());
    for (final ResourceRef ref : sunWebApp.getResourceRef()) {
        if (ref.getJndiName() != null) {
            String refName = ref.getResRefName();
            refName = normalize(refName);
            final JndiReference resEnvRef = resEnvMap.get(refName);
            if (resEnvRef != null) {
                resEnvRef.setMappedName(ref.getJndiName());
            }
        }
    }
    for (final ResourceEnvRef ref : sunWebApp.getResourceEnvRef()) {
        if (ref.getJndiName() != null) {
            String refName = ref.getResourceEnvRefName();
            refName = normalize(refName);
            final JndiReference resEnvRef = resEnvMap.get(refName);
            if (resEnvRef != null) {
                resEnvRef.setMappedName(ref.getJndiName());
            }
        }
    }
    for (final MessageDestinationRef ref : sunWebApp.getMessageDestinationRef()) {
        if (ref.getJndiName() != null) {
            String refName = ref.getMessageDestinationRefName();
            refName = normalize(refName);
            final JndiReference resEnvRef = resEnvMap.get(refName);
            if (resEnvRef != null) {
                resEnvRef.setMappedName(ref.getJndiName());
            }
        }
    }
    final Map<String, ServiceRef> serviceRefMap = webApp.getServiceRefMap();
    for (final org.apache.openejb.jee.sun.ServiceRef ref : sunWebApp.getServiceRef()) {
        String refName = ref.getServiceRefName();
        refName = normalize(refName);
        final ServiceRef serviceRef = serviceRefMap.get(refName);
        if (serviceRef != null) {
            final Map<String, PortComponentRef> ports = new TreeMap<>();
            for (final PortComponentRef portComponentRef : serviceRef.getPortComponentRef()) {
                ports.put(portComponentRef.getServiceEndpointInterface(), portComponentRef);
            }
            for (final PortInfo portInfo : ref.getPortInfo()) {
                final PortComponentRef portComponentRef = ports.get(portInfo.getServiceEndpointInterface());
                if (portComponentRef != null) {
                    final WsdlPort wsdlPort = portInfo.getWsdlPort();
                    if (wsdlPort != null) {
                        final QName qname = new QName(wsdlPort.getNamespaceURI(), wsdlPort.getLocalpart());
                        portComponentRef.setQName(qname);
                    }
                    for (final StubProperty stubProperty : portInfo.getStubProperty()) {
                        final String name = stubProperty.getName();
                        final String value = stubProperty.getValue();
                        portComponentRef.getProperties().setProperty(name, value);
                    }
                }
            }
            final String wsdlOverride = ref.getWsdlOverride();
            if (wsdlOverride != null && wsdlOverride.length() > 0) {
                final String serviceId = extractServiceId(wsdlOverride);
                serviceRef.setMappedName(serviceId);
            }
        }
    }
    // map wsdl locations
    if (webModule.getWebservices() != null) {
        final Map<String, WebserviceDescription> descriptions = webModule.getWebservices().getWebserviceDescriptionMap();
        for (final org.apache.openejb.jee.sun.WebserviceDescription sunDescription : sunWebApp.getWebserviceDescription()) {
            final WebserviceDescription description = descriptions.get(sunDescription.getWebserviceDescriptionName());
            if (description == null) {
                continue;
            }
            final String serviceId = extractSerivceId(sunDescription.getWsdlPublishLocation(), description.getWsdlFile());
            if (serviceId != null) {
                description.setId(serviceId);
            }
        }
    }
}
Also used : SunWebApp(org.apache.openejb.jee.sun.SunWebApp) WsdlPort(org.apache.openejb.jee.sun.WsdlPort) PortInfo(org.apache.openejb.jee.sun.PortInfo) MessageDestinationRef(org.apache.openejb.jee.sun.MessageDestinationRef) JndiReference(org.apache.openejb.jee.JndiReference) EjbRef(org.apache.openejb.jee.sun.EjbRef) ResourceEnvRef(org.apache.openejb.jee.sun.ResourceEnvRef) QName(javax.xml.namespace.QName) StubProperty(org.apache.openejb.jee.sun.StubProperty) TreeMap(java.util.TreeMap) PortComponentRef(org.apache.openejb.jee.PortComponentRef) WebserviceDescription(org.apache.openejb.jee.WebserviceDescription) ResourceRef(org.apache.openejb.jee.sun.ResourceRef) ServiceRef(org.apache.openejb.jee.ServiceRef) SunWebApp(org.apache.openejb.jee.sun.SunWebApp) WebApp(org.apache.openejb.jee.WebApp)

Example 4 with WebApp

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

the class WsDeployer method processPorts.

private void processPorts(final WebModule webModule) throws OpenEJBException {
    // map existing webservice port declarations by servlet link
    Webservices webservices = webModule.getWebservices();
    final Map<String, PortComponent> portMap = new TreeMap<>();
    if (webservices != null) {
        for (final WebserviceDescription webserviceDescription : webservices.getWebserviceDescription()) {
            for (final PortComponent portComponent : webserviceDescription.getPortComponent()) {
                final ServiceImplBean serviceImplBean = portComponent.getServiceImplBean();
                if (serviceImplBean != null && serviceImplBean.getServletLink() != null) {
                    portMap.put(serviceImplBean.getServletLink(), portComponent);
                }
            }
        }
    }
    // map existing servlet-mapping declarations
    final WebApp webApp = webModule.getWebApp();
    final Map<String, ServletMapping> servletMappings = new TreeMap<>();
    for (final ServletMapping servletMapping : webApp.getServletMapping()) {
        servletMappings.put(servletMapping.getServletName(), servletMapping);
    }
    // add port declarations for Pojo webservices
    WebserviceDescription webserviceDescription;
    for (final Servlet servlet : webApp.getServlet()) {
        // the implementation class will be replaced by the WsServlet in the WsRegistry
        final String className = servlet.getServletClass();
        // Skip JSPs
        if (className == null) {
            continue;
        }
        try {
            final Class<?> clazz = webModule.getClassLoader().loadClass(className);
            if (JaxWsUtils.isWebService(clazz)) {
                // add servlet mapping if not already declared
                ServletMapping servletMapping = servletMappings.get(servlet.getServletName());
                final String serviceName = JaxWsUtils.getServiceName(clazz);
                if (servletMapping == null) {
                    servletMapping = new ServletMapping();
                    servletMapping.setServletName(servlet.getServletName());
                    final String location = "/" + serviceName;
                    servletMapping.getUrlPattern().add(location);
                    webApp.getServletMapping().add(servletMapping);
                }
                // if we don't have a webservices document yet, we're gonna need one now
                if (webservices == null) {
                    webservices = new Webservices();
                    webModule.setWebservices(webservices);
                }
                // add web service description element (maps to service)
                webserviceDescription = webservices.getWebserviceDescriptionMap().get(serviceName);
                if (webserviceDescription == null) {
                    webserviceDescription = new WebserviceDescription();
                    webserviceDescription.setWebserviceDescriptionName(serviceName);
                    webservices.getWebserviceDescription().add(webserviceDescription);
                }
                // define port if not already declared
                PortComponent portComponent = portMap.get(servlet.getServletName());
                if (portComponent == null) {
                    portComponent = new PortComponent();
                    portComponent.setPortComponentName(clazz.getSimpleName());
                    final ServiceImplBean serviceImplBean = new ServiceImplBean();
                    serviceImplBean.setServletLink(servlet.getServletName());
                    portComponent.setServiceImplBean(serviceImplBean);
                    webserviceDescription.getPortComponent().add(portComponent);
                }
                // default portId == host.moduleId.servletName
                if (portComponent.getId() == null) {
                    portComponent.setId(webModule.getHost() + "." + webModule.getModuleId() + "." + servlet.getServletName());
                }
                if (webserviceDescription.getId() == null) {
                    webserviceDescription.setId(webModule.getHost() + "." + webModule.getModuleId() + "." + servlet.getServletName());
                }
                // set port values from annotations if not already set
                if (portComponent.getServiceEndpointInterface() == null) {
                    portComponent.setServiceEndpointInterface(JaxWsUtils.getServiceInterface(clazz));
                }
                if (portComponent.getWsdlPort() == null) {
                    portComponent.setWsdlPort(JaxWsUtils.getPortQName(clazz));
                }
                if (webserviceDescription.getWsdlFile() == null) {
                    webserviceDescription.setWsdlFile(JaxWsUtils.getServiceWsdlLocation(clazz, webModule.getClassLoader()));
                }
                if (portComponent.getWsdlService() == null) {
                    final Definition definition = getWsdl(webModule, webserviceDescription.getWsdlFile());
                    if (definition != null && definition.getServices().size() == 1) {
                        final QName serviceQName = (QName) definition.getServices().keySet().iterator().next();
                        portComponent.setWsdlService(serviceQName);
                    } else {
                        portComponent.setWsdlService(JaxWsUtils.getServiceQName(clazz));
                    }
                }
                if (portComponent.getProtocolBinding() == null) {
                    portComponent.setProtocolBinding(JaxWsUtils.getBindingUriFromAnn(clazz));
                }
                configMtomAnnotation(clazz, portComponent);
                if (SOAPBinding.SOAP12HTTP_MTOM_BINDING.equals(portComponent.getProtocolBinding()) || SOAPBinding.SOAP11HTTP_MTOM_BINDING.equals(portComponent.getProtocolBinding())) {
                    portComponent.setEnableMtom(true);
                }
                // handlers
                if (portComponent.getHandlerChains() == null) {
                    final HandlerChains handlerChains = getHandlerChains(clazz, portComponent.getServiceEndpointInterface(), webModule.getClassLoader());
                    portComponent.setHandlerChains(handlerChains);
                }
            }
        } catch (final Exception e) {
            throw new OpenEJBException("Unable to load servlet class: " + className, e);
        }
    }
}
Also used : ServletMapping(org.apache.openejb.jee.ServletMapping) OpenEJBException(org.apache.openejb.OpenEJBException) PortComponent(org.apache.openejb.jee.PortComponent) ServiceImplBean(org.apache.openejb.jee.ServiceImplBean) QName(javax.xml.namespace.QName) Definition(javax.wsdl.Definition) HandlerChains(org.apache.openejb.jee.HandlerChains) Webservices(org.apache.openejb.jee.Webservices) TreeMap(java.util.TreeMap) MalformedURLException(java.net.MalformedURLException) OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) WebserviceDescription(org.apache.openejb.jee.WebserviceDescription) Servlet(org.apache.openejb.jee.Servlet) WebApp(org.apache.openejb.jee.WebApp)

Example 5 with WebApp

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

the class AnnotationDeployerTest method findRestClasses.

@Test
public void findRestClasses() throws Exception {
    final WebApp webApp = new WebApp();
    webApp.setContextRoot("/");
    webApp.setId("web");
    webApp.setVersion("2.5");
    WebModule webModule = new WebModule(webApp, webApp.getContextRoot(), Thread.currentThread().getContextClassLoader(), "myapp", webApp.getId());
    webModule.setFinder(new AnnotationFinder(new ClassesArchive(RESTClass.class, RESTMethod.class, RESTApp.class)).link());
    final AnnotationDeployer annotationDeployer = new AnnotationDeployer();
    webModule = annotationDeployer.deploy(webModule);
    final Set<String> classes = webModule.getRestClasses();
    final Set<String> applications = webModule.getRestApplications();
    assertEquals(1, classes.size());
    assertTrue(classes.contains(RESTClass.class.getName()));
    // assertTrue(classes.contains(RESTMethod.class.getName()));
    assertEquals(1, applications.size());
    assertEquals(RESTApp.class.getName(), applications.iterator().next());
}
Also used : ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) WebApp(org.apache.openejb.jee.WebApp) Test(org.junit.Test)

Aggregations

WebApp (org.apache.openejb.jee.WebApp)26 AppInfo (org.apache.openejb.assembler.classic.AppInfo)8 Persistence (org.apache.openejb.jee.jpa.unit.Persistence)7 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)7 File (java.io.File)6 IOException (java.io.IOException)6 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)6 URL (java.net.URL)5 WebModule (org.apache.openejb.config.WebModule)5 MalformedURLException (java.net.MalformedURLException)4 LinkedHashSet (java.util.LinkedHashSet)4 JarFile (java.util.jar.JarFile)4 EnvEntry (org.apache.openejb.jee.EnvEntry)4 HashSet (java.util.HashSet)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)3 EjbJar (org.apache.openejb.jee.EjbJar)3 Classes (org.apache.openejb.testing.Classes)3 Module (org.apache.openejb.testing.Module)3 InputStream (java.io.InputStream)2