jaxp/src/share/classes/javax/xml/validation/SecuritySupport.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 * have any questions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
package javax.xml.validation;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.security.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.net.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.util.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * This class is duplicated for each JAXP subpackage so keep it in sync.
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * It is package private and therefore is not exposed as part of the JAXP
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * API.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * Security related methods that only work on J2SE 1.2 and newer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
class SecuritySupport  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    ClassLoader getContextClassLoader() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
        return (ClassLoader)
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
        AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
            public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
                ClassLoader cl = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
                //try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
                cl = Thread.currentThread().getContextClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
                //} catch (SecurityException ex) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
                if (cl == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
                    cl = ClassLoader.getSystemClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
                return cl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        });
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    String getSystemProperty(final String propName) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        return (String)
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
            AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
                public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
                    return System.getProperty(propName);
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
            });
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    FileInputStream getFileInputStream(final File file)
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        throws FileNotFoundException
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            return (FileInputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
                AccessController.doPrivileged(new PrivilegedExceptionAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                    public Object run() throws FileNotFoundException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
                        return new FileInputStream(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
                });
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        } catch (PrivilegedActionException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            throw (FileNotFoundException)e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    InputStream getURLInputStream(final URL url)
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
            return (InputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
                AccessController.doPrivileged(new PrivilegedExceptionAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                    public Object run() throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                        return url.openStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                });
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        } catch (PrivilegedActionException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
            throw (IOException)e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    URL getResourceAsURL(final ClassLoader cl,
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
                                           final String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        return (URL)
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                    URL url;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
                    if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
                        url = ClassLoader.getSystemResource(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                        url = cl.getResource(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
                    return url;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            });
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    Enumeration getResources(final ClassLoader cl,
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
                                           final String name) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        try{
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        return (Enumeration)
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
                public Object run() throws IOException{
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
                    Enumeration enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
                    if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
                        enumeration = ClassLoader.getSystemResources(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
                        enumeration = cl.getResources(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                    return enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            });
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        }catch(PrivilegedActionException e){
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
            throw (IOException)e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    InputStream getResourceAsStream(final ClassLoader cl,
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
                                           final String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        return (InputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
            AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
                    InputStream ris;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
                    if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                        ris = ClassLoader.getSystemResourceAsStream(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                        ris = cl.getResourceAsStream(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
                    return ris;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            });
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    boolean doesFileExist(final File f) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    return ((Boolean)
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
                public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
                    return new Boolean(f.exists());
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
            })).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
}