jaxp/src/share/classes/javax/xml/parsers/SecuritySupport.java
author jjg
Thu, 19 Jun 2008 15:52:31 -0700
changeset 811 687d51cb403b
parent 6 7f561c08de6b
permissions -rw-r--r--
6716866: some javac regression tests fail to compile with re-orged file manager Reviewed-by: darcy
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 2004-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.parsers;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.security.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.net.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.util.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * This class is duplicated for each JAXP subpackage so keep it in sync.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * It is package private and therefore is not exposed as part of the JAXP
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * API.
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * Security related methods that only work on J2SE 1.2 and newer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
class SecuritySupport  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    ClassLoader getContextClassLoader() throws SecurityException{
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
        return (ClassLoader)
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
                AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
            public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
                ClassLoader cl = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
                //try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
                cl = Thread.currentThread().getContextClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
                //} catch (SecurityException ex) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
                if (cl == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
                    cl = ClassLoader.getSystemClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
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 getResourceAsStream(final ClassLoader cl,
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                                           final String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        return (InputStream)
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
            AccessController.doPrivileged(new PrivilegedAction() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
                public Object run() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                    InputStream ris;
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                    if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                        ris = ClassLoader.getSystemResourceAsStream(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                        ris = cl.getResourceAsStream(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                    return ris;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            });
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    boolean doesFileExist(final File f) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    return ((Boolean)
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
                    return new Boolean(f.exists());
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            })).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
}