AbstractReflectiveHandlerMapping.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.server;
00020 
00021 import java.lang.reflect.Method;
00022 import java.lang.reflect.Modifier;
00023 import java.util.ArrayList;
00024 import java.util.HashMap;
00025 import java.util.Iterator;
00026 import java.util.List;
00027 import java.util.Map;
00028 
00029 import org.apache.xmlrpc.XmlRpcException;
00030 import org.apache.xmlrpc.XmlRpcHandler;
00031 import org.apache.xmlrpc.XmlRpcRequest;
00032 import org.apache.xmlrpc.common.TypeConverterFactory;
00033 import org.apache.xmlrpc.common.TypeConverterFactoryImpl;
00034 import org.apache.xmlrpc.metadata.ReflectiveXmlRpcMetaDataHandler;
00035 import org.apache.xmlrpc.metadata.Util;
00036 import org.apache.xmlrpc.metadata.XmlRpcListableHandlerMapping;
00037 import org.apache.xmlrpc.metadata.XmlRpcMetaDataHandler;
00038 import org.apache.xmlrpc.server.RequestProcessorFactoryFactory.RequestProcessorFactory;
00039 
00040 
00044 public abstract class AbstractReflectiveHandlerMapping
00045                 implements XmlRpcListableHandlerMapping {
00049     public interface AuthenticationHandler {
00053         boolean isAuthorized(XmlRpcRequest pRequest)
00054             throws XmlRpcException;
00055     }
00056 
00057     private TypeConverterFactory typeConverterFactory = new TypeConverterFactoryImpl();
00058     protected Map handlerMap = new HashMap();
00059     private AuthenticationHandler authenticationHandler;
00060     private RequestProcessorFactoryFactory requestProcessorFactoryFactory = new RequestProcessorFactoryFactory.RequestSpecificProcessorFactoryFactory();
00061     private boolean voidMethodEnabled;
00062 
00066     public void setTypeConverterFactory(TypeConverterFactory pFactory) {
00067         typeConverterFactory = pFactory;
00068     }
00069 
00073     public TypeConverterFactory getTypeConverterFactory() {
00074         return typeConverterFactory;
00075     }
00076 
00080     public void setRequestProcessorFactoryFactory(RequestProcessorFactoryFactory pFactory) {
00081         requestProcessorFactoryFactory = pFactory;
00082     }
00083 
00086     public RequestProcessorFactoryFactory getRequestProcessorFactoryFactory() {
00087         return requestProcessorFactoryFactory;
00088     }
00089 
00092     public AuthenticationHandler getAuthenticationHandler() {
00093         return authenticationHandler;
00094     }
00095 
00098     public void setAuthenticationHandler(AuthenticationHandler pAuthenticationHandler) {
00099         authenticationHandler = pAuthenticationHandler;
00100     }
00101 
00102     protected boolean isHandlerMethod(Method pMethod) {
00103         if (!Modifier.isPublic(pMethod.getModifiers())) {
00104             return false;  // Ignore methods, which aren't public
00105         }
00106         if (Modifier.isStatic(pMethod.getModifiers())) {
00107             return false;  // Ignore methods, which are static
00108         }
00109         if (!isVoidMethodEnabled()  &&  pMethod.getReturnType() == void.class) {
00110             return false;  // Ignore void methods.
00111         }
00112         if (pMethod.getDeclaringClass() == Object.class) {
00113             return false;  // Ignore methods from Object.class
00114         }
00115         return true;
00116     }
00117 
00143     protected void registerPublicMethods(String pKey,
00144                 Class pType) throws XmlRpcException {
00145         Map map = new HashMap();
00146         Method[] methods = pType.getMethods();
00147         for (int i = 0;  i < methods.length;  i++) {
00148             final Method method = methods[i];
00149             if (!isHandlerMethod(method)) {
00150                 continue;
00151             }
00152             String name = (pKey.length() > 0 ? pKey + "." : "") + method.getName();
00153             Method[] mArray;
00154             Method[] oldMArray = (Method[]) map.get(name);
00155             if (oldMArray == null) {
00156                 mArray = new Method[]{method};
00157             } else {
00158                 mArray = new Method[oldMArray.length+1];
00159                 System.arraycopy(oldMArray, 0, mArray, 0, oldMArray.length);
00160                 mArray[oldMArray.length] = method;
00161             }
00162             map.put(name, mArray);
00163         }
00164 
00165         for (Iterator iter = map.entrySet().iterator();  iter.hasNext();  ) {
00166             Map.Entry entry = (Map.Entry) iter.next();
00167             String name = (String) entry.getKey();
00168             Method[] mArray = (Method[]) entry.getValue();
00169             handlerMap.put(name, newXmlRpcHandler(pType, mArray));
00170         }
00171     }
00172 
00179     protected XmlRpcHandler newXmlRpcHandler(final Class pClass,
00180             final Method[] pMethods) throws XmlRpcException {
00181         String[][] sig = getSignature(pMethods);
00182         String help = getMethodHelp(pClass, pMethods);
00183         RequestProcessorFactory factory = requestProcessorFactoryFactory.getRequestProcessorFactory(pClass);
00184         if (sig == null  ||  help == null) {
00185                 return new ReflectiveXmlRpcHandler(this, typeConverterFactory,
00186                     pClass, factory, pMethods);
00187         }
00188         return new ReflectiveXmlRpcMetaDataHandler(this, typeConverterFactory,
00189                 pClass, factory, pMethods, sig, help);
00190     }
00191 
00194     protected String[][] getSignature(Method[] pMethods) {
00195         return Util.getSignature(pMethods);
00196     }
00197 
00201     protected String getMethodHelp(Class pClass, Method[] pMethods) {
00202         return Util.getMethodHelp(pClass, pMethods);
00203     }
00204 
00210     public XmlRpcHandler getHandler(String pHandlerName)
00211             throws XmlRpcNoSuchHandlerException, XmlRpcException {
00212         XmlRpcHandler result = (XmlRpcHandler) handlerMap.get(pHandlerName);
00213         if (result == null) {
00214             throw new XmlRpcNoSuchHandlerException("No such handler: " + pHandlerName);
00215         }
00216         return result;
00217     }
00218 
00219         public String[] getListMethods() throws XmlRpcException {
00220                 List list = new ArrayList();
00221                 for (Iterator iter = handlerMap.entrySet().iterator();
00222                      iter.hasNext();  ) {
00223                         Map.Entry entry = (Map.Entry) iter.next();
00224                         if (entry.getValue() instanceof XmlRpcMetaDataHandler) {
00225                                 list.add(entry.getKey());
00226                         }
00227                 }
00228                 
00229                 return (String[]) list.toArray(new String[list.size()]);
00230         }
00231 
00232         public String getMethodHelp(String pHandlerName) throws XmlRpcException {
00233                 XmlRpcHandler h = getHandler(pHandlerName);
00234                 if (h instanceof XmlRpcMetaDataHandler)
00235                         return ((XmlRpcMetaDataHandler)h).getMethodHelp();
00236                 throw new XmlRpcNoSuchHandlerException("No help available for method: "
00237                                 + pHandlerName);
00238         }
00239 
00240         public String[][] getMethodSignature(String pHandlerName) throws XmlRpcException {
00241                 XmlRpcHandler h = getHandler(pHandlerName);
00242                 if (h instanceof XmlRpcMetaDataHandler)
00243                         return ((XmlRpcMetaDataHandler)h).getSignatures();
00244                 throw new XmlRpcNoSuchHandlerException("No metadata available for method: "
00245                                 + pHandlerName);
00246         }
00247 
00253     public boolean isVoidMethodEnabled() {
00254         return voidMethodEnabled;
00255     }
00256 
00262     public void setVoidMethodEnabled(boolean pVoidMethodEnabled) {
00263         voidMethodEnabled = pVoidMethodEnabled;
00264     }
00265 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49