~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to src/org/apache/xerces/impl/dv/SecuritySupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * The Apache Software License, Version 1.1
3
 
 *
4
 
 *
5
 
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
6
 
 * reserved.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 *
12
 
 * 1. Redistributions of source code must retain the above copyright
13
 
 *    notice, this list of conditions and the following disclaimer.
14
 
 *
15
 
 * 2. Redistributions in binary form must reproduce the above copyright
16
 
 *    notice, this list of conditions and the following disclaimer in
17
 
 *    the documentation and/or other materials provided with the
18
 
 *    distribution.
19
 
 *
20
 
 * 3. The end-user documentation included with the redistribution,
21
 
 *    if any, must include the following acknowledgment:
22
 
 *       "This product includes software developed by the
23
 
 *        Apache Software Foundation (http://www.apache.org/)."
24
 
 *    Alternately, this acknowledgment may appear in the software itself,
25
 
 *    if and wherever such third-party acknowledgments normally appear.
26
 
 *
27
 
 * 4. The name "Apache Software Foundation" must not be used to endorse or
28
 
 *    promote products derived from this software without prior written
29
 
 *    permission. For written permission, please contact apache@apache.org.
30
 
 *
31
 
 * 5. Products derived from this software may not be called "Apache",
32
 
 *    nor may "Apache" appear in their name, without prior written
33
 
 *    permission of the Apache Software Foundation.
34
 
 *
35
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
 
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
 
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46
 
 * SUCH DAMAGE.
47
 
 * ====================================================================
48
 
 *
49
 
 * This software consists of voluntary contributions made by many
50
 
 * individuals on behalf of the Apache Software Foundation and was
51
 
 * originally based on software copyright (c) 1999-2002, Sun Microsystems,
52
 
 * Inc., http://www.sun.com.  For more information on the Apache Software
53
 
 * Foundation, please see <http://www.apache.org/>.
 
2
 * Copyright 2002,2004,2006 The Apache Software Foundation.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
54
15
 */
55
16
 
56
17
package org.apache.xerces.impl.dv;
57
18
 
58
 
import java.io.*;
 
19
import java.io.File;
 
20
import java.io.FileInputStream;
 
21
import java.io.FileNotFoundException;
 
22
import java.io.InputStream;
 
23
 
 
24
import java.security.AccessController;
 
25
import java.security.PrivilegedAction;
 
26
import java.security.PrivilegedActionException;
 
27
import java.security.PrivilegedExceptionAction;
59
28
 
60
29
/**
61
 
 * This class is duplicated for each JAXP subpackage so keep it in sync.
62
 
 * It is package private and therefore is not exposed as part of the JAXP
63
 
 * API.
64
 
 *
65
 
 * Base class with security related methods that work on JDK 1.1.
 
30
 * This class is duplicated for each subpackage so keep it in sync.
 
31
 * It is package private and therefore is not exposed as part of any API.
 
32
 * 
 
33
 * @xerces.internal
66
34
 */
67
 
class SecuritySupport {
68
 
 
69
 
    /*
70
 
     * Make this of type Object so that the verifier won't try to
71
 
     * prove its type, thus possibly trying to load the SecuritySupport12
72
 
     * class.
73
 
     */
74
 
    private static final Object securitySupport;
75
 
 
76
 
    static {
77
 
        SecuritySupport ss = null;
78
 
        try {
79
 
            Class c = Class.forName("java.security.AccessController");
80
 
            // if that worked, we're on 1.2.
81
 
            /*
82
 
            // don't reference the class explicitly so it doesn't
83
 
            // get dragged in accidentally.
84
 
            c = Class.forName("javax.mail.SecuritySupport12");
85
 
            Constructor cons = c.getConstructor(new Class[] { });
86
 
            ss = (SecuritySupport)cons.newInstance(new Object[] { });
87
 
            */
88
 
            /*
89
 
             * Unfortunately, we can't load the class using reflection
90
 
             * because the class is package private.  And the class has
91
 
             * to be package private so the APIs aren't exposed to other
92
 
             * code that could use them to circumvent security.  Thus,
93
 
             * we accept the risk that the direct reference might fail
94
 
             * on some JDK 1.1 JVMs, even though we would never execute
95
 
             * this code in such a case.  Sigh...
96
 
             */
97
 
            ss = new SecuritySupport12();
98
 
        } catch (Exception ex) {
99
 
            // ignore it
100
 
        } finally {
101
 
            if (ss == null)
102
 
                ss = new SecuritySupport();
103
 
            securitySupport = ss;
104
 
        }
105
 
    }
 
35
final class SecuritySupport {
 
36
 
 
37
    private static final SecuritySupport securitySupport = new SecuritySupport();
106
38
 
107
39
    /**
108
 
     * Return an appropriate instance of this class, depending on whether
109
 
     * we're on a JDK 1.1 or J2SE 1.2 (or later) system.
 
40
     * Return an instance of this class.
110
41
     */
111
42
    static SecuritySupport getInstance() {
112
 
        return (SecuritySupport)securitySupport;
 
43
        return securitySupport;
113
44
    }
114
45
 
115
46
    ClassLoader getContextClassLoader() {
116
 
        return null;
 
47
        return (ClassLoader)
 
48
        AccessController.doPrivileged(new PrivilegedAction() {
 
49
            public Object run() {
 
50
                ClassLoader cl = null;
 
51
                try {
 
52
                    cl = Thread.currentThread().getContextClassLoader();
 
53
                } catch (SecurityException ex) { }
 
54
                return cl;
 
55
            }
 
56
        });
117
57
    }
118
 
 
 
58
    
119
59
    ClassLoader getSystemClassLoader() {
120
 
        return null;
121
 
    }
122
 
 
123
 
    ClassLoader getParentClassLoader(ClassLoader cl) {
124
 
        return null;
125
 
    }
126
 
 
127
 
    String getSystemProperty(String propName) {
128
 
        return System.getProperty(propName);
129
 
    }
130
 
 
131
 
    FileInputStream getFileInputStream(File file)
132
 
        throws FileNotFoundException
 
60
        return (ClassLoader)
 
61
        AccessController.doPrivileged(new PrivilegedAction() {
 
62
            public Object run() {
 
63
                ClassLoader cl = null;
 
64
                try {
 
65
                    cl = ClassLoader.getSystemClassLoader();
 
66
                } catch (SecurityException ex) {}
 
67
                return cl;
 
68
            }
 
69
        });
 
70
    }
 
71
    
 
72
    ClassLoader getParentClassLoader(final ClassLoader cl) {
 
73
        return (ClassLoader)
 
74
        AccessController.doPrivileged(new PrivilegedAction() {
 
75
            public Object run() {
 
76
                ClassLoader parent = null;
 
77
                try {
 
78
                    parent = cl.getParent();
 
79
                } catch (SecurityException ex) {}
 
80
                
 
81
                // eliminate loops in case of the boot
 
82
                // ClassLoader returning itself as a parent
 
83
                return (parent == cl) ? null : parent;
 
84
            }
 
85
        });
 
86
    }
 
87
    
 
88
    String getSystemProperty(final String propName) {
 
89
        return (String)
 
90
        AccessController.doPrivileged(new PrivilegedAction() {
 
91
            public Object run() {
 
92
                return System.getProperty(propName);
 
93
            }
 
94
        });
 
95
    }
 
96
    
 
97
    FileInputStream getFileInputStream(final File file)
 
98
    throws FileNotFoundException
133
99
    {
134
 
        return new FileInputStream(file);
135
 
    }
136
 
 
137
 
    InputStream getResourceAsStream(ClassLoader cl, String name) {
138
 
        InputStream ris;
139
 
        if (cl == null) {
140
 
            ris = ClassLoader.getSystemResourceAsStream(name);
141
 
        } else {
142
 
            ris = cl.getResourceAsStream(name);
 
100
        try {
 
101
            return (FileInputStream)
 
102
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
 
103
                public Object run() throws FileNotFoundException {
 
104
                    return new FileInputStream(file);
 
105
                }
 
106
            });
 
107
        } catch (PrivilegedActionException e) {
 
108
            throw (FileNotFoundException)e.getException();
143
109
        }
144
 
        return ris;
145
 
    }
146
 
 
147
 
    boolean getFileExists(File f) {
148
 
        return f.exists();
149
 
    }
150
 
 
151
 
    long getLastModified(File f) {
152
 
        return f.lastModified();
153
 
    }
 
110
    }
 
111
    
 
112
    InputStream getResourceAsStream(final ClassLoader cl,
 
113
            final String name)
 
114
    {
 
115
        return (InputStream)
 
116
        AccessController.doPrivileged(new PrivilegedAction() {
 
117
            public Object run() {
 
118
                InputStream ris;
 
119
                if (cl == null) {
 
120
                    ris = ClassLoader.getSystemResourceAsStream(name);
 
121
                } else {
 
122
                    ris = cl.getResourceAsStream(name);
 
123
                }
 
124
                return ris;
 
125
            }
 
126
        });
 
127
    }
 
128
    
 
129
    boolean getFileExists(final File f) {
 
130
        return ((Boolean)
 
131
                AccessController.doPrivileged(new PrivilegedAction() {
 
132
                    public Object run() {
 
133
                        return f.exists() ? Boolean.TRUE : Boolean.FALSE;
 
134
                    }
 
135
                })).booleanValue();
 
136
    }
 
137
    
 
138
    long getLastModified(final File f) {
 
139
        return ((Long)
 
140
                AccessController.doPrivileged(new PrivilegedAction() {
 
141
                    public Object run() {
 
142
                        return new Long(f.lastModified());
 
143
                    }
 
144
                })).longValue();
 
145
    }
 
146
    
 
147
    private SecuritySupport () {}
154
148
}