KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > validation > SecuritySupport


1 /*
2  * @(#)SecuritySupport.java 1.3 05/01/04
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.xml.validation;
9
10 import java.io.IOException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.security.*;
13 import java.net.*;
14 import java.io.*;
15 import java.util.*;
16
17 /**
18  * This class is duplicated for each JAXP subpackage so keep it in sync.
19  * It is package private and therefore is not exposed as part of the JAXP
20  * API.
21  *
22  * Security related methods that only work on J2SE 1.2 and newer.
23  */

24 class SecuritySupport {
25
26     
27     ClassLoader JavaDoc getContextClassLoader() {
28     return (ClassLoader JavaDoc)
29         AccessController.doPrivileged(new PrivilegedAction() {
30         public Object JavaDoc run() {
31         ClassLoader JavaDoc cl = null;
32         try {
33             cl = Thread.currentThread().getContextClassLoader();
34         } catch (SecurityException JavaDoc ex) { }
35         return cl;
36         }
37     });
38     }
39
40     String JavaDoc getSystemProperty(final String JavaDoc propName) {
41     return (String JavaDoc)
42             AccessController.doPrivileged(new PrivilegedAction() {
43                 public Object JavaDoc run() {
44                     return System.getProperty(propName);
45                 }
46             });
47     }
48
49     FileInputStream getFileInputStream(final File file)
50         throws FileNotFoundException
51     {
52     try {
53             return (FileInputStream)
54                 AccessController.doPrivileged(new PrivilegedExceptionAction() {
55                     public Object JavaDoc run() throws FileNotFoundException {
56                         return new FileInputStream(file);
57                     }
58                 });
59     } catch (PrivilegedActionException e) {
60         throw (FileNotFoundException)e.getException();
61     }
62     }
63
64     InputStream getURLInputStream(final URL JavaDoc url)
65         throws IOException JavaDoc
66     {
67     try {
68             return (InputStream)
69                 AccessController.doPrivileged(new PrivilegedExceptionAction() {
70                     public Object JavaDoc run() throws IOException JavaDoc {
71                         return url.openStream();
72                     }
73                 });
74     } catch (PrivilegedActionException e) {
75         throw (IOException JavaDoc)e.getException();
76     }
77     }
78
79     URL JavaDoc getResourceAsURL(final ClassLoader JavaDoc cl,
80                                            final String JavaDoc name)
81     {
82         return (URL JavaDoc)
83             AccessController.doPrivileged(new PrivilegedAction() {
84                 public Object JavaDoc run() {
85                     URL JavaDoc url;
86                     if (cl == null) {
87                         url = ClassLoader.getSystemResource(name);
88                     } else {
89                         url = cl.getResource(name);
90                     }
91                     return url;
92                 }
93             });
94     }
95
96     Enumeration getResources(final ClassLoader JavaDoc cl,
97                                            final String JavaDoc name) throws IOException JavaDoc
98     {
99         try{
100         return (Enumeration)
101             AccessController.doPrivileged(new PrivilegedExceptionAction() {
102                 public Object JavaDoc run() throws IOException JavaDoc{
103                     Enumeration enumeration;
104                     if (cl == null) {
105                         enumeration = ClassLoader.getSystemResources(name);
106                     } else {
107                         enumeration = cl.getResources(name);
108                     }
109                     return enumeration;
110                 }
111             });
112         }catch(PrivilegedActionException e){
113             throw (IOException JavaDoc)e.getException();
114         }
115     }
116     
117     InputStream getResourceAsStream(final ClassLoader JavaDoc cl,
118                                            final String JavaDoc name)
119     {
120         return (InputStream)
121             AccessController.doPrivileged(new PrivilegedAction() {
122                 public Object JavaDoc run() {
123                     InputStream ris;
124                     if (cl == null) {
125                         ris = ClassLoader.getSystemResourceAsStream(name);
126                     } else {
127                         ris = cl.getResourceAsStream(name);
128                     }
129                     return ris;
130                 }
131             });
132     }
133
134     boolean doesFileExist(final File f) {
135     return ((Boolean JavaDoc)
136             AccessController.doPrivileged(new PrivilegedAction() {
137                 public Object JavaDoc run() {
138                     return new Boolean JavaDoc(f.exists());
139                 }
140             })).booleanValue();
141     }
142
143 }
144
Popular Tags