1 /* 2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.activation; 27 28 import java.security.*; 29 import java.net.*; 30 import java.io.*; 31 import java.util.*; 32 33 /** 34 * Security related methods that only work on J2SE 1.2 and newer. 35 * 36 * @since 1.6 37 */ 38 class SecuritySupport { 39 40 private SecuritySupport() { 41 // private constructor, can't create an instance 42 } 43 44 public static ClassLoader getContextClassLoader() { 45 return (ClassLoader) 46 AccessController.doPrivileged(new PrivilegedAction() { 47 public Object run() { 48 ClassLoader cl = null; 49 try { 50 cl = Thread.currentThread().getContextClassLoader(); 51 } catch (SecurityException ex) { } 52 return cl; 53 } 54 }); 55 } 56 57 public static InputStream getResourceAsStream(final Class c, 58 final String name) throws IOException { 59 try { 60 return (InputStream) 61 AccessController.doPrivileged(new PrivilegedExceptionAction() { 62 public Object run() throws IOException { 63 return c.getResourceAsStream(name); 64 } 65 }); 66 } catch (PrivilegedActionException e) { 67 throw (IOException)e.getException(); 68 } 69 } 70 71 public static URL[] getResources(final ClassLoader cl, final String name) { 72 return (URL[]) 73 AccessController.doPrivileged(new PrivilegedAction() { 74 public Object run() { 75 URL[] ret = null; 76 try { 77 List v = new ArrayList(); 78 Enumeration e = cl.getResources(name); 79 while (e != null && e.hasMoreElements()) { 80 URL url = (URL)e.nextElement(); 81 if (url != null) 82 v.add(url); 83 } 84 if (v.size() > 0) { 85 ret = new URL[v.size()]; 86 ret = (URL[])v.toArray(ret); 87 } 88 } catch (IOException ioex) { 89 } catch (SecurityException ex) { } 90 return ret; 91 } 92 }); 93 } 94 95 public static URL[] getSystemResources(final String name) { 96 return (URL[]) 97 AccessController.doPrivileged(new PrivilegedAction() { 98 public Object run() { 99 URL[] ret = null; 100 try { 101 List v = new ArrayList(); 102 Enumeration e = ClassLoader.getSystemResources(name); 103 while (e != null && e.hasMoreElements()) { 104 URL url = (URL)e.nextElement(); 105 if (url != null) 106 v.add(url); 107 } 108 if (v.size() > 0) { 109 ret = new URL[v.size()]; 110 ret = (URL[])v.toArray(ret); 111 } 112 } catch (IOException ioex) { 113 } catch (SecurityException ex) { } 114 return ret; 115 } 116 }); 117 } 118 119 public static InputStream openStream(final URL url) throws IOException { 120 try { 121 return (InputStream) 122 AccessController.doPrivileged(new PrivilegedExceptionAction() { 123 public Object run() throws IOException { 124 return url.openStream(); 125 } 126 }); 127 } catch (PrivilegedActionException e) { 128 throw (IOException)e.getException(); 129 } 130 } 131 }