Author: jochen
Date: Fri Oct  6 12:57:44 2006
New Revision: 453748

URL: http://svn.apache.org/viewvc?view=rev&rev=453748
Log:
Atomic properties of XmlRpcServer are now configurable as init parameters
in the XmlRpcServlet.
PR: XMLRPC-116
Submitted-by: Jimisola Laursen, [EMAIL PROTECTED]


Added:
    
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ReflectionUtil.java
Modified:
    
webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServlet.java
    webservices/xmlrpc/trunk/src/changes/changes.xml

Added: 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ReflectionUtil.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ReflectionUtil.java?view=auto&rev=453748
==============================================================================
--- 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ReflectionUtil.java
 (added)
+++ 
webservices/xmlrpc/trunk/common/src/main/java/org/apache/xmlrpc/util/ReflectionUtil.java
 Fri Oct  6 12:57:44 2006
@@ -0,0 +1,98 @@
+/*
+ * 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.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+
+/** A utility class for using reflection.
+ */
+public class ReflectionUtil {
+    /**
+     * This method attempts to set a property value on a given object by 
calling a
+     * matching setter.
+     * @param pObject The object, on which a property is being set.
+     * @param pPropertyName The property name.
+     * @param pPropertyValue The property value.
+     * @throws IllegalAccessException Setting the property value failed, 
because invoking
+     *   the setter raised an [EMAIL PROTECTED] IllegalAccessException}.
+     * @throws InvocationTargetException Setting the property value failed, 
because invoking
+     *   the setter raised another exception.
+     * @return Whether a matching setter was found. The value false indicates, 
that no such
+     *   setter exists.
+     */
+    public static boolean setProperty(Object pObject, String pPropertyName, 
String pPropertyValue)
+            throws IllegalAccessException, InvocationTargetException {
+        final String methodName = "set" + pPropertyName.substring(0, 
1).toUpperCase() + pPropertyName.substring(1);   
+        // try to find method signature that matches init param
+        Method[] methods = pObject.getClass().getMethods();
+
+        for (int i = 0;  i < methods.length;  i++) {
+            final Method method = methods[i];
+            if (!method.getName().equals(methodName)) {
+                continue; // Ignore methods, which does have the right name 
+            }
+            if (!Modifier.isPublic(method.getModifiers())) {
+                continue;  // Ignore methods, which aren't public
+            }
+            
+            Class[] parameterTypes = method.getParameterTypes();
+            if (parameterTypes.length != 1) {
+                continue; // Ignore methods, which don't not have exactly one 
parameter
+            }
+            
+            Class parameterType = parameterTypes[0];
+            final Object param;
+            try {
+                if (parameterType.equals(boolean.class) || 
parameterType.equals(Boolean.class)) {
+                    param = Boolean.valueOf(pPropertyValue);
+                } else if (parameterType.equals(char.class) || 
parameterType.equals(Character.class)) {
+                    if (pPropertyValue.length() != 1) {
+                        throw new IllegalArgumentException("Invalid value for 
parameter "
+                                + pPropertyName + "(length != 1):"
+                                + pPropertyValue);
+                    }
+                    param = new Character(pPropertyValue.charAt(0));
+                } else if (parameterType.equals(byte.class) || 
parameterType.equals(Byte.class)) {
+                    param = Byte.valueOf(pPropertyValue);
+                } else if (parameterType.equals(short.class) || 
parameterType.equals(Short.class)) {
+                    param = Short.valueOf(pPropertyValue);
+                } else if (parameterType.equals(int.class) || 
parameterType.equals(Integer.class)) {
+                    param = Integer.valueOf(pPropertyValue);
+                } else if (parameterType.equals(long.class) || 
parameterType.equals(Long.class)) {
+                    param = Long.valueOf(pPropertyValue);
+                } else if (parameterType.equals(float.class) || 
parameterType.equals(Float.class)) {
+                    param = Float.valueOf(pPropertyValue);
+                } else if (parameterType.equals(double.class) || 
parameterType.equals(Double.class)) {
+                    param = Double.valueOf(pPropertyValue);
+                } else if (parameterType.equals(String.class)) {
+                    param = pPropertyValue;
+                } else {
+                    throw new IllegalStateException("The property " + 
pPropertyName
+                            + " has an unsupported type of " + 
parameterType.getName());
+                }
+            } catch (NumberFormatException e) {
+                throw new IllegalArgumentException("Invalid value for property 
"
+                        + pPropertyName + ": " + pPropertyValue);
+            }
+            method.invoke(pObject, new Object[]{param});
+            return true;
+        }
+        return false;
+    }
+}

Modified: 
webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServlet.java
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServlet.java?view=diff&rev=453748&r1=453747&r2=453748
==============================================================================
--- 
webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServlet.java
 (original)
+++ 
webservices/xmlrpc/trunk/server/src/main/java/org/apache/xmlrpc/webserver/XmlRpcServlet.java
 Fri Oct  6 12:57:44 2006
@@ -16,7 +16,9 @@
 package org.apache.xmlrpc.webserver;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
+import java.util.Enumeration;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -29,12 +31,12 @@
 import org.apache.xmlrpc.XmlRpcConfig;
 import org.apache.xmlrpc.XmlRpcException;
 import org.apache.xmlrpc.common.TypeConverterFactory;
+import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
 import org.apache.xmlrpc.server.PropertyHandlerMapping;
 import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
 import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
 import org.apache.xmlrpc.server.XmlRpcServer;
-import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
-import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
+import org.apache.xmlrpc.util.ReflectionUtil;
 
 
 /** <p>A default servlet implementation The typical use would
@@ -64,15 +66,31 @@
                return server;
        }
 
+    private void handleInitParameters(ServletConfig pConfig) throws 
ServletException {
+        for (Enumeration en = pConfig.getInitParameterNames();  
en.hasMoreElements();  ) {
+            String name = (String) en.nextElement();
+            String value = pConfig.getInitParameter(name);
+            try {
+                if (ReflectionUtil.setProperty(server, name, value)) {
+                    throw new ServletException("Unknown init parameter " + 
name);
+                }
+            } catch (IllegalAccessException e) {
+                throw new ServletException("Illegal access to instance of " + 
server.getClass().getName()
+                        + " while setting property " + name + ": " + 
e.getMessage(), e);
+            } catch (InvocationTargetException e) {
+                Throwable t = e.getTargetException();
+                throw new ServletException("Failed to invoke setter for 
property " + name
+                        + " on instance of " + server.getClass().getName()
+                        + ": " + t.getMessage(), t);
+            }
+        }
+    }
+
        public void init(ServletConfig pConfig) throws ServletException {
                super.init(pConfig);
                try {
             server = newXmlRpcServer(pConfig);
-            String enabledForExtensionsParam = 
pConfig.getInitParameter("enabledForExtensions");
-            if (enabledForExtensionsParam != null) {
-                boolean b = 
Boolean.valueOf(enabledForExtensionsParam).booleanValue();
-                ((XmlRpcServerConfigImpl) 
server.getConfig()).setEnabledForExtensions(b);
-            }
+            handleInitParameters(pConfig);
                        server.setHandlerMapping(newXmlRpcHandlerMapping());
         } catch (XmlRpcException e) {
                        try {

Modified: webservices/xmlrpc/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/src/changes/changes.xml?view=diff&rev=453748&r1=453747&r2=453748
==============================================================================
--- webservices/xmlrpc/trunk/src/changes/changes.xml (original)
+++ webservices/xmlrpc/trunk/src/changes/changes.xml Fri Oct  6 12:57:44 2006
@@ -19,6 +19,11 @@
         The authentication handler, type converter and requestprocessor 
factories
         are now configurable as properties of the XmlRpcServlet.
       </action>
+      <action dev="jochen" type="add" issue="XMLRPC-116" due-to="Jimisola 
Laursen"
+          due-to-email="[EMAIL PROTECTED]">
+        Atomic properties of XmlRpcServer are now configurable as init 
parameters
+        in the XmlRpcServlet.
+      </action>
     </release>

     <release version="3.0.1-SNAPSHOT" date="Not yet released">

       <action dev="jochen" type="fix">



Reply via email to