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