1. /*
  2. * @(#)VersionHelper.java 1.5 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package com.sun.naming.internal;
  11. import java.io.InputStream;
  12. import java.io.IOException;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.util.Enumeration;
  16. import java.util.StringTokenizer;
  17. import java.util.Vector;
  18. import javax.naming.NamingEnumeration;
  19. /**
  20. * VersionHelper is used by JNDI to accomodate differences between
  21. * JDK 1.1.x and the Java 2 platform.
  22. *
  23. * @author Rosanna Lee
  24. * @author Scott Seligman
  25. * @version 1.5 00/02/02
  26. */
  27. public abstract class VersionHelper {
  28. private static VersionHelper helper = null;
  29. final static String[] PROPS = new String[] {
  30. javax.naming.Context.INITIAL_CONTEXT_FACTORY,
  31. javax.naming.Context.OBJECT_FACTORIES,
  32. javax.naming.Context.URL_PKG_PREFIXES,
  33. javax.naming.Context.STATE_FACTORIES,
  34. javax.naming.Context.PROVIDER_URL,
  35. javax.naming.Context.DNS_URL,
  36. // The following shouldn't create a runtime dependence on ldap package.
  37. javax.naming.ldap.LdapContext.CONTROL_FACTORIES
  38. };
  39. public final static int INITIAL_CONTEXT_FACTORY = 0;
  40. public final static int OBJECT_FACTORIES = 1;
  41. public final static int URL_PKG_PREFIXES = 2;
  42. public final static int STATE_FACTORIES = 3;
  43. public final static int PROVIDER_URL = 4;
  44. public final static int DNS_URL = 5;
  45. public final static int CONTROL_FACTORIES = 6;
  46. VersionHelper() {} // Disallow anyone from creating one of these.
  47. static {
  48. try {
  49. Class.forName("java.net.URLClassLoader"); // 1.2 test
  50. Class.forName("java.security.PrivilegedAction"); // 1.2 test
  51. helper = (VersionHelper)
  52. Class.forName(
  53. "com.sun.naming.internal.VersionHelper12").newInstance();
  54. } catch (Exception e) {
  55. }
  56. // Use 1.1 helper if 1.2 test fails, or if we cannot create 1.2 helper
  57. if (helper == null) {
  58. try {
  59. helper = (VersionHelper)
  60. Class.forName(
  61. "com.sun.naming.internal.VersionHelper11").newInstance();
  62. } catch (Exception e) {
  63. // should never happen
  64. }
  65. }
  66. }
  67. public static VersionHelper getVersionHelper() {
  68. return helper;
  69. }
  70. public abstract Class loadClass(String className)
  71. throws ClassNotFoundException;
  72. abstract Class loadClass(String className, ClassLoader cl)
  73. throws ClassNotFoundException;
  74. public abstract Class loadClass(String className, String codebase)
  75. throws ClassNotFoundException, MalformedURLException;
  76. /*
  77. * Returns a JNDI property from the system properties. Returns
  78. * null if the property is not set, or if there is no permission
  79. * to read it.
  80. */
  81. abstract String getJndiProperty(int i);
  82. /*
  83. * Reads each property in PROPS from the system properties, and
  84. * returns their values -- in order -- in an array. For each
  85. * unset property, the corresponding array element is set to null.
  86. * Returns null if there is no permission to call System.getProperties().
  87. */
  88. abstract String[] getJndiProperties();
  89. /*
  90. * Returns the resource of a given name associated with a particular
  91. * class (never null), or null if none can be found.
  92. */
  93. abstract InputStream getResourceAsStream(Class c, String name);
  94. /*
  95. * Returns an input stream for a file in <java.home>/lib,
  96. * or null if it cannot be located or opened.
  97. *
  98. * @param filename The file name, sans directory.
  99. */
  100. abstract InputStream getJavaHomeLibStream(String filename);
  101. /*
  102. * Returns an enumeration (never null) of InputStreams of the
  103. * resources of a given name associated with a particular class
  104. * loader. Null represents the bootstrap class loader in some
  105. * Java implementations.
  106. */
  107. abstract NamingEnumeration getResources(ClassLoader cl, String name)
  108. throws IOException;
  109. /*
  110. * Returns the context class loader associated with the current thread.
  111. * Null indicates the bootstrap class loader in some Java implementations.
  112. *
  113. * @throws SecurityException if the class loader is not accessible.
  114. */
  115. abstract ClassLoader getContextClassLoader();
  116. static protected URL[] getUrlArray(String codebase)
  117. throws MalformedURLException {
  118. // Parse codebase into separate URLs
  119. StringTokenizer parser = new StringTokenizer(codebase);
  120. Vector vec = new Vector(10);
  121. while (parser.hasMoreTokens()) {
  122. vec.addElement(parser.nextToken());
  123. }
  124. String[] url = new String[vec.size()];
  125. for (int i = 0; i < url.length; i++) {
  126. url[i] = (String)vec.elementAt(i);
  127. }
  128. URL[] urlArray = new URL[url.length];
  129. for (int i = 0; i < urlArray.length; i++) {
  130. urlArray[i] = new URL(url[i]);
  131. }
  132. return urlArray;
  133. }
  134. }