1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id$
20 */
21
22package org.apache.xml.utils;
23
24import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileNotFoundException;
27import java.io.InputStream;
28
29import java.security.AccessController;
30import java.security.PrivilegedAction;
31import java.security.PrivilegedActionException;
32import java.security.PrivilegedExceptionAction;
33
34import java.util.Properties;
35
36/**
37 * This class is duplicated for each Xalan-Java subpackage so keep it in sync.
38 * It is package private and therefore is not exposed as part of the Xalan-Java
39 * API.
40 *
41 * Security related methods that only work on J2SE 1.2 and newer.
42 */
43class SecuritySupport12 extends SecuritySupport {
44
45    ClassLoader getContextClassLoader() {
46        return (ClassLoader)
47                AccessController.doPrivileged(new PrivilegedAction() {
48            public Object run() {
49                ClassLoader cl = null;
50                try {
51                    cl = Thread.currentThread().getContextClassLoader();
52                } catch (SecurityException ex) { }
53                return cl;
54            }
55        });
56    }
57
58    ClassLoader getSystemClassLoader() {
59        return (ClassLoader)
60            AccessController.doPrivileged(new PrivilegedAction() {
61                public Object run() {
62                    ClassLoader cl = null;
63                    try {
64                        cl = ClassLoader.getSystemClassLoader();
65                    } catch (SecurityException ex) {}
66                    return cl;
67                }
68            });
69    }
70
71    ClassLoader getParentClassLoader(final ClassLoader cl) {
72        return (ClassLoader)
73            AccessController.doPrivileged(new PrivilegedAction() {
74                public Object run() {
75                    ClassLoader parent = null;
76                    try {
77                        parent = cl.getParent();
78                    } catch (SecurityException ex) {}
79
80                    // eliminate loops in case of the boot
81                    // ClassLoader returning itself as a parent
82                    return (parent == cl) ? null : parent;
83                }
84            });
85    }
86
87    String getSystemProperty(final String propName) {
88        return (String)
89            AccessController.doPrivileged(new PrivilegedAction() {
90                public Object run() {
91                    return System.getProperty(propName);
92                }
93            });
94    }
95
96    FileInputStream getFileInputStream(final File file)
97        throws FileNotFoundException
98    {
99        try {
100            return (FileInputStream)
101                AccessController.doPrivileged(new PrivilegedExceptionAction() {
102                    public Object run() throws FileNotFoundException {
103                        return new FileInputStream(file);
104                    }
105                });
106        } catch (PrivilegedActionException e) {
107            throw (FileNotFoundException)e.getException();
108        }
109    }
110
111    InputStream getResourceAsStream(final ClassLoader cl,
112                                           final String name)
113    {
114        return (InputStream)
115            AccessController.doPrivileged(new PrivilegedAction() {
116                public Object run() {
117                    InputStream ris;
118                    if (cl == null) {
119                        ris = ClassLoader.getSystemResourceAsStream(name);
120                    } else {
121                        ris = cl.getResourceAsStream(name);
122                    }
123                    return ris;
124                }
125            });
126    }
127
128    boolean getFileExists(final File f) {
129    return ((Boolean)
130            AccessController.doPrivileged(new PrivilegedAction() {
131                public Object run() {
132                    return new Boolean(f.exists());
133                }
134            })).booleanValue();
135    }
136
137    long getLastModified(final File f) {
138    return ((Long)
139            AccessController.doPrivileged(new PrivilegedAction() {
140                public Object run() {
141                    return new Long(f.lastModified());
142                }
143            })).longValue();
144    }
145
146}
147