1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: SecuritySupport12.java,v 1.1.1.1 2004/02/26 11:40:43 vk112360 Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.compiler;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.InputStream;
  24. import java.security.AccessController;
  25. import java.security.PrivilegedAction;
  26. import java.security.PrivilegedActionException;
  27. import java.security.PrivilegedExceptionAction;
  28. import java.util.Properties;
  29. /**
  30. * This class is duplicated for each Xalan-Java subpackage so keep it in sync.
  31. * It is package private and therefore is not exposed as part of the Xalan-Java
  32. * API.
  33. *
  34. * Security related methods that only work on J2SE 1.2 and newer.
  35. */
  36. class SecuritySupport12 extends SecuritySupport {
  37. ClassLoader getContextClassLoader() {
  38. return (ClassLoader)
  39. AccessController.doPrivileged(new PrivilegedAction() {
  40. public Object run() {
  41. ClassLoader cl = null;
  42. try {
  43. cl = Thread.currentThread().getContextClassLoader();
  44. } catch (SecurityException ex) { }
  45. return cl;
  46. }
  47. });
  48. }
  49. ClassLoader getSystemClassLoader() {
  50. return (ClassLoader)
  51. AccessController.doPrivileged(new PrivilegedAction() {
  52. public Object run() {
  53. ClassLoader cl = null;
  54. try {
  55. cl = ClassLoader.getSystemClassLoader();
  56. } catch (SecurityException ex) {}
  57. return cl;
  58. }
  59. });
  60. }
  61. ClassLoader getParentClassLoader(final ClassLoader cl) {
  62. return (ClassLoader)
  63. AccessController.doPrivileged(new PrivilegedAction() {
  64. public Object run() {
  65. ClassLoader parent = null;
  66. try {
  67. parent = cl.getParent();
  68. } catch (SecurityException ex) {}
  69. // eliminate loops in case of the boot
  70. // ClassLoader returning itself as a parent
  71. return (parent == cl) ? null : parent;
  72. }
  73. });
  74. }
  75. String getSystemProperty(final String propName) {
  76. return (String)
  77. AccessController.doPrivileged(new PrivilegedAction() {
  78. public Object run() {
  79. return System.getProperty(propName);
  80. }
  81. });
  82. }
  83. FileInputStream getFileInputStream(final File file)
  84. throws FileNotFoundException
  85. {
  86. try {
  87. return (FileInputStream)
  88. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  89. public Object run() throws FileNotFoundException {
  90. return new FileInputStream(file);
  91. }
  92. });
  93. } catch (PrivilegedActionException e) {
  94. throw (FileNotFoundException)e.getException();
  95. }
  96. }
  97. InputStream getResourceAsStream(final ClassLoader cl,
  98. final String name)
  99. {
  100. return (InputStream)
  101. AccessController.doPrivileged(new PrivilegedAction() {
  102. public Object run() {
  103. InputStream ris;
  104. if (cl == null) {
  105. ris = ClassLoader.getSystemResourceAsStream(name);
  106. } else {
  107. ris = cl.getResourceAsStream(name);
  108. }
  109. return ris;
  110. }
  111. });
  112. }
  113. boolean getFileExists(final File f) {
  114. return ((Boolean)
  115. AccessController.doPrivileged(new PrivilegedAction() {
  116. public Object run() {
  117. return new Boolean(f.exists());
  118. }
  119. })).booleanValue();
  120. }
  121. long getLastModified(final File f) {
  122. return ((Long)
  123. AccessController.doPrivileged(new PrivilegedAction() {
  124. public Object run() {
  125. return new Long(f.lastModified());
  126. }
  127. })).longValue();
  128. }
  129. }