Author: jochen Date: Tue Jun 13 13:38:36 2006 New Revision: 413980 URL: http://svn.apache.org/viewvc?rev=413980&view=rev Log: Added a factory for proxy objects.
Added: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ClientFactory.java webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/DynamicProxyTest.java webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/XmlRpcTestCase.java webservices/xmlrpc/trunk/tests/src/test/resources/org/apache/xmlrpc/test/DynamicProxyTest.properties Modified: webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/BaseTest.java Added: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ClientFactory.java URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ClientFactory.java?rev=413980&view=auto ============================================================================== --- webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ClientFactory.java (added) +++ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/util/ClientFactory.java Tue Jun 13 13:38:36 2006 @@ -0,0 +1,89 @@ +/* + * Copyright 1999,2006 The Apache Software Foundation. + * + * Licensed 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.xmlrpc.client.util; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import org.apache.xmlrpc.client.XmlRpcClient; + + +/** + * <p>The [EMAIL PROTECTED] ClientFactory} is a useful tool for simplifying the + * use of Apache XML-RPC. The rough idea is as follows: All XML-RPC + * handlers are implemented as interfaces. The server uses the actual + * implementation. The client uses the [EMAIL PROTECTED] ClientFactory} to + * obtain an implementation, which is based on running XML-RPC calls.</p> + */ +public class ClientFactory { + private final XmlRpcClient client; + private boolean objectMethodLocal; + + /** Creates a new instance. + * @param pClient A fully configured XML-RPC client, which is + * used internally to perform XML-RPC calls. + */ + public ClientFactory(XmlRpcClient pClient) { + client = pClient; + } + + /** Returns the factories client. + */ + public XmlRpcClient getClient() { + return client; + } + + /** Returns, whether a method declared by the [EMAIL PROTECTED] Object + * Object class} is performed by the local object, rather than + * by the server. Defaults to true. + */ + public boolean isObjectMethodLocal() { + return objectMethodLocal; + } + + /** Sets, whether a method declared by the [EMAIL PROTECTED] Object + * Object class} is performed by the local object, rather than + * by the server. Defaults to true. + */ + public void setObjectMethodLocal(boolean pObjectMethodLocal) { + objectMethodLocal = pObjectMethodLocal; + } + + /** Creates an object, which is implementing the given interface. + * The objects methods are internally calling an XML-RPC server + * by using the factories client. + */ + public Object newInstance(Class pClass) { + return newInstance(Thread.currentThread().getContextClassLoader(), pClass); + } + + /** Creates an object, which is implementing the given interface. + * The objects methods are internally calling an XML-RPC server + * by using the factories client. + */ + public Object newInstance(ClassLoader pClassLoader, final Class pClass) { + return Proxy.newProxyInstance(pClassLoader, new Class[]{pClass}, new InvocationHandler(){ + public Object invoke(Object pProxy, Method pMethod, Object[] pArgs) throws Throwable { + if (isObjectMethodLocal() && pMethod.getDeclaringClass().equals(Object.class)) { + return pMethod.invoke(pProxy, pArgs); + } + String methodName = pClass.getName() + "." + pMethod.getName(); + return client.execute(methodName, pArgs); + } + }); + } +} Modified: webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/BaseTest.java URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/BaseTest.java?rev=413980&r1=413979&r2=413980&view=diff ============================================================================== --- webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/BaseTest.java (original) +++ webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/BaseTest.java Tue Jun 13 13:38:36 2006 @@ -41,31 +41,11 @@ import org.w3c.dom.Node; import org.xml.sax.InputSource; -import junit.framework.TestCase; - /** An abstract test case, to be implemented for the various * transport classes. */ -public class BaseTest extends TestCase { - private ClientProvider[] providers; - - public void setUp() throws Exception { - if (providers == null) { - XmlRpcHandlerMapping mapping = getHandlerMapping(); - providers = new ClientProvider[]{ - new LocalTransportProvider(mapping), - new LocalStreamTransportProvider(mapping), - new LiteTransportProvider(mapping, true), - // new LiteTransportProvider(mapping, false), Doesn't support HTTP/1.1 - new SunHttpTransportProvider(mapping, true), - new SunHttpTransportProvider(mapping, false), - new CommonsProvider(mapping), - new ServletWebServerProvider(mapping, true), - new ServletWebServerProvider(mapping, false) - }; - } - } +public class BaseTest extends XmlRpcTestCase { /** The remote class being invoked by the test case. */ @@ -305,16 +285,6 @@ return new PropertyHandlerMapping(getClass().getClassLoader(), getClass().getResource("BaseTest.properties"), true); - } - - protected XmlRpcClientConfigImpl getConfig(ClientProvider pProvider) throws Exception { - return pProvider.getConfig(); - } - - protected XmlRpcClientConfig getExConfig(ClientProvider pProvider) throws Exception { - XmlRpcClientConfigImpl config = getConfig(pProvider); - config.setEnabledForExtensions(true); - return config; } /** Test, whether we can invoke a method, passing a byte value. Added: webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/DynamicProxyTest.java URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/DynamicProxyTest.java?rev=413980&view=auto ============================================================================== --- webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/DynamicProxyTest.java (added) +++ webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/DynamicProxyTest.java Tue Jun 13 13:38:36 2006 @@ -0,0 +1,75 @@ +/* + * Copyright 1999,2006 The Apache Software Foundation. + * + * Licensed 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.xmlrpc.test; + +import java.io.IOException; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.apache.xmlrpc.client.util.ClientFactory; +import org.apache.xmlrpc.server.PropertyHandlerMapping; +import org.apache.xmlrpc.server.XmlRpcHandlerMapping; + + +/** Test case for the [EMAIL PROTECTED] ClientFactory}. + */ +public class DynamicProxyTest extends XmlRpcTestCase { + /** An interface, which is being implemented by the + * server. + */ + public interface Adder { + /** Returns the sum of the given integers. + */ + public int add(int pNum1, int pNum2); + } + + /** Implementation of [EMAIL PROTECTED] Adder}, which is used by + * the server. + */ + public static class AdderImpl implements Adder { + public int add(int pNum1, int pNum2) { + return pNum1 + pNum2; + } + } + + protected XmlRpcHandlerMapping getHandlerMapping() throws IOException, XmlRpcException { + return new PropertyHandlerMapping(getClass().getClassLoader(), + getClass().getResource("DynamicProxyTest.properties"), + true); + } + + private ClientFactory getClientFactory(ClientProvider pProvider) throws Exception { + XmlRpcClient client = pProvider.getClient(); + client.setConfig(getConfig(pProvider)); + return new ClientFactory(client); + } + + /** Tests calling the [EMAIL PROTECTED] Adder#add(int,int)} method + * by using an object, which has been created by the + * [EMAIL PROTECTED] ClientFactory}. + */ + public void testAdderCall() throws Exception { + for (int i = 0; i < providers.length; i++) { + testAdderCall(providers[i]); + } + } + + private void testAdderCall(ClientProvider pProvider) throws Exception { + ClientFactory factory = getClientFactory(pProvider); + Adder adder = (Adder) factory.newInstance(Adder.class); + assertEquals(6, adder.add(2, 4)); + } +} Added: webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/XmlRpcTestCase.java URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/XmlRpcTestCase.java?rev=413980&view=auto ============================================================================== --- webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/XmlRpcTestCase.java (added) +++ webservices/xmlrpc/trunk/tests/src/test/java/org/apache/xmlrpc/test/XmlRpcTestCase.java Tue Jun 13 13:38:36 2006 @@ -0,0 +1,61 @@ +/* + * Copyright 1999,2005 The Apache Software Foundation. + * + * Licensed 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.xmlrpc.test; + +import java.io.IOException; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClientConfig; +import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; +import org.apache.xmlrpc.server.XmlRpcHandlerMapping; + +import junit.framework.TestCase; + + +/** Abstract base class for deriving test cases. + */ +public abstract class XmlRpcTestCase extends TestCase { + protected ClientProvider[] providers; + + protected abstract XmlRpcHandlerMapping getHandlerMapping() throws IOException, XmlRpcException; + + protected XmlRpcClientConfigImpl getConfig(ClientProvider pProvider) throws Exception { + return pProvider.getConfig(); + } + + protected XmlRpcClientConfig getExConfig(ClientProvider pProvider) throws Exception { + XmlRpcClientConfigImpl config = getConfig(pProvider); + config.setEnabledForExtensions(true); + return config; + } + + public void setUp() throws Exception { + if (providers == null) { + XmlRpcHandlerMapping mapping = getHandlerMapping(); + providers = new ClientProvider[]{ + new LocalTransportProvider(mapping), + new LocalStreamTransportProvider(mapping), + new LiteTransportProvider(mapping, true), + // new LiteTransportProvider(mapping, false), Doesn't support HTTP/1.1 + new SunHttpTransportProvider(mapping, true), + new SunHttpTransportProvider(mapping, false), + new CommonsProvider(mapping), + new ServletWebServerProvider(mapping, true), + new ServletWebServerProvider(mapping, false) + }; + } + } +} Added: webservices/xmlrpc/trunk/tests/src/test/resources/org/apache/xmlrpc/test/DynamicProxyTest.properties URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/tests/src/test/resources/org/apache/xmlrpc/test/DynamicProxyTest.properties?rev=413980&view=auto ============================================================================== --- webservices/xmlrpc/trunk/tests/src/test/resources/org/apache/xmlrpc/test/DynamicProxyTest.properties (added) +++ webservices/xmlrpc/trunk/tests/src/test/resources/org/apache/xmlrpc/test/DynamicProxyTest.properties Tue Jun 13 13:38:36 2006 @@ -0,0 +1 @@ +org.apache.xmlrpc.test.DynamicProxyTest$Adder=org.apache.xmlrpc.test.DynamicProxyTest$AdderImpl --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]