SecuritySupport.java revision 628:2bfaf29cc90b
1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 2002,2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package com.sun.org.apache.xml.internal.serialize;
22
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileNotFoundException;
26import java.io.InputStream;
27
28import java.security.AccessController;
29import java.security.PrivilegedAction;
30import java.security.PrivilegedActionException;
31import java.security.PrivilegedExceptionAction;
32
33/**
34 * This class is duplicated for each subpackage so keep it in sync.
35 * It is package private and therefore is not exposed as part of any API.
36 *
37 * @xerces.internal
38 */
39final class SecuritySupport {
40
41    private static final SecuritySupport securitySupport = new SecuritySupport();
42
43    /**
44     * Return an instance of this class.
45     */
46    static SecuritySupport getInstance() {
47        return securitySupport;
48    }
49
50    ClassLoader getContextClassLoader() {
51        return (ClassLoader)
52        AccessController.doPrivileged(new PrivilegedAction() {
53            public Object run() {
54                ClassLoader cl = null;
55                try {
56                    cl = Thread.currentThread().getContextClassLoader();
57                } catch (SecurityException ex) { }
58                return cl;
59            }
60        });
61    }
62
63    ClassLoader getSystemClassLoader() {
64        return (ClassLoader)
65        AccessController.doPrivileged(new PrivilegedAction() {
66            public Object run() {
67                ClassLoader cl = null;
68                try {
69                    cl = ClassLoader.getSystemClassLoader();
70                } catch (SecurityException ex) {}
71                return cl;
72            }
73        });
74    }
75
76    ClassLoader getParentClassLoader(final ClassLoader cl) {
77        return (ClassLoader)
78        AccessController.doPrivileged(new PrivilegedAction() {
79            public Object run() {
80                ClassLoader parent = null;
81                try {
82                    parent = cl.getParent();
83                } catch (SecurityException ex) {}
84
85                // eliminate loops in case of the boot
86                // ClassLoader returning itself as a parent
87                return (parent == cl) ? null : parent;
88            }
89        });
90    }
91
92    String getSystemProperty(final String propName) {
93        return (String)
94        AccessController.doPrivileged(new PrivilegedAction() {
95            public Object run() {
96                return System.getProperty(propName);
97            }
98        });
99    }
100
101    FileInputStream getFileInputStream(final File file)
102    throws FileNotFoundException
103    {
104        try {
105            return (FileInputStream)
106            AccessController.doPrivileged(new PrivilegedExceptionAction() {
107                public Object run() throws FileNotFoundException {
108                    return new FileInputStream(file);
109                }
110            });
111        } catch (PrivilegedActionException e) {
112            throw (FileNotFoundException)e.getException();
113        }
114    }
115
116    InputStream getResourceAsStream(final ClassLoader cl,
117            final String name)
118    {
119        return (InputStream)
120        AccessController.doPrivileged(new PrivilegedAction() {
121            public Object 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 getFileExists(final File f) {
134        return ((Boolean)
135                AccessController.doPrivileged(new PrivilegedAction() {
136                    public Object run() {
137                        return new Boolean(f.exists());
138                    }
139                })).booleanValue();
140    }
141
142    long getLastModified(final File f) {
143        return ((Long)
144                AccessController.doPrivileged(new PrivilegedAction() {
145                    public Object run() {
146                        return new Long(f.lastModified());
147                    }
148                })).longValue();
149    }
150
151    private SecuritySupport () {}
152}
153