KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > UploadDownloadMgrImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.support;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
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     /**
52         A Map keyed by uploadID to values of UploadInfo
53      */

54     private final Map JavaDoc<Object JavaDoc,UploadInfo> mUploadInfos;
55     
56     /**
57         A Map keyed by downloadID to values of DownloadInfo
58      */

59     private final Map JavaDoc<Object JavaDoc,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 JavaDoc<Object JavaDoc,UploadInfo>() );
69         mDownloadInfos = Collections.synchronizedMap( new HashMap JavaDoc<Object JavaDoc,DownloadInfo>() );
70         
71         mUploadIDs = new UniqueIDGenerator( "upload-" );
72         mDownloadIDs = new UniqueIDGenerator( "download-" );
73     }
74     
75     
76         public final String JavaDoc
77     getGroup()
78     {
79         return( AMX.GROUP_UTILITY );
80     }
81     
82     
83     
84     /**
85         Cleanup any threads that have been done for a proscribed
86         amount of time given by UPLOAD_KEEP_ALIVE_MILLIS.
87      */

88         private final void
89     staleUploadCheck()
90         throws IOException JavaDoc
91     {
92         String JavaDoc[] keys = null;
93         
94         synchronized( mUploadInfos )
95         {
96             keys = GSetUtil.toStringArray( mUploadInfos.keySet() );
97         }
98         
99         // block other threads from trying to do the same stale check
100
synchronized( this )
101         {
102             for( int i = 0; i < keys.length; ++i )
103             {
104                 final Object JavaDoc 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 JavaDoc
119     mangleUploadName( final String JavaDoc name )
120     {
121         String JavaDoc 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 JavaDoc
135     initiateUpload(
136         final String JavaDoc name,
137         final long totalSize )
138         throws IOException JavaDoc
139     {
140         staleUploadCheck();
141         
142         final String JavaDoc 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 JavaDoc uploadID,
154         final byte[] bytes)
155         throws IOException JavaDoc
156     {
157         final UploadInfo info = (UploadInfo)mUploadInfos.get( uploadID );
158         
159         if ( info == null )
160         {
161             throw new IllegalArgumentException JavaDoc( "" + 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 JavaDoc
175     takeUpload( final Object JavaDoc uploadID )
176     {
177         // don't remove it until we find out it's done
178
final UploadInfo info = (UploadInfo)mUploadInfos.get( uploadID );
179         
180         if ( info == null )
181         {
182             throw new IllegalArgumentException JavaDoc( "" + uploadID );
183         }
184         
185         synchronized( info )
186         {
187             // by being synchronized, we can safely block any uploadBytes() activity
188
// while we check for it being done
189
if ( ! info.isDone() )
190             {
191                 throw new IllegalArgumentException JavaDoc( "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     /**
208         Cleanup any downloads that have been accessed for a proscribed
209         amount of time given by DOWNLOAD_KEEP_ALIVE_MILLIS.
210      */

211         private final void
212     staleDownloadCheck()
213         throws IOException JavaDoc
214     {
215         String JavaDoc[] keys = null;
216         
217         synchronized( mDownloadInfos )
218         {
219             keys = GSetUtil.toStringArray( mDownloadInfos.keySet() );
220         }
221         
222         // block other threads from trying to do the same stale check
223
synchronized( this )
224         {
225             for( int i = 0; i < keys.length; ++i )
226             {
227                 final Object JavaDoc 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 JavaDoc
245     initiateDownload(
246         final File JavaDoc theFile,
247         boolean deleteWhenDone )
248         throws IOException JavaDoc
249     {
250         //setTrace( true );
251
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 JavaDoc downloadID )
265     {
266         final DownloadInfo info = (DownloadInfo)mDownloadInfos.get( downloadID );
267         if ( info == null )
268         {
269             throw new IllegalArgumentException JavaDoc( "" + downloadID );
270         }
271         return( info );
272     }
273
274         
275     /**
276         Get the total length the download will be, in bytes.
277         
278         @param downloadID the file download operation id, from initiateFileDownload()
279      */

280         public long
281     getDownloadLength( final Object JavaDoc downloadID )
282     {
283         try
284         {
285             final DownloadInfo info = getDownloadInfo( downloadID );
286             return( info.getLength() );
287         }
288         catch( Exception JavaDoc 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 JavaDoc downloadID,
305         final int requestSize )
306         throws IOException JavaDoc
307     {
308         if ( requestSize > getMaxDownloadChunkSize() )
309         {
310             trace( "Request too large: " + requestSize );
311             throw new IllegalArgumentException JavaDoc( "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