KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.tools.admingui.util.MBeanUtil;
26 import com.sun.enterprise.admin.common.JMXFileTransfer;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31
32 import javax.servlet.ServletRequest JavaDoc;
33
34
35 /**
36  *
37  */

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

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

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

62     // Get appName
63
ServletRequest JavaDoc request = ctx.getServletRequest();
64     String JavaDoc appName = request.getParameter("appName"); // NOI18N
65
if ((appName == null) || (appName.trim().length() == 0)) {
66         appName = request.getParameter("appClientName"); // NOI18N
67
}
68
69     // Create the tmpFile
70
String JavaDoc tmpFilePath = null;
71     InputStream JavaDoc tmpFile = null;
72     try {
73         tmpFilePath = new JMXFileTransfer(MBeanUtil.getMBeanServer())
74         .downloadClientStubs(
75             appName,
76             System.getProperty("java.io.tmpdir")); // NOI18N
77
tmpFile = new FileInputStream JavaDoc(tmpFilePath);
78     } catch (Exception JavaDoc ex) {
79         throw new RuntimeException JavaDoc(ex);
80     }
81
82     // Save some important stuff for cleanUp
83
ctx.setAttribute("tmpFilePath", tmpFilePath); // NOI18N
84
ctx.setAttribute("tmpFile", tmpFile); // NOI18N
85

86     // Return an InputStream to the tmpFile
87
return tmpFile;
88     }
89
90     /**
91      * <p> This method may be used to clean up any temporary resources. It
92      * will be invoked after the <code>InputStream</code> has been
93      * completely read.</p>
94      */

95     public void cleanUp(DownloadServlet.Context ctx) {
96     // Get the File information
97
String JavaDoc tmpFilePath =
98         (String JavaDoc) ctx.getAttribute("tmpFilePath"); // NOI18N
99
InputStream JavaDoc tmpFile =
100         (InputStream JavaDoc) ctx.getAttribute("tmpFile"); // NOI18N
101

102     // Close the InputStream
103
if (tmpFile != null) {
104         try {
105         tmpFile.close();
106         } catch (Exception JavaDoc ex) {
107         // Ignore...
108
}
109     }
110
111     // Delete the Temporary File
112
if (tmpFilePath != null) {
113         File JavaDoc file = new File JavaDoc(tmpFilePath);
114         try {
115         file.delete();
116         } catch (Exception JavaDoc ex) {
117         // Ignore...
118
}
119     }
120
121     // Null references...
122
ctx.removeAttribute("tmpFilePath"); // NOI18N
123
ctx.removeAttribute("tmpFile"); // NOI18N
124
}
125
126     /**
127      * <p> This method is responsible for returning the last modified date of
128      * the content, or -1 if not applicable. This information will be
129      * used for caching. This implementation always returns -1.</p>
130      *
131      * @return -1
132      */

133     public long getLastModified(DownloadServlet.Context context) {
134     return -1;
135     }
136 }
137
Popular Tags