1. /*
  2. * @(#)VersionHelper12.java 1.6 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.URLClassLoader;
  15. import java.net.URL;
  16. import java.security.AccessController;
  17. import java.security.PrivilegedAction;
  18. import java.security.PrivilegedActionException;
  19. import java.security.PrivilegedExceptionAction;
  20. import java.util.Enumeration;
  21. import java.util.Hashtable;
  22. import java.util.NoSuchElementException;
  23. import java.util.Properties;
  24. import javax.naming.*;
  25. /**
  26. * An implementation of VersionHelper for the Java 2 platform.
  27. *
  28. * @author Rosanna Lee
  29. * @author Scott Seligman
  30. * @version 1.6 00/02/02
  31. */
  32. final class VersionHelper12 extends VersionHelper {
  33. private boolean getSystemPropsFailed = false;
  34. VersionHelper12() {} // Disallow external from creating one of these.
  35. public Class loadClass(String className) throws ClassNotFoundException {
  36. ClassLoader cl = getContextClassLoader();
  37. return Class.forName(className, true, cl);
  38. }
  39. /**
  40. * Package private.
  41. */
  42. Class loadClass(String className, ClassLoader cl)
  43. throws ClassNotFoundException {
  44. return Class.forName(className, true, cl);
  45. }
  46. /**
  47. * @param className A non-null fully qualified class name.
  48. * @param codebase A non-null, space-separated list of URL strings.
  49. */
  50. public Class loadClass(String className, String codebase)
  51. throws ClassNotFoundException, MalformedURLException {
  52. ClassLoader cl;
  53. ClassLoader parent = getContextClassLoader();
  54. cl = URLClassLoader.newInstance(getUrlArray(codebase), parent);
  55. return Class.forName(className, true, cl);
  56. }
  57. String getJndiProperty(final int i) {
  58. return (String) AccessController.doPrivileged(
  59. new PrivilegedAction() {
  60. public Object run() {
  61. try {
  62. return System.getProperty(PROPS[i]);
  63. } catch (SecurityException e) {
  64. return null;
  65. }
  66. }
  67. }
  68. );
  69. }
  70. String[] getJndiProperties() {
  71. if (getSystemPropsFailed) {
  72. return null; // after one failure, don't bother trying again
  73. }
  74. Properties sysProps = (Properties) AccessController.doPrivileged(
  75. new PrivilegedAction() {
  76. public Object run() {
  77. try {
  78. return System.getProperties();
  79. } catch (SecurityException e) {
  80. getSystemPropsFailed = true;
  81. return null;
  82. }
  83. }
  84. }
  85. );
  86. if (sysProps == null) {
  87. return null;
  88. }
  89. String[] jProps = new String[PROPS.length];
  90. for (int i = 0; i < PROPS.length; i++) {
  91. jProps[i] = sysProps.getProperty(PROPS[i]);
  92. }
  93. return jProps;
  94. }
  95. InputStream getResourceAsStream(final Class c, final String name) {
  96. return (InputStream) AccessController.doPrivileged(
  97. new PrivilegedAction() {
  98. public Object run() {
  99. return c.getResourceAsStream(name);
  100. }
  101. }
  102. );
  103. }
  104. InputStream getJavaHomeLibStream(final String filename) {
  105. return (InputStream) AccessController.doPrivileged(
  106. new PrivilegedAction() {
  107. public Object run() {
  108. try {
  109. String javahome = System.getProperty("java.home");
  110. if (javahome == null) {
  111. return null;
  112. }
  113. String pathname = javahome + java.io.File.separator +
  114. "lib" + java.io.File.separator + filename;
  115. return new java.io.FileInputStream(pathname);
  116. } catch (Exception e) {
  117. return null;
  118. }
  119. }
  120. }
  121. );
  122. }
  123. NamingEnumeration getResources(final ClassLoader cl, final String name)
  124. throws IOException
  125. {
  126. Enumeration urls;
  127. try {
  128. urls = (Enumeration) AccessController.doPrivileged(
  129. new PrivilegedExceptionAction() {
  130. public Object run() throws IOException {
  131. return (cl == null)
  132. ? ClassLoader.getSystemResources(name)
  133. : cl.getResources(name);
  134. }
  135. }
  136. );
  137. } catch (PrivilegedActionException e) {
  138. throw (IOException)e.getException();
  139. }
  140. return new InputStreamEnumeration(urls);
  141. }
  142. ClassLoader getContextClassLoader() {
  143. return (ClassLoader) AccessController.doPrivileged(
  144. new PrivilegedAction() {
  145. public Object run() {
  146. return Thread.currentThread().getContextClassLoader();
  147. }
  148. }
  149. );
  150. }
  151. /**
  152. * Given an enumeration of URLs, an instance of this class represents
  153. * an enumeration of their InputStreams. Each operation on the URL
  154. * enumeration is performed within a doPrivileged block.
  155. * This is used to enumerate the resources under a foreign codebase.
  156. * This class is not MT-safe.
  157. */
  158. class InputStreamEnumeration implements NamingEnumeration {
  159. private final Enumeration urls;
  160. private Object nextElement = null;
  161. InputStreamEnumeration(Enumeration urls) {
  162. this.urls = urls;
  163. }
  164. /*
  165. * Returns the next InputStream, or null if there are no more.
  166. * An InputStream that cannot be opened is skipped.
  167. */
  168. private Object getNextElement() {
  169. return AccessController.doPrivileged(
  170. new PrivilegedAction() {
  171. public Object run() {
  172. while (urls.hasMoreElements()) {
  173. try {
  174. return ((URL)urls.nextElement()).openStream();
  175. } catch (IOException e) {
  176. // skip this URL
  177. }
  178. }
  179. return null;
  180. }
  181. }
  182. );
  183. }
  184. public boolean hasMore() {
  185. if (nextElement != null) {
  186. return true;
  187. }
  188. nextElement = getNextElement();
  189. return (nextElement != null);
  190. }
  191. public boolean hasMoreElements() {
  192. return hasMore();
  193. }
  194. public Object next() {
  195. if (hasMore()) {
  196. Object res = nextElement;
  197. nextElement = null;
  198. return res;
  199. } else {
  200. throw new NoSuchElementException();
  201. }
  202. }
  203. public Object nextElement() {
  204. return next();
  205. }
  206. public void close() {
  207. }
  208. }
  209. }