KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > admin > ws > ClassDownloadServlet


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  *This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20 package sync4j.server.admin.ws;
21
22 import java.io.*;
23 import java.lang.Class JavaDoc;
24 import java.lang.ClassLoader JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import sync4j.framework.logging.Sync4jLogger;
32
33
34 /**
35  * This servlet allows the downloading of a class as found by the servlet's
36  * class loader. It can be used to remotely download classes deployed on the
37  * server.
38  * <p>
39  * It accepts the following parameters:
40  * <table>
41  * <tr>
42  * <td>class</td>
43  * <td>the class to download in the dotted or slashed form and optionally ended
44  * by .class</td>
45  * </tr>
46  * </table>
47  *
48  * @author Stefano Fornari @ Funambol
49  *
50  * @version $Id: ClassDownloadServlet.java,v 1.3 2005/03/02 20:57:39 harrie Exp $
51  *
52  */

53 public final class ClassDownloadServlet
54 extends javax.servlet.http.HttpServlet JavaDoc {
55
56     // --------------------------------------------------------------- Constants
57

58     public static final String JavaDoc LOG_NAME = "server";
59
60     // ------------------------------------------------------------ Private data
61

62     private Logger JavaDoc log = Sync4jLogger.getLogger(LOG_NAME);
63
64     // ---------------------------------------------------------- Public methods
65

66     public void service(final HttpServletRequest request,
67                         final HttpServletResponse response)
68     throws ServletException, IOException {
69         if (log.isLoggable(Level.FINEST)) {
70             log.finest(request.toString());
71             log.finest(request.getRequestURI());
72         }
73         
74         String JavaDoc className = request.getPathInfo();
75         
76         if (className == null) {
77             className = "";
78         } else {
79             //
80
// skip the initial '/'
81
//
82
className = className.substring(1);
83         }
84         
85         if (log.isLoggable(Level.FINEST)) {
86             log.finest("Requesting class: " + className);
87         }
88         
89         //
90
// First of all normalize the class name in order to be retrieved
91
// by getResourceAsStream()
92
//
93
className = className.trim();
94         if (className.endsWith(".class")) {
95             int l = className.length();
96             
97             if (l>6) {
98                 className = className.substring(0, l-6);
99             } else {
100                 className = "";
101             }
102         }
103         className = className.replace('.', '/');
104         className = className + ".class";
105         
106         if (log.isLoggable(Level.FINEST)) {
107             log.finest("Looking for class: " + className);
108         }
109         
110         ClassLoader JavaDoc cl = this.getClass().getClassLoader();
111         
112         InputStream is = cl.getResourceAsStream(className);
113         
114         if (is == null) {
115             //
116
// Class not found!
117
//
118
response.sendError(response.SC_NOT_FOUND, className + " not found");
119             return;
120         }
121         
122         byte[] buf = new byte[4096];
123         int i = 0;
124         OutputStream os = null;
125         try {
126             os = response.getOutputStream();
127             
128             while ((i = is.read(buf)) > 0) {
129                 os.write(buf, 0, i);
130             }
131         } catch (IOException e) {
132             log.severe(e.getMessage());
133             log.throwing(getClass().getName(), "service", e);
134         } finally {
135             if (os != null) {
136                 os.close();
137             }
138             is.close();
139         }
140     }
141
142     // --------------------------------------------------------- Private methods
143
}
144
145
146
Popular Tags