< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/JSSecurityManager.java

Print this page




   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 com.sun.media.sound;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.InputStream;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 


  33 import java.util.ArrayList;
  34 import java.util.Iterator;
  35 import java.util.List;
  36 import java.util.Properties;
  37 import java.util.ServiceLoader;
  38 
  39 import java.security.AccessController;
  40 import java.security.PrivilegedAction;
  41 
  42 import javax.sound.sampled.AudioPermission;
  43 
  44 /** Managing security in the Java Sound implementation.
  45  * This class contains all code that uses and is used by
  46  * SecurityManager.doPrivileged().
  47  *
  48  * @author Matthias Pfisterer
  49  */
  50 final class JSSecurityManager {
  51 
  52     /** Prevent instantiation.
  53      */
  54     private JSSecurityManager() {
  55     }
  56 
  57     /** Checks if the VM currently has a SecurityManager installed.
  58      * Note that this may change over time. So the result of this method
  59      * should not be cached.
  60      *
  61      * @return true if a SecurityManger is installed, false otherwise.
  62      */
  63     private static boolean hasSecurityManager() {
  64         return (System.getSecurityManager() != null);
  65     }
  66 
  67 
  68     static void checkRecordPermission() throws SecurityException {
  69         if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
  70         SecurityManager sm = System.getSecurityManager();
  71         if (sm != null) {
  72             sm.checkPermission(new AudioPermission("record"));
  73         }
  74     }
  75 
  76     /** Load properties from a file.
  77         This method tries to load properties from the filename give into
  78         the passed properties object.
  79         If the file cannot be found or something else goes wrong,
  80         the method silently fails.
  81         @param properties The properties bundle to store the values of the
  82         properties file.
  83         @param filename The filename of the properties file to load. This
  84         filename is interpreted as relative to the subdirectory "conf" in
  85         the JRE directory.
  86      */
  87     static void loadProperties(final Properties properties,
  88                                final String filename) {
  89         if(hasSecurityManager()) {
  90             try {
  91                 // invoke the privileged action using 1.2 security
  92                 PrivilegedAction<Void> action = new PrivilegedAction<Void>() {

  93                         public Void run() {
  94                             loadPropertiesImpl(properties, filename);
  95                             return null;
  96                         }
  97                     };
  98                 AccessController.doPrivileged(action);
  99                 if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
 100             } catch (Exception e) {
 101                 if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
 102                 // try without using JDK 1.2 security
 103                 loadPropertiesImpl(properties, filename);
 104             }
 105         } else {
 106             // not JDK 1.2 security, assume we already have permission
 107             loadPropertiesImpl(properties, filename);
 108         }
 109     }
 110 
 111 
 112     private static void loadPropertiesImpl(Properties properties,
 113                                            String filename) {
 114         if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
 115         String fname = System.getProperty("java.home");
 116         try {
 117             if (fname == null) {
 118                 throw new Error("Can't find java.home ??");
 119             }
 120             File f = new File(fname, "conf");
 121             f = new File(f, filename);
 122             fname = f.getCanonicalPath();
 123             InputStream in = new FileInputStream(fname);
 124             BufferedInputStream bin = new BufferedInputStream(in);
 125             try {
 126                 properties.load(bin);
 127             } finally {
 128                 if (in != null) {
 129                     in.close();
 130                 }
 131             }


 159     }
 160 
 161     static synchronized <T> List<T> getProviders(final Class<T> providerClass) {
 162         List<T> p = new ArrayList<>(7);
 163         // ServiceLoader creates "lazy" iterator instance, but it ensures that
 164         // next/hasNext run with permissions that are restricted by whatever
 165         // creates the ServiceLoader instance, so it requires to be called from
 166         // privileged section
 167         final PrivilegedAction<Iterator<T>> psAction =
 168                 new PrivilegedAction<Iterator<T>>() {
 169                     @Override
 170                     public Iterator<T> run() {
 171                         return ServiceLoader.load(providerClass).iterator();
 172                     }
 173                 };
 174         final Iterator<T> ps = AccessController.doPrivileged(psAction);
 175 
 176         // the iterator's hasNext() method looks through classpath for
 177         // the provider class names, so it requires read permissions
 178         PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {

 179             public Boolean run() {
 180                 return ps.hasNext();
 181             }
 182         };
 183 
 184         while (AccessController.doPrivileged(hasNextAction)) {
 185             try {
 186                 // the iterator's next() method creates instances of the
 187                 // providers and it should be called in the current security
 188                 // context
 189                 T provider = ps.next();
 190                 if (providerClass.isInstance(provider)) {
 191                     // $$mp 2003-08-22
 192                     // Always adding at the beginning reverses the
 193                     // order of the providers. So we no longer have
 194                     // to do this in AudioSystem and MidiSystem.
 195                     p.add(0, provider);
 196                 }
 197             } catch (Throwable t) {
 198                 //$$fb 2002-11-07: do not fail on SPI not found


   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 com.sun.media.sound;
  27 
  28 import java.io.BufferedInputStream;

  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.InputStream;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.util.ArrayList;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Properties;
  38 import java.util.ServiceLoader;
  39 



  40 import javax.sound.sampled.AudioPermission;
  41 
  42 /** Managing security in the Java Sound implementation.
  43  * This class contains all code that uses and is used by
  44  * SecurityManager.doPrivileged().
  45  *
  46  * @author Matthias Pfisterer
  47  */
  48 final class JSSecurityManager {
  49 
  50     /** Prevent instantiation.
  51      */
  52     private JSSecurityManager() {
  53     }
  54 
  55     /** Checks if the VM currently has a SecurityManager installed.
  56      * Note that this may change over time. So the result of this method
  57      * should not be cached.
  58      *
  59      * @return true if a SecurityManger is installed, false otherwise.
  60      */
  61     private static boolean hasSecurityManager() {
  62         return (System.getSecurityManager() != null);
  63     }
  64 

  65     static void checkRecordPermission() throws SecurityException {
  66         if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
  67         SecurityManager sm = System.getSecurityManager();
  68         if (sm != null) {
  69             sm.checkPermission(new AudioPermission("record"));
  70         }
  71     }
  72 
  73     /** Load properties from a file.
  74         This method tries to load properties from the filename give into
  75         the passed properties object.
  76         If the file cannot be found or something else goes wrong,
  77         the method silently fails.
  78         @param properties The properties bundle to store the values of the
  79         properties file.
  80         @param filename The filename of the properties file to load. This
  81         filename is interpreted as relative to the subdirectory "conf" in
  82         the JRE directory.
  83      */
  84     static void loadProperties(final Properties properties,
  85                                final String filename) {
  86         if(hasSecurityManager()) {
  87             try {
  88                 // invoke the privileged action using 1.2 security
  89                 PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
  90                         @Override
  91                         public Void run() {
  92                             loadPropertiesImpl(properties, filename);
  93                             return null;
  94                         }
  95                     };
  96                 AccessController.doPrivileged(action);
  97                 if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
  98             } catch (Exception e) {
  99                 if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
 100                 // try without using JDK 1.2 security
 101                 loadPropertiesImpl(properties, filename);
 102             }
 103         } else {
 104             // not JDK 1.2 security, assume we already have permission
 105             loadPropertiesImpl(properties, filename);
 106         }
 107     }
 108 

 109     private static void loadPropertiesImpl(Properties properties,
 110                                            String filename) {
 111         if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
 112         String fname = System.getProperty("java.home");
 113         try {
 114             if (fname == null) {
 115                 throw new Error("Can't find java.home ??");
 116             }
 117             File f = new File(fname, "conf");
 118             f = new File(f, filename);
 119             fname = f.getCanonicalPath();
 120             InputStream in = new FileInputStream(fname);
 121             BufferedInputStream bin = new BufferedInputStream(in);
 122             try {
 123                 properties.load(bin);
 124             } finally {
 125                 if (in != null) {
 126                     in.close();
 127                 }
 128             }


 156     }
 157 
 158     static synchronized <T> List<T> getProviders(final Class<T> providerClass) {
 159         List<T> p = new ArrayList<>(7);
 160         // ServiceLoader creates "lazy" iterator instance, but it ensures that
 161         // next/hasNext run with permissions that are restricted by whatever
 162         // creates the ServiceLoader instance, so it requires to be called from
 163         // privileged section
 164         final PrivilegedAction<Iterator<T>> psAction =
 165                 new PrivilegedAction<Iterator<T>>() {
 166                     @Override
 167                     public Iterator<T> run() {
 168                         return ServiceLoader.load(providerClass).iterator();
 169                     }
 170                 };
 171         final Iterator<T> ps = AccessController.doPrivileged(psAction);
 172 
 173         // the iterator's hasNext() method looks through classpath for
 174         // the provider class names, so it requires read permissions
 175         PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
 176             @Override
 177             public Boolean run() {
 178                 return ps.hasNext();
 179             }
 180         };
 181 
 182         while (AccessController.doPrivileged(hasNextAction)) {
 183             try {
 184                 // the iterator's next() method creates instances of the
 185                 // providers and it should be called in the current security
 186                 // context
 187                 T provider = ps.next();
 188                 if (providerClass.isInstance(provider)) {
 189                     // $$mp 2003-08-22
 190                     // Always adding at the beginning reverses the
 191                     // order of the providers. So we no longer have
 192                     // to do this in AudioSystem and MidiSystem.
 193                     p.add(0, provider);
 194                 }
 195             } catch (Throwable t) {
 196                 //$$fb 2002-11-07: do not fail on SPI not found
< prev index next >