src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/SecuritySupport.java
changeset 47312 d4f959806fe9
parent 47311 ff631a3cadbc
child 47313 eb28be8f935d
equal deleted inserted replaced
47311:ff631a3cadbc 47312:d4f959806fe9
     1 /*
       
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  */
       
     4 /*
       
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     6  * contributor license agreements.  See the NOTICE file distributed with
       
     7  * this work for additional information regarding copyright ownership.
       
     8  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     9  * (the "License"); you may not use this file except in compliance with
       
    10  * the License.  You may obtain a copy of the License at
       
    11  *
       
    12  *      http://www.apache.org/licenses/LICENSE-2.0
       
    13  *
       
    14  * Unless required by applicable law or agreed to in writing, software
       
    15  * distributed under the License is distributed on an "AS IS" BASIS,
       
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    17  * See the License for the specific language governing permissions and
       
    18  * limitations under the License.
       
    19  */
       
    20 
       
    21 package com.sun.org.apache.xerces.internal.xinclude;
       
    22 
       
    23 import java.io.File;
       
    24 import java.io.FileInputStream;
       
    25 import java.io.FileNotFoundException;
       
    26 
       
    27 import java.security.AccessController;
       
    28 import java.security.PrivilegedAction;
       
    29 import java.security.PrivilegedActionException;
       
    30 import java.security.PrivilegedExceptionAction;
       
    31 
       
    32 /**
       
    33  * This class is duplicated for each subpackage so keep it in sync.
       
    34  * It is package private and therefore is not exposed as part of any API.
       
    35  *
       
    36  * @xerces.internal
       
    37  */
       
    38 final class SecuritySupport {
       
    39 
       
    40     private static final SecuritySupport securitySupport = new SecuritySupport();
       
    41 
       
    42     /**
       
    43      * Return an instance of this class.
       
    44      */
       
    45     static SecuritySupport getInstance() {
       
    46         return securitySupport;
       
    47     }
       
    48 
       
    49     ClassLoader getContextClassLoader() {
       
    50         return (ClassLoader)
       
    51         AccessController.doPrivileged(new PrivilegedAction() {
       
    52             public Object run() {
       
    53                 ClassLoader cl = null;
       
    54                 try {
       
    55                     cl = Thread.currentThread().getContextClassLoader();
       
    56                 } catch (SecurityException ex) { }
       
    57                 return cl;
       
    58             }
       
    59         });
       
    60     }
       
    61 
       
    62     ClassLoader getSystemClassLoader() {
       
    63         return (ClassLoader)
       
    64         AccessController.doPrivileged(new PrivilegedAction() {
       
    65             public Object run() {
       
    66                 ClassLoader cl = null;
       
    67                 try {
       
    68                     cl = ClassLoader.getSystemClassLoader();
       
    69                 } catch (SecurityException ex) {}
       
    70                 return cl;
       
    71             }
       
    72         });
       
    73     }
       
    74 
       
    75     ClassLoader getParentClassLoader(final ClassLoader cl) {
       
    76         return (ClassLoader)
       
    77         AccessController.doPrivileged(new PrivilegedAction() {
       
    78             public Object run() {
       
    79                 ClassLoader parent = null;
       
    80                 try {
       
    81                     parent = cl.getParent();
       
    82                 } catch (SecurityException ex) {}
       
    83 
       
    84                 // eliminate loops in case of the boot
       
    85                 // ClassLoader returning itself as a parent
       
    86                 return (parent == cl) ? null : parent;
       
    87             }
       
    88         });
       
    89     }
       
    90 
       
    91     String getSystemProperty(final String propName) {
       
    92         return (String)
       
    93         AccessController.doPrivileged(new PrivilegedAction() {
       
    94             public Object run() {
       
    95                 return System.getProperty(propName);
       
    96             }
       
    97         });
       
    98     }
       
    99 
       
   100     FileInputStream getFileInputStream(final File file)
       
   101     throws FileNotFoundException
       
   102     {
       
   103         try {
       
   104             return (FileInputStream)
       
   105             AccessController.doPrivileged(new PrivilegedExceptionAction() {
       
   106                 public Object run() throws FileNotFoundException {
       
   107                     return new FileInputStream(file);
       
   108                 }
       
   109             });
       
   110         } catch (PrivilegedActionException e) {
       
   111             throw (FileNotFoundException)e.getException();
       
   112         }
       
   113     }
       
   114 
       
   115     boolean getFileExists(final File f) {
       
   116         return ((Boolean)
       
   117                 AccessController.doPrivileged(new PrivilegedAction() {
       
   118                     public Object run() {
       
   119                         return f.exists();
       
   120                     }
       
   121                 })).booleanValue();
       
   122     }
       
   123 
       
   124     long getLastModified(final File f) {
       
   125         return ((Long)
       
   126                 AccessController.doPrivileged(new PrivilegedAction() {
       
   127                     public Object run() {
       
   128                         return f.lastModified();
       
   129                     }
       
   130                 })).longValue();
       
   131     }
       
   132 
       
   133     private SecuritySupport () {}
       
   134 }