Search in sources :

Example 1 with ServiceConfiguration

use of org.apache.openejb.assembler.classic.util.ServiceConfiguration in project tomee by apache.

the class RESTService method deployEJB.

private void deployEJB(final String appId, final String web, final String context, final BeanContext beanContext, final Collection<Object> additionalProviders, final Collection<ServiceInfo> serviceInfos) {
    final String nopath = getAddress(context, beanContext.getBeanClass());
    final RsHttpListener listener = createHttpListener();
    final RsRegistry.AddressInfo address = rsRegistry.createRsHttpListener(appId, web, listener, beanContext.getClassLoader(), nopath.substring(NOPATH_PREFIX.length() - 1), virtualHost, auth, realm);
    services.add(new DeployedService(address.complete, context, beanContext.getBeanClass().getName(), appId));
    listener.deployEJB(context, getFullContext(address.base, context), beanContext, additionalProviders, new ServiceConfiguration(beanContext.getProperties(), serviceInfos));
    LOGGER.info("REST Service: " + address.complete + "  -> EJB " + beanContext.getEjbName());
}
Also used : ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration)

Example 2 with ServiceConfiguration

use of org.apache.openejb.assembler.classic.util.ServiceConfiguration in project tomee by apache.

the class WsService method deployApp.

private void deployApp(final AppInfo appInfo, final Collection<BeanContext> ejbs) {
    final Collection<BeanContext> alreadyDeployed = deployedApplications.get(appInfo);
    final Map<String, WebAppInfo> webContextByEjb = new HashMap<>();
    for (final WebAppInfo webApp : appInfo.webApps) {
        for (final String ejb : webApp.ejbWebServices) {
            webContextByEjb.put(ejb, webApp);
        }
    }
    final Map<String, String> contextData = new HashMap<>();
    contextData.put("appId", appInfo.path);
    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
        final Map<String, PortInfo> ports = new TreeMap<>();
        for (final PortInfo port : ejbJar.portInfos) {
            ports.put(port.serviceLink, port);
        }
        URL moduleBaseUrl = null;
        if (ejbJar.path != null) {
            try {
                moduleBaseUrl = new File(ejbJar.path).toURI().toURL();
            } catch (final MalformedURLException e) {
                logger.error("Invalid ejb jar location " + ejbJar.path, e);
            }
        }
        StringTemplate deploymentIdTemplate = this.wsAddressTemplate;
        if (ejbJar.properties.containsKey(WS_ADDRESS_FORMAT)) {
            final String format = ejbJar.properties.getProperty(WS_ADDRESS_FORMAT);
            logger.info("Using " + WS_ADDRESS_FORMAT + " '" + format + "'");
            deploymentIdTemplate = new StringTemplate(format);
        }
        contextData.put("ejbJarId", ejbJar.moduleName);
        final String host = host(ejbJar, appInfo);
        for (final EnterpriseBeanInfo bean : ejbJar.enterpriseBeans) {
            if (bean instanceof StatelessBeanInfo || bean instanceof SingletonBeanInfo) {
                final BeanContext beanContext = containerSystem.getBeanContext(bean.ejbDeploymentId);
                if (beanContext == null || (ejbs != null && !ejbs.contains(beanContext))) {
                    continue;
                }
                final PortInfo portInfo = ports.get(bean.ejbName);
                if (portInfo == null || alreadyDeployed.contains(beanContext))
                    continue;
                final ClassLoader old = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(beanContext.getClassLoader());
                try {
                    final PortData port = WsBuilder.toPortData(portInfo, beanContext.getInjections(), moduleBaseUrl, beanContext.getClassLoader());
                    final HttpListener container = createEjbWsContainer(moduleBaseUrl, port, beanContext, new ServiceConfiguration(beanContext.getProperties(), appInfo.services));
                    // generate a location if one was not assigned
                    String location = port.getLocation();
                    if (location == null) {
                        location = autoAssignWsLocation(bean, port, contextData, deploymentIdTemplate);
                    }
                    if (!location.startsWith("/"))
                        location = "/" + location;
                    ejbLocations.put(bean.ejbDeploymentId, location);
                    final ClassLoader classLoader = beanContext.getClassLoader();
                    if (wsRegistry != null) {
                        String auth = authMethod;
                        String realm = realmName;
                        String transport = transportGuarantee;
                        if ("BASIC".equals(portInfo.authMethod) || "DIGEST".equals(portInfo.authMethod) || "CLIENT-CERT".equals(portInfo.authMethod)) {
                            auth = portInfo.authMethod;
                            realm = portInfo.realmName;
                            transport = portInfo.transportGuarantee;
                        }
                        final WebAppInfo webAppInfo = webContextByEjb.get(bean.ejbClass);
                        String context = webAppInfo != null ? webAppInfo.contextRoot : null;
                        String moduleId = webAppInfo != null ? webAppInfo.moduleId : null;
                        if (context == null && !OLD_WEBSERVICE_DEPLOYMENT) {
                            context = ejbJar.moduleName;
                        }
                        final List<String> addresses = wsRegistry.addWsContainer(container, classLoader, context, host, location, realm, transport, auth, moduleId);
                        alreadyDeployed.add(beanContext);
                        // one of the registered addresses to be the canonical address
                        final String address = HttpUtil.selectSingleAddress(addresses);
                        if (address != null) {
                            // register wsdl location
                            portAddressRegistry.addPort(portInfo.serviceId, portInfo.wsdlService, portInfo.portId, portInfo.wsdlPort, portInfo.seiInterfaceName, address);
                            setWsdl(container, address);
                            logger.info("Webservice(wsdl=" + address + ", qname=" + port.getWsdlService() + ") --> Ejb(id=" + portInfo.portId + ")");
                            ejbAddresses.put(bean.ejbDeploymentId, address);
                            addressesForApp(appInfo.appId).add(new EndpointInfo(address, port.getWsdlService(), beanContext.getBeanClass().getName()));
                        }
                    }
                } catch (final Throwable e) {
                    logger.error("Error deploying JAX-WS Web Service for EJB " + beanContext.getDeploymentID(), e);
                } finally {
                    Thread.currentThread().setContextClassLoader(old);
                }
            }
        }
    }
    if (ejbs == null || appInfo.webAppAlone) {
        for (final WebAppInfo webApp : appInfo.webApps) {
            afterApplicationCreated(appInfo, webApp);
        }
    }
// else called because of ear case where new ejbs are deployed in webapps
}
Also used : MalformedURLException(java.net.MalformedURLException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SingletonBeanInfo(org.apache.openejb.assembler.classic.SingletonBeanInfo) URL(java.net.URL) PortInfo(org.apache.openejb.assembler.classic.PortInfo) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) PortData(org.apache.openejb.core.webservices.PortData) StringTemplate(org.apache.openejb.util.StringTemplate) HttpListener(org.apache.openejb.server.httpd.HttpListener) StatelessBeanInfo(org.apache.openejb.assembler.classic.StatelessBeanInfo) TreeMap(java.util.TreeMap) BeanContext(org.apache.openejb.BeanContext) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) File(java.io.File)

Example 3 with ServiceConfiguration

use of org.apache.openejb.assembler.classic.util.ServiceConfiguration in project tomee by apache.

the class WsService method afterApplicationCreated.

public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
    final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost);
    if (webContext == null)
        return;
    // if already deployed skip this webapp
    if (!deployedWebApps.add(webApp))
        return;
    final Map<String, PortInfo> ports = new TreeMap<String, PortInfo>();
    for (final PortInfo port : webApp.portInfos) {
        ports.put(port.serviceLink, port);
    }
    URL moduleBaseUrl = null;
    try {
        moduleBaseUrl = new File(webApp.path).toURI().toURL();
    } catch (final MalformedURLException e) {
        logger.error("Invalid ejb jar location " + webApp.path, e);
    }
    // lazy init
    Collection<IdPropertiesInfo> pojoConfiguration = null;
    for (final ServletInfo servlet : webApp.servlets) {
        if (servlet.servletName == null) {
            continue;
        }
        final PortInfo portInfo = ports.get(servlet.servletName);
        if (portInfo == null) {
            continue;
        }
        final ClassLoader old = Thread.currentThread().getContextClassLoader();
        final ClassLoader classLoader = webContext.getClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        try {
            final Collection<Injection> injections = webContext.getInjections();
            final Context context = webContext.getJndiEnc();
            final Class target = classLoader.loadClass(servlet.servletClass);
            final Map<String, Object> bindings = webContext.getBindings();
            final PortData port = WsBuilder.toPortData(portInfo, injections, moduleBaseUrl, classLoader);
            pojoConfiguration = PojoUtil.findPojoConfig(pojoConfiguration, appInfo, webApp);
            final HttpListener container = createPojoWsContainer(classLoader, moduleBaseUrl, port, portInfo.serviceLink, target, context, webApp.contextRoot, bindings, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfiguration, target.getName()), appInfo.services));
            if (wsRegistry != null) {
                String auth = authMethod;
                String realm = realmName;
                String transport = transportGuarantee;
                if ("BASIC".equals(portInfo.authMethod) || "DIGEST".equals(portInfo.authMethod) || "CLIENT-CERT".equals(portInfo.authMethod)) {
                    auth = portInfo.authMethod;
                    realm = portInfo.realmName;
                    transport = portInfo.transportGuarantee;
                }
                // give servlet a reference to the webservice container
                final List<String> addresses = wsRegistry.setWsContainer(container, classLoader, webApp.contextRoot, host(webApp), servlet, realm, transport, auth, webApp.moduleId);
                // one of the registered addresses to be the connonical address
                final String address = HttpUtil.selectSingleAddress(addresses);
                // add address to global registry
                portAddressRegistry.addPort(portInfo.serviceId, portInfo.wsdlService, portInfo.portId, portInfo.wsdlPort, portInfo.seiInterfaceName, address);
                setWsdl(container, address);
                logger.info("Webservice(wsdl=" + address + ", qname=" + port.getWsdlService() + ") --> Pojo(id=" + portInfo.portId + ")");
                servletAddresses.put(webApp.moduleId + "." + servlet.servletName, address);
                addressesForApp(webApp.moduleId).add(new EndpointInfo(address, port.getWsdlService(), target.getName()));
            }
        } catch (final Throwable e) {
            logger.error("Error deploying CXF webservice for servlet " + portInfo.serviceLink, e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) MalformedURLException(java.net.MalformedURLException) WebContext(org.apache.openejb.core.WebContext) Injection(org.apache.openejb.Injection) TreeMap(java.util.TreeMap) URL(java.net.URL) PortInfo(org.apache.openejb.assembler.classic.PortInfo) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) PortData(org.apache.openejb.core.webservices.PortData) IdPropertiesInfo(org.apache.openejb.assembler.classic.IdPropertiesInfo) HttpListener(org.apache.openejb.server.httpd.HttpListener) File(java.io.File)

Example 4 with ServiceConfiguration

use of org.apache.openejb.assembler.classic.util.ServiceConfiguration in project tomee by apache.

the class RESTService method fullServletDeployment.

private void fullServletDeployment(final AppInfo appInfo, final WebAppInfo webApp, final WebContext webContext, final Map<String, EJBRestServiceInfo> restEjbs, final ClassLoader classLoader, final Collection<Injection> injections, final WebBeansContext owbCtx, final Context context, final Collection<Object> additionalProviders, final Collection<IdPropertiesInfo> initPojoConfigurations) {
    // The spec says:
    //
    // "The resources and providers that make up a JAX-RS application are configured via an application-supplied
    // subclass of Application. An implementation MAY provide alternate mechanisms for locating resource
    // classes and providers (e.g. runtime class scanning) but use of Application is the only portable means of
    //  configuration."
    //
    //  The choice here is to deploy using the Application if it exists or to use the scanned classes
    //  if there is no Application.
    //
    //  Like this providing an Application subclass user can totally control deployed services.
    Collection<IdPropertiesInfo> pojoConfigurations = null;
    boolean useApp = false;
    String appPrefix = webApp.contextRoot;
    for (final String app : webApp.restApplications) {
        // if multiple application classes reinit it
        appPrefix = webApp.contextRoot;
        if (!appPrefix.endsWith("/")) {
            appPrefix += "/";
        }
        final Application appInstance;
        final Class<?> appClazz;
        try {
            appClazz = classLoader.loadClass(app);
            appInstance = Application.class.cast(appClazz.newInstance());
            if (owbCtx.getBeanManagerImpl().isInUse()) {
                try {
                    webContext.inject(appInstance);
                } catch (final Exception e) {
                // not important since not required by the spec
                }
            }
        } catch (final Exception e) {
            throw new OpenEJBRestRuntimeException("can't create class " + app, e);
        }
        final String path = appPrefix(webApp, appClazz);
        if (path != null) {
            appPrefix += path;
        }
        final Set<Class<?>> classes = appInstance.getClasses();
        final Set<Object> singletons = appInstance.getSingletons();
        // look for providers
        for (final Class<?> clazz : classes) {
            if (isProvider(clazz)) {
                additionalProviders.add(clazz);
            }
        }
        for (final Object obj : singletons) {
            if (obj != null && isProvider(obj.getClass())) {
                additionalProviders.add(obj);
            }
        }
        for (final Object o : singletons) {
            if (o == null || additionalProviders.contains(o)) {
                continue;
            }
            if (hasEjbAndIsNotAManagedBean(restEjbs, o.getClass().getName())) {
                // no more a singleton if the ejb is not a singleton...but it is a weird case
                deployEJB(appInfo.appId, webApp.contextRoot, appPrefix, restEjbs.get(o.getClass().getName()).context, additionalProviders, appInfo.services);
            } else {
                pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                deploySingleton(appInfo.appId, webApp.contextRoot, appPrefix, o, appInstance, classLoader, additionalProviders, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfigurations, o.getClass().getName()), appInfo.services));
            }
        }
        for (final Class<?> clazz : classes) {
            if (additionalProviders.contains(clazz)) {
                continue;
            }
            if (hasEjbAndIsNotAManagedBean(restEjbs, clazz.getName())) {
                deployEJB(appInfo.appId, webApp.contextRoot, appPrefix, restEjbs.get(clazz.getName()).context, additionalProviders, appInfo.services);
            } else {
                pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                deployPojo(appInfo.appId, webApp.contextRoot, appPrefix, clazz, appInstance, classLoader, injections, context, owbCtx, additionalProviders, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfigurations, clazz.getName()), appInfo.services));
            }
        }
        useApp = useApp || classes.size() + singletons.size() > 0;
        LOGGER.info("REST application deployed: " + app);
    }
    if (!useApp) {
        if (webApp.restApplications.isEmpty() || webApp.restApplications.size() > 1) {
            appPrefix = webApp.contextRoot;
        }
        // else keep application prefix
        final Set<String> restClasses = new HashSet<>(webApp.restClass);
        restClasses.addAll(webApp.ejbRestServices);
        for (final String clazz : restClasses) {
            if (restEjbs.containsKey(clazz)) {
                final BeanContext ctx = restEjbs.get(clazz).context;
                if (hasEjbAndIsNotAManagedBean(restEjbs, clazz)) {
                    deployEJB(appInfo.appId, webApp.contextRoot, appPrefix, restEjbs.get(clazz).context, additionalProviders, appInfo.services);
                } else {
                    deployPojo(appInfo.appId, webApp.contextRoot, appPrefix, ctx.getBeanClass(), null, ctx.getClassLoader(), ctx.getInjections(), context, owbCtx, additionalProviders, new ServiceConfiguration(ctx.getProperties(), appInfo.services));
                }
            } else {
                try {
                    final Class<?> loadedClazz = classLoader.loadClass(clazz);
                    pojoConfigurations = PojoUtil.findPojoConfig(pojoConfigurations, appInfo, webApp);
                    deployPojo(appInfo.appId, webApp.contextRoot, appPrefix, loadedClazz, null, classLoader, injections, context, owbCtx, additionalProviders, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfigurations, loadedClazz.getName()), appInfo.services));
                } catch (final ClassNotFoundException e) {
                    throw new OpenEJBRestRuntimeException("can't find class " + clazz, e);
                }
            }
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) ServiceException(org.apache.openejb.server.ServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BeanContext(org.apache.openejb.BeanContext) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) IdPropertiesInfo(org.apache.openejb.assembler.classic.IdPropertiesInfo) MetaAnnotatedClass(org.apache.xbean.finder.MetaAnnotatedClass) Application(javax.ws.rs.core.Application) HashSet(java.util.HashSet)

Example 5 with ServiceConfiguration

use of org.apache.openejb.assembler.classic.util.ServiceConfiguration in project tomee by apache.

the class CxfUtil method configureBus.

public static void configureBus() {
    if (USER_COUNT.incrementAndGet() > 1) {
        return;
    }
    final SystemInstance systemInstance = SystemInstance.get();
    final Bus bus = getBus();
    // ensure cxf classes are loaded from container to avoid conflicts with app
    if ("true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.CxfContainerClassLoader", "true"))) {
        bus.setExtension(new CxfContainerClassLoader(), ClassLoader.class);
    }
    // activate jmx, by default isEnabled() == false in InstrumentationManagerImpl
    final boolean hasMonitoring = hasMonitoring(systemInstance);
    if (hasMonitoring || "true".equalsIgnoreCase(systemInstance.getProperty("openejb.cxf.jmx", "true"))) {
        final InstrumentationManager mgr = bus.getExtension(InstrumentationManager.class);
        if (InstrumentationManagerImpl.class.isInstance(mgr)) {
            // just to keep everything consistent
            bus.setExtension(LocalMBeanServer.get(), MBeanServer.class);
            final InstrumentationManagerImpl manager = InstrumentationManagerImpl.class.cast(mgr);
            manager.setEnabled(true);
            manager.setServer(LocalMBeanServer.get());
            manager.setDaemon(true);
            try {
                // avoid to bother our nice logs
                LogUtils.getL7dLogger(InstrumentationManagerImpl.class).setLevel(Level.WARNING);
            } catch (final Throwable th) {
            // no-op
            }
            // failed when bus was constructed or even if passed we switch the MBeanServer
            manager.init();
        }
    }
    if (hasMonitoring) {
        new CounterRepository().setBus(bus);
    }
    final ServiceConfiguration configuration = new ServiceConfiguration(systemInstance.getProperties(), systemInstance.getComponent(OpenEjbConfiguration.class).facilities.services);
    final Collection<ServiceInfo> serviceInfos = configuration.getAvailableServices();
    Properties properties = configuration.getProperties();
    if (properties == null) {
        properties = new Properties();
    }
    final String featuresIds = properties.getProperty(BUS_PREFIX + FEATURES);
    if (featuresIds != null) {
        final List<Feature> features = createFeatures(serviceInfos, featuresIds);
        if (features != null) {
            features.addAll(bus.getFeatures());
            bus.setFeatures(features);
        }
    }
    final Properties busProperties = ServiceInfos.serviceProperties(serviceInfos, properties.getProperty(BUS_PREFIX + ENDPOINT_PROPERTIES));
    if (busProperties != null) {
        bus.getProperties().putAll(PropertiesHelper.map(busProperties));
    }
    configureInterceptors(bus, BUS_PREFIX, serviceInfos, configuration.getProperties());
    systemInstance.getProperties().setProperty(BUS_CONFIGURED_FLAG, "true");
    systemInstance.fireEvent(new BusCreated(bus));
}
Also used : Bus(org.apache.cxf.Bus) CounterRepository(org.apache.cxf.management.counters.CounterRepository) InstrumentationManager(org.apache.cxf.management.InstrumentationManager) Properties(java.util.Properties) BusCreated(org.apache.openejb.server.cxf.transport.event.BusCreated) Feature(org.apache.cxf.feature.Feature) AbstractFeature(org.apache.cxf.feature.AbstractFeature) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) InstrumentationManagerImpl(org.apache.cxf.management.jmx.InstrumentationManagerImpl) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) SystemInstance(org.apache.openejb.loader.SystemInstance)

Aggregations

ServiceConfiguration (org.apache.openejb.assembler.classic.util.ServiceConfiguration)7 BeanContext (org.apache.openejb.BeanContext)4 MalformedURLException (java.net.MalformedURLException)3 Application (javax.ws.rs.core.Application)3 IdPropertiesInfo (org.apache.openejb.assembler.classic.IdPropertiesInfo)3 File (java.io.File)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 TreeMap (java.util.TreeMap)2 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)2 EnterpriseBeanInfo (org.apache.openejb.assembler.classic.EnterpriseBeanInfo)2 PortInfo (org.apache.openejb.assembler.classic.PortInfo)2 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)2 PortData (org.apache.openejb.core.webservices.PortData)2 HttpListener (org.apache.openejb.server.httpd.HttpListener)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1