author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
child 18215 | b2afd66ce6db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2009, 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.util.Collections; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Properties; |
|
33 |
||
34 |
import javax.sound.sampled.spi.AudioFileReader; |
|
35 |
import javax.sound.sampled.spi.AudioFileWriter; |
|
36 |
import javax.sound.sampled.spi.FormatConversionProvider; |
|
37 |
import javax.sound.sampled.spi.MixerProvider; |
|
38 |
||
39 |
import javax.sound.midi.spi.MidiFileReader; |
|
40 |
import javax.sound.midi.spi.MidiFileWriter; |
|
41 |
import javax.sound.midi.spi.SoundbankReader; |
|
42 |
import javax.sound.midi.spi.MidiDeviceProvider; |
|
43 |
||
3452
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
44 |
import javax.sound.midi.Receiver; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
45 |
import javax.sound.midi.Sequencer; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
46 |
import javax.sound.midi.Synthesizer; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
47 |
import javax.sound.midi.Transmitter; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
48 |
import javax.sound.sampled.Clip; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
49 |
import javax.sound.sampled.Port; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
50 |
import javax.sound.sampled.SourceDataLine; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
51 |
import javax.sound.sampled.TargetDataLine; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
52 |
|
2 | 53 |
|
54 |
/** |
|
55 |
* JDK13Services uses the Service class in JDK 1.3 |
|
56 |
* to discover a list of service providers installed |
|
57 |
* in the system. |
|
58 |
* |
|
59 |
* This class is public because it is called from javax.sound.midi.MidiSystem |
|
60 |
* and javax.sound.sampled.AudioSystem. The alternative would be to make |
|
61 |
* JSSecurityManager public, which is considered worse. |
|
62 |
* |
|
63 |
* @author Matthias Pfisterer |
|
64 |
*/ |
|
65 |
public class JDK13Services { |
|
66 |
||
67 |
/** The default for the length of the period to hold the cache. |
|
68 |
This value is given in milliseconds. It is equivalent to |
|
69 |
1 minute. |
|
70 |
*/ |
|
71 |
private static final long DEFAULT_CACHING_PERIOD = 60000; |
|
72 |
||
73 |
/** Filename of the properties file for default provider properties. |
|
74 |
This file is searched in the subdirectory "lib" of the JRE directory |
|
75 |
(this behaviour is hardcoded). |
|
76 |
*/ |
|
77 |
private static final String PROPERTIES_FILENAME = "sound.properties"; |
|
78 |
||
79 |
/** Cache for the providers. |
|
80 |
Class objects of the provider type (MixerProvider, MidiDeviceProvider |
|
81 |
...) are used as keys. The values are instances of ProviderCache. |
|
82 |
*/ |
|
83 |
private static Map providersCacheMap = new HashMap(); |
|
84 |
||
85 |
||
86 |
/** The length of the period to hold the cache. |
|
87 |
This value is given in milliseconds. |
|
88 |
*/ |
|
89 |
private static long cachingPeriod = DEFAULT_CACHING_PERIOD; |
|
90 |
||
91 |
/** Properties loaded from the properties file for default provider |
|
92 |
properties. |
|
93 |
*/ |
|
94 |
private static Properties properties; |
|
95 |
||
96 |
||
97 |
/** Private, no-args constructor to ensure against instantiation. |
|
98 |
*/ |
|
99 |
private JDK13Services() { |
|
100 |
} |
|
101 |
||
102 |
||
103 |
/** Set the period provider lists are cached. |
|
104 |
This method is only intended for testing. |
|
105 |
*/ |
|
106 |
public static void setCachingPeriod(int seconds) { |
|
107 |
cachingPeriod = seconds * 1000L; |
|
108 |
} |
|
109 |
||
110 |
||
111 |
/** Obtains a List containing installed instances of the |
|
112 |
providers for the requested service. |
|
113 |
The List of providers is cached for the period of time given by |
|
114 |
{@link #cachingPeriod cachingPeriod}. During this period, the same |
|
115 |
List instance is returned for the same type of provider. After this |
|
116 |
period, a new instance is constructed and returned. The returned |
|
117 |
List is immutable. |
|
118 |
@param serviceClass The type of providers requested. This should be one |
|
119 |
of AudioFileReader.class, AudioFileWriter.class, |
|
120 |
FormatConversionProvider.class, MixerProvider.class, |
|
121 |
MidiDeviceProvider.class, MidiFileReader.class, MidiFileWriter.class or |
|
122 |
SoundbankReader.class. |
|
123 |
@return A List of providers of the requested type. This List is |
|
124 |
immutable. |
|
125 |
*/ |
|
126 |
public static synchronized List getProviders(Class serviceClass) { |
|
127 |
ProviderCache cache = (ProviderCache) providersCacheMap.get(serviceClass); |
|
128 |
if (cache == null) { |
|
129 |
cache = new ProviderCache(); |
|
130 |
providersCacheMap.put(serviceClass, cache); |
|
131 |
} |
|
132 |
if (cache.providers == null || |
|
133 |
System.currentTimeMillis() > cache.lastUpdate + cachingPeriod) { |
|
134 |
cache.providers = Collections.unmodifiableList(JSSecurityManager.getProviders(serviceClass)); |
|
135 |
cache.lastUpdate = System.currentTimeMillis(); |
|
136 |
} |
|
137 |
return cache.providers; |
|
138 |
} |
|
139 |
||
140 |
||
141 |
/** Obtain the provider class name part of a default provider property. |
|
142 |
@param typeClass The type of the default provider property. This |
|
143 |
should be one of Receiver.class, Transmitter.class, Sequencer.class, |
|
144 |
Synthesizer.class, SourceDataLine.class, TargetDataLine.class, |
|
145 |
Clip.class or Port.class. |
|
146 |
@return The value of the provider class name part of the property |
|
147 |
(the part before the hash sign), if available. If the property is |
|
148 |
not set or the value has no provider class name part, null is returned. |
|
149 |
*/ |
|
150 |
public static synchronized String getDefaultProviderClassName(Class typeClass) { |
|
151 |
String value = null; |
|
152 |
String defaultProviderSpec = getDefaultProvider(typeClass); |
|
153 |
if (defaultProviderSpec != null) { |
|
154 |
int hashpos = defaultProviderSpec.indexOf('#'); |
|
155 |
if (hashpos == 0) { |
|
156 |
// instance name only; leave value as null |
|
157 |
} else if (hashpos > 0) { |
|
158 |
value = defaultProviderSpec.substring(0, hashpos); |
|
159 |
} else { |
|
160 |
value = defaultProviderSpec; |
|
161 |
} |
|
162 |
} |
|
163 |
return value; |
|
164 |
} |
|
165 |
||
166 |
||
167 |
/** Obtain the instance name part of a default provider property. |
|
168 |
@param typeClass The type of the default provider property. This |
|
169 |
should be one of Receiver.class, Transmitter.class, Sequencer.class, |
|
170 |
Synthesizer.class, SourceDataLine.class, TargetDataLine.class, |
|
171 |
Clip.class or Port.class. |
|
172 |
@return The value of the instance name part of the property (the |
|
173 |
part after the hash sign), if available. If the property is not set |
|
174 |
or the value has no instance name part, null is returned. |
|
175 |
*/ |
|
176 |
public static synchronized String getDefaultInstanceName(Class typeClass) { |
|
177 |
String value = null; |
|
178 |
String defaultProviderSpec = getDefaultProvider(typeClass); |
|
179 |
if (defaultProviderSpec != null) { |
|
180 |
int hashpos = defaultProviderSpec.indexOf('#'); |
|
181 |
if (hashpos >= 0 && hashpos < defaultProviderSpec.length() - 1) { |
|
182 |
value = defaultProviderSpec.substring(hashpos + 1); |
|
183 |
} |
|
184 |
} |
|
185 |
return value; |
|
186 |
} |
|
187 |
||
188 |
||
189 |
/** Obtain the value of a default provider property. |
|
190 |
@param typeClass The type of the default provider property. This |
|
191 |
should be one of Receiver.class, Transmitter.class, Sequencer.class, |
|
192 |
Synthesizer.class, SourceDataLine.class, TargetDataLine.class, |
|
193 |
Clip.class or Port.class. |
|
194 |
@return The complete value of the property, if available. |
|
195 |
If the property is not set, null is returned. |
|
196 |
*/ |
|
197 |
private static synchronized String getDefaultProvider(Class typeClass) { |
|
3452
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
198 |
if (!SourceDataLine.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
199 |
&& !TargetDataLine.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
200 |
&& !Clip.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
201 |
&& !Port.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
202 |
&& !Receiver.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
203 |
&& !Transmitter.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
204 |
&& !Synthesizer.class.equals(typeClass) |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
205 |
&& !Sequencer.class.equals(typeClass)) { |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
206 |
return null; |
e18ac2718a06
6738524: JDK13Services allows read access to system properties from untrusted code
amenkov
parents:
2
diff
changeset
|
207 |
} |
2 | 208 |
String value; |
209 |
String propertyName = typeClass.getName(); |
|
210 |
value = JSSecurityManager.getProperty(propertyName); |
|
211 |
if (value == null) { |
|
212 |
value = getProperties().getProperty(propertyName); |
|
213 |
} |
|
214 |
if ("".equals(value)) { |
|
215 |
value = null; |
|
216 |
} |
|
217 |
return value; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
/** Obtain a properties bundle containing property values from the |
|
222 |
properties file. If the properties file could not be loaded, |
|
223 |
the properties bundle is empty. |
|
224 |
*/ |
|
225 |
private static synchronized Properties getProperties() { |
|
226 |
if (properties == null) { |
|
227 |
properties = new Properties(); |
|
228 |
JSSecurityManager.loadProperties(properties, PROPERTIES_FILENAME); |
|
229 |
} |
|
230 |
return properties; |
|
231 |
} |
|
232 |
||
233 |
// INNER CLASSES |
|
234 |
||
235 |
private static class ProviderCache { |
|
236 |
// System time of the last update in milliseconds. |
|
237 |
public long lastUpdate; |
|
238 |
||
239 |
// The providers. |
|
240 |
public List providers; |
|
241 |
} |
|
242 |
} |