Author: rmannibucau
Date: Fri Sep 21 07:56:47 2012
New Revision: 1388361

URL: http://svn.apache.org/viewvc?rev=1388361&view=rev
Log:
TOMEE-424 custom @Context (from trunk)

Added:
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ContextReferenceInfo.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/ContextRef.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/MapObjectReference.java
    
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomContextTest.java
Modified:
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/util/ServiceInfos.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/JndiEncInfoBuilder.java
    
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/rest/ThreadLocalContextManager.java
    
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java

Added: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ContextReferenceInfo.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ContextReferenceInfo.java?rev=1388361&view=auto
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ContextReferenceInfo.java
 (added)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ContextReferenceInfo.java
 Fri Sep 21 07:56:47 2012
@@ -0,0 +1,20 @@
+/*
+ * 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.assembler.classic;
+
+public class ContextReferenceInfo extends ResourceReferenceInfo {
+}

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiEncBuilder.java
 Fri Sep 21 07:56:47 2012
@@ -29,6 +29,7 @@ import org.apache.openejb.core.ivm.namin
 import org.apache.openejb.core.ivm.naming.JaxWsServiceReference;
 import org.apache.openejb.core.ivm.naming.JndiReference;
 import org.apache.openejb.core.ivm.naming.JndiUrlReference;
+import org.apache.openejb.core.ivm.naming.MapObjectReference;
 import org.apache.openejb.core.ivm.naming.ObjectReference;
 import org.apache.openejb.core.ivm.naming.PersistenceContextReference;
 import org.apache.openejb.core.ivm.naming.Reference;
@@ -270,50 +271,61 @@ public class JndiEncBuilder {
         }
 
         for (ResourceReferenceInfo referenceInfo : jndiEnc.resourceRefs) {
+            if (!(referenceInfo instanceof ContextReferenceInfo)) {
+                if (referenceInfo.location != null) {
+                    Reference reference = 
buildReferenceLocation(referenceInfo.location);
+                    bindings.put(normalize(referenceInfo.referenceName), 
reference);
+                    continue;
+                }
 
-            if (referenceInfo.location != null) {
-                Reference reference = 
buildReferenceLocation(referenceInfo.location);
-                bindings.put(normalize(referenceInfo.referenceName), 
reference);
-                continue;
-            }
+                Class<?> type = getType(referenceInfo.referenceType, 
referenceInfo);
 
-            Class<?> type = getType(referenceInfo.referenceType, 
referenceInfo);
+                Object reference;
+                if (URL.class.equals(type)) {
+                    reference = new URLReference(referenceInfo.resourceID);
+                } else if (type.isAnnotationPresent(ManagedBean.class)) {
+                    ManagedBean managed = 
type.getAnnotation(ManagedBean.class);
+                    String name = managed.value().length() == 0 ? 
type.getSimpleName() : managed.value();
+                    reference = new LinkRef("module/" + name);
+                } else if (referenceInfo.resourceID != null) {
+                    String jndiName = "openejb/Resource/" + 
referenceInfo.resourceID;
+                    reference = new IntraVmJndiReference(jndiName);
+                } else {
+                    String jndiName = "openejb/Resource/" + 
referenceInfo.referenceName;
+                    reference = new IntraVmJndiReference(jndiName);
+                }
 
-            Object reference;
-            if (URL.class.equals(type)) {
-                reference = new URLReference(referenceInfo.resourceID);
-            } else if (type.isAnnotationPresent(ManagedBean.class)) {
-                ManagedBean managed = type.getAnnotation(ManagedBean.class);
-                String name = managed.value().length() == 0 ? 
type.getSimpleName() : managed.value();
-                reference = new LinkRef("module/" + name);
-            } else if (Request.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.REQUEST);
-            } else if (HttpServletRequest.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_REQUEST);
-            } else if (ServletRequest.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.SERVLET_REQUEST);
-            } else if (UriInfo.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.URI_INFO);
-            } else if (HttpHeaders.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_HEADERS);
-            } else if (SecurityContext.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.SECURITY_CONTEXT);
-            } else if (ContextResolver.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.CONTEXT_RESOLVER);
-            } else if (Providers.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.PROVIDERS);
-            } else if (ServletConfig.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.SERVLET_CONFIG);
-            } else if (HttpServletResponse.class.equals(type)) {
-                reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_RESPONSE);
-            } else if (referenceInfo.resourceID != null) {
-                String jndiName = "openejb/Resource/" + 
referenceInfo.resourceID;
-                reference = new IntraVmJndiReference(jndiName);
+                bindings.put(normalize(referenceInfo.referenceName), 
reference);
             } else {
-                String jndiName = "openejb/Resource/" + 
referenceInfo.referenceName;
-                reference = new IntraVmJndiReference(jndiName);
+                final Class<?> type = getType(referenceInfo.referenceType, 
referenceInfo);
+                Object reference;
+
+                if (Request.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.REQUEST);
+                } else if (HttpServletRequest.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_REQUEST);
+                } else if (ServletRequest.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.SERVLET_REQUEST);
+                } else if (UriInfo.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.URI_INFO);
+                } else if (HttpHeaders.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_HEADERS);
+                } else if (SecurityContext.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.SECURITY_CONTEXT);
+                } else if (ContextResolver.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.CONTEXT_RESOLVER);
+                } else if (Providers.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.PROVIDERS);
+                } else if (ServletConfig.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.SERVLET_CONFIG);
+                } else if (HttpServletResponse.class.equals(type)) {
+                    reference = new 
ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_RESPONSE);
+                } else {
+                    reference = new 
MapObjectReference(ThreadLocalContextManager.OTHERS, 
referenceInfo.referenceType);
+                }
+
+                bindings.put(normalize(referenceInfo.referenceName), 
reference);
             }
-            bindings.put(normalize(referenceInfo.referenceName), reference);
         }
 
         for (ResourceEnvReferenceInfo referenceInfo : jndiEnc.resourceEnvRefs) 
{

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/util/ServiceInfos.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/util/ServiceInfos.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/util/ServiceInfos.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/util/ServiceInfos.java
 Fri Sep 21 07:56:47 2012
@@ -76,7 +76,15 @@ public final class ServiceInfos {
 
         final List<Object> instances = new ArrayList<Object>();
         for (String id : ids) {
-            final Object instance = resolve(serviceInfos, id);
+            Object instance = resolve(serviceInfos, id);
+            if (instance == null) {  // maybe id == classname
+                try {
+                    instance = 
Thread.currentThread().getContextClassLoader().loadClass(id).newInstance();
+                } catch (Exception e) {
+                    // ignore
+                }
+            }
+
             if (instance != null) {
                 instances.add(instance);
             }

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 Fri Sep 21 07:56:47 2012
@@ -3619,7 +3619,7 @@ public class AnnotationDeployer implemen
         }
 
         private void buildContext(JndiConsumer consumer, Member member) {
-            ResourceRef ref = new ResourceRef();
+            final ContextRef ref = new ContextRef();
             ref.setName(member.getDeclaringClass().getName() + "/" + 
member.getName());
             ref.setResType(member.getType().getName());
 

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 Fri Sep 21 07:56:47 2012
@@ -724,7 +724,15 @@ public class AutoConfig implements Dynam
             }
 
             String destinationId = (mappedName.length() == 0) ? ref.getName() 
: mappedName;
-            destinationId = getResourceId(moduleId, destinationId, refType, 
appResources);
+            try {
+                destinationId = getResourceId(moduleId, destinationId, 
refType, appResources);
+            } catch (OpenEJBException ex) {
+                if (!(ref instanceof ContextRef)) {
+                    throw ex;
+                } else { // let jaxrs provider manage it
+                    continue;
+                }
+            }
             ref.setMappedName(destinationId);
         }
 
@@ -1080,25 +1088,32 @@ public class AutoConfig implements Dynam
                 return;
             }
         } catch (Throwable t) {
+            // no-op
         }
 
-        ResourceLink link = ejbDeployment.getResourceLink(refName);
-        if (link == null) {
-            String id = (mappedName.length() == 0) ? ref.getName() : 
mappedName;
-            if (id.startsWith("java:")) {
-                id = id.substring("java:".length());
+        try {
+            ResourceLink link = ejbDeployment.getResourceLink(refName);
+            if (link == null) {
+                String id = (mappedName.length() == 0) ? ref.getName() : 
mappedName;
+                if (id.startsWith("java:")) {
+                    id = id.substring("java:".length());
+                }
+                id = getResourceId(ejbDeployment.getDeploymentId(), id, 
refType, appResources);
+                logger.info("Auto-linking resource-ref '" + refName + "' in 
bean " + ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
+
+                link = new ResourceLink();
+                link.setResId(id);
+                link.setResRefName(refName);
+                ejbDeployment.addResourceLink(link);
+            } else {
+                String id = getResourceId(ejbDeployment.getDeploymentId(), 
link.getResId(), refType, appResources);
+                link.setResId(id);
+                link.setResRefName(refName);
+            }
+        } catch (OpenEJBException ex) {
+            if (!(ref instanceof ContextRef)) {
+                throw ex;
             }
-            id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, 
appResources);
-            logger.info("Auto-linking resource-ref '" + refName + "' in bean " 
+ ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
-
-            link = new ResourceLink();
-            link.setResId(id);
-            link.setResRefName(refName);
-            ejbDeployment.addResourceLink(link);
-        } else {
-            String id = getResourceId(ejbDeployment.getDeploymentId(), 
link.getResId(), refType, appResources);
-            link.setResId(id);
-            link.setResRefName(refName);
         }
     }
 

Added: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/ContextRef.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/ContextRef.java?rev=1388361&view=auto
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/ContextRef.java
 (added)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/ContextRef.java
 Fri Sep 21 07:56:47 2012
@@ -0,0 +1,22 @@
+/*
+ * 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.config;
+
+import org.apache.openejb.jee.ResourceRef;
+
+public class ContextRef extends ResourceRef {
+}

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/JndiEncInfoBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/JndiEncInfoBuilder.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/JndiEncInfoBuilder.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/config/JndiEncInfoBuilder.java
 Fri Sep 21 07:56:47 2012
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.ContextReferenceInfo;
 import org.apache.openejb.assembler.classic.EjbJarInfo;
 import org.apache.openejb.assembler.classic.EjbLocalReferenceInfo;
 import org.apache.openejb.assembler.classic.EjbReferenceInfo;
@@ -283,7 +284,12 @@ public class JndiEncInfoBuilder {
 
     private void buildResourceRefInfos(JndiConsumer item, JndiEncInfo 
moduleJndiEnc, JndiEncInfo compJndiEnc) {
         for (ResourceRef res : item.getResourceRef()) {
-            ResourceReferenceInfo info = new ResourceReferenceInfo();
+            final ResourceReferenceInfo info;
+            if (res instanceof ContextRef) {
+                info = new ContextReferenceInfo();
+            } else {
+                info = new ResourceReferenceInfo();
+            }
 
             if (res.getResAuth() != null) {
                 info.referenceAuth = res.getResAuth().toString();

Added: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/MapObjectReference.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/MapObjectReference.java?rev=1388361&view=auto
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/MapObjectReference.java
 (added)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/MapObjectReference.java
 Fri Sep 21 07:56:47 2012
@@ -0,0 +1,35 @@
+/*
+ * 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.core.ivm.naming;
+
+import javax.naming.NamingException;
+import java.util.Map;
+
+public class MapObjectReference extends Reference {
+
+    private ThreadLocal<Map<String, Object>> obj;
+    private String key;
+
+    public MapObjectReference(final ThreadLocal<Map<String, Object>> obj, 
final String key) {
+        this.obj = obj;
+        this.key = key;
+    }
+
+    public Object getObject() throws NamingException {
+        return obj.get().get(key);
+    }
+}

Modified: 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/rest/ThreadLocalContextManager.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/rest/ThreadLocalContextManager.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/rest/ThreadLocalContextManager.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/container/openejb-core/src/main/java/org/apache/openejb/rest/ThreadLocalContextManager.java
 Fri Sep 21 07:56:47 2012
@@ -18,6 +18,7 @@
 package org.apache.openejb.rest;
 
 import javax.ws.rs.core.Application;
+import java.util.Map;
 
 public class ThreadLocalContextManager {
     public static final ThreadLocalRequest REQUEST = new ThreadLocalRequest();
@@ -31,6 +32,7 @@ public class ThreadLocalContextManager {
     public static final ThreadLocalContextResolver CONTEXT_RESOLVER = new 
ThreadLocalContextResolver();
     public static final ThreadLocalProviders PROVIDERS = new 
ThreadLocalProviders();
     public static final ThreadLocal<Application> APPLICATION = new 
ThreadLocal<Application>();
+    public static final ThreadLocal<Map<String, Object>> OTHERS = new 
ThreadLocal<Map<String, Object>>();
 
     public static void reset() {
         REQUEST.remove();
@@ -44,5 +46,11 @@ public class ThreadLocalContextManager {
         CONTEXT_RESOLVER.remove();
         PROVIDERS.remove();
         APPLICATION.remove();
+
+        final Map<String, Object> map = OTHERS.get();
+        if (map != null) {
+            map.clear();
+        }
+        OTHERS.remove();
     }
 }

Modified: 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java?rev=1388361&r1=1388360&r2=1388361&view=diff
==============================================================================
--- 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
 (original)
+++ 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
 Fri Sep 21 07:56:47 2012
@@ -17,10 +17,13 @@
 package org.apache.openejb.server.cxf.rs;
 
 import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.jaxrs.ext.ContextProvider;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
 import org.apache.openejb.InvalidateReferenceException;
 import org.apache.openejb.rest.ThreadLocalContextManager;
 
@@ -38,6 +41,8 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.rmi.RemoteException;
+import java.util.HashMap;
+import java.util.Map;
 
 public class OpenEJBEJBInvoker extends JAXRSInvoker {
     @Override
@@ -77,6 +82,18 @@ public class OpenEJBEJBInvoker extends J
             } else if (ServletConfig.class.equals(type)) {
                 ServletConfig servletConfig = 
JAXRSUtils.createContextValue(exchange.getInMessage(), null, 
ServletConfig.class);
                 ThreadLocalContextManager.SERVLET_CONFIG.set(servletConfig);
+            } else {
+                final Message message = exchange.getInMessage();
+                final ContextProvider<?> provider = 
ProviderFactory.getInstance(message).createContextProvider(type, message);
+                if (provider != null) {
+                    final Object value = provider.createContext(message);
+                    Map<String, Object> map = 
ThreadLocalContextManager.OTHERS.get();
+                    if (map == null) {
+                        map = new HashMap<String, Object>();
+                        ThreadLocalContextManager.OTHERS.set(map);
+                    }
+                    map.put(type.getName(), value);
+                }
             }
         }
 

Added: 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomContextTest.java
URL: 
http://svn.apache.org/viewvc/openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomContextTest.java?rev=1388361&view=auto
==============================================================================
--- 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomContextTest.java
 (added)
+++ 
openejb/branches/openejb-4.5.0/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomContextTest.java
 Fri Sep 21 07:56:47 2012
@@ -0,0 +1,113 @@
+/*
+ * 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.server.cxf.rs;
+
+import org.apache.cxf.jaxrs.ext.ContextProvider;
+import org.apache.cxf.message.Message;
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.Empty;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Configuration;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.loader.IO;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.Singleton;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.ext.Provider;
+import javax.ws.rs.ext.Providers;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class CustomContextTest {
+    @Module
+    public static EjbModule service() throws Exception {
+        final EjbModule module = new EjbModule(new EjbJar(), new OpenejbJar());
+
+        final SingletonBean bean = new 
SingletonBean(CustomContextInjectedBean.class);
+        bean.setLocalBean(new Empty());
+
+        module.getEjbJar().addEnterpriseBean(bean);
+
+        module.getOpenejbJar().addEjbDeployment(bean);
+        
module.getOpenejbJar().getEjbDeployment().iterator().next().getProperties().setProperty("cxf.jaxrs.providers",
 CustomProvider.class.getName());
+
+        return module;
+    }
+
+    @Configuration
+    public static Properties configuration() throws Exception {
+        final Properties properties = new Properties();
+        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, 
"true");
+        return properties;
+    }
+
+    @Test
+    public void rest() throws IOException {
+        final String response = IO.slurp(new 
URL("http://127.0.0.1:4204/CustomContextTest/custom-context/check";));
+        assertEquals("true", response);
+    }
+
+    @Singleton
+    @Path("/custom-context")
+    public static class CustomContextInjectedBean {
+        @Context
+        private IFoo foo;
+
+        @GET
+        @Path("/check")
+        public boolean check() {
+            return foo != null && foo.getMsg() != null;
+        }
+    }
+
+    public static interface IFoo {
+        Message getMsg();
+    }
+
+    public static class Foo implements IFoo {
+        private final Message msg;
+
+        public Foo(final Message message) {
+            msg = message;
+        }
+
+        public Message getMsg() {
+            return msg;
+        }
+    }
+
+    @Provider
+    public static class CustomProvider implements ContextProvider<IFoo> {
+        @Override
+        public IFoo createContext(final Message message) {
+            return new Foo(message);
+        }
+    }
+}


Reply via email to