KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > SecuritySupport


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)SecuritySupport.java 1.3 05/11/16
24  *
25  * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.security.*;
31 import java.net.*;
32 import java.io.*;
33 import java.util.*;
34
35 /**
36  * Security related methods that only work on J2SE 1.2 and newer.
37  */

38 class SecuritySupport {
39
40     private SecuritySupport() {
41     // private constructor, can't create an instance
42
}
43
44     public static ClassLoader JavaDoc getContextClassLoader() {
45     return (ClassLoader JavaDoc)
46         AccessController.doPrivileged(new PrivilegedAction() {
47         public Object JavaDoc run() {
48         ClassLoader JavaDoc cl = null;
49         try {
50             cl = Thread.currentThread().getContextClassLoader();
51         } catch (SecurityException JavaDoc ex) { }
52         return cl;
53         }
54     });
55     }
56
57     public static InputStream getResourceAsStream(final Class JavaDoc c,
58                 final String JavaDoc name) throws IOException {
59     try {
60         return (InputStream)
61         AccessController.doPrivileged(new PrivilegedExceptionAction() {
62             public Object JavaDoc run() throws IOException {
63             return c.getResourceAsStream(name);
64             }
65         });
66     } catch (PrivilegedActionException e) {
67         throw (IOException)e.getException();
68     }
69     }
70
71     public static URL[] getResources(final ClassLoader JavaDoc cl, final String JavaDoc name) {
72     return (URL[])
73         AccessController.doPrivileged(new PrivilegedAction() {
74         public Object JavaDoc run() {
75         URL[] ret = null;
76         try {
77             List v = new ArrayList();
78             Enumeration e = cl.getResources(name);
79             while (e != null && e.hasMoreElements()) {
80             URL url = (URL)e.nextElement();
81             if (url != null)
82                 v.add(url);
83             }
84             if (v.size() > 0) {
85             ret = new URL[v.size()];
86             ret = (URL[])v.toArray(ret);
87             }
88         } catch (IOException ioex) {
89         } catch (SecurityException JavaDoc ex) { }
90         return ret;
91         }
92     });
93     }
94
95     public static URL[] getSystemResources(final String JavaDoc name) {
96     return (URL[])
97         AccessController.doPrivileged(new PrivilegedAction() {
98         public Object JavaDoc run() {
99         URL[] ret = null;
100         try {
101             List v = new ArrayList();
102             Enumeration e = ClassLoader.getSystemResources(name);
103             while (e != null && e.hasMoreElements()) {
104             URL url = (URL)e.nextElement();
105             if (url != null)
106                 v.add(url);
107             }
108             if (v.size() > 0) {
109             ret = new URL[v.size()];
110             ret = (URL[])v.toArray(ret);
111             }
112         } catch (IOException ioex) {
113         } catch (SecurityException JavaDoc ex) { }
114         return ret;
115         }
116     });
117     }
118
119     public static InputStream openStream(final URL url) throws IOException {
120     try {
121         return (InputStream)
122         AccessController.doPrivileged(new PrivilegedExceptionAction() {
123             public Object JavaDoc run() throws IOException {
124             return url.openStream();
125             }
126         });
127     } catch (PrivilegedActionException e) {
128         throw (IOException)e.getException();
129     }
130     }
131 }
132
Popular Tags