Search in sources :

Example 1 with EJBRestServiceInfo

use of org.apache.openejb.server.rest.EJBRestServiceInfo in project tomee by apache.

the class CxfRsHttpListener method deployApplication.

@Override
public void deployApplication(final Application application, final String prefix, final String webContext, final Collection<Object> additionalProviders, final Map<String, EJBRestServiceInfo> restEjbs, final ClassLoader classLoader, final Collection<Injection> injections, final Context context, final WebBeansContext owbCtx, final ServiceConfiguration serviceConfiguration) {
    final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
    try {
        final JAXRSServerFactoryBean factory = newFactory(prefix, createServiceJmxName(classLoader), createEndpointName(application));
        configureFactory(additionalProviders, serviceConfiguration, factory, owbCtx);
        factory.setApplication(application);
        final List<Class<?>> classes = new ArrayList<>();
        for (final Class<?> clazz : application.getClasses()) {
            if (!additionalProviders.contains(clazz) && !clazz.isInterface()) {
                classes.add(clazz);
                final EJBRestServiceInfo restServiceInfo = getEjbRestServiceInfo(restEjbs, clazz);
                if (restServiceInfo != null) {
                    final Object proxy = ProxyEJB.subclassProxy(restServiceInfo.context);
                    factory.setResourceProvider(clazz, new NoopResourceProvider(restServiceInfo.context.getBeanClass(), proxy));
                } else {
                    factory.setResourceProvider(clazz, new OpenEJBPerRequestPojoResourceProvider(classLoader, clazz, injections, context, owbCtx));
                }
            }
        }
        for (final Object o : application.getSingletons()) {
            if (!additionalProviders.contains(o)) {
                final Class<?> clazz = realClass(o.getClass());
                classes.add(clazz);
                final EJBRestServiceInfo restServiceInfo = getEjbRestServiceInfo(restEjbs, clazz);
                if (restServiceInfo != null) {
                    final Object proxy = ProxyEJB.subclassProxy(restServiceInfo.context);
                    factory.setResourceProvider(clazz, new NoopResourceProvider(restServiceInfo.context.getBeanClass(), proxy));
                } else {
                    if (owbCtx != null && owbCtx.getBeanManagerImpl().isInUse()) {
                        final CdiSingletonResourceProvider provider = new CdiSingletonResourceProvider(classLoader, clazz, o, injections, context, owbCtx);
                        singletons.add(provider);
                        factory.setResourceProvider(clazz, provider);
                    } else {
                        factory.setResourceProvider(clazz, new SingletonResourceProvider(o));
                    }
                }
            }
        }
        factory.setResourceClasses(classes);
        factory.setInvoker(new AutoJAXRSInvoker(restEjbs));
        this.context = webContext;
        if (!webContext.startsWith("/")) {
            this.context = "/" + webContext;
        }
        // /webcontext/servlet for event firing
        final Level level = SERVER_IMPL_LOGGER.getLevel();
        try {
            SERVER_IMPL_LOGGER.setLevel(Level.OFF);
        } catch (final UnsupportedOperationException e) {
        //ignore
        }
        try {
            server = factory.create();
            fixProviderIfKnown();
            fireServerCreated(oldLoader);
            final ServerProviderFactory spf = ServerProviderFactory.class.cast(server.getEndpoint().get(ServerProviderFactory.class.getName()));
            LOGGER.info("Using readers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "messageReaders"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
            LOGGER.info("Using writers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "messageWriters"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
            LOGGER.info("Using exception mappers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "exceptionMappers"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
        } finally {
            try {
                SERVER_IMPL_LOGGER.setLevel(level);
            } catch (final UnsupportedOperationException e) {
            //ignore
            }
        }
        final int servletIdx = 1 + this.context.substring(1).indexOf('/');
        if (servletIdx > 0) {
            this.servlet = this.context.substring(servletIdx);
            this.context = this.context.substring(0, servletIdx);
        }
        destination = (HttpDestination) server.getDestination();
        final String base;
        if (prefix.endsWith("/")) {
            base = prefix.substring(0, prefix.length() - 1);
        } else if (prefix.endsWith(wildcard)) {
            base = prefix.substring(0, prefix.length() - wildcard.length());
        } else {
            base = prefix;
        }
        // stack info to log to get nice logs
        logEndpoints(application, prefix, restEjbs, factory, base);
    } finally {
        if (oldLoader != null) {
            CxfUtil.clearBusLoader(oldLoader);
        }
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ManagedEndpoint(org.apache.cxf.endpoint.ManagedEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) Level(java.util.logging.Level)

Example 2 with EJBRestServiceInfo

use of org.apache.openejb.server.rest.EJBRestServiceInfo in project tomee by apache.

the class AutoJAXRSInvoker method invoke.

@Override
public Object invoke(final Exchange exchange, final Object o) {
    // mainly a select the right invoker impl
    final ClassResourceInfo cri = (ClassResourceInfo) exchange.get("root.resource.class");
    if (cri != null) {
        final String clazz = cri.getServiceClass().getName();
        final EJBRestServiceInfo restServiceInfo = ejbs.get(clazz);
        if (restServiceInfo != null && !BeanType.MANAGED.equals(restServiceInfo.context.getComponentType())) {
            return ejbInvoker.invoke(exchange, o);
        }
    }
    return jaxrsInvoker.invoke(exchange, o);
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo)

Example 3 with EJBRestServiceInfo

use of org.apache.openejb.server.rest.EJBRestServiceInfo in project tomee by apache.

the class CxfRsHttpListener method getEjbRestServiceInfo.

private EJBRestServiceInfo getEjbRestServiceInfo(Map<String, EJBRestServiceInfo> restEjbs, Class<?> clazz) {
    String name = clazz.getName();
    EJBRestServiceInfo restServiceInfo = restEjbs.get(name);
    if (name.endsWith(DynamicSubclass.IMPL_SUFFIX)) {
        name = name.substring(0, name.length() - DynamicSubclass.IMPL_SUFFIX.length());
        restServiceInfo = restEjbs.get(name);
        if (restServiceInfo != null) {
            // AutoJAXRSInvoker relies on it
            restEjbs.put(clazz.getName(), restServiceInfo);
        }
    }
    return restServiceInfo;
}
Also used : EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo)

Aggregations

EJBRestServiceInfo (org.apache.openejb.server.rest.EJBRestServiceInfo)3 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Level (java.util.logging.Level)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 ManagedEndpoint (org.apache.cxf.endpoint.ManagedEndpoint)1 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)1 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)1