axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject cvs commit: xml-axis-wsif/java/test/providers PlugableProvidersTest.java
Date Fri, 26 Jul 2002 14:26:15 GMT
antelder    2002/07/26 07:26:15

  Modified:    java/src/org/apache/wsif/base WSIFServiceImpl.java
               java/src/org/apache/wsif/util WSIFUtils.java
               java/test/providers PlugableProvidersTest.java
  Added:       java/src/org/apache/wsif/util WSIFPluggableProviders.java
  Log:
  Clean up pluggable provider support:
  - create a new WSIFPluggableProviders class to encapsulate all the provider finding and
choosing logic
  - remove all the provider methods from WSIFServiceImpl leaving only what were public methods
which now are depricated and call methods in WSIFPluggableProviders
  
  Revision  Changes    Path
  1.18      +24 -366   xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceImpl.java
  
  Index: WSIFServiceImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceImpl.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- WSIFServiceImpl.java	25 Jul 2002 16:40:57 -0000	1.17
  +++ WSIFServiceImpl.java	26 Jul 2002 14:26:15 -0000	1.18
  @@ -57,12 +57,6 @@
   
   package org.apache.wsif.base;
   
  -import java.io.BufferedReader;
  -import java.io.IOException;
  -import java.io.InputStreamReader;
  -import java.net.URL;
  -import java.security.AccessController;
  -import java.security.PrivilegedAction;
   import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.HashMap;
  @@ -104,7 +98,7 @@
   import org.apache.wsif.logging.Trc;
   import org.apache.wsif.providers.WSIFDynamicTypeMap;
   import org.apache.wsif.spi.WSIFProvider;
  -import org.apache.wsif.util.WSIFProperties;
  +import org.apache.wsif.util.WSIFPluggableProviders;
   import org.apache.wsif.util.WSIFUtils;
   import org.apache.wsif.wsdl.extensions.ejb.EJBBindingSerializer;
   import org.apache.wsif.wsdl.extensions.format.FormatBindingSerializer;
  @@ -125,9 +119,6 @@
    * @author Mark Whitlock <whitlock@apache.org>
    */
   public class WSIFServiceImpl implements WSIFService {
  -    private static boolean autoLoadProviders = true;
  -    private static HashMap namespaceProviders;
  -    private static WSIFProvider[] availableProviders = null;
       private static PrivateCompositeExtensionRegistry providersExtRegs =
           new PrivateCompositeExtensionRegistry();
       private static WSIFMessageFactory msgFactory = null;
  @@ -165,10 +156,6 @@
           throws WSIFException {
           Trc.entry(this, wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
   
  -        // load WSIFProviders
  -        if (availableProviders == null)
  -            availableProviders = getAllDynamicWSIFProviders();
  -
           // load WSDL defintion
           Definition def = null;
           try {
  @@ -212,10 +199,6 @@
           throws WSIFException {
           Trc.entry(this, wsdlLoc, cl, serviceNS, serviceName, portTypeNS, portTypeName);
   
  -        // load WSIFProviders
  -        if (availableProviders == null)
  -            availableProviders = getAllDynamicWSIFProviders();
  -
           // load WSDL defintion
           Definition def = null;
           try {
  @@ -266,10 +249,6 @@
           throws WSIFException {
           Trc.entry(this, def, service, portType);
   
  -        // load WSIFProviders
  -        if (availableProviders == null)
  -            availableProviders = getAllDynamicWSIFProviders();
  -
           init(def, service, portType);
           if (Trc.ON)
               Trc.exit(deep());
  @@ -285,10 +264,6 @@
           throws WSIFException {
           Trc.entry(this, def, serviceNS, serviceName);
   
  -        // load WSIFProviders
  -        if (availableProviders == null)
  -            availableProviders = getAllDynamicWSIFProviders();
  -
           // select WSDL service if given by name or only one
           Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
           init(def, service, null);
  @@ -311,10 +286,6 @@
           throws WSIFException {
           Trc.entry(this, def, serviceNS, serviceName, portTypeNS, portTypeName);
   
  -        // load WSIFProviders
  -        if (availableProviders == null)
  -            availableProviders = getAllDynamicWSIFProviders();
  -
           checkWSDLForWSIF(def);
   
           // select WSDL service if given by name or only one
  @@ -409,7 +380,7 @@
           ExtensibilityElement bindingFirstEx =
               (ExtensibilityElement) bindingExList.get(0);
           String bindingNS = bindingFirstEx.getElementType().getNamespaceURI();
  -        WSIFProvider provider = getDynamicWSIFProvider(bindingNS);
  +        WSIFProvider provider = WSIFPluggableProviders.getProvider(bindingNS);
           if (provider != null) {
               return provider.createDynamicWSIFPort(def, service, port, typeMap);
           } else {
  @@ -512,329 +483,40 @@
       }
   
       /**
  -     * Gets a WSIFProvider for a particular namespace URI.
  -     * @param namespaceURI  the URI of the namespace the WSIFProvider must support 
  -     * @return    a WSIFProvider supporting the requested URI,
  -     *            or null if no providers are available. 
  +     * @deprecated this method is replaced by the getProvider
  +     * method in the org.apache.util.WSIFPluggableProviders class
        */
       public static WSIFProvider getDynamicWSIFProvider(String namespaceURI) {
           Trc.entry(null, namespaceURI);
  -        WSIFProvider provider;
  -
  -        // the namespaceProviders Hashtable URIs end with a '/'
  -        if (!namespaceURI.endsWith("/")) {
  -            namespaceURI += "/";
  -        }
  -
  -        if (namespaceProviders == null) {
  -            namespaceProviders = new HashMap();
  -        } else {
  -            provider = (WSIFProvider) namespaceProviders.get(namespaceURI);
  -            if (provider != null) {
  -                return provider;
  -            }
  -        }
  -        WSIFProvider[] providers = getSupportingProviders(namespaceURI);
  -        if (providers.length == 0) {
  -            return null;
  -        }
  -        if (providers.length == 1) {
  -            provider = providers[0];
  -        } else {
  -            provider = chooseProvider(providers, namespaceURI);
  -        }
  -        namespaceProviders.put(namespaceURI, provider);
  -        Trc.exit(provider);
  -        return provider;
  -    }
  -
  -    /**
  -     * Gets all the available WSIFProvider that support a particular 
  -     * namespace URI.
  -     * @param namesapceURI   the namespace the WSIFProvider must support. 
  -     * @return    an array of WSIFProvider. The array will have a length
  -     * of zero if no WSIFProvider are available for the requested namespace.
  -     */
  -    private static WSIFProvider[] getSupportingProviders(String namespaceURI) {
  -        return getSupportingProviders(namespaceURI, true);
  -    }
  -
  -    /**
  -     * Gets all the available WSIFProvider that support a particular 
  -     * namespace URI.
  -     * @param namesapceURI   the namespace the WSIFProvider must support.
  -     * @param issueMessage Flag to inicate whether or not to log when multiple
  -     * providers are found for the same namespace. 
  -     * @return    an array of WSIFProvider. The array will have a length
  -     * of zero if no WSIFProvider are available for the requested namespace.
  -     */
  -    private static WSIFProvider[] getSupportingProviders(
  -        String namespaceURI,
  -        boolean issueMessage) {
  -        if (availableProviders == null) {
  -            availableProviders = getAllDynamicWSIFProviders();
  -        }
  -        ArrayList supportingProviders = new ArrayList();
  -        String[] uris;
  -        for (int i = 0; i < availableProviders.length; i++) {
  -            uris = availableProviders[i].getBindingNamespaceURIs();
  -            for (int j = 0; j < uris.length; j++) {
  -            	if (namespaceURI != null && namespaceURI.equals(uris[j])) {
  -                    supportingProviders.add(availableProviders[i]);
  -                }
  -            }
  -        }
  -        if (namespaceProviders != null) {
  -            if (namespaceProviders.get(namespaceURI) != null) {
  -                supportingProviders.add(namespaceProviders.get(namespaceURI));
  -            }
  -        }       
  -        WSIFProvider[] providerArray = new WSIFProvider[supportingProviders.size()];
  -        providerArray = (WSIFProvider[]) supportingProviders.toArray(providerArray);
  -        if (providerArray.length > 1 && issueMessage) {
  -            issueMultipleProvidersMsg(namespaceURI, providerArray);
  -        }
  -        return providerArray;
  -    }
  -
  -    /**
  -     * Gets all the available WSIFProviders. 
  -     * WSIFProviders are located using the J2SE 1.3 JAR file extensions 
  -     * to support service providers.
  -     * @return    an array of WSIFProvider.
  -     */
  -    private static WSIFProvider[] getAllDynamicWSIFProviders() {
  -       if (!autoLoadProviders) {
  -          return new WSIFProvider[0];
  -       }
  -
  -       ArrayList al = (ArrayList) AccessController.doPrivileged(
  -          new PrivilegedAction() {
  -             public Object run() {
  -                return findPlugableProviders();
  -             }
  -          } );
  -          
  -       return (WSIFProvider[]) al.toArray( new WSIFProvider[al.size()] );
  -    }
  -
  -    private static ArrayList findPlugableProviders() {
  -       Object o;
  -       ArrayList classNames = new ArrayList();
  -       ArrayList providers = new ArrayList();
  -
  -       // find all the class names mentioned in all the META-INF files
  -       ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -       try {
  -       	  for(Enumeration e = loader.getResources( PLUGABLE_PROVIDER_FILENAME ); e.hasMoreElements();
) {
  -             classNames.addAll( readMETAINFClassNames( (URL) e.nextElement() ) );
  -       	  } 
  -       } catch (Exception ex) {
  -          Trc.exception( ex );
  -          MessageLogger.log(
  -             "WSIF.0003W",
  -             "an error occured locating META-INF resources: " + ex.getMessage() );
  -          return providers;
  -       }
  -
  -       // instantiate a provider for each of the named classes
  -       for (Iterator i=classNames.iterator(); i.hasNext(); ) {
  -          try {
  -             o = Class.forName( (String) i.next(), true, loader ).newInstance();
  -             if (o instanceof org.apache.wsif.spi.WSIFProvider) {
  -                providers.add( o );
  -             } else {
  -                MessageLogger.log(
  -                   "WSIF.0003W",
  -             	   "The provider class specified," + 
  -                   ( (o == null) ? null : o.getClass().getName() ) +
  -                   ", does not implement org.apache.wsif.spi.WSIFProvider" );
  -             }
  -          } catch (ClassNotFoundException ex) {
  -             MessageLogger.log(
  -                "WSIF.0003W",
  -                "cannot find class - " + ex.getMessage() );
  -          } catch (Exception ex) {
  -             Trc.exception( ex );
  -             MessageLogger.log(
  -                "WSIF.0003W",
  -                "an error occured instantiating class - " + ex.getMessage() );
  -          }
  -       }
  -
  -       return providers;
  -    }
  -
  -    private static ArrayList readMETAINFClassNames(URL u) {
  -        ArrayList classNames = new ArrayList();
  -	    BufferedReader in = null;
  -        String inputLine;
  -        int i;
  -    	try {
  -	       in = new BufferedReader( new InputStreamReader( u.openStream() ) );
  -	       while ((inputLine = in.readLine()) != null) {
  -              i = inputLine.indexOf('#');
  -              if(i >= 0) {
  -                 inputLine = inputLine.substring(0, i);
  -              }
  -              inputLine = inputLine.trim();
  -              if (inputLine.length() > 0 ) {
  -                 classNames.add( inputLine );
  -              }
  -	       }
  -    	} catch (IOException ex) {
  -           Trc.exception( ex );
  -           MessageLogger.log(
  -              "WSIF.0003W",
  -              "an error occured reading META-INF '" + u + "' file: " + ex.getMessage()
);
  -    	} finally {
  -    		if ( in != null ) {
  -    		   try {
  -    		      in.close();
  -    		   } catch (IOException ex) {
  -                  Trc.exception( ex );
  -                  MessageLogger.log(
  -                     "WSIF.0003W",
  -                     "an error occured closing META-INF '" + u + "' file: " + ex.getMessage()
);
  -    		   }
  -    		}
  -    	}
  -    	return classNames; 
  -    }
  -
  -    /**
  -     * This sets if the WSIFProviders will be automatically located and loaded
  -     * using the J2SE 1.3 JAR file extensions to support service providers.
  -     * @param b   true means all the WSIFProviders will be loaded automatically,
  -     *            false means all WSIFProviders must be manually set with the
  -     *            WSIFServiceImpl.setDynamicWSIFProvider method
  -     */
  -    public static void setAutoLoadProviders(boolean b) {
  -        Trc.entry(null, b);
  -        autoLoadProviders = b;
  -        Trc.exit();
  -    }
  -
  -    /**
  -     * Chooses a particular WSIFProvider. If the passed array of providers
  -     * contains more than one element then a choice is made based on a WSIF 
  -     * properties file default setting.     
  -     * @param providers   an array of WSIFProvider 
  -     * @return    a WSIFProvider. Returns null if the input array is null or
  -     *            has a length of zero, the first element if the array contains
  -     *            only one element, or an element based on the property file setting.
  -     */
  -    private static WSIFProvider chooseProvider(
  -        WSIFProvider[] providers,
  -        String uri) {
  -        if (providers == null || providers.length < 1) {
  -            return null;
  -        } else if (providers.length == 1) {
  -            return providers[0];
  -        }
  -
  -        int i = providers.length - 1;
  -        while (i > 0 && !isDefaultProvider(providers[i], uri)) {
  -            i--;
  -        }
  -
  -        issueChosenProviderMsg(uri, providers[i]);
  -
  -        return providers[i];
  -    }
  -
  -    /**
  -     * Tests if a class name is defined in the WSIF properties file as being the
  -     * default WSIF provider for the namespace URI.
  -     * @param className  the class name to test
  -     * @param uri the namespace URI
  -     * @return    true if className is defined as the default WSIFprovider, 
  -     *            otherwise false.
  -     */
  -    private static boolean isDefaultProvider(WSIFProvider provider, String uri) {
  -        String className = provider.getClass().getName();
  -        String defaultURI;
  -        try {
  -            String key = WSIFConstants.WSIF_PROP_PROVIDER_PFX1 + className;
  -            int n = Integer.parseInt(WSIFProperties.getProperty(key));
  -            for (int i = 1; i <= n; i++) {
  -                key = WSIFConstants.WSIF_PROP_PROVIDER_PFX2 + i + "." + className;
  -                defaultURI = WSIFProperties.getProperty(key);
  -                if (uri != null && uri.equals(defaultURI)) {                	
  -                    return true;
  -                }
  -            }
  -        } catch (NumberFormatException e) { // ignore any error
  -        } // ignore any error
  -        return false;
  +        WSIFProvider p = 
  +           WSIFPluggableProviders.getProvider( namespaceURI );
  +        Trc.exit( p );
  +        return p;
       }
   
       /**
  -     * This method is deprecated as the Pluggable Provider support means the correct
  -     * provider should be located automatically.
  -     * It may still be required to use this method to override which provider is chosen,
  -     * for example to use the ApacheSoap provider instead of the default Axis SOAP provider.
  -     * Calling this method with a null provider resets the provider supporting the 
  -     * namespaceURI, so that the next request for for that namespaceURI will return
  -     * to using the default provider. 
  -     * @param providerNamespaceURI    the namespaceURI
  -     * @param provider   the WSIFProvider to be used for the namespaceURI
  -     * @deprecated Plugable providers should make calling this unnecessary
  +     * @deprecated this method is replaced by the overrideDefaultProvider
  +     * method in the org.apache.util.WSIFPluggableProviders class
        */
       public static void setDynamicWSIFProvider(
           String providerNamespaceURI,
           WSIFProvider provider) {
           Trc.entry(null, providerNamespaceURI, provider);
   
  -        if (namespaceProviders == null) {
  -            namespaceProviders = new HashMap();
  -        }
  -
  -        // the namespaceProviders HashMap URIs end with a '/'
  -        if (!providerNamespaceURI.endsWith("/")) {
  -            providerNamespaceURI += "/";
  -        }
  -
  -        if (provider == null) {
  -            namespaceProviders.remove(providerNamespaceURI);
  -        } else {
  -            namespaceProviders.put(providerNamespaceURI, provider);
  -            issueChosenProviderMsg(providerNamespaceURI, provider);
  -        }
  +        WSIFPluggableProviders.overrideDefaultProvider(
  +           providerNamespaceURI, provider );
   
           Trc.exit();
       }
   
       /**
  -     * Issues a MessageLoger warning saying multiple providers 
  -     * exist with support for the same namespaceURI.
  -     * @param uri   the namespaceURI with multiple WSIFProviders 
  -     * @param providers  an array of the providers supporting the namespaceURI 
  +     * @deprecated this method is replaced by the setAutoLoadProviders
  +     * method in the org.apache.util.WSIFPluggableProviders class
        */
  -    private static void issueMultipleProvidersMsg(
  -        String uri,
  -        WSIFProvider[] providers) {
  -        String providerNames = providers[0].getClass().getName();
  -        for (int i = 1; i < providers.length; i++) {
  -            providerNames += ", " + providers[i].getClass().getName();
  -        }
  -        MessageLogger.log("WSIF.0006W",uri, providerNames);
  -    }
  -
  -    /**
  -     * Issues a MessageLoger information message saying which provider has 
  -     * been chosen to support a namespaceURI when multiple providers are available.
  -     * @param uri   the namespaceURI with multiple WSIFProviders 
  -     * @param providers  an array of the providers supporting the namespaceURI 
  -     */
  -    private static void issueChosenProviderMsg(
  -        String uri,
  -        WSIFProvider provider) {
  -
  -        MessageLogger.log(
  -            "WSIF.0007I",
  -            provider == null ? "null" : provider.getClass().getName(),
  -            uri);
  +    public static void setAutoLoadProviders(boolean b) {
  +        Trc.entry(null, b);
  +        WSIFPluggableProviders.setAutoLoadProviders( b );
  +        Trc.exit();
       }
   
       /**
  @@ -1042,31 +724,12 @@
   					// Allow address namespace to be the same as binding
                   }
                   // Check for a provider that supports the 
  -                if (bindingNS == null) {
  -                    continue;
  -                } else {
  -                    WSIFProvider[] pvds = getSupportingProviders(bindingNS, false);
  -                    if (pvds.length == 0)
  -                        continue;
  -                    boolean foundp = false;
  -                    for (int k = 0; k < pvds.length; k++) {
  -                        String[] adrs = pvds[k].getAddressNamespaceURIs();
  -                        for (int l = 0; l < adrs.length; l++) {
  -                        	if (addressNS != null && addressNS.equals(adrs[l])) {
  -                                foundp = true;
  -                                break;
  -                            }
  -                        }
  -                    }
  -                    if (!foundp)
  -                        continue;
  -                }
  -
  -                // check if port has the same port type
  -                if (binding.getPortType().getQName().equals(portType.getQName())) {
  -                    //if (port.getBinding().getPortType() == portType) {
  -                    String portName = port.getName();
  -                    myPortsMap.put(portName, port);
  +                if (WSIFPluggableProviders.isProviderAvailable(bindingNS, addressNS) )
{
  +                   // check if port has the same port type
  +                   if (binding.getPortType().getQName().equals(portType.getQName())) {
  +                      String portName = port.getName();
  +                      myPortsMap.put(portName, port);
  +                   }
                   }
               } catch (NullPointerException e) {
                   // Binding or extensibility element or QName was null
  @@ -1310,11 +973,6 @@
           String buff = "";
           try {
               buff = new String(this.toString() + "\n");
  -            buff += " autoLoadProviders:" + autoLoadProviders;
  -            buff += " namespaceProviders:"
  -                + (namespaceProviders == null ? "null" : namespaceProviders.toString());
  -            buff += " availableProviders:"
  -                + (availableProviders == null ? "null" : availableProviders.toString());
               buff += " providersExtRegs:"
                   + (providersExtRegs == null ? "null" : providersExtRegs.toString());
               buff += " msgFactory:" + (msgFactory == null ? "null" : msgFactory.toString());
  
  
  
  1.12      +1 -2      xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java
  
  Index: WSIFUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- WSIFUtils.java	26 Jul 2002 10:28:35 -0000	1.11
  +++ WSIFUtils.java	26 Jul 2002 14:26:15 -0000	1.12
  @@ -92,7 +92,6 @@
   import org.apache.wsif.WSIFConstants;
   import org.apache.wsif.WSIFException;
   import org.apache.wsif.WSIFService;
  -import org.apache.wsif.base.WSIFServiceImpl;
   import org.apache.wsif.compiler.schema.tools.Schema2Java;
   import org.apache.wsif.format.WSIFFormatHandler;
   import org.apache.wsif.logging.MessageLogger;
  @@ -769,7 +768,7 @@
       private static void initializeProviders() {
           synchronized (providersInitialized) {
               if (!providersInitialized.booleanValue()) {
  -                WSIFServiceImpl.getDynamicWSIFProvider("/");
  +                WSIFPluggableProviders.getProvider("/");
                   providersInitialized = new Boolean(true);
               }
           }
  
  
  
  1.1                  xml-axis-wsif/java/src/org/apache/wsif/util/WSIFPluggableProviders.java
  
  Index: WSIFPluggableProviders.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "WSIF" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 2001, 2002, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.wsif.util;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URL;
  import java.security.AccessController;
  import java.security.PrivilegedAction;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Iterator;
  
  import org.apache.wsif.WSIFConstants;
  import org.apache.wsif.logging.MessageLogger;
  import org.apache.wsif.logging.Trc;
  import org.apache.wsif.spi.WSIFProvider;
  
  /**
   * Utility methods for pluggable provider support.
   *
   * @author Ant Elder <antelder@apache.org>
   */
  public class WSIFPluggableProviders {
  	
  	private static boolean autoLoadProviders = true;
      private static ArrayList providersInClasspath;
      private static HashMap defaultNSProviders;
  
      private static final String PLUGABLE_PROVIDER_FILENAME = 
         "META-INF/services/org.apache.wsif.spi.WSIFProvider";
  
      /**
       * Gets a WSIFProvider for a particular namespace URI.
       * @param namespaceURI  the URI of the namespace the WSIFProvider must support 
       * @return    a WSIFProvider supporting the requested URI,
       *            or null if no providers are available. 
       */
      public static WSIFProvider getProvider(String namespaceURI) {
          Trc.entry(null, namespaceURI);
          WSIFProvider provider;
  
          // the defaultNSProviders Hashtable URIs end with a '/'
          if (!namespaceURI.endsWith("/")) {
              namespaceURI += "/";
          }
  
          if (defaultNSProviders == null) {
              defaultNSProviders = new HashMap();
          } else {
              provider = (WSIFProvider) defaultNSProviders.get(namespaceURI);
              if (provider != null) {
                  return provider;
              }
          }
          ArrayList providers = getSupportingProviders(namespaceURI, true);
          if (providers.size() == 0) {
              return null;
          }
          if (providers.size() == 1) {
              provider = (WSIFProvider)providers.get(0);
          } else {
              provider = chooseProvider(providers, namespaceURI);
          }
          defaultNSProviders.put(namespaceURI, provider);
          Trc.exit(provider);
          return provider;
      }
  
      /**
       * This method is deprecated as the Pluggable Provider support means the correct
       * provider should be located automatically.
       * It may still be required to use this method to override which provider is chosen,
       * for example to use the ApacheSoap provider instead of the default Axis SOAP provider.
       * Calling this method with a null provider resets the provider supporting the 
       * namespaceURI, so that the next request for for that namespaceURI will return
       * to using the default provider. 
       * @param providerNamespaceURI    the namespaceURI
       * @param provider   the WSIFProvider to be used for the namespaceURI
       */
      public static void overrideDefaultProvider(
          String providerNamespaceURI,
          WSIFProvider provider) {
          Trc.entry(null, providerNamespaceURI, provider);
  
          if (defaultNSProviders == null) {
              defaultNSProviders = new HashMap();
          }
  
          // the defaultNSProviders HashMap URIs end with a '/'
          if (!providerNamespaceURI.endsWith("/")) {
              providerNamespaceURI += "/";
          }
  
          if (provider == null) {
              WSIFProvider p = (WSIFProvider) defaultNSProviders.get(providerNamespaceURI);
              if ( providersInClasspath != null ) {
                 providersInClasspath.remove( p );
              }
              if ( defaultNSProviders != null ) {
                 defaultNSProviders.remove(providerNamespaceURI);
              }
          } else {
              defaultNSProviders.put(providerNamespaceURI, provider);
              issueChosenProviderMsg(providerNamespaceURI, provider);
          }
  
          Trc.exit();
      }
  
      /**
       * Tests if a provider is available for the given namespaces.
       * @param ns1   the WSDL binding namespace URI
       * @param ns2   the WSDL port addresses namespace URI
       * @return   true if a provider is available for the given
       *            namespaces, otherwise false. 
       */
      public static boolean isProviderAvailable(String ns1) {
      	return isProviderAvailable( ns1, ns1 );
      }
      
      public static boolean isProviderAvailable(String ns1, String ns2) {
      	boolean supported = false;
          ArrayList ps = getSupportingProviders( ns1, false );
          if ( ps.size() > 0 ) {
             if ( ns2 == null || ns2.length() < 1 || ns2.equals( ns1 ) ) {
                supported = true;
             } else {
             	  String[] supportedNS;
             	  for (int i=0; i<ps.size() && !supported; i++) {
                   supportedNS = ((WSIFProvider)ps.get(i)).getAddressNamespaceURIs();
             	     for (int j=0; j<supportedNS.length && !supported; j++) {
                      if ( ns2.equals( supportedNS[j] )) {
                         supported = true;
                      }
             	     }
             	  }
             }
          }
          return supported;
      }
  
      /**
       * This sets if the WSIFProviders will be automatically located and loaded
       * using the J2SE 1.3 JAR file extensions to support service providers.
       * Changing the state of the auto loading of providers clears any providers
       * that have already been loaded or choosen as a default provider.  
       * @param b   true means all the WSIFProviders will be loaded automatically,
       *            false means all WSIFProviders must be manually set with the
       *            setDynamicWSIFProvider method
       */
      public static void setAutoLoadProviders(boolean b) {
          Trc.entry(null, b);
          if ( autoLoadProviders != b ) {
             providersInClasspath = null;
             defaultNSProviders = null;
          }
          autoLoadProviders = b;
          Trc.exit();
      }
  
      public static boolean isAutoLoadProviders() {
          Trc.entry(null);
          Trc.exit(autoLoadProviders);
          return autoLoadProviders;
      }
  
      /**
       * Gets all the available WSIFProvider that support a particular 
       * namespace URI.
       * @param namesapceURI   the namespace the WSIFProvider must support.
       * @param issueMessage Flag to inicate whether or not to log when multiple
       * providers are found for the same namespace. 
       * @return    an array of WSIFProvider. The array will have a length
       * of zero if no WSIFProvider are available for the requested namespace.
       */
      private static ArrayList getSupportingProviders(
          String namespaceURI,
          boolean issueMessage) {
          if (providersInClasspath == null) {
              providersInClasspath = getAllDynamicWSIFProviders();
          }
          ArrayList supportingProviders = new ArrayList();
          String[] uris;
          WSIFProvider p;
          for (Iterator i = providersInClasspath.iterator(); i.hasNext(); ) {
          	p = (WSIFProvider) i.next();
              uris = p.getBindingNamespaceURIs();
              for (int j = 0; j < uris.length; j++) {
              	if (namespaceURI != null && namespaceURI.equals(uris[j])) {
                      supportingProviders.add( p );
                  }
              }
          }
          if (defaultNSProviders != null) {
              if (defaultNSProviders.get(namespaceURI) != null) {
                  supportingProviders.add(defaultNSProviders.get(namespaceURI));
              }
          }       
  
          if (supportingProviders.size() > 1 && issueMessage) {
              issueMultipleProvidersMsg(namespaceURI, supportingProviders);
          }
          
          return supportingProviders;
      }
  
      /**
       * Gets all the available WSIFProviders. 
       * WSIFProviders are located using the J2SE 1.3 JAR file extensions 
       * to support service providers.
       * @return    an array of WSIFProvider.
       */
      private static ArrayList getAllDynamicWSIFProviders() {
         if (!autoLoadProviders) {
            return new ArrayList();
         }
  
         ArrayList al = (ArrayList) AccessController.doPrivileged(
            new PrivilegedAction() {
               public Object run() {
                  return findPlugableProviders();
               }
            } );
            
         return al;
      }
  
      private static ArrayList findPlugableProviders() {
         Object o;
         ArrayList classNames = new ArrayList();
         ArrayList providers = new ArrayList();
  
         // find all the class names mentioned in all the META-INF files
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         try {
         	  for(Enumeration e = loader.getResources( PLUGABLE_PROVIDER_FILENAME ); e.hasMoreElements();
) {
               classNames.addAll( readMETAINFClassNames( (URL) e.nextElement() ) );
         	  } 
         } catch (Exception ex) {
            Trc.exception( ex );
            MessageLogger.log(
               "WSIF.0003W",
               "an error occured locating META-INF resources: " + ex.getMessage() );
            return providers;
         }
  
         // instantiate a provider for each of the named classes
         for (Iterator i=classNames.iterator(); i.hasNext(); ) {
            try {
               o = Class.forName( (String) i.next(), true, loader ).newInstance();
               if (o instanceof org.apache.wsif.spi.WSIFProvider) {
                  providers.add( o );
               } else {
                  MessageLogger.log(
                     "WSIF.0003W",
               	   "The provider class specified," + 
                     ( (o == null) ? null : o.getClass().getName() ) +
                     ", does not implement org.apache.wsif.spi.WSIFProvider" );
               }
            } catch (ClassNotFoundException ex) {
               MessageLogger.log(
                  "WSIF.0003W",
                  "cannot find class - " + ex.getMessage() );
            } catch (Exception ex) {
               Trc.exception( ex );
               MessageLogger.log(
                  "WSIF.0003W",
                  "an error occured instantiating class - " + ex.getMessage() );
            }
         }
  
         return providers;
      }
  
      private static ArrayList readMETAINFClassNames(URL u) {
          ArrayList classNames = new ArrayList();
  	    BufferedReader in = null;
          String inputLine;
          int i;
      	try {
  	       in = new BufferedReader( new InputStreamReader( u.openStream() ) );
  	       while ((inputLine = in.readLine()) != null) {
                i = inputLine.indexOf('#');
                if(i >= 0) {
                   inputLine = inputLine.substring(0, i);
                }
                inputLine = inputLine.trim();
                if (inputLine.length() > 0 ) {
                   classNames.add( inputLine );
                }
  	       }
      	} catch (IOException ex) {
             Trc.exception( ex );
             MessageLogger.log(
                "WSIF.0003W",
                "an error occured reading META-INF '" + u + "' file: " + ex.getMessage() );
      	} finally {
      		if ( in != null ) {
      		   try {
      		      in.close();
      		   } catch (IOException ex) {
                    Trc.exception( ex );
                    MessageLogger.log(
                       "WSIF.0003W",
                       "an error occured closing META-INF '" + u + "' file: " + ex.getMessage()
);
      		   }
      		}
      	}
      	return classNames; 
      }
  
      /**
       * Chooses a particular WSIFProvider. If the passed array of providers
       * contains more than one element then a choice is made based on a WSIF 
       * properties file default setting.     
       * @param providers   an array of WSIFProvider 
       * @return    a WSIFProvider. Returns null if the input array is null or
       *            has a length of zero, the first element if the array contains
       *            only one element, or an element based on the property file setting.
       */
      private static WSIFProvider chooseProvider(
          ArrayList providers,
          String uri) {
          if (providers == null || providers.size() < 1) {
              return null;
          } else if (providers.size() == 1) {
              return (WSIFProvider)providers.get(0);
          }
  
          int i = providers.size() - 1;
          while (i > 0 && !isDefaultProvider((WSIFProvider)providers.get(i), uri))
{
              i--;
          }
          WSIFProvider p = (WSIFProvider) providers.get(i);
          issueChosenProviderMsg( uri, p );
  
          return p;
      }
  
      /**
       * Tests if a class name is defined in the WSIF properties file as being the
       * default WSIF provider for the namespace URI.
       * @param className  the class name to test
       * @param uri the namespace URI
       * @return    true if className is defined as the default WSIFprovider, 
       *            otherwise false.
       */
      private static boolean isDefaultProvider(WSIFProvider provider, String uri) {
          String className = provider.getClass().getName();
          String defaultURI;
          try {
              String key = WSIFConstants.WSIF_PROP_PROVIDER_PFX1 + className;
              int n = Integer.parseInt(WSIFProperties.getProperty(key));
              for (int i = 1; i <= n; i++) {
                  key = WSIFConstants.WSIF_PROP_PROVIDER_PFX2 + i + "." + className;
                  defaultURI = WSIFProperties.getProperty(key);
                  if (uri != null && uri.equals(defaultURI)) {                	
                      return true;
                  }
              }
          } catch (NumberFormatException e) { // ignore any error
          } // ignore any error
          return false;
      }
  
      /**
       * Issues a MessageLoger warning saying multiple providers 
       * exist with support for the same namespaceURI.
       * @param uri   the namespaceURI with multiple WSIFProviders 
       * @param providers  an array of the providers supporting the namespaceURI 
       */
      private static void issueMultipleProvidersMsg(
          String uri,
          ArrayList providers) {
          String providerNames = providers.get(0).getClass().getName();
          for (int i = 1; i < providers.size(); i++) {
              providerNames += ", " + providers.get(i).getClass().getName();
          }
          MessageLogger.log("WSIF.0006W",uri, providerNames);
      }
  
      /**
       * Issues a MessageLoger information message saying which provider has 
       * been chosen to support a namespaceURI when multiple providers are available.
       * @param uri   the namespaceURI with multiple WSIFProviders 
       * @param providers  an array of the providers supporting the namespaceURI 
       */
      private static void issueChosenProviderMsg(
          String uri,
          WSIFProvider provider) {
  
          MessageLogger.log(
              "WSIF.0007I",
              provider == null ? "null" : provider.getClass().getName(),
              uri);
      }
  
  }
  
  
  
  1.4       +100 -50   xml-axis-wsif/java/test/providers/PlugableProvidersTest.java
  
  Index: PlugableProvidersTest.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/test/providers/PlugableProvidersTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PlugableProvidersTest.java	25 Jul 2002 16:41:44 -0000	1.3
  +++ PlugableProvidersTest.java	26 Jul 2002 14:26:15 -0000	1.4
  @@ -65,6 +65,7 @@
   import org.apache.wsif.base.WSIFServiceImpl;
   import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
   import org.apache.wsif.spi.WSIFProvider;
  +import org.apache.wsif.util.WSIFPluggableProviders;
   
   /**
    * Junit test to test the plugable providers facility for WSIFDynamicProviders
  @@ -72,64 +73,113 @@
   public class PlugableProvidersTest extends TestCase {
   
       public PlugableProvidersTest(String name) {
  -        super(name);
  +    	 super(name);
       }
  +        
  +	public static void main(String[] args) {
  +		junit.textui.TestRunner.run (suite());
  +	}
  +		
  +	public static Test suite() {
  +		return new TestSuite(PlugableProvidersTest.class);
  +	}
  +	  
  +    public void testEJBProvider() { 
  +    	assertTrue( doit("http://schemas.xmlsoap.org/wsdl/ejb/",
  +    	     "org.apache.wsif.providers.ejb.WSIFDynamicProvider_EJB") );
  +    }
  +        	     
  +    public void testJavaProvider() { 
  +    	assertTrue( doit("http://schemas.xmlsoap.org/wsdl/java/",
  +    	     "org.apache.wsif.providers.java.WSIFDynamicProvider_Java") );
  +    }
  +    	     
  +    public void testDefaultSoapProvider() { 
  +     	assertTrue( doit("http://schemas.xmlsoap.org/wsdl/soap/",
  +    	     "org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP") );
  +    }
  +    	     
  +
  +    public void testSetDefaultSoapProvider() { 
  +       WSIFServiceImpl.setDynamicWSIFProvider( "http://schemas.xmlsoap.org/wsdl/soap/",
  +          new WSIFDynamicProvider_ApacheAxis()
  +       );
  +       assertTrue( doit("http://schemas.xmlsoap.org/wsdl/soap/",
  +    	     "org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis") );
  +       // set null so uses default next time
  +       WSIFServiceImpl.setDynamicWSIFProvider( 
  +          "http://schemas.xmlsoap.org/wsdl/soap/", null );
   
  -    public static void main(String[] args) {
  -        TestRunner.run(suite());
       }
  -
  -    public static Test suite() {
  -        return new TestSuite(PlugableProvidersTest.class);
  -    }
  -
  -    public void testEJBProvider() {
  -        assertTrue(
  -            doit(
  -                "http://schemas.xmlsoap.org/wsdl/ejb/",
  -                "org.apache.wsif.providers.ejb.WSIFDynamicProvider_EJB"));
  -    }
  -
  -    public void testJavaProvider() {
  -        assertTrue(
  -            doit(
  -                "http://schemas.xmlsoap.org/wsdl/java/",
  -                "org.apache.wsif.providers.java.WSIFDynamicProvider_Java"));
  -    }
  -
  -    public void testDefaultSoapProvider() {
  -        assertTrue(
  -            doit(
  -                "http://schemas.xmlsoap.org/wsdl/soap/",
  -                "org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP"));
  -    }
  -
  -    public void testSetDefaultSoapProvider() {
  -        WSIFServiceImpl.setDynamicWSIFProvider(
  -            "http://schemas.xmlsoap.org/wsdl/soap/",
  -            new WSIFDynamicProvider_ApacheAxis());
  -        assertTrue(
  -            doit(
  -                "http://schemas.xmlsoap.org/wsdl/soap/",
  -                "org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis"));
  -        // set null so uses default next time
  -        WSIFServiceImpl.setDynamicWSIFProvider(
  -            "http://schemas.xmlsoap.org/wsdl/soap/",
  -            null);
  -
  -    }
  -
  +   	     
       //public void testSoapRMIProvider() { 
       //	assertTrue( doit("http://schemas.xmlsoap.org/wsdl/soap/xxx",
       //	     "org.apache.wsif.providers.soap.soaprmi.WSIFDynamicProvider_SoapRMI") );
       //}
  +    	     
  +    public void testPPMethods() { 
  +    
  +       // test isProviderAvailable method
  +       assertTrue( "isProviderAvailable 1", 
  +          WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/soap/",
  +             "http://schemas.xmlsoap.org/wsdl/soap/") );
  +       assertTrue( "isProviderAvailable 2", 
  +          WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/soap/",
  +             "http://schemas.xmlsoap.org/wsdl/jms/") );
  +       assertTrue( "isProviderAvailable 3", 
  +          !WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/soap/",
  +             "http://schemas.xmlsoap.org/wsdl/xxx/") );
  +       assertTrue( "isProviderAvailable 4", 
  +          !WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/xxx/",
  +             "http://schemas.xmlsoap.org/wsdl/jms/") );
  +       assertTrue( "isProviderAvailable 5", 
  +          !WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/xxx/",
  +             "http://schemas.xmlsoap.org/wsdl/xxx/") );
  +       assertTrue( "isProviderAvailable 6", 
  +          WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/java/") );
  +       assertTrue( "isProviderAvailable 7", 
  +          !WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/xxx/") );
  +
  +       // test provider auto loading
  +       WSIFPluggableProviders.setAutoLoadProviders( false );
  +       assertTrue( "AutoLoadProviders 1", 
  +          !WSIFPluggableProviders.isAutoLoadProviders() );
  +       assertTrue( "AutoLoadProviders 2", 
  +          !WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/soap/",
  +             "http://schemas.xmlsoap.org/wsdl/soap/") );
  +       WSIFPluggableProviders.overrideDefaultProvider( 
  +          "http://schemas.xmlsoap.org/wsdl/soap/",
  +          new WSIFDynamicProvider_ApacheAxis() );
  +       assertTrue( "AutoLoadProviders 3", 
  +          WSIFPluggableProviders.isProviderAvailable( 
  +             "http://schemas.xmlsoap.org/wsdl/soap/",
  +             "http://schemas.xmlsoap.org/wsdl/soap/") );
  +       WSIFPluggableProviders.setAutoLoadProviders( true );
  +       assertTrue( "AutoLoadProviders 1", 
  +          WSIFPluggableProviders.isAutoLoadProviders() );
  +       assertTrue( doit(
  +          "http://schemas.xmlsoap.org/wsdl/soap/",
  +          "org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP") );
  +
  +    }
  +        	     
  +    private boolean doit (String namespaceURI, String providerClassName) {  
  +       WSIFProvider provider = WSIFPluggableProviders.getProvider( namespaceURI );
  +       String pn = provider.getClass().getName();
  +       return pn.equals( providerClassName );
  +    }
  +    
  +}
  +
   
  -    private boolean doit(String namespaceURI, String providerClassName) {
   
  -        WSIFProvider provider = WSIFServiceImpl.getDynamicWSIFProvider(namespaceURI);
  -        String pn = provider.getClass().getName();
  -        return pn.equals(providerClassName);
   
  -    }
   
  -}
  
  
  

Mime
View raw message