Author: rmannibucau
Date: Sun Jul 20 12:36:13 2014
New Revision: 1612083
URL: http://svn.apache.org/r1612083
Log:
removing jaxrs cdi extension since it makes wrong wrapping/unwrapping of
ApplicationFactory, replacing it by internal extension and a particular event
to customize webbeanscontext
Added:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/WebBeansContextCreated.java
Removed:
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/cdi/
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/resources/META-INF/services/
Modified:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
tomee/tomee/trunk/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
Modified:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java?rev=1612083&r1=1612082&r2=1612083&view=diff
==============================================================================
---
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
(original)
+++
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
Sun Jul 20 12:36:13 2014
@@ -147,6 +147,7 @@ public class ThreadSingletonServiceImpl
webBeansContext = new WebappWebBeansContext(services,
properties, appContext.getWebBeansContext());
startupObject.getWebContext().setWebbeansContext(webBeansContext);
}
+ SystemInstance.get().fireEvent(new
WebBeansContextCreated(webBeansContext));
OpenEJBTransactionService.class.cast(services.get(TransactionService.class)).setWebBeansContext(webBeansContext);
// do it only here to get the webbeanscontext
Added:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/WebBeansContextCreated.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/WebBeansContextCreated.java?rev=1612083&view=auto
==============================================================================
---
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/WebBeansContextCreated.java
(added)
+++
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/WebBeansContextCreated.java
Sun Jul 20 12:36:13 2014
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi;
+
+import org.apache.openejb.observer.Event;
+import org.apache.webbeans.config.WebBeansContext;
+
+@Event
+public class WebBeansContextCreated {
+ private final WebBeansContext context;
+
+ public WebBeansContextCreated(final WebBeansContext webBeansContext) {
+ this.context = webBeansContext;
+ }
+
+ public WebBeansContext getContext() {
+ return context;
+ }
+
+ @Override
+ public String toString() {
+ return "WebBeansContextCreated{" +
+ "context=" + context +
+ '}';
+ }
+}
Modified:
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java?rev=1612083&r1=1612082&r2=1612083&view=diff
==============================================================================
---
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
(original)
+++
tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
Sun Jul 20 12:36:13 2014
@@ -19,19 +19,60 @@ package org.apache.openejb.server.cxf.rs
import org.apache.cxf.Bus;
import org.apache.cxf.binding.BindingFactoryManager;
import org.apache.cxf.jaxrs.JAXRSBindingFactory;
+import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.WebAppInfo;
+import
org.apache.openejb.assembler.classic.event.AssemblerAfterApplicationCreated;
+import org.apache.openejb.cdi.WebBeansContextCreated;
import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.observer.Observes;
+import org.apache.openejb.rest.AbstractRestThreadLocalProxy;
import org.apache.openejb.rest.RESTResourceFinder;
+import org.apache.openejb.rest.ThreadLocalContextManager;
import org.apache.openejb.server.ServiceException;
import org.apache.openejb.server.cxf.transport.HttpTransportFactory;
import org.apache.openejb.server.cxf.transport.util.CxfUtil;
import org.apache.openejb.server.rest.RESTService;
import org.apache.openejb.server.rest.RsHttpListener;
-
+import org.apache.openejb.spi.ContainerSystem;
+import org.apache.webbeans.annotation.AnyLiteral;
+import org.apache.webbeans.annotation.EmptyAnnotationLiteral;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Providers;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Type;
import java.net.Socket;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.Properties;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
public class CxfRSService extends RESTService {
@@ -53,6 +94,26 @@ public class CxfRSService extends RESTSe
return NAME;
}
+ public void integrateCDIAndJaxRsInjections(@Observes final
WebBeansContextCreated event) {
+ contextCDIIntegration(event.getContext());
+ }
+
+ private void contextCDIIntegration(final WebBeansContext wbc) {
+ final BeanManagerImpl beanManagerImpl = wbc.getBeanManagerImpl();
+ beanManagerImpl.addAdditionalQualifier(Context.class);
+ beanManagerImpl.addInternalBean(new
ContextBean<>(SecurityContext.class,
ThreadLocalContextManager.SECURITY_CONTEXT));
+ beanManagerImpl.addInternalBean(new ContextBean<>(UriInfo.class,
ThreadLocalContextManager.URI_INFO));
+ beanManagerImpl.addInternalBean(new
ContextBean<>(HttpServletRequest.class,
ThreadLocalContextManager.HTTP_SERVLET_REQUEST));
+ beanManagerImpl.addInternalBean(new
ContextBean<>(HttpServletResponse.class,
ThreadLocalContextManager.HTTP_SERVLET_RESPONSE));
+ beanManagerImpl.addInternalBean(new ContextBean<>(HttpHeaders.class,
ThreadLocalContextManager.HTTP_HEADERS));
+ beanManagerImpl.addInternalBean(new ContextBean<>(Request.class,
ThreadLocalContextManager.REQUEST));
+ beanManagerImpl.addInternalBean(new
ContextBean<>(ServletRequest.class, ThreadLocalContextManager.SERVLET_REQUEST));
+ beanManagerImpl.addInternalBean(new
ContextBean<>(ServletContext.class, ThreadLocalContextManager.SERVLET_CONTEXT));
+ beanManagerImpl.addInternalBean(new ContextBean<>(ServletConfig.class,
ThreadLocalContextManager.SERVLET_CONFIG));
+ beanManagerImpl.addInternalBean(new ContextBean<>(Providers.class,
ThreadLocalContextManager.PROVIDERS));
+ beanManagerImpl.addInternalBean(new
ContextBean<>(ContextResolver.class,
ThreadLocalContextManager.CONTEXT_RESOLVER));
+ }
+
@Override
public void init(final Properties properties) throws Exception {
super.init(properties);
@@ -105,4 +166,97 @@ public class CxfRSService extends RESTSe
protected RsHttpListener createHttpListener() {
return new CxfRsHttpListener(httpTransportFactory, getWildcard());
}
+
+ private static class ContextLiteral extends
EmptyAnnotationLiteral<Context> implements Context {
+ private static final long serialVersionUID = 1L;
+
+ public static final AnnotationLiteral<Context> INSTANCE = new
ContextLiteral();
+ }
+
+ private static class ContextBean<T> implements Bean<T> {
+ private final Class<T> type;
+ private final Set<Type> types;
+ private final Set<Annotation> qualifiers;
+ private final T proxy;
+
+ public ContextBean(final Class<T> type, final
AbstractRestThreadLocalProxy<T> proxy) {
+ this.type = type;
+ this.proxy =
+ (T)
Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new
Class<?>[]{type, Serializable.class}, new DelegateHandler(proxy));
+ this.types = new HashSet<Type>(asList(Object.class, type));
+ this.qualifiers = new
HashSet<Annotation>(asList(ContextLiteral.INSTANCE, AnyLiteral.INSTANCE));
+ }
+
+ @Override
+ public Set<Type> getTypes() {
+ return types;
+ }
+
+ @Override
+ public Set<Annotation> getQualifiers() {
+ return qualifiers;
+ }
+
+ @Override
+ public Class<? extends Annotation> getScope() {
+ return ApplicationScoped.class;
+ }
+
+ @Override
+ public String getName() {
+ return null;
+ }
+
+ @Override
+ public boolean isNullable() {
+ return false;
+ }
+
+ @Override
+ public Set<InjectionPoint> getInjectionPoints() {
+ return Collections.<InjectionPoint>emptySet();
+ }
+
+ @Override
+ public Class<?> getBeanClass() {
+ return type;
+ }
+
+ @Override
+ public Set<Class<? extends Annotation>> getStereotypes() {
+ return Collections.<Class<? extends Annotation>>emptySet();
+ }
+
+ @Override
+ public boolean isAlternative() {
+ return false;
+ }
+
+ @Override
+ public T create(final CreationalContext<T> tCreationalContext) {
+ return proxy;
+ }
+
+ @Override
+ public void destroy(final T t, final CreationalContext<T>
tCreationalContext) {
+ // no-op
+ }
+ }
+
+ private static class DelegateHandler<T> implements InvocationHandler {
+ private final AbstractRestThreadLocalProxy<T> proxy;
+
+ public DelegateHandler(final AbstractRestThreadLocalProxy<T> proxy) {
+ this.proxy = proxy;
+ }
+
+ @Override
+ public Object invoke(final Object ignored, final Method method, final
Object[] args) throws Throwable {
+ try {
+ return method.invoke(proxy.get(), args);
+ } catch (final InvocationTargetException ite) {
+ throw ite.getCause();
+ }
+ }
+ }
}
Modified:
tomee/tomee/trunk/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
URL:
http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java?rev=1612083&r1=1612082&r2=1612083&view=diff
==============================================================================
---
tomee/tomee/trunk/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
(original)
+++
tomee/tomee/trunk/server/openejb-rest/src/main/java/org/apache/openejb/server/rest/RESTService.java
Sun Jul 20 12:36:13 2014
@@ -69,7 +69,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -88,12 +87,12 @@ public abstract class RESTService implem
private static final int PORT = -1;
public static final String NOPATH_PREFIX = "http://nopath/";
- private final Set<AppInfo> deployedApplications = new HashSet<AppInfo>();
- private final Set<WebAppInfo> deployedWebApps = new HashSet<WebAppInfo>();
+ private final Set<AppInfo> deployedApplications = new HashSet<>();
+ private final Set<WebAppInfo> deployedWebApps = new HashSet<>();
private Assembler assembler;
private CoreContainerSystem containerSystem;
private RsRegistry rsRegistry;
- private final List<DeployedService> services = new
ArrayList<DeployedService>();
+ private final List<DeployedService> services = new ArrayList<>();
private String virtualHost = "localhost";
private String auth = "NONE";
private String realm = "PropertiesLogin";
@@ -129,7 +128,7 @@ public abstract class RESTService implem
final ClassLoader oldLoader =
Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
- final Collection<Object> additionalProviders = new HashSet<Object>();
+ final Collection<Object> additionalProviders = new HashSet<>();
addAppProvidersIfNeeded(appInfo, webApp, classLoader,
additionalProviders);
Collection<IdPropertiesInfo> pojoConfigurations = null; // done lazily
@@ -401,7 +400,7 @@ public abstract class RESTService implem
appPrefix = webApp.contextRoot;
} // else keep application prefix
- final Set<String> restClasses = new
HashSet<String>(webApp.restClass);
+ final Set<String> restClasses = new HashSet<>(webApp.restClass);
restClasses.addAll(webApp.ejbRestServices);
for (final String clazz : restClasses) {
@@ -528,7 +527,7 @@ public abstract class RESTService implem
}
private static <T> boolean isProvider(final Class<T> clazz) {
- return new
MetaAnnotatedClass<T>(clazz).isAnnotationPresent(Provider.class);
+ return new
MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class);
}
private boolean hasEjbAndIsNotAManagedBean(final Map<String,
EJBRestServiceInfo> restEjbs, final String clazz) {
@@ -544,7 +543,7 @@ public abstract class RESTService implem
}
private Collection<Object> appProviders(final Collection<String>
jaxRsProviders, final ClassLoader classLoader) {
- final Collection<Object> additionalProviders = new HashSet<Object>();
+ final Collection<Object> additionalProviders = new HashSet<>();
for (final String name : jaxRsProviders) {
try {
final Class<?> providerClass = classLoader.loadClass(name);
@@ -586,7 +585,7 @@ public abstract class RESTService implem
if (useDiscoveredProviders(appInfo)) {
providers = appProviders(appInfo.jaxRsProviders,
appClassLoader);
} else {
- providers = new ArrayList<Object>();
+ providers = new ArrayList<>();
}
if
("true".equalsIgnoreCase(appInfo.properties.getProperty(OPENEJB_USE_APPLICATION_PROPERTY,
APPLICATION_DEPLOYMENT))) {
@@ -594,7 +593,7 @@ public abstract class RESTService implem
addEjbToApplication(application, restEjbs);
// merge configurations at app level since a single
deployment is available
- final List<IdPropertiesInfo> pojoConfigurations = new
ArrayList<IdPropertiesInfo>();
+ final List<IdPropertiesInfo> pojoConfigurations = new
ArrayList<>();
BeanContext comp = null;
for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
for (final EnterpriseBeanInfo bean :
ejbJar.enterpriseBeans) {
@@ -673,7 +672,7 @@ public abstract class RESTService implem
protected abstract boolean containsJaxRsConfiguration(final Properties
properties);
protected Map<String, EJBRestServiceInfo> getRestEjbs(final AppInfo
appInfo, final String webapp) {
- final Map<String, BeanContext> beanContexts = new HashMap<String,
BeanContext>();
+ final Map<String, BeanContext> beanContexts = new HashMap<>();
for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
if (ejbJar.webapp && webapp != null &&
!ejbJar.moduleId.equals(webapp)) {
continue;
@@ -691,7 +690,7 @@ public abstract class RESTService implem
}
}
- final Map<String, EJBRestServiceInfo> restEjbs = new HashMap<String,
EJBRestServiceInfo>();
+ final Map<String, EJBRestServiceInfo> restEjbs = new HashMap<>();
for (final WebAppInfo webApp : appInfo.webApps) {
for (final String ejb : webApp.ejbRestServices) {
if (beanContexts.containsKey(ejb)) {
@@ -913,7 +912,7 @@ public abstract class RESTService implem
final AppInfo app = event.getApp();
if (deployedApplications.contains(app)) {
for (final WebAppInfo webApp : app.webApps) {
- final List<DeployedService> toRemove = new
ArrayList<DeployedService>();
+ final List<DeployedService> toRemove = new ArrayList<>();
for (final DeployedService service : services) {
if (service.isInWebApp(webApp)) {
undeployRestObject(service.address);
@@ -954,7 +953,7 @@ public abstract class RESTService implem
public void stop() throws ServiceException {
if (assembler != null) {
SystemInstance.get().removeObserver(this);
- for (final AppInfo appInfo : new
ArrayList<AppInfo>(deployedApplications)) {
+ for (final AppInfo appInfo : new
ArrayList<>(deployedApplications)) {
undeploy(new AssemblerBeforeApplicationDestroyed(appInfo,
null));
}
}