Guest User

Untitled

a guest
Jul 6th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.14 KB | None | 0 0
  1. /*******************************************************************************
  2.  * Copyright 2011 Inhibi Ltd. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are
  6.  * permitted provided that the following conditions are met:
  7.  *
  8.  *     1. Redistributions of source code must retain the above copyright
  9.  * notice, this list of
  10.  *        conditions and the following disclaimer.
  11.  *
  12.  *     2. Redistributions in binary form must reproduce the above copyright
  13.  * notice, this list
  14.  *        of conditions and the following disclaimer in the documentation
  15.  * and/or other materials
  16.  *        provided with the distribution.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY INHIBI LTD ``AS IS'' AND ANY
  19.  * EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND
  22.  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  23.  * INHIBI LTD OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR
  26.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27.  * SUBSTITUTE GOODS OR
  28.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON
  30.  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31.  * (INCLUDING
  32.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  *
  35.  * The views and conclusions contained in the software and documentation
  36.  * are those of the authors and should not be interpreted as representing
  37.  * official policies, either expressed or implied, of Inhibi Ltd.
  38.  *
  39.  * Contributors:
  40.  *    Inhibi Ltd - initial API and implementation
  41.  *******************************************************************************/
  42. package pl.doa.jvm;
  43.  
  44. import java.lang.reflect.Field;
  45. import java.net.URL;
  46. import java.net.URLStreamHandler;
  47. import java.net.URLStreamHandlerFactory;
  48.  
  49. import pl.doa.IDOA;
  50.  
  51. public class DOAURLHandlerFactory implements URLStreamHandlerFactory {
  52.  
  53.     private final IDOA doa;
  54.     private final URLStreamHandlerFactory otherFactory;
  55.  
  56.     public DOAURLHandlerFactory(IDOA doa, URLStreamHandlerFactory otherFactory) {
  57.         this.doa = doa;
  58.         this.otherFactory = otherFactory;
  59.     }
  60.  
  61.     public DOAURLHandlerFactory(IDOA doa) {
  62.         this(doa, null);
  63.     }
  64.  
  65.     @Override
  66.     public URLStreamHandler createURLStreamHandler(String protocol) {
  67.         if ("doa".equals(protocol)) {
  68.             return new DOAStreamHandler(doa);
  69.         }
  70.         if (otherFactory == null) {
  71.             return null;
  72.         }
  73.         return otherFactory.createURLStreamHandler(protocol);
  74.     }
  75.  
  76.     public static void attachFactory(IDOA doa) throws Exception {
  77.         Field factoryField = URL.class.getDeclaredField("factory");
  78.         factoryField.setAccessible(true);
  79.  
  80.         URLStreamHandlerFactory factorySet =
  81.                 (URLStreamHandlerFactory) factoryField.get(URL.class);
  82.         if (factorySet == null) {
  83.             URL.setURLStreamHandlerFactory(new DOAURLHandlerFactory(doa));
  84.         } else {
  85.             URLStreamHandlerFactory newFactory =
  86.                     new DOAURLHandlerFactory(doa, factorySet);
  87.             factoryField.set(URL.class, newFactory);
  88.         }
  89.  
  90.     }
  91. }
Add Comment
Please, Sign In to add comment