Commits

James Dumay committed 8c3500a

XMLRPC-1 - Implement basic functionality

  • Participants
  • Parent commits bf7995c

Comments (0)

Files changed (9)

+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.atlassian.pom</groupId>
+        <artifactId>atlassian-public-pom</artifactId>
+        <version>15</version>
+    </parent>
+    <groupId>com.atlassian.xmlrpc</groupId>
+    <artifactId>atlassian-xmlrpc-binder</artifactId>
+    <packaging>jar</packaging>
+    <version>1.0-SNAPSHOT</version>
+    <name>Atlassian XML-RPC Binder</name>
+    <url>http://www.atlassian.com/</url>
+    <properties>
+        <jdkLevel>1.5</jdkLevel>
+    </properties>
+    <licenses>
+        <license>
+            <name>Apache 2</name>
+            <distribution>repo</distribution>
+            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+        </license>
+    </licenses>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.xmlrpc</groupId>
+            <artifactId>xmlrpc-client</artifactId>
+            <version>3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>cglib</groupId>
+            <artifactId>cglib-full</artifactId>
+            <version>2.0.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.xmlrpc</groupId>
+            <artifactId>xmlrpc-server</artifactId>
+            <version>3.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>3.8.1</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

File src/main/java/com/atlassian/xmlrpc/Binder.java

+package com.atlassian.xmlrpc;
+
+import java.net.URL;
+
+/*
+ * 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.
+ */
+
+/**
+ *
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public interface Binder
+{
+    /**
+     * 
+     * @param <T>
+     * @param bindClass
+     * @return
+     */
+    public <T> T bind(Class<T> bindClass, URL url) throws BindingException;
+}

File src/main/java/com/atlassian/xmlrpc/BindingException.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class BindingException extends Exception
+{   
+    public BindingException(String message)
+    {
+        super(message);
+    }
+    
+    public BindingException(Throwable cause)
+    {
+        super(cause);
+    }
+    
+    public BindingException(String message, Throwable cause)
+    {
+        super(message, cause);
+    }
+}

File src/main/java/com/atlassian/xmlrpc/DefaultBinder.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+import java.net.URL;
+import net.sf.cglib.proxy.Enhancer;
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class DefaultBinder implements Binder
+{
+    public <T> T bind(Class<T> bindClass, URL url) throws BindingException
+    {
+        if (!bindClass.isInterface())
+        {
+            throw new BindingException("Class " + bindClass.getName() + "is not an interface");
+        }
+
+        ServiceObject serviceObject = bindClass.getAnnotation(ServiceObject.class);
+        if (serviceObject == null)
+        {
+            throw new BindingException("Could not find ServiceObject annotation on " + bindClass.getName());
+        }
+        
+        Enhancer enhancer = new Enhancer();
+        enhancer.setInterfaces(new Class[] { bindClass });
+        enhancer.setSuperclass(Object.class);
+        enhancer.setCallback(new RPCCallMethodInterceptor(url, serviceObject.objectName()));
+        
+        return (T)enhancer.create();
+    }
+}

File src/main/java/com/atlassian/xmlrpc/RPCCallMethodInterceptor.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Vector;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+import org.apache.xmlrpc.client.XmlRpcClient;
+import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class RPCCallMethodInterceptor implements MethodInterceptor
+{
+    private final XmlRpcClient client;
+    
+    private final String serviceObject;
+
+    public RPCCallMethodInterceptor(URL url, String serviceObject)
+    {
+        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
+        clientConfig.setServerURL(url);
+        this.client = new XmlRpcClient();
+        this.client.setConfig(clientConfig);
+        this.serviceObject = serviceObject;
+    }
+    
+    public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) 
+        throws Throwable 
+    {
+        try
+        {
+            return client.execute(serviceObject + "." + method.getName(), new Vector(Arrays.asList(args)));
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Could not execute RPC method", e);
+        }
+    }
+}

File src/main/java/com/atlassian/xmlrpc/ServiceObject.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+@Retention(value=RetentionPolicy.RUNTIME)
+public @interface ServiceObject
+{
+    String objectName();
+}

File src/test/java/com/atlassian/xmlrpc/HelloWorldService.java

+package com.atlassian.xmlrpc;
+/*
+ * 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.
+ */
+
+/**
+ *
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+@ServiceObject(objectName="HelloWorld")
+public interface HelloWorldService
+{
+    String say(String name);
+}

File src/test/java/com/atlassian/xmlrpc/HelloWorldServiceImpl.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class HelloWorldServiceImpl implements HelloWorldService
+{
+    public String say(String name)
+    {
+        return "Hello " + name;
+    }
+}

File src/test/java/com/atlassian/xmlrpc/SimpleServiceTest.java

+package com.atlassian.xmlrpc;
+
+/*
+ * 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.
+ */
+
+import java.net.URL;
+import java.util.concurrent.Callable;
+import org.apache.xmlrpc.webserver.WebServer;
+import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
+import org.apache.xmlrpc.server.PropertyHandlerMapping;
+import org.apache.xmlrpc.server.XmlRpcServer;
+import junit.framework.TestCase;
+
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class SimpleServiceTest extends TestCase
+{
+    public void testSimpleBinding() throws Exception
+    {
+        final int port = 9001;
+        WebServer server = new WebServer(port);
+        PropertyHandlerMapping phm = new PropertyHandlerMapping();
+        phm.addHandler("HelloWorld", com.atlassian.xmlrpc.HelloWorldServiceImpl.class);
+        XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
+        xmlRpcServer.setHandlerMapping(phm);
+        
+        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
+        serverConfig.setEnabledForExtensions(true);
+        serverConfig.setContentLengthOptional(false);
+
+        server.start();
+
+        try
+        {
+            URL url = new URL("http://localhost:" + port);
+            Binder binder = new DefaultBinder();
+            HelloWorldService service = binder.bind(HelloWorldService.class, url);
+            String result = service.say("James");
+            assertEquals("Hello James", result);
+        }
+        finally
+        {
+            server.shutdown();
+        }
+    }
+    
+    public void testThrowsExceptionIfBinderIsClass()
+        throws Exception
+    {
+        Binder binder = new DefaultBinder();
+        try
+        {
+            binder.bind(Object.class, new URL("http://localhost"));
+            fail("Should fail as binder type was class");
+        }
+        catch (BindingException e)
+        {
+            assertTrue(true);
+        }
+    }
+    
+    public void testThrowsExceptionIfServiceObjectAnnotationIsNotPresent()
+        throws Exception
+    {   
+        Binder binder = new DefaultBinder();
+        try
+        {
+            binder.bind(Callable.class, new URL("http://localhost"));
+            fail("Should fail as Callable does not have ServiceObject annotation");
+        }
+        catch (BindingException e)
+        {
+            assertTrue(true);
+        }
+    }
+}