camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1360031 - in /camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/jaxrs/ test/java/org/apache/camel/component/cxf/jaxrs/testbean/
Date Wed, 11 Jul 2012 06:31:36 GMT
Author: ningjiang
Date: Wed Jul 11 06:31:35 2012
New Revision: 1360031

URL: http://svn.apache.org/viewvc?rev=1360031&view=rev
Log:
CAMEL-4014 supports to use Interface as the service class

Added:
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CamelResourceProvider.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SubResourceClassInvocationHandler.java
Modified:
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerServiceResource.java

Added: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CamelResourceProvider.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CamelResourceProvider.java?rev=1360031&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CamelResourceProvider.java
(added)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CamelResourceProvider.java
Wed Jul 11 06:31:35 2012
@@ -0,0 +1,73 @@
+/**
+ * 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.camel.component.cxf.jaxrs;
+
+import java.lang.reflect.Proxy;
+
+import org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider;
+import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.message.Message;
+
+public class CamelResourceProvider implements ResourceProvider {
+    // Using the dynamical proxy to provide the instance of client to invoke
+    private Class<?> clazz;
+    private ResourceProvider provider;
+    
+    public CamelResourceProvider(Class<?> clazz) {
+        this.clazz = clazz;
+        if (!clazz.isInterface()) {
+            provider = new PerRequestResourceProvider(clazz);
+        }
+    }
+
+    @Override
+    public Object getInstance(Message m) {
+        Object result = null;
+        if (provider != null) {
+            result = provider.getInstance(m);
+        } else {
+            // create the instance with the invoker
+        	result = Proxy.newProxyInstance(
+				    clazz.getClassLoader(),
+				    new Class[]{clazz},
+				    new SubResourceClassInvocationHandler());
+        }
+        return result;
+    }
+
+    @Override
+    public void releaseInstance(Message m, Object o) {
+        if (provider != null) {
+            provider.releaseInstance(m, o);
+        }
+    }
+
+    @Override
+    public Class<?> getResourceClass() {
+        if (provider != null) {
+            return provider.getResourceClass();
+        } else {
+            return clazz;
+        }
+    }
+
+    @Override
+    public boolean isSingleton() {
+        return false;
+    }
+
+}

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java?rev=1360031&r1=1360030&r2=1360031&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
(original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsEndpoint.java
Wed Jul 11 06:31:35 2012
@@ -140,7 +140,11 @@ public class CxfRsEndpoint extends Defau
             sfb.setAddress(getAddress());
         }
         if (getResourceClasses() != null) {
-            List res = CastUtils.cast(getResourceClasses());
+            List<Class<?>> res = CastUtils.cast(getResourceClasses());
+            // setup the resource providers
+            for(Class<?>clazz : res) {
+            	sfb.setResourceProvider(clazz, new CamelResourceProvider(clazz));
+            }
             sfb.setResourceClasses(res);
         }
         sfb.setStart(false);

Added: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SubResourceClassInvocationHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SubResourceClassInvocationHandler.java?rev=1360031&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SubResourceClassInvocationHandler.java
(added)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/SubResourceClassInvocationHandler.java
Wed Jul 11 06:31:35 2012
@@ -0,0 +1,51 @@
+/**
+ * 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.camel.component.cxf.jaxrs;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.apache.cxf.jaxrs.utils.ResourceUtils;
+// This class only return the sub class instance
+public class SubResourceClassInvocationHandler implements InvocationHandler {
+
+	@Override
+	public Object invoke(Object proxy, Method method, Object[] parameters)
+			throws Throwable {
+		Object result = null;
+		Class<?> returnType = method.getReturnType();
+		System.out.println("returnType class " + returnType);
+		if (!returnType.isAssignableFrom(Void.class)) {
+			// create a instance to return
+			if (returnType.isInterface()) {
+				// create a new proxy for it
+				result =  Proxy.newProxyInstance(
+					    returnType.getClassLoader(),
+					    new Class[]{returnType},
+					    new SubResourceClassInvocationHandler());
+			} else {
+				// get the constructor and create a new instance
+				Constructor<?> c = ResourceUtils.findResourceConstructor(returnType, true);
+				result =  c.newInstance(new Object[]{});
+			}
+		}
+		return result;
+	}
+
+}

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerServiceResource.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerServiceResource.java?rev=1360031&r1=1360030&r2=1360031&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerServiceResource.java
(original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/testbean/CustomerServiceResource.java
Wed Jul 11 06:31:35 2012
@@ -22,27 +22,17 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.core.Response;
 
-/**
- *
- * @version
- */
+
 // START SNIPPET: example
 @Path("/customerservice/")
-public class CustomerServiceResource {
-
-    public CustomerServiceResource() {
-    }
+public interface CustomerServiceResource {
 
     @GET
     @Path("/customers/{id}/")
-    public Customer getCustomer(@PathParam("id") String id) {
-        return null;
-    }
+    public Customer getCustomer(@PathParam("id") String id);
 
     @PUT
     @Path("/customers/")
-    public Response updateCustomer(Customer customer) {
-        return null;
-    }
+    public Response updateCustomer(Customer customer);
 }
 // END SNIPPET: example



Mime
View raw message