FileDocCategorySizeDatePackage
UploadDownloadMgrImpl.javaAPI DocGlassfish v2 API8597Fri May 04 22:23:42 BST 2007com.sun.enterprise.management.support

UploadDownloadMgrImpl

public final class UploadDownloadMgrImpl extends com.sun.enterprise.management.support.AMXImplBase implements com.sun.appserv.management.base.Utility, com.sun.appserv.management.base.Singleton, com.sun.appserv.management.base.UploadDownloadMgr

Fields Summary
private final Map
mUploadInfos
A Map keyed by uploadID to values of UploadInfo
private final Map
mDownloadInfos
A Map keyed by downloadID to values of DownloadInfo
private final UniqueIDGenerator
mUploadIDs
private final UniqueIDGenerator
mDownloadIDs
private static final long
SECOND_MILLIS
private static final long
UPLOAD_KEEP_ALIVE_MILLIS
private static final long
DOWNLOAD_KEEP_ALIVE_MILLIS
Constructors Summary
public UploadDownloadMgrImpl()

		mUploadInfos	= Collections.synchronizedMap( new HashMap<Object,UploadInfo>() );
		mDownloadInfos	= Collections.synchronizedMap( new HashMap<Object,DownloadInfo>() );
		
		mUploadIDs		= new UniqueIDGenerator( "upload-" );
		mDownloadIDs	= new UniqueIDGenerator( "download-" );
	
Methods Summary
public byte[]downloadBytes(java.lang.Object downloadID, int requestSize)

    	if ( requestSize > getMaxDownloadChunkSize() )
    	{
			trace( "Request too large: " + requestSize );
    		throw new IllegalArgumentException( "request too large: " + requestSize );
    	}
    	
		final DownloadInfo	info	= getDownloadInfo( downloadID );
		
		final byte[]	bytes	= info.read( requestSize );
		
		if ( info.isDone() )
		{
			trace( "download done: " + info.getID() );
			staleDownloadCheck();
		}
		
		return( bytes );
    
private DownloadInfogetDownloadInfo(java.lang.Object downloadID)

		final DownloadInfo	info	= (DownloadInfo)mDownloadInfos.get( downloadID );
		if ( info == null )
		{
			throw new IllegalArgumentException( "" + downloadID );
		}
		return( info );
	
public longgetDownloadLength(java.lang.Object downloadID)
Get the total length the download will be, in bytes.

param
downloadID the file download operation id, from initiateFileDownload()

    	try
    	{
			final DownloadInfo	info	= getDownloadInfo( downloadID );
			return( info.getLength() );
		}
		catch( Exception e )
		{
			e.printStackTrace();
			assert( false );
		}
		return( 0 );
    
public final java.lang.StringgetGroup()

		return( AMX.GROUP_UTILITY );
	
public intgetMaxDownloadChunkSize()

    	return( 5 * 1024 * 1024 );
    
public java.lang.ObjectinitiateDownload(java.io.File theFile, boolean deleteWhenDone)

		//setTrace( true );
		staleDownloadCheck();
		
		final DownloadInfo	info	=
			new DownloadInfo( mDownloadIDs.createID(),  theFile, deleteWhenDone );
		
		trace( "Created download info: " + info.getID() );
		mDownloadInfos.put( info.getID(), info );
		
		return( info.getID() );
	
public java.lang.ObjectinitiateUpload(java.lang.String name, long totalSize)

		staleUploadCheck();
		
		final String	actualName	= mangleUploadName( name );
		
		final UploadInfo	info	= new UploadInfo( mUploadIDs.createID(),  actualName, totalSize );
		
		mUploadInfos.put( info.getID(), info );
		
		return( info.getID() );
	
private java.lang.StringmangleUploadName(java.lang.String name)

		String	result	= name;
		
		if ( result != null )
		{
			result	= result.replaceAll( "/", "_" );
			result	= result.replaceAll( "\\\\", "_" );
			result	= result.replaceAll( ":", "_" );

		}
		
		return( result );
	
private final voidstaleDownloadCheck()
Cleanup any downloads that have been accessed for a proscribed amount of time given by DOWNLOAD_KEEP_ALIVE_MILLIS.

	
	


			         		     	 
		  
	
		 
	
		String[]	keys	= null;
		
		synchronized( mDownloadInfos )
		{
			keys	= GSetUtil.toStringArray( mDownloadInfos.keySet() );
		}
		
		// block other threads from trying to do the same stale check
		synchronized( this )
		{
			for( int i = 0; i < keys.length; ++i )
			{
				final Object		key	= keys[ i ];
				
				final DownloadInfo	info	= mDownloadInfos.get( key );
				
				if ( info != null  && info.isDone() )
				{
					mDownloadInfos.remove( key );
					trace( "Cleaning up stale download: " + info.getID() +
						"length was " + info.getLength() );
					info.cleanup();
				}
			}
		}
	
private final voidstaleUploadCheck()
Cleanup any threads that have been done for a proscribed amount of time given by UPLOAD_KEEP_ALIVE_MILLIS.

		String[]	keys	= null;
		
		synchronized( mUploadInfos )
		{
			keys	= GSetUtil.toStringArray( mUploadInfos.keySet() );
		}
		
		// block other threads from trying to do the same stale check
		synchronized( this )
		{
			for( int i = 0; i < keys.length; ++i )
			{
				final Object		key	= keys[ i ];
				
				final UploadInfo	info	= mUploadInfos.get( key );
				
				if ( info != null && info.getMillisSinceLastAccess() > UPLOAD_KEEP_ALIVE_MILLIS )
				{
					trace( "Cleaning up stale upload: " + info.getID() );
					mUploadInfos.remove( key );
					info.cleanup();
				}
			}
		}
	
public java.io.FiletakeUpload(java.lang.Object uploadID)

		// don't remove it until we find out it's done
		final UploadInfo	info	= (UploadInfo)mUploadInfos.get( uploadID );
		
		if ( info == null )
		{
			throw new IllegalArgumentException( "" + uploadID );
		}
		
		synchronized( info )
		{
			// by being synchronized, we can safely block any uploadBytes() activity
			// while we check for it being done
			if ( ! info.isDone() )
			{
				throw new IllegalArgumentException( "not done:" + uploadID );
			}
			
			mUploadInfos.remove( uploadID );
		}
		return( info.getFile() );
	
public booleanuploadBytes(java.lang.Object uploadID, byte[] bytes)

		final UploadInfo	info	= (UploadInfo)mUploadInfos.get( uploadID );
		
		if ( info == null )
		{
			throw new IllegalArgumentException( "" + uploadID );
		}
		
		boolean	done	= false;
		synchronized( info )
		{
			done	= info.write( bytes );
		}
		
		return( done );