KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > servlet > DiagnosticReportContentSource


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.tools.admingui.servlet;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 import javax.servlet.ServletRequest JavaDoc;
30
31
32 /**
33  *
34  */

35 public class DiagnosticReportContentSource implements DownloadServlet.ContentSource {
36
37     /**
38      * <p> This method returns a unique string used to identify this
39      * {@link DownloadServlet#ContentSource}. This string must be
40      * specified in order to select the appropriate
41      * {@link DownloadServlet#ContentSource} when using the
42      * {@link DownloadServlet}.</p>
43      */

44     public String JavaDoc getId() {
45     return "diagReport"; // NOI18N
46
}
47
48     /**
49      * <p> This method is responsible for generating the content and
50      * returning an InputStream to that content. It is also
51      * responsible for setting any attribute values in the
52      * {@link DownloadServlet#Context}, such as {@link DownloadServlet#EXTENSION} or
53      * {@link DownloadServlet#CONTENT_TYPE}.</p>
54      */

55     public InputStream JavaDoc getInputStream(DownloadServlet.Context ctx) {
56     // Set the extension so it can be mapped to a MIME type
57
ctx.setAttribute(DownloadServlet.EXTENSION, "jar"); // NOI18N
58

59     // Get file name
60
ServletRequest JavaDoc request = ctx.getServletRequest();
61     String JavaDoc filename = request.getParameter("diagReportConfirmation."
62         + "diagReportConfirmation.report"); // NOI18N
63
if ((filename == null) || (filename.trim().length() == 0)) {
64         throw new IllegalArgumentException JavaDoc(
65             "Report name not specified!"); // NOI18N
66
}
67
68     // Open a Stream
69
InputStream JavaDoc stream = null;
70     try {
71         stream = new FileInputStream JavaDoc(filename);
72     } catch (Exception JavaDoc ex) {
73         throw new RuntimeException JavaDoc(ex);
74     }
75
76     // Save some important stuff for cleanUp
77
ctx.setAttribute("stream", stream); // NOI18N
78

79     // Return the InputStream
80
return stream;
81     }
82
83     /**
84      * <p> This method may be used to clean up any temporary resources. It
85      * will be invoked after the <code>InputStream</code> has been
86      * completely read.</p>
87      */

88     public void cleanUp(DownloadServlet.Context ctx) {
89     // Get the File information
90
InputStream JavaDoc stream =
91         (InputStream JavaDoc) ctx.getAttribute("stream"); // NOI18N
92

93     // Close the InputStream
94
if (stream != null) {
95         try {
96         stream.close();
97         } catch (Exception JavaDoc ex) {
98         // Ignore...
99
}
100     }
101
102     // Null references...
103
ctx.removeAttribute("stream"); // NOI18N
104
}
105
106     /**
107      * <p> This method is responsible for returning the last modified date of
108      * the content, or -1 if not applicable. This information will be
109      * used for caching. This implementation always returns -1.</p>
110      *
111      * @return -1
112      */

113     public long getLastModified(DownloadServlet.Context context) {
114     return -1;
115     }
116 }
117
Popular Tags