jaxp/src/java.xml/share/classes/javax/xml/xpath/SecuritySupport.java
changeset 31288 204ed502eb8a
parent 31287 d6581a7c221f
child 31497 4a6b2e733c0d
equal deleted inserted replaced
31287:d6581a7c221f 31288:204ed502eb8a
     1 /*
     1 /*
     2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package javax.xml.xpath;
    26 package javax.xml.xpath;
    27 
    27 
    28 import java.net.URL;
    28 import java.net.URL;
    29 import java.security.*;
    29 import java.security.*;
    30 import java.io.*;
    30 import java.io.*;
    31 import java.util.*;
       
    32 
    31 
    33 /**
    32 /**
    34  * This class is duplicated for each JAXP subpackage so keep it in sync.
    33  * This class is duplicated for each JAXP subpackage so keep it in sync.
    35  * It is package private and therefore is not exposed as part of the JAXP
    34  * It is package private and therefore is not exposed as part of the JAXP
    36  * API.
    35  * API.
    39  */
    38  */
    40 class SecuritySupport  {
    39 class SecuritySupport  {
    41 
    40 
    42 
    41 
    43     ClassLoader getContextClassLoader() {
    42     ClassLoader getContextClassLoader() {
    44         return (ClassLoader)
    43         return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
    45                 AccessController.doPrivileged(new PrivilegedAction() {
    44             @Override
    46             public Object run() {
    45             public ClassLoader run() {
    47                 ClassLoader cl = null;
    46                 ClassLoader cl = null;
    48                 try {
    47                 try {
    49                     cl = Thread.currentThread().getContextClassLoader();
    48                     cl = Thread.currentThread().getContextClassLoader();
    50                 } catch (SecurityException ex) { }
    49                 } catch (SecurityException ex) { }
    51                 return cl;
    50                 return cl;
    52             }
    51             }
    53         });
    52         });
    54     }
    53     }
    55 
    54 
    56     String getSystemProperty(final String propName) {
    55     String getSystemProperty(final String propName) {
    57         return (String)
    56         return AccessController.doPrivileged(new PrivilegedAction<String>() {
    58             AccessController.doPrivileged(new PrivilegedAction() {
    57             @Override
    59                 public Object run() {
    58             public String run() {
    60                     return System.getProperty(propName);
    59                 return System.getProperty(propName);
    61                 }
    60             }
    62             });
    61         });
    63     }
    62     }
    64 
    63 
    65     FileInputStream getFileInputStream(final File file)
    64     FileInputStream getFileInputStream(final File file)
    66         throws FileNotFoundException
    65         throws FileNotFoundException
    67     {
    66     {
    68         try {
    67         try {
    69             return (FileInputStream)
    68             return AccessController.doPrivileged(
    70                 AccessController.doPrivileged(new PrivilegedExceptionAction() {
    69                 new PrivilegedExceptionAction<FileInputStream>() {
    71                     public Object run() throws FileNotFoundException {
    70                     @Override
       
    71                     public FileInputStream run() throws FileNotFoundException {
    72                         return new FileInputStream(file);
    72                         return new FileInputStream(file);
    73                     }
    73                     }
    74                 });
    74                 });
    75         } catch (PrivilegedActionException e) {
    75         } catch (PrivilegedActionException e) {
    76             throw (FileNotFoundException)e.getException();
    76             throw (FileNotFoundException)e.getException();
    77         }
    77         }
    78     }
    78     }
    79 
    79 
    80     InputStream getURLInputStream(final URL url)
       
    81         throws IOException
       
    82     {
       
    83         try {
       
    84             return (InputStream)
       
    85                 AccessController.doPrivileged(new PrivilegedExceptionAction() {
       
    86                     public Object run() throws IOException {
       
    87                         return url.openStream();
       
    88                     }
       
    89                 });
       
    90         } catch (PrivilegedActionException e) {
       
    91             throw (IOException)e.getException();
       
    92         }
       
    93     }
       
    94 
       
    95     // Used for debugging purposes
    80     // Used for debugging purposes
    96     String getClassSource(Class<?> cls) {
    81     String getClassSource(Class<?> cls) {
    97         return AccessController.doPrivileged(new PrivilegedAction<String>() {
    82         return AccessController.doPrivileged(new PrivilegedAction<String>() {
       
    83             @Override
    98             public String run() {
    84             public String run() {
    99                CodeSource cs = cls.getProtectionDomain().getCodeSource();
    85                 CodeSource cs = cls.getProtectionDomain().getCodeSource();
   100                if (cs != null) {
    86                 if (cs != null) {
   101                     return cs.getLocation().toString();
    87                    URL loc = cs.getLocation();
   102                } else {
    88                    return loc != null ? loc.toString() : "(no location)";
       
    89                 } else {
   103                    return "(no code source)";
    90                    return "(no code source)";
   104                }
    91                 }
   105             }
    92             }
   106         });
    93         });
   107     }
    94     }
   108 
    95 
   109     boolean doesFileExist(final File f) {
    96     boolean doesFileExist(final File f) {
   110         return ((Boolean)
    97         return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
   111             AccessController.doPrivileged(new PrivilegedAction() {
    98             @Override
   112                 public Object run() {
    99             public Boolean run() {
   113                     return new Boolean(f.exists());
   100                 return f.exists();
   114                 }
   101             }
   115             })).booleanValue();
   102         });
   116     }
   103     }
   117 
   104 
   118 }
   105 }