geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/net/protocol Protocols.java
Date Wed, 03 Sep 2003 09:25:32 GMT
jdillon     2003/09/03 02:25:32

  Modified:    modules/common/src/java/org/apache/geronimo/common/net/protocol
                        Protocols.java
  Log:
   o Added registerURLStreamHandler() to attempt to put a handler instance
     into URL's handler map cache
  
  Revision  Changes    Path
  1.3       +49 -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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Protocols.java	1 Sep 2003 15:16:39 -0000	1.2
  +++ Protocols.java	3 Sep 2003 09:25:32 -0000	1.3
  @@ -63,6 +63,11 @@
   import java.util.LinkedList;
   import java.util.Iterator;
   import java.util.StringTokenizer;
  +import java.util.Map;
  +
  +import java.lang.reflect.Field;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  @@ -81,6 +86,11 @@
   {
       private static final Log log = LogFactory.getLog(Protocols.class);
       
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    //                     Protocol Handler Packages                       //
  +    /////////////////////////////////////////////////////////////////////////
  +    
       public static final String HANDLER_PACKAGES = "java.protocol.handler.pkgs";
       public static final String SYSTEM_HANDLER_PACKAGES = System.getProperty(HANDLER_PACKAGES);
       
  @@ -143,6 +153,11 @@
           setHandlerPackages(list);
       }
       
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    //                        URL Stream Handlers                          //
  +    /////////////////////////////////////////////////////////////////////////
  +    
       public static Class getURLStreamHandlerType(final String protocol)
       {
           if (protocol == null) {
  @@ -208,6 +223,39 @@
                   URL url = new URL(protocols[i], "", -1, "");
               }
               catch (Exception ignore) {}
  +        }
  +    }
  +    
  +    public static void registerURLStreamHandler(final String protocol,
  +                                                final URLStreamHandler handler)
  +    {
  +        if (protocol == null) {
  +            throw new NullArgumentException("protocol");
  +        }
  +        if (handler == null) {
  +            throw new NullArgumentException("handler");
  +        }
  +        
  +        // This way is "naughty" but works great
  +        Map handlers = (Map)AccessController.doPrivileged(
  +            new PrivilegedAction() {
  +                public Object run() { 
  +                    try {
  +                        Field field = URL.class.getDeclaredField("handlers");
  +                        field.setAccessible(true);
  +                        
  +                        return field.get(null);
  +                    }
  +                    catch (Exception e) {
  +                        log.warn("Failed to access URL 'handlers' field", e);
  +                    }
  +                    return null;
  +                }
  +            }
  +        );
  +        
  +        if (handlers != null) {
  +            handlers.put(protocol, handler);
           }
       }
   }
  
  
  

Mime
View raw message