1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Collections ; 31 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 36 import com.sun.appserv.management.base.AMX; 37 import com.sun.appserv.management.base.Utility; 38 import com.sun.appserv.management.base.Singleton; 39 import com.sun.appserv.management.base.XTypes; 40 import com.sun.appserv.management.base.UploadDownloadMgr; 41 42 import com.sun.enterprise.management.support.AMXImplBase; 43 44 import com.sun.appserv.management.util.misc.GSetUtil; 45 46 47 48 public final class UploadDownloadMgrImpl extends AMXImplBase 49 implements Utility, Singleton, UploadDownloadMgr 50 { 51 54 private final Map <Object ,UploadInfo> mUploadInfos; 55 56 59 private final Map <Object ,DownloadInfo> mDownloadInfos; 60 61 62 private final UniqueIDGenerator mUploadIDs; 63 private final UniqueIDGenerator mDownloadIDs; 64 65 public 66 UploadDownloadMgrImpl( ) 67 { 68 mUploadInfos = Collections.synchronizedMap( new HashMap <Object ,UploadInfo>() ); 69 mDownloadInfos = Collections.synchronizedMap( new HashMap <Object ,DownloadInfo>() ); 70 71 mUploadIDs = new UniqueIDGenerator( "upload-" ); 72 mDownloadIDs = new UniqueIDGenerator( "download-" ); 73 } 74 75 76 public final String 77 getGroup() 78 { 79 return( AMX.GROUP_UTILITY ); 80 } 81 82 83 84 88 private final void 89 staleUploadCheck() 90 throws IOException 91 { 92 String [] keys = null; 93 94 synchronized( mUploadInfos ) 95 { 96 keys = GSetUtil.toStringArray( mUploadInfos.keySet() ); 97 } 98 99 synchronized( this ) 101 { 102 for( int i = 0; i < keys.length; ++i ) 103 { 104 final Object key = keys[ i ]; 105 106 final UploadInfo info = mUploadInfos.get( key ); 107 108 if ( info != null && info.getMillisSinceLastAccess() > UPLOAD_KEEP_ALIVE_MILLIS ) 109 { 110 trace( "Cleaning up stale upload: " + info.getID() ); 111 mUploadInfos.remove( key ); 112 info.cleanup(); 113 } 114 } 115 } 116 } 117 118 private String 119 mangleUploadName( final String name ) 120 { 121 String result = name; 122 123 if ( result != null ) 124 { 125 result = result.replaceAll( "/", "_" ); 126 result = result.replaceAll( "\\\\", "_" ); 127 result = result.replaceAll( ":", "_" ); 128 129 } 130 131 return( result ); 132 } 133 134 public Object 135 initiateUpload( 136 final String name, 137 final long totalSize ) 138 throws IOException 139 { 140 staleUploadCheck(); 141 142 final String actualName = mangleUploadName( name ); 143 144 final UploadInfo info = new UploadInfo( mUploadIDs.createID(), actualName, totalSize ); 145 146 mUploadInfos.put( info.getID(), info ); 147 148 return( info.getID() ); 149 } 150 151 public boolean 152 uploadBytes( 153 final Object uploadID, 154 final byte[] bytes) 155 throws IOException 156 { 157 final UploadInfo info = (UploadInfo)mUploadInfos.get( uploadID ); 158 159 if ( info == null ) 160 { 161 throw new IllegalArgumentException ( "" + uploadID ); 162 } 163 164 boolean done = false; 165 synchronized( info ) 166 { 167 done = info.write( bytes ); 168 } 169 170 return( done ); 171 } 172 173 174 public File 175 takeUpload( final Object uploadID ) 176 { 177 final UploadInfo info = (UploadInfo)mUploadInfos.get( uploadID ); 179 180 if ( info == null ) 181 { 182 throw new IllegalArgumentException ( "" + uploadID ); 183 } 184 185 synchronized( info ) 186 { 187 if ( ! info.isDone() ) 190 { 191 throw new IllegalArgumentException ( "not done:" + uploadID ); 192 } 193 194 mUploadInfos.remove( uploadID ); 195 } 196 return( info.getFile() ); 197 } 198 199 200 private static final long SECOND_MILLIS = 60 * 1000; 201 private static final long UPLOAD_KEEP_ALIVE_MILLIS = 60 * SECOND_MILLIS; 202 private static final long DOWNLOAD_KEEP_ALIVE_MILLIS = 180 * SECOND_MILLIS; 203 204 205 206 207 211 private final void 212 staleDownloadCheck() 213 throws IOException 214 { 215 String [] keys = null; 216 217 synchronized( mDownloadInfos ) 218 { 219 keys = GSetUtil.toStringArray( mDownloadInfos.keySet() ); 220 } 221 222 synchronized( this ) 224 { 225 for( int i = 0; i < keys.length; ++i ) 226 { 227 final Object key = keys[ i ]; 228 229 final DownloadInfo info = mDownloadInfos.get( key ); 230 231 if ( info != null && info.isDone() ) 232 { 233 mDownloadInfos.remove( key ); 234 trace( "Cleaning up stale download: " + info.getID() + 235 "length was " + info.getLength() ); 236 info.cleanup(); 237 } 238 } 239 } 240 } 241 242 243 244 public Object 245 initiateDownload( 246 final File theFile, 247 boolean deleteWhenDone ) 248 throws IOException 249 { 250 staleDownloadCheck(); 252 253 final DownloadInfo info = 254 new DownloadInfo( mDownloadIDs.createID(), theFile, deleteWhenDone ); 255 256 trace( "Created download info: " + info.getID() ); 257 mDownloadInfos.put( info.getID(), info ); 258 259 return( info.getID() ); 260 } 261 262 263 private DownloadInfo 264 getDownloadInfo( Object downloadID ) 265 { 266 final DownloadInfo info = (DownloadInfo)mDownloadInfos.get( downloadID ); 267 if ( info == null ) 268 { 269 throw new IllegalArgumentException ( "" + downloadID ); 270 } 271 return( info ); 272 } 273 274 275 280 public long 281 getDownloadLength( final Object downloadID ) 282 { 283 try 284 { 285 final DownloadInfo info = getDownloadInfo( downloadID ); 286 return( info.getLength() ); 287 } 288 catch( Exception e ) 289 { 290 e.printStackTrace(); 291 assert( false ); 292 } 293 return( 0 ); 294 } 295 296 public int 297 getMaxDownloadChunkSize() 298 { 299 return( 5 * 1024 * 1024 ); 300 } 301 302 public byte[] 303 downloadBytes( 304 final Object downloadID, 305 final int requestSize ) 306 throws IOException 307 { 308 if ( requestSize > getMaxDownloadChunkSize() ) 309 { 310 trace( "Request too large: " + requestSize ); 311 throw new IllegalArgumentException ( "request too large: " + requestSize ); 312 } 313 314 final DownloadInfo info = getDownloadInfo( downloadID ); 315 316 final byte[] bytes = info.read( requestSize ); 317 318 if ( info.isDone() ) 319 { 320 trace( "download done: " + info.getID() ); 321 staleDownloadCheck(); 322 } 323 324 return( bytes ); 325 } 326 327 } 328 329 330 331 332 333 334 335 | Popular Tags |