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