KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cyberneko > html > SecuritySupport12


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.cyberneko.html;
18
19 import java.security.*;
20 import java.io.*;
21
22 /**
23  * This class is duplicated for each JAXP subpackage so keep it in sync.
24  * It is package private and therefore is not exposed as part of the JAXP
25  * API.
26  *
27  * Security related methods that only work on J2SE 1.2 and newer.
28  */

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