Author: veithen Date: Mon Mar 29 23:43:21 2010 New Revision: 928941 URL: http://svn.apache.org/viewvc?rev=928941&view=rev Log: Eliminated some duplicate code originally introduced in r439555 and that has grown over time.
Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/deployment/util/Utils.java axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/receivers/AbstractMessageReceiver.java axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/Utils.java Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/deployment/util/Utils.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/deployment/util/Utils.java?rev=928941&r1=928940&r2=928941&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/deployment/util/Utils.java (original) +++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/deployment/util/Utils.java Mon Mar 29 23:43:21 2010 @@ -62,9 +62,7 @@ import javax.xml.namespace.QName; import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLStreamException; import java.io.*; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @@ -429,60 +427,11 @@ public class Utils { public static void fillAxisService(final AxisService axisService, AxisConfiguration axisConfig, ArrayList<String> excludeOperations, ArrayList<String> nonRpcMethods) throws Exception { - String serviceClass; - Parameter implInfoParam = axisService - .getParameter(Constants.SERVICE_CLASS); - ClassLoader serviceClassLoader = axisService.getClassLoader(); - - if (implInfoParam != null) { - serviceClass = (String)implInfoParam.getValue(); - } else { - // if Service_Class is null, every AbstractMR will look for - // ServiceObjectSupplier. This is user specific and may contain - // other looks. - implInfoParam = axisService - .getParameter(Constants.SERVICE_OBJECT_SUPPLIER); - if (implInfoParam != null) { - String className = ((String)implInfoParam.getValue()).trim(); - final Class serviceObjectMaker = Loader.loadClass( - serviceClassLoader, className); - if (serviceObjectMaker.getModifiers() != Modifier.PUBLIC) { - throw new AxisFault("Service class " + className - + " must have public as access Modifier"); - } - - // Find static getServiceObject() method, call it if there - final Method method = (Method)org.apache.axis2.java.security.AccessController - .doPrivileged(new PrivilegedExceptionAction() { - public Object run() throws NoSuchMethodException { - return serviceObjectMaker.getMethod( - "getServiceObject", - AxisService.class); - } - }); - Object obj = null; - if (method != null) { - obj = org.apache.axis2.java.security.AccessController - .doPrivileged(new PrivilegedExceptionAction() { - public Object run() - throws InstantiationException, - IllegalAccessException, - InvocationTargetException { - return method.invoke(serviceObjectMaker.newInstance(), - axisService); - } - }); - } - if (obj == null) { - log.warn("ServiceObjectSupplier implmentation Object could not be found"); - throw new DeploymentException( - "ServiceClass or ServiceObjectSupplier implmentation Object could not be found"); - } - serviceClass = obj.getClass().getName(); - } else { - return; - } + Class<?> serviceClass = org.apache.axis2.util.Utils.getServiceClass(axisService); + if (serviceClass == null) { + return; } + ClassLoader serviceClassLoader = axisService.getClassLoader(); // adding name spaces NamespaceMap map = new NamespaceMap(); map.put(Java2WSDLConstants.AXIS2_NAMESPACE_PREFIX, @@ -495,14 +444,14 @@ public class Utils { .getParameter(Java2WSDLConstants.DOC_LIT_BARE_PARAMETER); if (generateBare != null && "true".equals(generateBare.getValue())) { schemaGenerator = new DocLitBareSchemaGenerator(serviceClassLoader, - serviceClass.trim(), + serviceClass.getName(), axisService.getSchemaTargetNamespace(), axisService .getSchemaTargetNamespacePrefix(), axisService); } else { schemaGenerator = new DefaultSchemaGenerator(serviceClassLoader, - serviceClass.trim(), + serviceClass.getName(), axisService.getSchemaTargetNamespace(), axisService .getSchemaTargetNamespacePrefix(), Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/receivers/AbstractMessageReceiver.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/receivers/AbstractMessageReceiver.java?rev=928941&r1=928940&r2=928941&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/receivers/AbstractMessageReceiver.java (original) +++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/receivers/AbstractMessageReceiver.java Mon Mar 29 23:43:21 2010 @@ -34,25 +34,21 @@ import org.apache.axis2.context.MessageC import org.apache.axis2.context.ServiceContext; import org.apache.axis2.description.AxisService; import org.apache.axis2.description.InOnlyAxisOperation; -import org.apache.axis2.description.Parameter; import org.apache.axis2.description.WSDL2Constants; import org.apache.axis2.engine.AxisEngine; import org.apache.axis2.engine.DependencyManager; import org.apache.axis2.engine.MessageReceiver; import org.apache.axis2.i18n.Messages; import org.apache.axis2.util.JavaUtils; -import org.apache.axis2.util.Loader; import org.apache.axis2.util.MessageContextBuilder; +import org.apache.axis2.util.Utils; import org.apache.axis2.wsdl.WSDLUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.lang.reflect.Method; -import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; -import java.security.PrivilegedExceptionAction; public abstract class AbstractMessageReceiver implements MessageReceiver { protected static final Log log = LogFactory.getLog(AbstractMessageReceiver.class); @@ -214,55 +210,12 @@ public abstract class AbstractMessageRec * @throws AxisFault */ protected Object makeNewServiceObject(MessageContext msgContext) throws AxisFault { - try { - final AxisService service = msgContext.getAxisService(); - ClassLoader classLoader = service.getClassLoader(); - - // allow alternative definition of makeNewServiceObject - if (service.getParameter(Constants.SERVICE_OBJECT_SUPPLIER) != null) { - Parameter serviceObjectParam = - service.getParameter(Constants.SERVICE_OBJECT_SUPPLIER); - final Class serviceObjectMaker = Loader.loadClass(classLoader, ((String) - serviceObjectParam.getValue()).trim()); - - // Find static getServiceObject() method, call it if there - final Method method = (Method) org.apache.axis2.java.security.AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Object run() throws NoSuchMethodException { - return serviceObjectMaker.getMethod("getServiceObject", - new Class[]{AxisService.class}); - } - } - ); - if (method != null) { - return org.apache.axis2.java.security.AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Object run() throws InvocationTargetException, IllegalAccessException, InstantiationException { - return method.invoke(serviceObjectMaker.newInstance(), new Object[]{service}); - } - } - ); - } - } - - Parameter implInfoParam = service.getParameter(Constants.SERVICE_CLASS); - if (implInfoParam != null) { - final Class implClass = Loader.loadClass( - classLoader, - ((String) implInfoParam.getValue()).trim()); - return org.apache.axis2.java.security.AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Object run() throws InstantiationException, IllegalAccessException { - return implClass.newInstance(); - } - } - ); - } else { - throw new AxisFault( - Messages.getMessage("paramIsNotSpecified", "SERVICE_OBJECT_SUPPLIER")); - } - } catch (Exception e) { - throw AxisFault.makeFault(e); + Object serviceObject = Utils.createServiceObject(msgContext.getAxisService()); + if (serviceObject == null) { + throw new AxisFault( + Messages.getMessage("paramIsNotSpecified", "SERVICE_OBJECT_SUPPLIER")); + } else { + return serviceObject; } } Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/Utils.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/Utils.java?rev=928941&r1=928940&r2=928941&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/Utils.java (original) +++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/util/Utils.java Mon Mar 29 23:43:21 2010 @@ -59,11 +59,14 @@ import javax.xml.namespace.QName; import java.io.File; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import java.text.ParseException; import java.util.HashMap; import java.util.Iterator; import java.util.Enumeration; import java.util.Map; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.SocketException; import java.net.NetworkInterface; import java.net.InetAddress; @@ -649,4 +652,104 @@ public class Utils { int index = uri.indexOf(':'); return index > 0 ? uri.substring(0, index) : null; } + + /** + * Create a service object for a given service. The method first looks for + * the {...@link Constants#SERVICE_OBJECT_SUPPLIER} service parameter and if + * this parameter is present, it will use the specified class to create the + * service object. If the parameter is not present, it will create an + * instance of the class specified by the {...@link Constants#SERVICE_CLASS} + * parameter. + * + * @param service + * the service + * @return The service object or <code>null</code> if neither the + * {...@link Constants#SERVICE_OBJECT_SUPPLIER} nor the + * {...@link Constants#SERVICE_CLASS} parameter was found on the + * service, i.e. if the service doesn't specify how to create a + * service object. If the return value is non null, it will always + * be a newly created instance. + * @throws AxisFault + * if an error occurred while attempting to instantiate the + * service object + */ + public static Object createServiceObject(final AxisService service) throws AxisFault { + try { + ClassLoader classLoader = service.getClassLoader(); + + // allow alternative definition of makeNewServiceObject + Parameter serviceObjectSupplierParam = + service.getParameter(Constants.SERVICE_OBJECT_SUPPLIER); + if (serviceObjectSupplierParam != null) { + final Class<?> serviceObjectMaker = Loader.loadClass(classLoader, ((String) + serviceObjectSupplierParam.getValue()).trim()); + + // Find static getServiceObject() method, call it if there + final Method method = org.apache.axis2.java.security.AccessController.doPrivileged( + new PrivilegedExceptionAction<Method>() { + public Method run() throws NoSuchMethodException { + return serviceObjectMaker.getMethod("getServiceObject", + AxisService.class); + } + } + ); + return org.apache.axis2.java.security.AccessController.doPrivileged( + new PrivilegedExceptionAction<Object>() { + public Object run() throws InvocationTargetException, IllegalAccessException, InstantiationException { + return method.invoke(serviceObjectMaker.newInstance(), new Object[]{service}); + } + } + ); + } else { + Parameter serviceClassParam = service.getParameter(Constants.SERVICE_CLASS); + if (serviceClassParam != null) { + final Class<?> serviceClass = Loader.loadClass( + classLoader, + ((String) serviceClassParam.getValue()).trim()); + return org.apache.axis2.java.security.AccessController.doPrivileged( + new PrivilegedExceptionAction<Object>() { + public Object run() throws InstantiationException, IllegalAccessException { + return serviceClass.newInstance(); + } + } + ); + } else { + return null; + } + } + } catch (Exception e) { + throw AxisFault.makeFault(e); + } + } + + /** + * Get the service class for a given service. This method will first check + * the {...@link Constants#SERVICE_CLASS} service parameter and if that + * parameter is not present, inspect the instance returned by the service + * object supplier specified by {...@link Constants#SERVICE_OBJECT_SUPPLIER}. + * + * @param service + * the service + * @return The service class or <code>null</code> if neither the + * {...@link Constants#SERVICE_CLASS} nor the + * {...@link Constants#SERVICE_OBJECT_SUPPLIER} parameter was found on + * the service, i.e. if the service doesn't specify a service class. + * @throws AxisFault + * if an error occurred while attempting to load the service + * class or to instantiate the service object + */ + public static Class<?> getServiceClass(AxisService service) throws AxisFault { + Parameter serviceClassParam = service.getParameter(Constants.SERVICE_CLASS); + if (serviceClassParam != null) { + try { + return Loader.loadClass(service.getClassLoader(), + ((String) serviceClassParam.getValue()).trim()); + } catch (Exception e) { + throw AxisFault.makeFault(e); + } + } else { + Object serviceObject = createServiceObject(service); + return serviceObject == null ? null : serviceObject.getClass(); + } + } }