Coverage Report - org.apache.commons.jnet.Installer
 
Classes in this File Line Coverage Branch Coverage Complexity
Installer
0%
0/33
0%
0/26
7.5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.jnet;
 18  
 
 19  
 import java.lang.reflect.Field;
 20  
 import java.lang.reflect.Modifier;
 21  
 import java.net.URL;
 22  
 import java.net.URLStreamHandlerFactory;
 23  
 
 24  
 /**
 25  
  * The installer is a general purpose class to install an own
 26  
  * {@link URLStreamHandlerFactory} in any environment.
 27  
  *
 28  
  * TODO Uninstall
 29  
  */
 30  0
 public class Installer {
 31  
 
 32  
     /**
 33  
      * Set the url stream handler factory.
 34  
      * @param factory
 35  
      * @throws Exception
 36  
      */
 37  
     public static void setURLStreamHandlerFactory(URLStreamHandlerFactory factory)
 38  
     throws Exception {
 39  
         try {
 40  
             // if we can set the factory, its the first!
 41  0
             URL.setURLStreamHandlerFactory(factory);
 42  0
         } catch (Error err) {
 43  
             // let's use reflection to get the field holding the factory
 44  0
             final Field[] fields = URL.class.getDeclaredFields();
 45  0
             int index = 0;
 46  0
             Field factoryField = null;
 47  0
             while ( factoryField == null && index < fields.length ) {
 48  0
                 final Field current = fields[index];
 49  0
                 if ( Modifier.isStatic( current.getModifiers() ) && current.getType().equals( URLStreamHandlerFactory.class ) ) {
 50  0
                     factoryField = current;
 51  0
                     factoryField.setAccessible(true);
 52  0
                 } else {
 53  0
                     index++;
 54  
                 }
 55  0
             }
 56  0
             if ( factoryField == null ) {
 57  0
                 throw new Exception("Unable to detect static field in the URL class for the URLStreamHandlerFactory. Please report this error together with your exact environment to the Apache Excalibur project.");
 58  
             }
 59  
             try {
 60  0
                 URLStreamHandlerFactory oldFactory = (URLStreamHandlerFactory)factoryField.get(null);
 61  0
                 if ( factory instanceof ParentAwareURLStreamHandlerFactory ) {
 62  0
                     ((ParentAwareURLStreamHandlerFactory)factory).setParentFactory(oldFactory);
 63  
                 }
 64  0
                 factoryField.set(null, factory);
 65  0
             } catch (IllegalArgumentException e) {
 66  0
                 throw new Exception("Unable to set url stream handler factory " + factory);
 67  0
             } catch (IllegalAccessException e) {
 68  0
                 throw new Exception("Unable to set url stream handler factory " + factory);
 69  0
             }
 70  0
         }
 71  0
     }
 72  
 
 73  
     protected static Field getStaticURLStreamHandlerFactoryField() {
 74  0
         Field[] fields = URL.class.getDeclaredFields();
 75  0
         for ( int i = 0; i < fields.length; i++ ) {
 76  0
             if ( Modifier.isStatic( fields[i].getModifiers() ) && fields[i].getType().equals( URLStreamHandlerFactory.class ) ) {
 77  0
                 fields[i].setAccessible( true );
 78  0
                 return fields[i];
 79  
             }
 80  
         }
 81  0
         return null;
 82  
     }
 83  
 }