/
Manager.java
281 lines (252 loc) · 9.92 KB
/
Manager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package javax.media;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.protocol.DataSource;
import javax.media.protocol.URLDataSource;
/**
* This file is a stripped down version of the Manager class from FMJ (fmj-sf.net)
* @author Ken Larson
*
*/
public final class Manager {
public static Player createPlayer(URL sourceURL) throws IOException,
NoPlayerException
{
return createPlayer(new MediaLocator(sourceURL));
}
public static Player createPlayer(MediaLocator sourceLocator)
throws IOException, NoPlayerException
{
final String protocol = sourceLocator.getProtocol();
final Vector<?> dataSourceList = getDataSourceList(protocol);
for (int i = 0; i < dataSourceList.size(); ++i)
{
String dataSourceClassName = (String) dataSourceList.get(i);
try
{
final Class<?> dataSourceClass = Class.forName(dataSourceClassName);
final DataSource dataSource = (DataSource) dataSourceClass.newInstance();
dataSource.setLocator(sourceLocator);
dataSource.connect();
return createPlayer(dataSource);
// TODO: JMF seems to disconnect data sources in this method, based on this stack trace:
// java.lang.NullPointerException
// at com.sun.media.protocol.rtp.DataSource.disconnect(DataSource.java:207)
// at javax.media.Manager.createPlayer(Manager.java:425)
// at net.sf.fmj.ui.application.ContainerPlayer.createNewPlayer(ContainerPlayer.java:357)
}
catch (NoPlayerException e)
{ // no need to log, will be logged by call to createPlayer.
continue;
}
catch (ClassNotFoundException e)
{
logger.warning("createPlayer: " + e); // no need for call stack
continue;
}
catch (IOException e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (NoClassDefFoundError e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (Exception e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
}
// if none found, try URLDataSource:
final URL url;
try
{
url = sourceLocator.getURL();
}
catch (Exception e)
{ logger.log(Level.WARNING, "" + e, e);
throw new NoPlayerException();
}
final URLDataSource dataSource = new URLDataSource(url);
dataSource.connect(); // TODO: there is a problem because we connect to the datasource here, but
// the following call may try twice or more to use the datasource with a player, once
// for the right content type, and multiple times for unknown. The first attempt (for example) may actually
// read data, in which case the second one will be missing data when it reads.
// really, the datasource needs to be recreated.
// The workaround for now is that URLDataSource (and others) allows repeated connect() calls.
return createPlayer(dataSource);
}
public static Player createPlayer(DataSource source) throws IOException,
NoPlayerException
{
return createPlayer(source, source.getContentType());
}
public static DataSource createDataSource(URL sourceURL)
throws IOException, NoDataSourceException
{
return createDataSource(new MediaLocator(sourceURL));
}
public static DataSource createDataSource(MediaLocator sourceLocator)
throws IOException, NoDataSourceException
{
final String protocol = sourceLocator.getProtocol();
final Vector<?> dataSourceList = getDataSourceList(protocol);
for (int i = 0; i < dataSourceList.size(); ++i)
{
String dataSourceClassName = (String) dataSourceList.get(i);
try
{
final Class<?> dataSourceClass = Class.forName(dataSourceClassName);
final DataSource dataSource = (DataSource) dataSourceClass.newInstance();
dataSource.setLocator(sourceLocator);
dataSource.connect();
return dataSource;
}
catch (ClassNotFoundException e)
{
logger.warning("createDataSource: " + e); // no need for call stack
continue;
}
catch (IOException e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (NoClassDefFoundError e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (Exception e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
}
// if none found, try URLDataSource:
final URL url;
try
{
url = sourceLocator.getURL();
}
catch (Exception e)
{ logger.log(Level.WARNING, "" + e, e);
throw new NoDataSourceException();
}
final URLDataSource dataSource = new URLDataSource(url);
dataSource.connect();
return dataSource;
}
public static TimeBase getSystemTimeBase()
{
return systemTimeBase;
}
public static Vector<?> getDataSourceList(String protocolName)
{
return getClassList(protocolName, PackageManager.getProtocolPrefixList(), "protocol", "DataSource");
}
public static Vector<?> getHandlerClassList(String contentName)
{
return getClassList(toPackageFriendly(contentName), PackageManager.getContentPrefixList(), "content", "Handler");
}
private static Player createPlayer(DataSource source, String contentType)
throws IOException, NoPlayerException
{
final Vector<?> handlerClassList = getHandlerClassList(contentType);
for (int i = 0; i < handlerClassList.size(); ++i)
{
final String handlerClassName = (String) handlerClassList.get(i);
try
{
System.out.println(handlerClassName);
final Class<?> handlerClass = Class.forName(handlerClassName);
if (!Player.class.isAssignableFrom(handlerClass) &&
!MediaProxy.class.isAssignableFrom(handlerClass))
continue; // skip any classes that will not be matched below.
final MediaHandler handler = (MediaHandler) handlerClass.newInstance();
handler.setSource(source);
if (handler instanceof Player)
{ return (Player) handler;
}
else if (handler instanceof MediaProxy)
{
final MediaProxy mediaProxy = (MediaProxy) handler;
return createPlayer(mediaProxy.getDataSource());
}
}
catch (ClassNotFoundException e)
{
logger.warning("createPlayer: " + e); // no need for call stack
continue;
}
catch (IncompatibleSourceException e)
{
logger.warning("createPlayer(" + source + ", " + contentType + "): " + e); // no need for call stack
continue;
}
catch (IOException e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (NoPlayerException e)
{ // no need to log, will be logged by call to createPlayer.
continue;
}
catch (NoClassDefFoundError e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
catch (Exception e)
{
logger.log(Level.WARNING, "" + e, e);
continue;
}
}
throw new NoPlayerException("No player found for " + source.getLocator());
}
private static char toPackageFriendly(char c)
{ if (c >= 'a' && c <= 'z')
return c;
else if (c >= 'A' && c <= 'Z')
return c;
else if (c >= '0' && c <= '9')
return c;
else if (c == '.')
return c;
else if (c == '/')
return '.';
else
return '_';
}
private static String toPackageFriendly(String contentName)
{
final StringBuffer b = new StringBuffer();
for (int i = 0; i < contentName.length(); ++i)
{
final char c = contentName.charAt(i);
b.append(toPackageFriendly(c));
}
return b.toString();
}
public static Vector<String> getClassList(String contentName, Vector<?> packages, String component2, String className)
{
final Vector<String> result = new Vector<String>();
//result.add("media." + component2 + "." + contentName + "." + className);
for (int i = 0; i < packages.size(); ++i)
{
result.add(((String) packages.get(i)) + ".media." + component2 + "." + contentName + "." + className);
}
return result;
}
public static final String UNKNOWN_CONTENT_NAME = "unknown";
private static final TimeBase systemTimeBase = new SystemTimeBase();
private static final Logger logger = Logger.getLogger(Manager.class.getName());
}