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