KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > naming > internal > VersionHelper12


1 /*
2  * @(#)VersionHelper12.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
8 package com.sun.naming.internal;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17 import java.security.PrivilegedActionException JavaDoc;
18 import java.security.PrivilegedExceptionAction JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.naming.*;
25
26 /**
27  * An implementation of VersionHelper for the Java 2 platform.
28  *
29  * @author Rosanna Lee
30  * @author Scott Seligman
31  * @version 1.9 03/12/19
32  */

33
34 final class VersionHelper12 extends VersionHelper {
35
36     private boolean getSystemPropsFailed = false;
37
38     VersionHelper12() {} // Disallow external from creating one of these.
39

40     public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
41     ClassLoader JavaDoc cl = getContextClassLoader();
42     return Class.forName(className, true, cl);
43     }
44
45     /**
46       * Package private.
47       */

48     Class JavaDoc loadClass(String JavaDoc className, ClassLoader JavaDoc cl)
49     throws ClassNotFoundException JavaDoc {
50     return Class.forName(className, true, cl);
51     }
52
53     /**
54      * @param className A non-null fully qualified class name.
55      * @param codebase A non-null, space-separated list of URL strings.
56      */

57     public Class JavaDoc loadClass(String JavaDoc className, String JavaDoc codebase)
58     throws ClassNotFoundException JavaDoc, MalformedURLException JavaDoc {
59     ClassLoader JavaDoc cl;
60
61     ClassLoader JavaDoc parent = getContextClassLoader();
62     cl = URLClassLoader.newInstance(getUrlArray(codebase), parent);
63
64     return Class.forName(className, true, cl);
65     }
66
67     String JavaDoc getJndiProperty(final int i) {
68     return (String JavaDoc) AccessController.doPrivileged(
69         new PrivilegedAction JavaDoc() {
70         public Object JavaDoc run() {
71             try {
72             return System.getProperty(PROPS[i]);
73             } catch (SecurityException JavaDoc e) {
74             return null;
75             }
76             }
77         }
78     );
79     }
80
81     String JavaDoc[] getJndiProperties() {
82     if (getSystemPropsFailed) {
83         return null; // after one failure, don't bother trying again
84
}
85     Properties JavaDoc sysProps = (Properties JavaDoc) AccessController.doPrivileged(
86         new PrivilegedAction JavaDoc() {
87         public Object JavaDoc run() {
88             try {
89             return System.getProperties();
90             } catch (SecurityException JavaDoc e) {
91             getSystemPropsFailed = true;
92             return null;
93             }
94         }
95         }
96     );
97     if (sysProps == null) {
98         return null;
99     }
100     String JavaDoc[] jProps = new String JavaDoc[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 JavaDoc getResourceAsStream(final Class JavaDoc c, final String JavaDoc name) {
108     return (InputStream JavaDoc) AccessController.doPrivileged(
109         new PrivilegedAction JavaDoc() {
110         public Object JavaDoc run() {
111                 return c.getResourceAsStream(name);
112             }
113         }
114     );
115     }
116
117     InputStream JavaDoc getJavaHomeLibStream(final String JavaDoc filename) {
118     return (InputStream JavaDoc) AccessController.doPrivileged(
119         new PrivilegedAction JavaDoc() {
120         public Object JavaDoc run() {
121             try {
122             String JavaDoc javahome = System.getProperty("java.home");
123             if (javahome == null) {
124                 return null;
125             }
126             String JavaDoc pathname = javahome + java.io.File.separator +
127                 "lib" + java.io.File.separator + filename;
128             return new java.io.FileInputStream JavaDoc(pathname);
129             } catch (Exception JavaDoc e) {
130             return null;
131             }
132         }
133         }
134     );
135     }
136
137     NamingEnumeration getResources(final ClassLoader JavaDoc cl, final String JavaDoc name)
138         throws IOException JavaDoc
139     {
140     Enumeration JavaDoc urls;
141     try {
142         urls = (Enumeration JavaDoc) AccessController.doPrivileged(
143         new PrivilegedExceptionAction JavaDoc() {
144             public Object JavaDoc run() throws IOException JavaDoc {
145             return (cl == null)
146                 ? ClassLoader.getSystemResources(name)
147                 : cl.getResources(name);
148             }
149         }
150         );
151     } catch (PrivilegedActionException JavaDoc e) {
152         throw (IOException JavaDoc)e.getException();
153     }
154     return new InputStreamEnumeration(urls);
155     }
156
157     ClassLoader JavaDoc getContextClassLoader() {
158     return (ClassLoader JavaDoc) AccessController.doPrivileged(
159         new PrivilegedAction JavaDoc() {
160         public Object JavaDoc run() {
161             return Thread.currentThread().getContextClassLoader();
162         }
163         }
164     );
165     }
166
167
168     /**
169      * Given an enumeration of URLs, an instance of this class represents
170      * an enumeration of their InputStreams. Each operation on the URL
171      * enumeration is performed within a doPrivileged block.
172      * This is used to enumerate the resources under a foreign codebase.
173      * This class is not MT-safe.
174      */

175     class InputStreamEnumeration implements NamingEnumeration {
176
177     private final Enumeration JavaDoc urls;
178
179     private Object JavaDoc nextElement = null;
180
181     InputStreamEnumeration(Enumeration JavaDoc urls) {
182         this.urls = urls;
183     }
184
185     /*
186      * Returns the next InputStream, or null if there are no more.
187      * An InputStream that cannot be opened is skipped.
188      */

189     private Object JavaDoc getNextElement() {
190         return AccessController.doPrivileged(
191         new PrivilegedAction JavaDoc() {
192             public Object JavaDoc run() {
193             while (urls.hasMoreElements()) {
194                 try {
195                 return ((URL JavaDoc)urls.nextElement()).openStream();
196                 } catch (IOException JavaDoc e) {
197                 // skip this URL
198
}
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 JavaDoc next() {
219         if (hasMore()) {
220         Object JavaDoc res = nextElement;
221         nextElement = null;
222         return res;
223         } else {
224         throw new NoSuchElementException JavaDoc();
225         }
226     }
227
228     public Object JavaDoc nextElement() {
229         return next();
230     }
231
232     public void close() {
233     }
234     }
235 }
236
Popular Tags