KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > cmdline > 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  * $Id: SecuritySupport12.java,v 1.1.1.1 2004/02/26 11:38:46 vk112360 Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.cmdline;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import java.security.PrivilegedActionException JavaDoc;
30 import java.security.PrivilegedExceptionAction JavaDoc;
31
32 import java.util.Properties JavaDoc;
33
34 /**
35  * This class is duplicated for each Xalan-Java subpackage so keep it in sync.
36  * It is package private and therefore is not exposed as part of the Xalan-Java
37  * API.
38  *
39  * Security related methods that only work on J2SE 1.2 and newer.
40  */

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