1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.naming.internal;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.net.MalformedURLException;
  31 import java.net.URL;
  32 import java.util.StringTokenizer;
  33 import java.util.Vector;
  34 
  35 import javax.naming.NamingEnumeration;
  36 
  37 /**
  38  * VersionHelper was used by JNDI to accommodate differences between
  39  * JDK 1.1.x and the Java 2 platform. As this is no longer necessary
  40  * since JNDI's inclusion in the platform, this class currently
  41  * serves as a set of utilities for performing system-level things,
  42  * such as class-loading and reading system properties.
  43  *
  44  * @author Rosanna Lee
  45  * @author Scott Seligman
  46  */
  47 
  48 public abstract class VersionHelper {
  49     private static VersionHelper helper = null;
  50 
  51     final static String[] PROPS = new String[] {
  52         javax.naming.Context.INITIAL_CONTEXT_FACTORY,
  53         javax.naming.Context.OBJECT_FACTORIES,
  54         javax.naming.Context.URL_PKG_PREFIXES,
  55         javax.naming.Context.STATE_FACTORIES,
  56         javax.naming.Context.PROVIDER_URL,
  57         javax.naming.Context.DNS_URL,
  58         // The following shouldn't create a runtime dependence on ldap package.
  59         javax.naming.ldap.LdapContext.CONTROL_FACTORIES
  60     };
  61 
  62     public final static int INITIAL_CONTEXT_FACTORY = 0;
  63     public final static int OBJECT_FACTORIES = 1;
  64     public final static int URL_PKG_PREFIXES = 2;
  65     public final static int STATE_FACTORIES = 3;
  66     public final static int PROVIDER_URL = 4;
  67     public final static int DNS_URL = 5;
  68     public final static int CONTROL_FACTORIES = 6;
  69 
  70     VersionHelper() {} // Disallow anyone from creating one of these.
  71 
  72     static {
  73         helper = new VersionHelper12();
  74     }
  75 
  76     public static VersionHelper getVersionHelper() {
  77         return helper;
  78     }
  79 
  80     public abstract Class<?> loadClass(String className)
  81         throws ClassNotFoundException;
  82 
  83     abstract Class<?> loadClass(String className, ClassLoader cl)
  84         throws ClassNotFoundException;
  85 
  86     public abstract Class<?> loadClass(String className, String codebase)
  87         throws ClassNotFoundException, MalformedURLException;
  88 
  89     /*
  90      * Returns a JNDI property from the system properties.  Returns
  91      * null if the property is not set, or if there is no permission
  92      * to read it.
  93      */
  94     abstract String getJndiProperty(int i);
  95 
  96     /*
  97      * Reads each property in PROPS from the system properties, and
  98      * returns their values -- in order -- in an array.  For each
  99      * unset property, the corresponding array element is set to null.
 100      * Returns null if there is no permission to call System.getProperties().
 101      */
 102     abstract String[] getJndiProperties();
 103 
 104     /*
 105      * Returns the resource of a given name associated with a particular
 106      * class (never null), or null if none can be found.
 107      */
 108     abstract InputStream getResourceAsStream(Class<?> c, String name);
 109 
 110     /*
 111      * Returns an input stream for a file in <java.home>/lib,
 112      * or null if it cannot be located or opened.
 113      *
 114      * @param filename  The file name, sans directory.
 115      */
 116     abstract InputStream getJavaHomeLibStream(String filename);
 117 
 118     /*
 119      * Returns an enumeration (never null) of InputStreams of the
 120      * resources of a given name associated with a particular class
 121      * loader.  Null represents the bootstrap class loader in some
 122      * Java implementations.
 123      */
 124     abstract NamingEnumeration<InputStream> getResources(
 125             ClassLoader cl, String name)
 126         throws IOException;
 127 
 128     /*
 129      * Returns the context class loader associated with the current thread.
 130      * Null indicates the bootstrap class loader in some Java implementations.
 131      *
 132      * @throws SecurityException if the class loader is not accessible.
 133      */
 134     abstract ClassLoader getContextClassLoader();
 135 
 136     static protected URL[] getUrlArray(String codebase)
 137         throws MalformedURLException {
 138         // Parse codebase into separate URLs
 139         StringTokenizer parser = new StringTokenizer(codebase);
 140         Vector<String> vec = new Vector<>(10);
 141         while (parser.hasMoreTokens()) {
 142             vec.addElement(parser.nextToken());
 143         }
 144         String[] url = new String[vec.size()];
 145         for (int i = 0; i < url.length; i++) {
 146             url[i] = vec.elementAt(i);
 147         }
 148 
 149         URL[] urlArray = new URL[url.length];
 150         for (int i = 0; i < urlArray.length; i++) {
 151             urlArray[i] = new URL(url[i]);
 152         }
 153         return urlArray;
 154     }
 155 }