KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > deploy > DownloadFileSource


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  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/deploy/DownloadFileSource.java,v 1.3 2005/12/25 03:39:58 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:39:58 $
28  */

29 package com.sun.enterprise.management.deploy;
30
31 import java.io.File JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36     This class is responsible for generating a File for downloading, given
37     a moduleID and filename within that module. The file may be pre-existing,
38     in which case isTempFile() should return false, or it may be a generated
39     temporary file, in which case isTempFile() should return true. If isTempFile()
40     returns true, the caller will delete the file at some point.
41  */

42 final class DownloadFileSource
43 {
44     private final String JavaDoc mModuleID;
45     private final String JavaDoc mFilename;
46     
47         public
48     DownloadFileSource(
49         final String JavaDoc moduleID,
50         final String JavaDoc filename )
51         throws IOException JavaDoc
52     {
53         mModuleID = moduleID;
54         mFilename = filename;
55     }
56     
57         public File JavaDoc
58     getDownloadFile()
59         throws IOException JavaDoc
60     {
61         return( createDownloadFile( mModuleID, mFilename ) );
62     }
63     
64         public boolean
65     isTempFile()
66     {
67         return( true );
68     }
69     
70     /**
71         This implementation is for testing; the real one needs to find or produce
72         a file corresponding to the requested module and filename.
73      */

74         private File JavaDoc
75     createDownloadFile(
76         final String JavaDoc moduleID,
77         final String JavaDoc filename)
78         throws IOException JavaDoc
79     {
80         final String JavaDoc name = "./" + moduleID + "_" + filename + System.currentTimeMillis();
81         
82         final File JavaDoc theFile = new File JavaDoc( name );
83         theFile.createNewFile();
84         
85         final FileOutputStream JavaDoc out = new FileOutputStream JavaDoc( theFile );
86         
87         final byte[] temp = new byte[ 1024 * 1024 ];
88         
89         long remaining = temp.length * 10;
90         while ( remaining != 0 )
91         {
92             out.write( temp );
93             assert( remaining >= temp.length );
94             remaining -= temp.length;
95         }
96         
97         out.close();
98         
99         return( theFile );
100     }
101 }
102
103
Popular Tags