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: SecuritySupport12.java 468654 2006-10-28 07:09:23Z minchau $
20 */
21
22package org.apache.xml.serializer;
23
24import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileNotFoundException;
27import java.io.InputStream;
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 Xalan-Java subpackage so keep it in sync.
35 * It is package private and therefore is not exposed as part of the Xalan-Java
36 * API.
37 *
38 * Security related methods that only work on J2SE 1.2 and newer.
39 */
40class SecuritySupport12 extends SecuritySupport {
41
42    ClassLoader getContextClassLoader() {
43        return (ClassLoader)
44                AccessController.doPrivileged(new PrivilegedAction() {
45            public Object run() {
46                ClassLoader cl = null;
47                try {
48                    cl = Thread.currentThread().getContextClassLoader();
49                } catch (SecurityException ex) { }
50                return cl;
51            }
52        });
53    }
54
55    ClassLoader getSystemClassLoader() {
56        return (ClassLoader)
57            AccessController.doPrivileged(new PrivilegedAction() {
58                public Object run() {
59                    ClassLoader cl = null;
60                    try {
61                        cl = ClassLoader.getSystemClassLoader();
62                    } catch (SecurityException ex) {}
63                    return cl;
64                }
65            });
66    }
67
68    ClassLoader getParentClassLoader(final ClassLoader cl) {
69        return (ClassLoader)
70            AccessController.doPrivileged(new PrivilegedAction() {
71                public Object run() {
72                    ClassLoader parent = null;
73                    try {
74                        parent = cl.getParent();
75                    } catch (SecurityException ex) {}
76
77                    // eliminate loops in case of the boot
78                    // ClassLoader returning itself as a parent
79                    return (parent == cl) ? null : parent;
80                }
81            });
82    }
83
84    String getSystemProperty(final String propName) {
85        return (String)
86            AccessController.doPrivileged(new PrivilegedAction() {
87                public Object run() {
88                    return System.getProperty(propName);
89                }
90            });
91    }
92
93    FileInputStream getFileInputStream(final File file)
94        throws FileNotFoundException
95    {
96        try {
97            return (FileInputStream)
98                AccessController.doPrivileged(new PrivilegedExceptionAction() {
99                    public Object run() throws FileNotFoundException {
100                        return new FileInputStream(file);
101                    }
102                });
103        } catch (PrivilegedActionException e) {
104            throw (FileNotFoundException)e.getException();
105        }
106    }
107
108    InputStream getResourceAsStream(final ClassLoader cl,
109                                           final String name)
110    {
111        return (InputStream)
112            AccessController.doPrivileged(new PrivilegedAction() {
113                public Object run() {
114                    InputStream ris;
115                    if (cl == null) {
116                        ris = ClassLoader.getSystemResourceAsStream(name);
117                    } else {
118                        ris = cl.getResourceAsStream(name);
119                    }
120                    return ris;
121                }
122            });
123    }
124
125    boolean getFileExists(final File f) {
126    return ((Boolean)
127            AccessController.doPrivileged(new PrivilegedAction() {
128                public Object run() {
129                    return new Boolean(f.exists());
130                }
131            })).booleanValue();
132    }
133
134    long getLastModified(final File f) {
135    return ((Long)
136            AccessController.doPrivileged(new PrivilegedAction() {
137                public Object run() {
138                    return new Long(f.lastModified());
139                }
140            })).longValue();
141    }
142
143}
144