KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: SecuritySupport.java,v 1.4 2006/06/09 10:59:33 sunithareddy Exp $
23  * @(#)SecuritySupport.java 1.1 06/07/13
24  *
25  * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.xml.stream;
29
30 import java.security.*;
31 import java.net.*;
32 import java.io.*;
33 import java.util.*;
34
35 /**
36  * This class is duplicated for each JAXP subpackage so keep it in sync.
37  * It is package private and therefore is not exposed as part of the JAXP
38  * API.
39  *
40  * Security related methods that only work on J2SE 1.2 and newer.
41  */

42 class SecuritySupport {
43
44     
45     ClassLoader JavaDoc getContextClassLoader() throws SecurityException JavaDoc{
46     return (ClassLoader JavaDoc)
47         AccessController.doPrivileged(new PrivilegedAction() {
48         public Object JavaDoc run() {
49                 ClassLoader JavaDoc cl = null;
50                 //try {
51
cl = Thread.currentThread().getContextClassLoader();
52                 //} catch (SecurityException ex) { }
53

54                 if (cl == null)
55                     cl = ClassLoader.getSystemClassLoader();
56                 
57                 return cl;
58             }
59         });
60     }
61
62     String JavaDoc getSystemProperty(final String JavaDoc propName) {
63     return (String JavaDoc)
64             AccessController.doPrivileged(new PrivilegedAction() {
65                 public Object JavaDoc run() {
66                     return System.getProperty(propName);
67                 }
68             });
69     }
70
71     FileInputStream getFileInputStream(final File file)
72         throws FileNotFoundException
73     {
74     try {
75             return (FileInputStream)
76                 AccessController.doPrivileged(new PrivilegedExceptionAction() {
77                     public Object JavaDoc run() throws FileNotFoundException {
78                         return new FileInputStream(file);
79                     }
80                 });
81     } catch (PrivilegedActionException e) {
82         throw (FileNotFoundException)e.getException();
83     }
84     }
85
86     InputStream getResourceAsStream(final ClassLoader JavaDoc cl,
87                                            final String JavaDoc name)
88     {
89         return (InputStream)
90             AccessController.doPrivileged(new PrivilegedAction() {
91                 public Object JavaDoc run() {
92                     InputStream ris;
93                     if (cl == null) {
94                         ris = ClassLoader.getSystemResourceAsStream(name);
95                     } else {
96                         ris = cl.getResourceAsStream(name);
97                     }
98                     return ris;
99                 }
100             });
101     }
102
103     boolean doesFileExist(final File f) {
104     return ((Boolean JavaDoc)
105             AccessController.doPrivileged(new PrivilegedAction() {
106                 public Object JavaDoc run() {
107                     return new Boolean JavaDoc(f.exists());
108                 }
109             })).booleanValue();
110     }
111
112 }
113
Popular Tags