1. /*
  2. * @(#)VersionHelper.java 1.9 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.naming.internal;
  8. import java.io.InputStream;
  9. import java.io.IOException;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Enumeration;
  13. import java.util.StringTokenizer;
  14. import java.util.Vector;
  15. import javax.naming.NamingEnumeration;
  16. /**
  17. * VersionHelper was used by JNDI to accommodate differences between
  18. * JDK 1.1.x and the Java 2 platform. Since this is no longer necessary
  19. * due to JNDI's presence in the Java 2 Platform v1.3 and higher, this
  20. * class currently serves as a set of utilities for performing
  21. * system-level things, such as class-loading and reading system properties.
  22. *
  23. * @author Rosanna Lee
  24. * @author Scott Seligman
  25. * @version 1.9 03/12/19
  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. helper = new VersionHelper12();
  49. }
  50. public static VersionHelper getVersionHelper() {
  51. return helper;
  52. }
  53. public abstract Class loadClass(String className)
  54. throws ClassNotFoundException;
  55. abstract Class loadClass(String className, ClassLoader cl)
  56. throws ClassNotFoundException;
  57. public abstract Class loadClass(String className, String codebase)
  58. throws ClassNotFoundException, MalformedURLException;
  59. /*
  60. * Returns a JNDI property from the system properties. Returns
  61. * null if the property is not set, or if there is no permission
  62. * to read it.
  63. */
  64. abstract String getJndiProperty(int i);
  65. /*
  66. * Reads each property in PROPS from the system properties, and
  67. * returns their values -- in order -- in an array. For each
  68. * unset property, the corresponding array element is set to null.
  69. * Returns null if there is no permission to call System.getProperties().
  70. */
  71. abstract String[] getJndiProperties();
  72. /*
  73. * Returns the resource of a given name associated with a particular
  74. * class (never null), or null if none can be found.
  75. */
  76. abstract InputStream getResourceAsStream(Class c, String name);
  77. /*
  78. * Returns an input stream for a file in <java.home>/lib,
  79. * or null if it cannot be located or opened.
  80. *
  81. * @param filename The file name, sans directory.
  82. */
  83. abstract InputStream getJavaHomeLibStream(String filename);
  84. /*
  85. * Returns an enumeration (never null) of InputStreams of the
  86. * resources of a given name associated with a particular class
  87. * loader. Null represents the bootstrap class loader in some
  88. * Java implementations.
  89. */
  90. abstract NamingEnumeration getResources(ClassLoader cl, String name)
  91. throws IOException;
  92. /*
  93. * Returns the context class loader associated with the current thread.
  94. * Null indicates the bootstrap class loader in some Java implementations.
  95. *
  96. * @throws SecurityException if the class loader is not accessible.
  97. */
  98. abstract ClassLoader getContextClassLoader();
  99. static protected URL[] getUrlArray(String codebase)
  100. throws MalformedURLException {
  101. // Parse codebase into separate URLs
  102. StringTokenizer parser = new StringTokenizer(codebase);
  103. Vector vec = new Vector(10);
  104. while (parser.hasMoreTokens()) {
  105. vec.addElement(parser.nextToken());
  106. }
  107. String[] url = new String[vec.size()];
  108. for (int i = 0; i < url.length; i++) {
  109. url[i] = (String)vec.elementAt(i);
  110. }
  111. URL[] urlArray = new URL[url.length];
  112. for (int i = 0; i < urlArray.length; i++) {
  113. urlArray[i] = new URL(url[i]);
  114. }
  115. return urlArray;
  116. }
  117. }