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.4 2004/04/01 18:45:30 minchau Exp $
018: */
019:
020: package org.apache.xml.serializer;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.InputStream;
026: import java.security.AccessController;
027: import java.security.PrivilegedAction;
028: import java.security.PrivilegedActionException;
029: import java.security.PrivilegedExceptionAction;
030:
031: /**
032: * This class is duplicated for each Xalan-Java subpackage so keep it in sync.
033: * It is package private and therefore is not exposed as part of the Xalan-Java
034: * API.
035: *
036: * Security related methods that only work on J2SE 1.2 and newer.
037: */
038: class SecuritySupport12 extends SecuritySupport {
039:
040: ClassLoader getContextClassLoader() {
041: return (ClassLoader) AccessController
042: .doPrivileged(new PrivilegedAction() {
043: public Object run() {
044: ClassLoader cl = null;
045: try {
046: cl = Thread.currentThread()
047: .getContextClassLoader();
048: } catch (SecurityException ex) {
049: }
050: return cl;
051: }
052: });
053: }
054:
055: ClassLoader getSystemClassLoader() {
056: return (ClassLoader) AccessController
057: .doPrivileged(new PrivilegedAction() {
058: public Object run() {
059: ClassLoader cl = null;
060: try {
061: cl = ClassLoader.getSystemClassLoader();
062: } catch (SecurityException ex) {
063: }
064: return cl;
065: }
066: });
067: }
068:
069: ClassLoader getParentClassLoader(final ClassLoader cl) {
070: return (ClassLoader) AccessController
071: .doPrivileged(new PrivilegedAction() {
072: public Object run() {
073: ClassLoader parent = null;
074: try {
075: parent = cl.getParent();
076: } catch (SecurityException ex) {
077: }
078:
079: // eliminate loops in case of the boot
080: // ClassLoader returning itself as a parent
081: return (parent == cl) ? null : parent;
082: }
083: });
084: }
085:
086: String getSystemProperty(final String propName) {
087: return (String) AccessController
088: .doPrivileged(new PrivilegedAction() {
089: public Object run() {
090: return System.getProperty(propName);
091: }
092: });
093: }
094:
095: FileInputStream getFileInputStream(final File file)
096: throws FileNotFoundException {
097: try {
098: return (FileInputStream) AccessController
099: .doPrivileged(new PrivilegedExceptionAction() {
100: public Object run()
101: throws FileNotFoundException {
102: return new FileInputStream(file);
103: }
104: });
105: } catch (PrivilegedActionException e) {
106: throw (FileNotFoundException) e.getException();
107: }
108: }
109:
110: InputStream getResourceAsStream(final ClassLoader cl,
111: final String name) {
112: return (InputStream) AccessController
113: .doPrivileged(new PrivilegedAction() {
114: public Object run() {
115: InputStream ris;
116: if (cl == null) {
117: ris = ClassLoader
118: .getSystemResourceAsStream(name);
119: } else {
120: ris = cl.getResourceAsStream(name);
121: }
122: return ris;
123: }
124: });
125: }
126:
127: boolean getFileExists(final File f) {
128: return ((Boolean) AccessController
129: .doPrivileged(new PrivilegedAction() {
130: public Object run() {
131: return new Boolean(f.exists());
132: }
133: })).booleanValue();
134: }
135:
136: long getLastModified(final File f) {
137: return ((Long) AccessController
138: .doPrivileged(new PrivilegedAction() {
139: public Object run() {
140: return new Long(f.lastModified());
141: }
142: })).longValue();
143: }
144:
145: }
|