1 7 8 package com.sun.naming.internal; 9 10 import java.io.InputStream ; 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.net.URLClassLoader ; 14 import java.net.URL ; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import java.security.PrivilegedActionException ; 18 import java.security.PrivilegedExceptionAction ; 19 import java.util.Enumeration ; 20 import java.util.Hashtable ; 21 import java.util.NoSuchElementException ; 22 import java.util.Properties ; 23 24 import javax.naming.*; 25 26 33 34 final class VersionHelper12 extends VersionHelper { 35 36 private boolean getSystemPropsFailed = false; 37 38 VersionHelper12() {} 40 public Class loadClass(String className) throws ClassNotFoundException { 41 ClassLoader cl = getContextClassLoader(); 42 return Class.forName(className, true, cl); 43 } 44 45 48 Class loadClass(String className, ClassLoader cl) 49 throws ClassNotFoundException { 50 return Class.forName(className, true, cl); 51 } 52 53 57 public Class loadClass(String className, String codebase) 58 throws ClassNotFoundException , MalformedURLException { 59 ClassLoader cl; 60 61 ClassLoader parent = getContextClassLoader(); 62 cl = URLClassLoader.newInstance(getUrlArray(codebase), parent); 63 64 return Class.forName(className, true, cl); 65 } 66 67 String getJndiProperty(final int i) { 68 return (String ) AccessController.doPrivileged( 69 new PrivilegedAction () { 70 public Object run() { 71 try { 72 return System.getProperty(PROPS[i]); 73 } catch (SecurityException e) { 74 return null; 75 } 76 } 77 } 78 ); 79 } 80 81 String [] getJndiProperties() { 82 if (getSystemPropsFailed) { 83 return null; } 85 Properties sysProps = (Properties ) AccessController.doPrivileged( 86 new PrivilegedAction () { 87 public Object run() { 88 try { 89 return System.getProperties(); 90 } catch (SecurityException e) { 91 getSystemPropsFailed = true; 92 return null; 93 } 94 } 95 } 96 ); 97 if (sysProps == null) { 98 return null; 99 } 100 String [] jProps = new String [PROPS.length]; 101 for (int i = 0; i < PROPS.length; i++) { 102 jProps[i] = sysProps.getProperty(PROPS[i]); 103 } 104 return jProps; 105 } 106 107 InputStream getResourceAsStream(final Class c, final String name) { 108 return (InputStream ) AccessController.doPrivileged( 109 new PrivilegedAction () { 110 public Object run() { 111 return c.getResourceAsStream(name); 112 } 113 } 114 ); 115 } 116 117 InputStream getJavaHomeLibStream(final String filename) { 118 return (InputStream ) AccessController.doPrivileged( 119 new PrivilegedAction () { 120 public Object run() { 121 try { 122 String javahome = System.getProperty("java.home"); 123 if (javahome == null) { 124 return null; 125 } 126 String pathname = javahome + java.io.File.separator + 127 "lib" + java.io.File.separator + filename; 128 return new java.io.FileInputStream (pathname); 129 } catch (Exception e) { 130 return null; 131 } 132 } 133 } 134 ); 135 } 136 137 NamingEnumeration getResources(final ClassLoader cl, final String name) 138 throws IOException 139 { 140 Enumeration urls; 141 try { 142 urls = (Enumeration ) AccessController.doPrivileged( 143 new PrivilegedExceptionAction () { 144 public Object run() throws IOException { 145 return (cl == null) 146 ? ClassLoader.getSystemResources(name) 147 : cl.getResources(name); 148 } 149 } 150 ); 151 } catch (PrivilegedActionException e) { 152 throw (IOException )e.getException(); 153 } 154 return new InputStreamEnumeration(urls); 155 } 156 157 ClassLoader getContextClassLoader() { 158 return (ClassLoader ) AccessController.doPrivileged( 159 new PrivilegedAction () { 160 public Object run() { 161 return Thread.currentThread().getContextClassLoader(); 162 } 163 } 164 ); 165 } 166 167 168 175 class InputStreamEnumeration implements NamingEnumeration { 176 177 private final Enumeration urls; 178 179 private Object nextElement = null; 180 181 InputStreamEnumeration(Enumeration urls) { 182 this.urls = urls; 183 } 184 185 189 private Object getNextElement() { 190 return AccessController.doPrivileged( 191 new PrivilegedAction () { 192 public Object run() { 193 while (urls.hasMoreElements()) { 194 try { 195 return ((URL )urls.nextElement()).openStream(); 196 } catch (IOException e) { 197 } 199 } 200 return null; 201 } 202 } 203 ); 204 } 205 206 public boolean hasMore() { 207 if (nextElement != null) { 208 return true; 209 } 210 nextElement = getNextElement(); 211 return (nextElement != null); 212 } 213 214 public boolean hasMoreElements() { 215 return hasMore(); 216 } 217 218 public Object next() { 219 if (hasMore()) { 220 Object res = nextElement; 221 nextElement = null; 222 return res; 223 } else { 224 throw new NoSuchElementException (); 225 } 226 } 227 228 public Object nextElement() { 229 return next(); 230 } 231 232 public void close() { 233 } 234 } 235 } 236 | Popular Tags |