001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: SecuritySupport.java 1225414 2011-12-29 02:38:30Z mrglavas $
020     */
021    
022    package org.apache.xalan.extensions;
023    
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileNotFoundException;
027    import java.io.InputStream;
028    import java.security.AccessController;
029    import java.security.PrivilegedAction;
030    import java.security.PrivilegedActionException;
031    import java.security.PrivilegedExceptionAction;
032    
033    /**
034     * This class is duplicated for each Xalan-Java subpackage so keep it in sync.
035     * It is package private and therefore is not exposed as part of the Xalan-Java
036     * API.
037     *
038     * Security related methods that only work on J2SE 1.2 and newer.
039     */
040    final class SecuritySupport {
041    
042        static ClassLoader getContextClassLoader() {
043            return (ClassLoader)
044                    AccessController.doPrivileged(new PrivilegedAction() {
045                public Object run() {
046                    ClassLoader cl = null;
047                    try {
048                        cl = Thread.currentThread().getContextClassLoader();
049                    } catch (SecurityException ex) { }
050                    return cl;
051                }
052            });
053        }
054    
055        static ClassLoader getSystemClassLoader() {
056            return (ClassLoader)
057                AccessController.doPrivileged(new PrivilegedAction() {
058                    public Object run() {
059                        ClassLoader cl = null;
060                        try {
061                            cl = ClassLoader.getSystemClassLoader();
062                        } catch (SecurityException ex) {}
063                        return cl;
064                    }
065                });
066        }
067    
068        static ClassLoader getParentClassLoader(final ClassLoader cl) {
069            return (ClassLoader)
070                AccessController.doPrivileged(new PrivilegedAction() {
071                    public Object run() {
072                        ClassLoader parent = null;
073                        try {
074                            parent = cl.getParent();
075                        } catch (SecurityException ex) {}
076    
077                        // eliminate loops in case of the boot
078                        // ClassLoader returning itself as a parent
079                        return (parent == cl) ? null : parent;
080                    }
081                });
082        }
083    
084        static String getSystemProperty(final String propName) {
085            return (String)
086                AccessController.doPrivileged(new PrivilegedAction() {
087                    public Object run() {
088                        return System.getProperty(propName);
089                    }
090                });
091        }
092    
093        static FileInputStream getFileInputStream(final File file)
094            throws FileNotFoundException
095        {
096            try {
097                return (FileInputStream)
098                    AccessController.doPrivileged(new PrivilegedExceptionAction() {
099                        public Object run() throws FileNotFoundException {
100                            return new FileInputStream(file);
101                        }
102                    });
103            } catch (PrivilegedActionException e) {
104                throw (FileNotFoundException)e.getException();
105            }
106        }
107    
108        static InputStream getResourceAsStream(final ClassLoader cl,
109                                               final String name)
110        {
111            return (InputStream)
112                AccessController.doPrivileged(new PrivilegedAction() {
113                    public Object run() {
114                        InputStream ris;
115                        if (cl == null) {
116                            ris = ClassLoader.getSystemResourceAsStream(name);
117                        } else {
118                            ris = cl.getResourceAsStream(name);
119                        }
120                        return ris;
121                    }
122                });
123        }
124        
125        static boolean getFileExists(final File f) {
126        return ((Boolean)
127                AccessController.doPrivileged(new PrivilegedAction() {
128                    public Object run() {
129                        return f.exists() ? Boolean.TRUE : Boolean.FALSE;
130                    }
131                })).booleanValue();
132        }
133        
134        static long getLastModified(final File f) {
135        return ((Long)
136                AccessController.doPrivileged(new PrivilegedAction() {
137                    public Object run() {
138                        return new Long(f.lastModified());
139                    }
140                })).longValue();
141        }
142        
143        private SecuritySupport () {}
144    }