KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > servlet > fusion > XQFusionServlet


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.servlet.fusion;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import javax.servlet.ServletConfig JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.xml.parsers.SAXParserFactory JavaDoc;
38
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.AttributesImpl JavaDoc;
41 import org.xquark.mediator.Mediator;
42 import org.xquark.serialize.XMLSerializer;
43 import org.xquark.xml.xdbc.*;
44
45 public class XQFusionServlet extends HttpServlet JavaDoc {
46
47     private static final String JavaDoc JNDI_SOURCE = "java:comp/env/jdbc/TestDB";
48     private Mediator mediator;
49     private SAXParserFactory JavaDoc factory;
50     private HashMap JavaDoc mappings;
51     private ServletContext JavaDoc context = null;
52
53     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
54         super.init(config);
55         context = config.getServletContext();
56         factory = SAXParserFactory.newInstance();
57         factory.setNamespaceAware(true);
58         File JavaDoc configFile = new File JavaDoc(config.getServletContext().getRealPath("WEB-INF/queries/main.xml"));
59         try {
60             mediator = new Mediator(configFile.toURL().toString());
61         } catch (MalformedURLException JavaDoc e) {
62             throw new ServletException JavaDoc("Could not create mediator", e);
63         } catch (XMLDBCException e) {
64             throw new ServletException JavaDoc("Could not create mediator", e);
65         }
66     }
67
68     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69     throws ServletException JavaDoc, IOException JavaDoc
70     {
71         long t0 = System.currentTimeMillis();
72         PrintWriter JavaDoc out = response.getWriter();
73         XMLConnection conn = null;
74         try {
75             String JavaDoc query = request.getParameter("exec");
76             if (query != null) {
77                 conn = mediator.getConnection();
78                 execQuery(conn, query, out);
79             }
80             else {
81                 reportError(
82                     response,
83                     "Unrecognized command: <B>"
84                         + request.getContextPath()
85                         + request.getServletPath()
86                         + request.getQueryString()
87                         + "</B>.",
88                     "query.jsp");
89                 return;
90             }
91         }
92         catch (XMLDBCException e) {
93             reportException(response, e, "query.jsp");
94         }
95         finally {
96             out.close();
97             if (conn != null)
98             try {
99                 conn.close();
100             } catch (XMLDBCException e) {
101                 // ignore
102
}
103             context.log("Request processed in "+(System.currentTimeMillis()-t0)+" ms.");
104         }
105     }
106
107     private void execQuery(XMLConnection conn, String JavaDoc query, PrintWriter JavaDoc out)
108         throws ServletException JavaDoc, XMLDBCException {
109         PreparedXMLStatement stmt = null;
110         XMLResultSet xrs = null;
111         try {
112             stmt = conn.prepareStatement(query);
113             xrs = stmt.executeQuery();
114             XMLSerializer serializer = new XMLSerializer(out, "iso-8859-1");
115             xrs.setContentHandler(serializer);
116             xrs.setLexicalHandler(serializer);
117             serializer.startDocument();
118             //serializer.processingInstruction("xml-stylesheet", "href=\"xsl/beautify.xsl\" type=\"text/xsl\"");
119
serializer.startElement(
120                 "",
121                 "results",
122                 "results",
123                 new AttributesImpl JavaDoc());
124             while (xrs.hasNext())
125                 xrs.nextAsSAX();
126             serializer.endElement("", "results", "results");
127             serializer.endDocument();
128         } catch (SAXException JavaDoc e) {
129             throw new ServletException JavaDoc("Error while producing document result", e);
130         }
131         finally {
132             if (xrs != null)
133                 xrs.close();
134             if (stmt != null)
135                 stmt.close();
136         }
137     }
138
139     private void reportMessage(
140         HttpServletResponse JavaDoc response,
141         String JavaDoc message,
142         String JavaDoc url)
143         throws IOException JavaDoc {
144         response.sendRedirect(
145             response.encodeRedirectURL(
146                 "info.jsp?url=" + url + "&message=" + message));
147     }
148
149     private void reportError(
150         HttpServletResponse JavaDoc response,
151         String JavaDoc error,
152         String JavaDoc url)
153         throws IOException JavaDoc {
154         response.sendRedirect(
155             response.encodeRedirectURL(
156                 "error.jsp?url=" + url + "&message=" + error));
157     }
158
159     private void reportException(
160         HttpServletResponse JavaDoc response,
161         Exception JavaDoc e,
162         String JavaDoc url)
163         throws IOException JavaDoc {
164         /*
165         StringWriter sw = new StringWriter();
166         e.printStackTrace(new PrintWriter(sw));
167         String message = "<pre>"+sw.toString()+"</pre>";
168         response.sendRedirect(response.encodeRedirectURL("exception.jsp?url="+url+"&message="+message));
169         */

170         PrintWriter JavaDoc out = response.getWriter();
171         out.println("<html><body><pre>");
172         e.printStackTrace(out);
173         out.println("</pre></body></html>");
174     }
175 }
176
Popular Tags