You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/11/22 07:05:55 UTC

svn commit: r1412431 - in /camel/trunk/components/camel-xmlrpc/src: main/java/org/apache/camel/component/xmlrpc/ test/java/org/apache/camel/component/xmlrpc/

Author: ningjiang
Date: Thu Nov 22 06:05:53 2012
New Revision: 1412431

URL: http://svn.apache.org/viewvc?rev=1412431&view=rev
Log:
CAMEL-5814 supporting to configure the XmlRpcClient

Added:
    camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcClientConfigurer.java
      - copied, changed from r1412428, camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java
    camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/MyClientConfigurer.java
      - copied, changed from r1412428, camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java
    camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java
Modified:
    camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java
    camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java

Copied: camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcClientConfigurer.java (from r1412428, camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcClientConfigurer.java?p2=camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcClientConfigurer.java&p1=camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java&r1=1412428&r2=1412431&rev=1412431&view=diff
==============================================================================
--- camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java (original)
+++ camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcClientConfigurer.java Thu Nov 22 06:05:53 2012
@@ -16,20 +16,19 @@
  */
 package org.apache.camel.component.xmlrpc;
 
-import java.util.Map;
-
-import org.apache.camel.Endpoint;
-import org.apache.camel.impl.DefaultComponent;
+import org.apache.xmlrpc.client.XmlRpcClient;
 
 /**
- * Represents the component that manages {@link XmlRpcEndpoint}.
+ * A pluggable strategy for configuring the XmlRpcClient used by this component
+ *
  */
-public class XmlRpcComponent extends DefaultComponent {
+public interface XmlRpcClientConfigurer {
+    
+    /**
+     * Configure the XmlRpcClient 
+     *
+     * @param client the client of XmlRpc
+     */
+    void configureXmlRpcClient(XmlRpcClient client);
 
-    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        // current we just use the uri as the server address
-        Endpoint endpoint = new XmlRpcEndpoint(uri, this, remaining);
-        setProperties(endpoint, parameters);
-        return endpoint;
-    }
 }

Modified: camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java?rev=1412431&r1=1412430&r2=1412431&view=diff
==============================================================================
--- camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java (original)
+++ camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java Thu Nov 22 06:05:53 2012
@@ -20,6 +20,7 @@ import java.util.Map;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
 
 /**
  * Represents the component that manages {@link XmlRpcEndpoint}.
@@ -28,8 +29,13 @@ public class XmlRpcComponent extends Def
 
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
         // current we just use the uri as the server address
-        Endpoint endpoint = new XmlRpcEndpoint(uri, this, remaining);
-        setProperties(endpoint, parameters);
+        XmlRpcEndpoint endpoint = new XmlRpcEndpoint(uri, this, remaining);
+        XmlRpcClientConfigImpl clientConfig = endpoint.getClientConfig();
+        // find out the clientConfigurer first
+        XmlRpcClientConfigurer clientConfigurer = resolveAndRemoveReferenceParameter(parameters, "clientConfigurer", XmlRpcClientConfigurer.class);
+        endpoint.setClientConfigurer(clientConfigurer);
+        // we just use the XmlRpcClientConfig to take the parameters
+        setProperties(clientConfig, parameters);
         return endpoint;
     }
 }

Modified: camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java?rev=1412431&r1=1412430&r2=1412431&view=diff
==============================================================================
--- camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java (original)
+++ camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcEndpoint.java Thu Nov 22 06:05:53 2012
@@ -32,6 +32,8 @@ import org.apache.xmlrpc.client.XmlRpcCl
  */
 public class XmlRpcEndpoint extends DefaultEndpoint {
     private String address;
+    private XmlRpcClientConfigurer clientConfigurer;
+    private XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
 
     public XmlRpcEndpoint() {
     }
@@ -62,9 +64,13 @@ public class XmlRpcEndpoint extends Defa
     public XmlRpcClient createClient() throws MalformedURLException {
         XmlRpcClient client = new XmlRpcClient();
         // setup the client with the configuration from the XmlRpcEndpoint
-        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
+        XmlRpcClientConfigImpl config = clientConfig.cloneMe();
+        // setup the server url
         config.setServerURL(new URL(getAddress()));
         client.setConfig(config);
+        if (clientConfigurer != null) {
+            clientConfigurer.configureXmlRpcClient(client);
+        }
         return client;
     }
 
@@ -75,6 +81,24 @@ public class XmlRpcEndpoint extends Defa
     public void setAddress(String address) {
         this.address = address;
     }
+
+    public XmlRpcClientConfigurer getClientConfigurer() {
+        return clientConfigurer;
+    }
+
+    public void setClientConfigurer(XmlRpcClientConfigurer configurer) {
+        this.clientConfigurer = configurer;
+    }
+    
+    public void setClientConfig(XmlRpcClientConfigImpl config) {
+        this.clientConfig = config;
+    }
+    
+    public XmlRpcClientConfigImpl getClientConfig() {
+        return clientConfig;
+    }
+    
+    
     
     
 }

Copied: camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/MyClientConfigurer.java (from r1412428, camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/MyClientConfigurer.java?p2=camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/MyClientConfigurer.java&p1=camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java&r1=1412428&r2=1412431&rev=1412431&view=diff
==============================================================================
--- camel/trunk/components/camel-xmlrpc/src/main/java/org/apache/camel/component/xmlrpc/XmlRpcComponent.java (original)
+++ camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/MyClientConfigurer.java Thu Nov 22 06:05:53 2012
@@ -15,21 +15,21 @@
  * limitations under the License.
  */
 package org.apache.camel.component.xmlrpc;
+//START SNIPPET: e1
+import org.apache.xmlrpc.client.XmlRpcClient;
+import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
 
-import java.util.Map;
+public class MyClientConfigurer implements XmlRpcClientConfigurer {
 
-import org.apache.camel.Endpoint;
-import org.apache.camel.impl.DefaultComponent;
-
-/**
- * Represents the component that manages {@link XmlRpcEndpoint}.
- */
-public class XmlRpcComponent extends DefaultComponent {
-
-    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        // current we just use the uri as the server address
-        Endpoint endpoint = new XmlRpcEndpoint(uri, this, remaining);
-        setProperties(endpoint, parameters);
-        return endpoint;
+    @Override
+    public void configureXmlRpcClient(XmlRpcClient client) {
+        // get the configure first
+        XmlRpcClientConfigImpl clientConfig = (XmlRpcClientConfigImpl)client.getClientConfig();
+        // change the value of clientConfig
+        clientConfig.setEnabledForExtensions(true);
+        // set the option on the XmlRpcClient
+        client.setMaxThreads(10);
     }
+
 }
+//END SNIPPET: e1

Added: camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java?rev=1412431&view=auto
==============================================================================
--- camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java (added)
+++ camel/trunk/components/camel-xmlrpc/src/test/java/org/apache/camel/component/xmlrpc/XmlRpcEndpointTest.java Thu Nov 22 06:05:53 2012
@@ -0,0 +1,58 @@
+/**
+ * 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.xmlrpc;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
+import org.apache.camel.spi.Registry;
+import org.apache.xmlrpc.client.XmlRpcClient;
+import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class XmlRpcEndpointTest extends Assert {
+    
+    private CamelContext camelContext = new DefaultCamelContext(createRegistry());
+    
+    protected Registry createRegistry() {
+        SimpleRegistry answer = new SimpleRegistry();
+        // Binding the client configurer
+        answer.put("myClientConfigurer", new MyClientConfigurer());
+        return answer;
+    }
+    
+    // create the endpoint with parameters
+    @Test
+    public void testEndpointSetting() throws Exception {
+        XmlRpcEndpoint endpoint = (XmlRpcEndpoint)camelContext.getEndpoint("xmlrpc:http://www.example.com?userAgent=myAgent&gzipCompressing=true&connectionTimeout=30");
+        XmlRpcClientConfigImpl clientConfig = endpoint.getClientConfig();
+        assertEquals("Get a wrong userAgent", "myAgent", clientConfig.getUserAgent());
+        assertEquals("Get a wrong gzipCompressing", true, clientConfig.isGzipCompressing());
+        assertEquals("Get a wrong connectionTimeout", 30, clientConfig.getConnectionTimeout());
+        assertEquals("Get a wrong endpoint address", "http://www.example.com", endpoint.getAddress());
+    }
+    
+    @Test
+    public void testClientConfigurer() throws Exception {
+        XmlRpcEndpoint endpoint = (XmlRpcEndpoint)camelContext.getEndpoint("xmlrpc:http://www.example.com?clientConfigurer=#myClientConfigurer");
+        XmlRpcClient client = endpoint.createClient();
+        assertEquals("Get a worng maxThreads", 10, client.getMaxThreads());
+        assertEquals("Get a wrong value of enabledForExtensions", true, ((XmlRpcClientConfigImpl)client.getClientConfig()).isEnabledForExtensions());
+    }
+
+}