FileDocCategorySizeDatePackage
DataSource.javaAPI DocJMF 2.1.1e11682Mon May 12 12:20:54 BST 2003com.sun.media.protocol

DataSource

public class DataSource extends PullDataSource

Fields Summary
protected boolean
connected
private String
contentType
private PullSourceStream[]
pssArray
private CachedPullSourceStream
cachedStream
private long
contentLength
private InputStream
inputStream
private String
fileSeparator
private boolean
downLoadThreadStarted
private boolean
isEnabledCaching
private ExtendedCachingControl[]
cachingControls
private static JMFSecurity
jmfSecurity
private static boolean
securityPrivelege
private Method[]
m
private Class[]
cl
private Object[]
args
Constructors Summary
Methods Summary
public voidconnect()

	if (connected)
	    return;

	MediaLocator locator = getLocator();
	if (locator == null) {
	    throw(new IOException(this + ": connect() failed"));
	}

	URL url;
	URLConnection urlC;
	try {
	    url = locator.getURL();
	    urlC = url.openConnection(); // This will not throw security exception
	    urlC.setAllowUserInteraction(true);
	} catch (MalformedURLException e) {
	    // System.err.println(getLocator() +
	    //	       ": Don't know how to deal with non-URL locator yet!");
	    throw(new IOException(this + ": connect() failed"));
	}
	String protocol = url.getProtocol();


	// Note that even if we don't have connect privileges we can play
	// media from the same server from which the applet is downloaded.
	// Try to see if you can getInputStream without asking for
	// connect privilege
	boolean needConnectPermission = true;
	try {
	    inputStream = urlC.getInputStream();
	    needConnectPermission = false;
	    // System.out.println("got getInputStream without asking for security needConnectPermission " + needConnectPermission);

	} catch (Throwable e) {
	    // System.err.println("Unable to getInputStream without asking for permission " + e);
	}

	if (inputStream == null) {
	    if ( /*securityPrivelege &&*/ (jmfSecurity != null) ) {
		try {
		    if (jmfSecurity.getName().startsWith("jmf-security")) {
			jmfSecurity.requestPermission(m, cl, args, JMFSecurity.CONNECT);
			m[0].invoke(cl[0], args[0]);
		    } else if (jmfSecurity.getName().startsWith("internet")) {
			PolicyEngine.checkPermission(PermissionID.NETIO);
			PolicyEngine.assertPermission(PermissionID.NETIO);
		    }
		} catch (Exception e) {
		    if (JMFSecurityManager.DEBUG) {
			System.err.println("Unable to get connect " +
					   " privilege  " + e);
		    }
		    jmfSecurity.permissionFailureNotification(JMFSecurity.CONNECT);
		    throw new IOException("Unable to get connect permission" + e.getMessage());
		    // securityPrivelege = false;
		}
	    }


	    try {
		if ( (jmfSecurity != null) && (jmfSecurity.getName().startsWith("jdk12"))) {
		    Constructor cons = jdk12ConnectionAction.cons;
		    inputStream = (InputStream) jdk12.doPrivM.invoke(
                                           jdk12.ac,
					   new Object[] {
 					  cons.newInstance(
 					   new Object[] {
                                               urlC,
                                           })});

		} else {
		    inputStream = urlC.getInputStream();
		}
	    } catch (Throwable e) {
		// System.err.println("Unable to open a URL connection " + e);
		throw new IOException(JMFI18N.getResource("error.connectionerror") +
				      e.getMessage());
	    }
	}

	if (inputStream == null) {
	    throw new IOException(JMFI18N.getResource("error.connectionerror") +
				  "Unable to open a URL connection");
	}

	if (protocol.equals("ftp")) {
	    contentType = "content/unknown";
	    // The contentType will be obtained after the
	    // getCorrectedContentType call
	} else {
	    contentType = urlC.getContentType();
	    contentLength = urlC.getContentLength();
	    // System.out.println("contentLength is " + contentLength);
	}

	contentType = ContentType.getCorrectedContentType(contentType,
							  locator.getRemainder());
	contentType = ContentDescriptor.mimeTypeToPackageName(contentType);

	// System.out.println("contentType is " + contentType);
	
	boolean cachingRequested = ((Boolean) Manager.getHint(Manager.CACHING)).booleanValue();

	// Don't do caching for hotmedia or flash
	if (  contentType.endsWith(".mvr") ||
              contentType.endsWith("x_shockwave_flash") ||
              contentType.endsWith("futuresplash") ) {
	    // System.err.println("Caching not done for hotmedia or flash");
	    cachingRequested = false;
	}

	String filePrefix = null;
	if ( cachingRequested ) {
	    // user wants caching. check to see if caching is allowed
	    filePrefix = Manager.getCacheDirectory();
	    if (filePrefix != null) {
		Object allowCachingObj = com.sun.media.util.Registry.get("secure.allowCaching");
		if (allowCachingObj != null) {
		    isEnabledCaching = ((Boolean) allowCachingObj).booleanValue();
		}
	    }
	}

	if (isEnabledCaching) {
	    // TODO: remove file name extension, eg .mov from cache file
	    String fileName = filePrefix + fileSeparator +
		generateFileName(getLocator().getRemainder());
	    try {
		cachedStream = new
		    CachedPullSourceStream(inputStream, fileName, contentLength, protocol);
		pssArray[0] = cachedStream;
		cachingControls = new ExtendedCachingControl[1];
		cachingControls[0] = new CachingControl(cachedStream);
		com.sun.media.Log.comment("Caching in " + filePrefix);
	    }  catch(IOException e) {
		isEnabledCaching = false;
	    }
	}


	if (!isEnabledCaching) {
	    try {
		pssArray[0] = new BasicPullSourceStream(url,
							inputStream,
							contentLength,
							needConnectPermission
							);
		cachedStream=null;
	    } catch(Exception ie) {
		pssArray[0] = null;
		throw new IOException(JMFI18N.getResource("error.connectionerror") +
				      ie.getMessage());
	    }
        }
	connected = true;
    
private static java.lang.StringconvertNonAlphaNumericToUnderscore(java.lang.String in)

	if (in == null)
	    return null;

	// ... run through each char and convert
	//             !([A-Za-z0--9])   ->  '_'
	int len = in.length();
	char nm[] = new char[len];
	in.getChars(0, len, nm, 0);
	for (int i = 0; i < len; i++) {
	    char c = nm[i];
	    if (!(c == '." ||
		  'A" <= c && c <= 'Z" ||
		  'a" <= c && c <= 'z" ||
		  '0" <= c && c <= '9")) {
		nm[i] = '_";
	    }
	}
	return new String(nm);
    
public voiddisconnect()

	if (!connected)
	    return;
	if (cachedStream != null) {
	    cachedStream.close();
	    cachedStream = null;
	}
	pssArray[0] = null;
	connected = false;
    
public static java.lang.StringgenerateFileName(java.lang.String infile)

 
        String filename, ext = null;
        int sepindex = 0;
        java.util.Random generator = new java.util.Random();
        int dotindex = infile.lastIndexOf('.");
        int suffix = generator.nextInt();
 
        //
        // if dotindex is not found, it implies extension
        // doesn't exist. Then set the dotindex to the
        // length of the input file, infile.
        if (dotindex != -1) {
            ext = new String(infile.substring(dotindex));
	} else {
            dotindex = infile.length();
	}

        sepindex = infile.lastIndexOf(File.separatorChar);
	// some URL's on Wintel use either slash. So should we.
	sepindex = Math.max(infile.lastIndexOf('/"), sepindex);
        //
        // If sepindex equals to -1, the input file name doesn't
        // have a separator. Copy the filename from 0 up to the
        // the extension.
	if (sepindex >= dotindex) {
	    dotindex = infile.length();
	    ext = null;
	}

        filename = infile.substring(sepindex + 1, dotindex);
	String in;
	if (ext != null)
	    in = new String(filename + suffix + ext);
	else
	    in = new String(filename + suffix);
 	return convertNonAlphaNumericToUnderscore(in);
    
public java.lang.StringgetContentType()


     
	try {
	    jmfSecurity = JMFSecurityManager.getJMFSecurity();
	    securityPrivelege = true;
	} catch (SecurityException e) {
	}
    
	if (!connected)
	    return null;
	return contentType;
    
public java.lang.ObjectgetControl(java.lang.String controlType)

	if ( (cachingControls.length > 0) &&
	     (controlType.equals("javax.media.CachingControl")) ) {
	    return cachingControls[0];
	} else {
	    return null;
	}
    
public java.lang.Object[]getControls()

	return cachingControls;
    
public javax.media.TimegetDuration()

	return null;
    
public javax.media.protocol.PullSourceStream[]getStreams()

	return pssArray;
    
public voidstart()

	if (!connected)
	    return;
	// TODO: see if you need downLoadThreadStarted
	if (cachedStream != null) {
	    if (!downLoadThreadStarted) {
		cachedStream.startDownload();
		downLoadThreadStarted = true;
	    } else {
		cachedStream.resumeDownload();
	    }
	}
    
public voidstop()

 // TODO
	if (!connected)
	    return;
// 	if (cachedStream != null) {
// 	    cachedStream.pauseDownload();
// 	}