You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ow...@apache.org on 2002/08/08 17:24:31 UTC

cvs commit: xml-axis-wsif/java/src/org/apache/wsif WSIFServiceFactory.java

owenb       2002/08/08 08:24:31

  Modified:    java/src/org/apache/wsif/base WSIFServiceImpl.java
                        WSIFServiceFactoryImpl.java
               java/src/org/apache/wsif WSIFServiceFactory.java
  Log:
  Added initial support for caching WSIFServices in the service factory
  
  Revision  Changes    Path
  1.23      +21 -0     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.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- WSIFServiceImpl.java	7 Aug 2002 15:10:25 -0000	1.22
  +++ WSIFServiceImpl.java	8 Aug 2002 15:24:31 -0000	1.23
  @@ -300,6 +300,27 @@
       }
   
       /**
  +      * Create a WSIF service instance from another instance. This constructor
  +      * is used by the caching mechanism in WSIFServiceFactoryImpl
  +      */
  +    WSIFServiceImpl(WSIFServiceImpl wsi)
  +        throws WSIFException {
  +        Trc.entry(this, wsi);
  +        copyInitializedService(wsi);
  +        if (Trc.ON)
  +            Trc.exit(deep());
  +    }
  +
  +	private void copyInitializedService(WSIFServiceImpl svc) {
  +		this.def = svc.def;
  +        this.service = svc.service;
  +        this.portType = svc.portType;
  +        this.myPortsArr = svc.myPortsArr;
  +        this.myPortsMap = svc.myPortsMap;
  +        this.typeMap = svc.typeMap;
  +	}
  +
  +    /**
        * Set the preferred port
        * @param portName The name of the port to use
        */
  
  
  
  1.3       +300 -22   xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceFactoryImpl.java
  
  Index: WSIFServiceFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceFactoryImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- WSIFServiceFactoryImpl.java	19 Jul 2002 15:46:29 -0000	1.2
  +++ WSIFServiceFactoryImpl.java	8 Aug 2002 15:24:31 -0000	1.3
  @@ -57,15 +57,18 @@
   
   package org.apache.wsif.base;
   
  +import java.util.HashMap;
  +import java.util.Map;
  +
   import javax.wsdl.Definition;
   import javax.wsdl.PortType;
   import javax.wsdl.Service;
  +import javax.xml.namespace.QName;
   
   import org.apache.wsif.WSIFException;
   import org.apache.wsif.WSIFService;
   import org.apache.wsif.WSIFServiceFactory;
   import org.apache.wsif.logging.Trc;
  -import org.apache.wsif.util.WSIFUtils;
   
   /**
    * Factory class to create WSIFService's.
  @@ -74,6 +77,9 @@
    */
   public class WSIFServiceFactoryImpl extends WSIFServiceFactory{
   
  +    private boolean useCache = false;
  +    private Map cache = new HashMap();
  +
       /**
        * Create a WSIFService from WSDL document URL.
        * <br> If serviceName or serviceNS is null,
  @@ -82,17 +88,57 @@
        *   then WSDL document must have exactly one portType in it
        *   and all ports of the selected service must
        *    implement the same portType.
  +     * @param wsdlLoc The URL for the wsdl's location
  +     * @param serviceNS The namespace of the service
  +     * @param serviceName The name of the service
  +     * @param portTypeNS The namespace of the port type
  +     * @param portTypeName The name of the port type
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(
  -            String wsdlLoc,
  -            String serviceNS,
  -            String serviceName,
  -            String portTypeNS,
  -            String portTypeName)
  +        String wsdlLoc,
  +        String serviceNS,
  +        String serviceName,
  +        String portTypeNS,
  +        String portTypeName)
               throws WSIFException {
  -        Trc.entry(this, wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
  +        Trc.entry(
  +            this,
  +            wsdlLoc,
  +            serviceNS,
  +            serviceName,
  +            portTypeNS,
  +            portTypeName);
  +
  +        String key = "";
  +        if (useCache) {
  +            key =
  +                genCacheKey(
  +                    wsdlLoc,
  +                    serviceNS,
  +                    serviceName,
  +                    portTypeNS,
  +                    portTypeName);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi =
  -            new WSIFServiceImpl(wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
  +            new WSIFServiceImpl(
  +                wsdlLoc,
  +                serviceNS,
  +                serviceName,
  +                portTypeNS,
  +                portTypeName);
  +
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
   
           Trc.exit(wsi);
           return wsi;
  @@ -107,16 +153,49 @@
        *   then WSDL document must have exactly one portType in it
        *   and all ports of the selected service must
        *    implement the same portType.
  +     * @param wsdlLoc The URL for the wsdl's location
  +     * @param cl A ClassLoader to use in locating the wsdl
  +     * @param serviceNS The namespace of the service
  +     * @param serviceName The name of the service
  +     * @param portTypeNS The namespace of the port type
  +     * @param portTypeName The name of the port type
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(
  -            String wsdlLoc,
  -            ClassLoader cl,
  -            String serviceNS,
  -            String serviceName,
  -            String portTypeNS,
  -            String portTypeName)
  +        String wsdlLoc,
  +        ClassLoader cl,
  +        String serviceNS,
  +        String serviceName,
  +        String portTypeNS,
  +        String portTypeName)
               throws WSIFException {
  -        Trc.entry(this, wsdlLoc, cl, serviceNS, serviceName, portTypeNS, portTypeName);
  +        Trc.entry(
  +            this,
  +            wsdlLoc,
  +            cl,
  +            serviceNS,
  +            serviceName,
  +            portTypeNS,
  +            portTypeName);
  +
  +        String key = "";
  +        if (useCache) {
  +            key =
  +                genCacheKey(
  +                    wsdlLoc,
  +                    serviceNS,
  +                    serviceName,
  +                    portTypeNS,
  +                    portTypeName);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi =
               new WSIFServiceImpl(
                   wsdlLoc,
  @@ -126,59 +205,258 @@
                   portTypeNS,
                   portTypeName);
   
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
  +
           Trc.exit(wsi);
           return wsi;
       }
   
       /**
        * Returns a new WSIFService.
  +     * @param def The Definition object representing the wsdl
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(Definition def) throws WSIFException {
           Trc.entry(this, def);
  +        String key = "";
  +        if (useCache) {
  +            key = genCacheKey(def, null, null);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi = new WSIFServiceImpl(def);
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
  +
           Trc.exit(wsi);
           return wsi;
       }
   
       /**
        * Returns a new WSIFService.
  +     * @param def The Definition object representing the wsdl
  +     * @param service The Service object representing the service to use
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(Definition def, Service service)
               throws WSIFException {
           Trc.entry(this, def, service);
  +        String key = "";
  +        if (useCache) {
  +            key = genCacheKey(def, service, null);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi = new WSIFServiceImpl(def, service);
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
           Trc.exit(wsi);
           return wsi;
       }
   
       /**
        * Returns a new WSIFService.
  +     * @param def The Definition object representing the wsdl
  +     * @param service The Service object representing the service to use
  +     * @param portType The PortType object representing the port type to use
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(
           Definition def,
           Service service,
           PortType portType)
  -        throws WSIFException {
  +            throws WSIFException {
           Trc.entry(this, def, service, portType);
  +        String key = "";
  +        if (useCache) {
  +            key = genCacheKey(def, service, portType);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi = new WSIFServiceImpl(def, service, portType);
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
  +
           Trc.exit(wsi);
           return wsi;
       }
   
       /**
        * Returns a new WSIFService.
  +     * @param def The Definition object representing the wsdl
  +     * @param serviceNS The namespace of the service
  +     * @param serviceName The name of the service
  +     * @param portTypeNS The namespace of the port type
  +     * @param portTypeName The name of the port type
  +     * @return The service
  +     * @exception A WSIFException if an error occurs when creating the service
        */
       public WSIFService getService(
  -            Definition def,
  -            String serviceNS,
  -            String serviceName,
  -            String portTypeNS,
  -            String portTypeName)
  +        Definition def,
  +        String serviceNS,
  +        String serviceName,
  +        String portTypeNS,
  +        String portTypeName)
               throws WSIFException {
           Trc.entry(this, def, serviceNS, serviceName, portTypeNS, portTypeName);
  +        String key = "";
  +        if (useCache) {
  +            key =
  +                genCacheKey(
  +                    def,
  +                    serviceNS,
  +                    serviceName,
  +                    portTypeNS,
  +                    portTypeName);
  +            WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
  +            if (cachedWSI != null) {
  +                WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
  +                Trc.exit(wsi);                
  +                return wsi;
  +            }
  +        }
  +
           WSIFServiceImpl wsi =
  -            new WSIFServiceImpl(def, serviceNS, serviceName, portTypeNS, portTypeName);
  +            new WSIFServiceImpl(
  +                def,
  +                serviceNS,
  +                serviceName,
  +                portTypeNS,
  +                portTypeName);
  +        if (useCache && !key.equals("")) {
  +            cache.put(key, wsi);
  +        }
  +
           Trc.exit(wsi);
           return wsi;
       }
  +
  +    /**
  +     * Set caching of servies on/off. The default is off. If caching is on then
  +     * a call to getService will first check if a service matching the parameters
  +     * specified has already been created and if so a reference to that instance
  +     * of WSIFService is returned.
  +     * @param on Flag to indicate whether or not caching of services should be used
  +     */
  +    public void cachingOn(boolean on) {
  +        useCache = on;
  +    }
  +
  +    private String genCacheKey(
  +        Definition def,
  +        Service service,
  +        PortType portType) {
  +
  +        String db =
  +            (def.getDocumentBaseURI() == null) ? def.getDocumentBaseURI() : "null";
  +        QName serviceName = (def != null) ? service.getQName() : null;
  +        String sn = (serviceName != null) ? serviceName.toString() : "null";
  +        QName portTypeName = (def != null) ? portType.getQName() : null;
  +        String ptn = (portTypeName != null) ? portTypeName.toString() : "null";
  +        StringBuffer key = new StringBuffer();
  +        key.append("D=");
  +        key.append(db);
  +        key.append("S=");
  +        key.append(sn);
  +        key.append("P=");
  +        key.append(ptn);
  +
  +        String ret = key.toString();
  +        // If no distinguishable information is available then don't add to cache
  +        if (ret.equals("D=nullS=nullP=null")) return "";        
  +        return ret;
  +    }
  +
  +    private String genCacheKey(
  +        String wsdlLoc,
  +        String serviceNS,
  +        String serviceName,
  +        String portTypeNS,
  +        String portTypeName) {
  +
  +        StringBuffer key = new StringBuffer();
  +        if (wsdlLoc == null)
  +            wsdlLoc = "";
  +        if (serviceNS == null)
  +            serviceNS = "";
  +        if (serviceName == null)
  +            serviceName = "";
  +        if (portTypeNS == null)
  +            portTypeNS = "";
  +        if (portTypeName == null)
  +            portTypeName = "";
  +        key.append("W=");
  +        key.append(wsdlLoc);
  +        key.append("SN=");
  +        key.append(serviceNS);
  +        key.append("SS=");
  +        key.append(serviceName);
  +        key.append("PN=");
  +        key.append(portTypeNS);
  +        key.append("PS=");
  +        key.append(portTypeName);
  +        
  +        String ret = key.toString();
  +        // If no distinguishable information is available then don't add to cache
  +        if (ret.equals("W=SN=SS=PN=PS=")) return "";         
  +        return ret;
  +    }
  +
  +    private String genCacheKey(
  +        Definition def,
  +        String serviceNS,
  +        String serviceName,
  +        String portTypeNS,
  +        String portTypeName) {
  +
  +        StringBuffer key = new StringBuffer();
  +        String db =
  +            (def.getDocumentBaseURI() == null) ? def.getDocumentBaseURI() : "null";
  +        if (serviceNS == null)
  +            serviceNS = "";
  +        if (serviceName == null)
  +            serviceName = "";
  +        if (portTypeNS == null)
  +            portTypeNS = "";
  +        if (portTypeName == null)
  +            portTypeName = "";
  +        key.append("D=");
  +        key.append(db);
  +        key.append("SN=");
  +        key.append(serviceNS);
  +        key.append("SS=");
  +        key.append(serviceName);
  +        key.append("PN=");
  +        key.append(portTypeNS);
  +        key.append("PS=");
  +        key.append(portTypeName);
  +        
  +        String ret = key.toString();
  +        // If no distinguishable information is available then don't add to cache
  +        if (ret.equals("D=nullSN=SS=PN=PS=")) return "";
  +        return ret;
  +    }    
   }
  
  
  
  1.6       +7 -0      xml-axis-wsif/java/src/org/apache/wsif/WSIFServiceFactory.java
  
  Index: WSIFServiceFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/WSIFServiceFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WSIFServiceFactory.java	19 Jul 2002 15:46:32 -0000	1.5
  +++ WSIFServiceFactory.java	8 Aug 2002 15:24:31 -0000	1.6
  @@ -156,4 +156,11 @@
           String portTypeNS,
           String portTypeName)
           throws WSIFException;
  +     
  +    /**
  +     * Set caching on services on/off. Off is the default
  +     * @param on Flag to indicate whether or not caching of services should be used
  +     */    
  +    public void cachingOn(boolean on) {
  +    }        
   }