KickJava   Java API By Example, From Geeks To Geeks.

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

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