You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2003/09/01 17:16:39 UTC

cvs commit: incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/net/protocol URLStreamHandlerFactory.java Protocols.java

jdillon     2003/09/01 08:16:39

  Modified:    modules/common/src/java/org/apache/geronimo/common/net/protocol
                        Protocols.java
  Added:       modules/common/src/java/org/apache/geronimo/common/net/protocol
                        URLStreamHandlerFactory.java
  Log:
   o Added methods to handle lookup of URLStreamHandler classes & instances
   o Added URLStreamHandlerFactory delegate to Protocols
  
  Revision  Changes    Path
  1.2       +78 -1     incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/net/protocol/Protocols.java
  
  Index: Protocols.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/net/protocol/Protocols.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Protocols.java	30 Aug 2003 11:57:14 -0000	1.1
  +++ Protocols.java	1 Sep 2003 15:16:39 -0000	1.2
  @@ -57,13 +57,20 @@
   package org.apache.geronimo.common.net.protocol;
   
   import java.net.URL;
  +import java.net.URLStreamHandler;
   
   import java.util.List;
   import java.util.LinkedList;
  +import java.util.Iterator;
   import java.util.StringTokenizer;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +
   import org.apache.geronimo.common.NullArgumentException;
   import org.apache.geronimo.common.Strings;
  +import org.apache.geronimo.common.Classes;
  +import org.apache.geronimo.common.ThrowableHandler;
   
   /**
    * Protocol utilties.
  @@ -72,6 +79,8 @@
    */
   public class Protocols
   {
  +    private static final Log log = LogFactory.getLog(Protocols.class);
  +    
       public static final String HANDLER_PACKAGES = "java.protocol.handler.pkgs";
       public static final String SYSTEM_HANDLER_PACKAGES = System.getProperty(HANDLER_PACKAGES);
       
  @@ -132,5 +141,73 @@
           List list = getHandlerPackages();
           list.add(0, name);
           setHandlerPackages(list);
  +    }
  +    
  +    public static Class getURLStreamHandlerType(final String protocol)
  +    {
  +        if (protocol == null) {
  +            throw new NullArgumentException("protocol");
  +        }
  +        
  +        Iterator iter = getHandlerPackages().iterator();
  +        while (iter.hasNext()) {
  +            String pkg = (String)iter.next();
  +            String classname = pkg + "." + protocol + ".Handler";
  +            
  +            try {
  +                return Classes.loadClass(classname);
  +            }
  +            catch (ClassNotFoundException e) {
  +                ThrowableHandler.add(e);
  +            }
  +        }
  +        
  +        return null;
  +    }
  +    
  +    public static URLStreamHandler createURLStreamHandler(final String protocol)
  +    {
  +        // protocol checked by getURLStreamHandlerType
  +        
  +        Class type = getURLStreamHandlerType(protocol);
  +        
  +        if (type != null) {
  +            try {
  +                Object obj = type.newInstance();
  +                if (obj instanceof URLStreamHandler) {
  +                    return (URLStreamHandler)obj;
  +                }
  +                else {
  +                    // Handler is not instance of URLStreamHandler; ignoring
  +                }
  +            }
  +            catch (Exception e) {
  +                ThrowableHandler.add(e);
  +            }
  +        }
  +        
  +        return null;
  +    }
  +    
  +    /**
  +     * Preload the specific protocol handlers, so that URL knows about
  +     * them even if the handler factory is changed.
  +     */
  +    public static void preloadURLStreamHandlers(final String[] protocols)
  +    {
  +        if (protocols == null) {
  +            throw new NullArgumentException("protocols");
  +        }
  +        
  +        for (int i=0; i<protocols.length; i++) {
  +            if (protocols[i] == null) {
  +                throw new NullArgumentException("protocols", i);
  +            }
  +            
  +            try {
  +                URL url = new URL(protocols[i], "", -1, "");
  +            }
  +            catch (Exception ignore) {}
  +        }
       }
   }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/net/protocol/URLStreamHandlerFactory.java
  
  Index: URLStreamHandlerFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" 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",
   *    "Apache Geronimo", 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.common.net.protocol;
  
  import java.net.URLStreamHandler;
  
  /**
   * A factory for loading URL protocol handlers.
   *
   * <p>Delegates to {@link Protocols}.
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 15:16:39 $
   */
  public class URLStreamHandlerFactory
      implements java.net.URLStreamHandlerFactory
  {
      public URLStreamHandler createURLStreamHandler(final String protocol)
      {
          // protocol is checked by Protocols for null
          
          return Protocols.createURLStreamHandler(protocol);
      }
  }