author | serb |
Thu, 07 Aug 2014 17:02:48 +0400 | |
changeset 26030 | 576ffa819e23 |
parent 23889 | c8412b2715b5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
26030 | 2 |
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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; |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
37 |
import java.util.ServiceLoader; |
2 | 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 |
*/ |
|
18215 | 50 |
final class JSSecurityManager { |
2 | 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 "lib" 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 |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
92 |
PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
93 |
public Void run() { |
2 | 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, "lib"); |
|
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 |
} |
|
132 |
} catch (Throwable t) { |
|
133 |
if (Printer.trace) { |
|
134 |
System.err.println("Could not load properties file \"" + fname + "\""); |
|
135 |
t.printStackTrace(); |
|
136 |
} |
|
137 |
} |
|
138 |
if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed"); |
|
139 |
} |
|
140 |
||
18215 | 141 |
/** Create a Thread in the current ThreadGroup. |
2 | 142 |
*/ |
143 |
static Thread createThread(final Runnable runnable, |
|
144 |
final String threadName, |
|
145 |
final boolean isDaemon, final int priority, |
|
146 |
final boolean doStart) { |
|
18215 | 147 |
Thread thread = new Thread(runnable); |
2 | 148 |
if (threadName != null) { |
149 |
thread.setName(threadName); |
|
150 |
} |
|
151 |
thread.setDaemon(isDaemon); |
|
152 |
if (priority >= 0) { |
|
153 |
thread.setPriority(priority); |
|
154 |
} |
|
155 |
if (doStart) { |
|
156 |
thread.start(); |
|
157 |
} |
|
158 |
return thread; |
|
159 |
} |
|
160 |
||
23889 | 161 |
static synchronized <T> List<T> getProviders(final Class<T> providerClass) { |
162 |
List<T> p = new ArrayList<>(7); |
|
20468
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
163 |
// ServiceLoader creates "lazy" iterator instance, but it ensures that |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
164 |
// next/hasNext run with permissions that are restricted by whatever |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
165 |
// creates the ServiceLoader instance, so it requires to be called from |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
166 |
// privileged section |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
167 |
final PrivilegedAction<Iterator<T>> psAction = |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
168 |
new PrivilegedAction<Iterator<T>>() { |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
169 |
@Override |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
170 |
public Iterator<T> run() { |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
171 |
return ServiceLoader.load(providerClass).iterator(); |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
172 |
} |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
173 |
}; |
4424783ed2ce
8022119: test api/javax_sound/sampled/spi/MixerProvider/indexTGF_MixerProviderTests fails
serb
parents:
18215
diff
changeset
|
174 |
final Iterator<T> ps = AccessController.doPrivileged(psAction); |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
175 |
|
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
176 |
// the iterator's hasNext() method looks through classpath for |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
177 |
// the provider class names, so it requires read permissions |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
178 |
PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
179 |
public Boolean run() { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
180 |
return ps.hasNext(); |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
181 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
182 |
}; |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
183 |
|
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
184 |
while (AccessController.doPrivileged(hasNextAction)) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
185 |
try { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
186 |
// the iterator's next() method creates instances of the |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
187 |
// providers and it should be called in the current security |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
188 |
// context |
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
189 |
T provider = ps.next(); |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
190 |
if (providerClass.isInstance(provider)) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
191 |
// $$mp 2003-08-22 |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
192 |
// Always adding at the beginning reverses the |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
193 |
// order of the providers. So we no longer have |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
194 |
// to do this in AudioSystem and MidiSystem. |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
195 |
p.add(0, provider); |
2 | 196 |
} |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
197 |
} catch (Throwable t) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
198 |
//$$fb 2002-11-07: do not fail on SPI not found |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
199 |
if (Printer.err) t.printStackTrace(); |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
200 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
201 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
202 |
return p; |
2 | 203 |
} |
204 |
} |