KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > parsers > SecuritySupport


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.parsers;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28
29 /**
30  * This class is duplicated for each subpackage so keep it in sync.
31  * It is package private and therefore is not exposed as part of any API.
32  *
33  * @xerces.internal
34  */

35 final class SecuritySupport {
36
37     private static final SecuritySupport securitySupport = new SecuritySupport();
38
39     /**
40      * Return an instance of this class.
41      */

42     static SecuritySupport getInstance() {
43         return securitySupport;
44     }
45
46     ClassLoader JavaDoc getContextClassLoader() {
47         return (ClassLoader JavaDoc)
48         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
49             public Object JavaDoc run() {
50                 ClassLoader JavaDoc cl = null;
51                 try {
52                     cl = Thread.currentThread().getContextClassLoader();
53                 } catch (SecurityException JavaDoc ex) { }
54                 return cl;
55             }
56         });
57     }
58     
59     ClassLoader JavaDoc getSystemClassLoader() {
60         return (ClassLoader JavaDoc)
61         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
62             public Object JavaDoc run() {
63                 ClassLoader JavaDoc cl = null;
64                 try {
65                     cl = ClassLoader.getSystemClassLoader();
66                 } catch (SecurityException JavaDoc ex) {}
67                 return cl;
68             }
69         });
70     }
71     
72     ClassLoader JavaDoc getParentClassLoader(final ClassLoader JavaDoc cl) {
73         return (ClassLoader JavaDoc)
74         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
75             public Object JavaDoc run() {
76                 ClassLoader JavaDoc parent = null;
77                 try {
78                     parent = cl.getParent();
79                 } catch (SecurityException JavaDoc ex) {}
80                 
81                 // eliminate loops in case of the boot
82
// ClassLoader returning itself as a parent
83
return (parent == cl) ? null : parent;
84             }
85         });
86     }
87     
88     String JavaDoc getSystemProperty(final String JavaDoc propName) {
89         return (String JavaDoc)
90         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
91             public Object JavaDoc run() {
92                 return System.getProperty(propName);
93             }
94         });
95     }
96     
97     FileInputStream JavaDoc getFileInputStream(final File JavaDoc file)
98     throws FileNotFoundException JavaDoc
99     {
100         try {
101             return (FileInputStream JavaDoc)
102             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
103                 public Object JavaDoc run() throws FileNotFoundException JavaDoc {
104                     return new FileInputStream JavaDoc(file);
105                 }
106             });
107         } catch (PrivilegedActionException JavaDoc e) {
108             throw (FileNotFoundException JavaDoc)e.getException();
109         }
110     }
111     
112     InputStream JavaDoc getResourceAsStream(final ClassLoader JavaDoc cl,
113             final String JavaDoc name)
114     {
115         return (InputStream JavaDoc)
116         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
117             public Object JavaDoc run() {
118                 InputStream JavaDoc ris;
119                 if (cl == null) {
120                     ris = ClassLoader.getSystemResourceAsStream(name);
121                 } else {
122                     ris = cl.getResourceAsStream(name);
123                 }
124                 return ris;
125             }
126         });
127     }
128     
129     boolean getFileExists(final File JavaDoc f) {
130         return ((Boolean JavaDoc)
131                 AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
132                     public Object JavaDoc run() {
133                         return new Boolean JavaDoc(f.exists());
134                     }
135                 })).booleanValue();
136     }
137     
138     long getLastModified(final File JavaDoc f) {
139         return ((Long JavaDoc)
140                 AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
141                     public Object JavaDoc run() {
142                         return new Long JavaDoc(f.lastModified());
143                     }
144                 })).longValue();
145     }
146     
147     private SecuritySupport () {}
148 }
149
Popular Tags