comparison servlet/src/digilib/servlet/ServletOps.java @ 290:5d0c0da080ec gen2 scaleable_1

digilib servlet version 2 ("scaleable digilib") - first stab at using thread pools to limit resource use - using Dug Leas util.concurrent - doesn't mix with tomcat :-(
author robcast
date Thu, 21 Oct 2004 20:53:37 +0200
parents 0ff3ede32060
children 1cec876f2788
comparison
equal deleted inserted replaced
289:9f7b864f955f 290:5d0c0da080ec
1 /* ServletOps -- Servlet utility class 1 /*
2 2 * ServletOps -- Servlet utility class
3 Digital Image Library servlet components 3 *
4 4 * Digital Image Library servlet components
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) 5 *
6 6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
7 This program is free software; you can redistribute it and/or modify it 7 *
8 under the terms of the GNU General Public License as published by the 8 * This program is free software; you can redistribute it and/or modify it under
9 Free Software Foundation; either version 2 of the License, or (at your 9 * the terms of the GNU General Public License as published by the Free Software
10 option) any later version. 10 * Foundation; either version 2 of the License, or (at your option) any later
11 11 * version.
12 Please read license.txt for the full details. A copy of the GPL 12 *
13 may be found at http://www.gnu.org/copyleft/lgpl.html 13 * Please read license.txt for the full details. A copy of the GPL may be found
14 14 * at http://www.gnu.org/copyleft/lgpl.html
15 You should have received a copy of the GNU General Public License 15 *
16 along with this program; if not, write to the Free Software 16 * You should have received a copy of the GNU General Public License along with
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 18 * Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 19 *
20 */
20 21
21 package digilib.servlet; 22 package digilib.servlet;
22 23
23 import javax.servlet.*; 24 import java.io.File;
24 import javax.servlet.http.*; 25 import java.io.FileInputStream;
25 import java.io.*; 26 import java.io.IOException;
26 import java.util.*; 27 import java.io.OutputStream;
27 28 import java.io.PrintWriter;
28 import digilib.*; 29 import java.util.StringTokenizer;
29 import digilib.io.*; 30
30 31 import javax.servlet.ServletConfig;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.log4j.Logger;
35
36 import EDU.oswego.cs.dl.util.concurrent.Executor;
37 import digilib.io.FileOpException;
38 import digilib.io.FileOps;
31 39
32 public class ServletOps { 40 public class ServletOps {
33 41
34 private Utils util = null; 42 private static Logger logger = Logger.getLogger("servlet.op");
35 private Hashtable confTable = null; 43
36 44 /**
37 public ServletOps() { 45 * convert a string with a list of pathnames into an array of strings using
38 util = new Utils(); 46 * the system's path seperator string
39 } 47 */
40 48 public static String[] getPathArray(String paths) {
41 public ServletOps(Utils u) { 49 // split list into directories
42 util = u; 50 StringTokenizer dirs = new StringTokenizer(paths,
43 } 51 java.io.File.pathSeparator);
44 52 int n = dirs.countTokens();
45 public ServletOps(Utils u, ServletConfig sc) throws ServletException { 53 if (n < 1) {
46 util = u; 54 return null;
47 setConfig(sc); 55 }
48 } 56 // add directories into array
49 57 String[] pathArray = new String[n];
50 public void setUtils(Utils u) { 58 for (int i = 0; i < n; i++) {
51 util = u; 59 pathArray[i] = dirs.nextToken();
52 } 60 }
53 61 return pathArray;
54 /** 62 }
55 * read parameter list from the XML file in init parameter "config-file" 63
56 */ 64 /**
57 public void setConfig(ServletConfig c) throws ServletException { 65 * get a real File for a config File.
58 // reset parameter table 66 *
59 confTable = null; 67 * If the File is not absolute the path is appended to the WEB-INF directory
60 if (c == null) { 68 * of the web-app.
61 return; 69 *
62 } 70 * @param file
63 // get config file name 71 * @param sc
64 String fn = c.getInitParameter("config-file"); 72 * @return
65 if (fn == null) { 73 */
66 util.dprintln(4, "setConfig: no param config-file"); 74 public static File getConfigFile(File f, ServletConfig sc) {
67 return; 75 // is the filename absolute?
68 } 76 if (!f.isAbsolute()) {
69 File f = new File(fn); 77 // relative path -> use getRealPath to resolve in WEB-INF
70 // setup config file list reader 78 String fn = sc.getServletContext().getRealPath(
71 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); 79 "WEB-INF/" + f.getPath());
72 try { 80 f = new File(fn);
73 confTable = lilo.loadURL(f.toURL().toString()); 81 }
74 } catch (Exception e) { 82 return f;
75 util.dprintln(4, "setConfig: unable to read file "+fn); 83 }
76 throw new ServletException(e); 84
77 } 85 /**
78 } 86 * get a real file name for a config file pathname.
79 87 *
80 /** 88 * If filename starts with "/" its treated as absolute else the path is
81 * print a servlet response and exit 89 * appended to the WEB-INF directory of the web-app.
82 */ 90 *
83 public static void htmlMessage(String s, HttpServletResponse response) throws IOException { 91 * @param filename
84 response.setContentType("text/html; charset=iso-8859-1"); 92 * @param sc
85 PrintWriter out = response.getWriter(); 93 * @return
86 out.println("<html>"); 94 */
87 out.println("<head><title>Scaler</title></head>"); 95 public static String getConfigFile(String filename, ServletConfig sc) {
88 out.println("<body>"); 96 File f = new File(filename);
89 out.println("<p>"+s+"</p>"); 97 // is the filename absolute?
90 out.println("</body></html>"); 98 if (!f.isAbsolute()) {
91 } 99 // relative path -> use getRealPath to resolve in WEB-INF
92 100 filename = sc.getServletContext()
93 /** 101 .getRealPath("WEB-INF/" + filename);
94 * get a parameter from request and return it if set, otherwise return default 102 }
95 */ 103 return filename;
96 public int tryToGetParam(String s, int i, HttpServletRequest r) { 104 }
97 try { 105
98 i = Integer.parseInt(r.getParameter(s)); 106 /**
99 } catch(Exception e) { 107 * print a servlet response and exit
100 util.dprintln(4, "trytoGetParam(int) failed on param "+s); 108 */
101 //e.printStackTrace(); 109 public static void htmlMessage(String msg, HttpServletResponse response)
102 } 110 throws IOException {
103 return i; 111 htmlMessage("Scaler", msg, response);
104 } 112 }
105 public float tryToGetParam(String s, float f, HttpServletRequest r) { 113
106 try { 114 /**
107 f = Float.parseFloat(r.getParameter(s)); 115 * print a servlet response and exit
108 } catch(Exception e) { 116 */
109 util.dprintln(4, "trytoGetParam(float) failed on param "+s); 117 public static void htmlMessage(String title, String msg,
110 //e.printStackTrace(); 118 HttpServletResponse response) throws IOException {
111 } 119 response.setContentType("text/html; charset=iso-8859-1");
112 return f; 120 PrintWriter out = response.getWriter();
113 } 121 out.println("<html>");
114 public String tryToGetParam(String s, String x, HttpServletRequest r) { 122 out.println("<head><title>" + title + "</title></head>");
115 if (r.getParameter(s) != null) { 123 out.println("<body>");
116 x = r.getParameter(s); 124 out.println("<p>" + msg + "</p>");
117 } else { 125 out.println("</body></html>");
118 util.dprintln(4, "trytoGetParam(string) failed on param "+s); 126 }
119 } 127
120 return x; 128 /**
121 } 129 * Transfers an image file as-is with the mime type mt.
122 130 *
123 131 * The local file is copied to the <code>OutputStream</code> of the
124 /** 132 * <code>ServletResponse</code>. If mt is null then the mime-type is
125 * get an init parameter from config and return it if set, otherwise return default 133 * auto-detected with mimeForFile.
126 */ 134 *
127 public int tryToGetInitParam(String s, int i) { 135 * @param mt
128 //System.out.println("trytogetInitParam("+s+", "+i+")"); 136 * mime-type of the file.
129 try { 137 * @param f
130 //System.out.println("trytogetInitParam: "+(String)confTable.get(s)); 138 * Image file to be sent.
131 i = Integer.parseInt((String)confTable.get(s)); 139 * @param res
132 } catch(Exception e) { 140 * ServletResponse where the image file will be sent.
133 util.dprintln(4, "trytogetInitParam(int) failed on param "+s); 141 * @throws FileOpException
134 //e.printStackTrace(); 142 * Exception is thrown for a IOException.
135 } 143 */
136 return i; 144 public static void sendFileImmediately(File f, String mt,
137 } 145 HttpServletResponse response) throws FileOpException {
138 public float tryToGetInitParam(String s, float f) { 146 logger.debug("sendRawFile(" + mt + ", " + f + ")");
139 try { 147 if (mt == null) {
140 f = Float.parseFloat((String)confTable.get(s)); 148 // auto-detect mime-type
141 } catch(Exception e) { 149 mt = FileOps.mimeForFile(f);
142 util.dprintln(4, "trytoGetInitParam(float) failed on param "+s); 150 if (mt == null) {
143 //e.printStackTrace(); 151 throw new FileOpException("Unknown file type.");
144 } 152 }
145 return f; 153 }
146 } 154 response.setContentType(mt);
147 public String tryToGetInitParam(String s, String x) { 155 // open file
148 if ((confTable != null)&&((String)confTable.get(s) != null)) { 156 try {
149 x = (String)confTable.get(s); 157 if (mt.equals("application/octet-stream")) {
150 } else { 158 response.addHeader("Content-Disposition",
151 util.dprintln(4, "trytoGetInitParam(string) failed on param "+s); 159 "attachment; filename=\"" + f.getName() + "\"");
152 } 160 }
153 return x; 161 FileInputStream inFile = new FileInputStream(f);
154 } 162 OutputStream outStream = response.getOutputStream();
163 byte dataBuffer[] = new byte[4096];
164 int len;
165 while ((len = inFile.read(dataBuffer)) != -1) {
166 // copy out file
167 outStream.write(dataBuffer, 0, len);
168 }
169 inFile.close();
170 response.flushBuffer();
171 } catch (IOException e) {
172 throw new FileOpException("Unable to send file.");
173 }
174 }
175
176 /**
177 * Transfers an image file as-is with the mime type mt using a work queue.
178 *
179 * The local file is copied to the <code>OutputStream</code> of the
180 * <code>ServletResponse</code>. If mt is null then the mime-type is
181 * auto-detected with mimeForFile.
182 *
183 * @param mt
184 * mime-type of the file.
185 * @param f
186 * Image file to be sent.
187 * @param res
188 * ServletResponse where the image file will be sent.
189 * @throws FileOpException
190 * Exception is thrown for a IOException.
191 */
192 public static void sendFile(File f, String mimetype,
193 HttpServletResponse response, Executor workQueue)
194 throws FileOpException {
195 // we're cheating
196 sendFileImmediately(f, mimetype, response);
197 /*
198 // create worker
199 DigilibSender job = new DigilibSender(f, null, response);
200 try {
201 logger.debug("queue size: "
202 + ((DigilibManager) workQueue).getQueueSize());
203 workQueue.execute(job);
204 logger.debug("job sent!");
205 synchronized (job) {
206 while (job.isBusy()) {
207 job.wait();
208 }
209 }
210 } catch (InterruptedException e) {
211 throw new FileOpException("INTERRUPTED: Unable to send file. " + e);
212 }
213 */
214
215 }
155 216
156 } 217 }