[Mulgara-svn] r1518 - in trunk/src/jar: server/java/org/mulgara/server web/java/org/mulgara/webquery
pag at mulgara.org
pag at mulgara.org
Tue Feb 17 16:11:22 UTC 2009
Author: pag
Date: 2009-02-17 08:11:21 -0800 (Tue, 17 Feb 2009)
New Revision: 1518
Modified:
trunk/src/jar/server/java/org/mulgara/server/HttpServices.java
trunk/src/jar/web/java/org/mulgara/webquery/QueryServlet.java
trunk/src/jar/web/java/org/mulgara/webquery/TutorialServlet.java
Log:
Refactored servlets to allow deployment in a WAR file
Modified: trunk/src/jar/server/java/org/mulgara/server/HttpServices.java
===================================================================
--- trunk/src/jar/server/java/org/mulgara/server/HttpServices.java 2009-02-17 16:10:35 UTC (rev 1517)
+++ trunk/src/jar/server/java/org/mulgara/server/HttpServices.java 2009-02-17 16:11:21 UTC (rev 1518)
@@ -371,9 +371,8 @@
// create the web context
try {
- AbstractServer serverMBean = (AbstractServer)hostServer.getServerMBean();
String rmiName = hostServer.getServerName();
- Servlet servlet = (Servlet)Reflect.newInstance(Class.forName(servletClass), hostName, rmiName, serverMBean);
+ Servlet servlet = (Servlet)Reflect.newInstance(Class.forName(servletClass), hostName, rmiName, hostServer);
String webPath = "/" + servletPath;
new org.mortbay.jetty.servlet.Context(server, webPath, SESSIONS).addServlet(new ServletHolder(servlet), "/*");
return new Service(name, webPath);
Modified: trunk/src/jar/web/java/org/mulgara/webquery/QueryServlet.java
===================================================================
--- trunk/src/jar/web/java/org/mulgara/webquery/QueryServlet.java 2009-02-17 16:10:35 UTC (rev 1517)
+++ trunk/src/jar/web/java/org/mulgara/webquery/QueryServlet.java 2009-02-17 16:11:21 UTC (rev 1518)
@@ -27,24 +27,26 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.MissingResourceException;
import java.util.Set;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
-import javax.servlet.http.HttpServlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.mulgara.connection.Connection;
-import org.mulgara.connection.SessionConnection;
import org.mulgara.itql.TqlInterpreter;
import org.mulgara.parser.Interpreter;
import org.mulgara.parser.MulgaraLexerException;
import org.mulgara.parser.MulgaraParserException;
import org.mulgara.protocol.http.MimeMultiNamedPart;
+import org.mulgara.protocol.http.MulgaraServlet;
import org.mulgara.protocol.http.ServletDataSource;
import org.mulgara.query.Answer;
import org.mulgara.query.Query;
@@ -53,8 +55,7 @@
import org.mulgara.query.operation.Command;
import org.mulgara.query.operation.CreateGraph;
import org.mulgara.query.operation.Load;
-import org.mulgara.server.AbstractServer;
-import org.mulgara.server.SessionFactory;
+import org.mulgara.server.SessionFactoryProvider;
import org.mulgara.sparql.SparqlInterpreter;
import org.mulgara.util.SparqlUtil;
import org.mulgara.util.StackTrace;
@@ -75,17 +76,11 @@
* @author Paul Gearon
* @copyright © 2008 <a href="http://www.fedora-commons.org/">Fedora Commons</a>
*/
-public class QueryServlet extends HttpServlet {
+public class QueryServlet extends MulgaraServlet {
/** Serialization by default */
private static final long serialVersionUID = -8407263937557243990L;
- /** The default name to use for the host. */
- private static final String DEFAULT_HOSTNAME = "localhost";
-
- /** Session value for database connection. */
- private static final String CONNECTION = "session.connection";
-
/** Session value for the TQL interpreter. */
private static final String TQL_INTERPRETER = "session.tql.interpreter";
@@ -99,20 +94,14 @@
protected static final String HTTP_PUT_NS = "http-put://upload/";
/** The name of the host for the application. */
- private final String hostname;
+ private String hostname;
/** The name of the server for the application. */
- private final String servername;
+ private String servername;
- /** The server for finding a session factory. */
- private final AbstractServer server;
-
/** The default graph URI to use. */
private String defaultGraphUri;
- /** Session factory for accessing the database. */
- private SessionFactory cachedSessionFactory;
-
/** The path down to the template resource. */
private String templatePath;
@@ -122,25 +111,63 @@
/** Debugging text. */
private String debugText = "";
+ /** Indicates if this servlet has been initialized. */
+ private boolean initialized = false;
+
+
/**
* Creates the servlet for the named host.
* @param hostname The host name to use, or <code>null</code> if this is not known.
* @param servername The name of the current server.
*/
- public QueryServlet(String hostname, String servername, AbstractServer server) throws IOException {
- this.hostname = (hostname != null) ? hostname : DEFAULT_HOSTNAME;
- this.servername = servername;
- this.cachedSessionFactory = null;
- this.server = server;
- URL path = getClass().getClassLoader().getResource(ResourceFile.RESOURCES + getTemplateFile());
- if (path == null) throw new IOException("Resource not found: " + ResourceFile.RESOURCES + getTemplateFile());
- templatePath = path.toString();
- resourcePath = templatePath.split("!")[0];
- defaultGraphUri = "rmi://" + hostname + "/" + servername + "#sampledata";
+ public QueryServlet(String hostname, String servername, SessionFactoryProvider server) {
+ super(server);
+ init(hostname, servername);
}
/**
+ * Creates the servlet for the named host.
+ * @param hostname The host name to use, or <code>null</code> if this is not known.
+ * @param servername The name of the current server.
+ */
+ public QueryServlet() {
+ initialized = false;
+ }
+
+
+ /**
+ * Called by a servlet environment, particularly when this is in a Web ARchive (WAR) file.
+ * @see org.mulgara.protocol.http.MulgaraServlet#init(javax.servlet.ServletConfig)
+ */
+ public void init(ServletConfig config) {
+ super.init(config);
+ ServletContext context = config.getServletContext();
+ init(context.getInitParameter(HOST_NAME_PARAM), context.getInitParameter(SERVER_NAME_PARAM));
+ }
+
+
+ /**
+ * Initialize this class with parameters passed from either a Servlet environment,
+ * or a constructing class.
+ * @param hostname The name of this host. If null then the localhost is presumed.
+ * @param servername The name of this service. If null then the default of server1 is used.
+ */
+ private void init(String hostname, String servername) {
+ if (!initialized) {
+ URL path = getClass().getClassLoader().getResource(ResourceFile.RESOURCES + getTemplateFile());
+ if (path == null) throw new MissingResourceException("Missing template file", getClass().getName(), ResourceFile.RESOURCES + getTemplateFile());
+ templatePath = path.toString();
+ resourcePath = templatePath.split("!")[0];
+ this.hostname = (hostname != null) ? hostname : DEFAULT_HOSTNAME;
+ this.servername = (servername != null) ? servername : DEFAULT_SERVERNAME;
+ defaultGraphUri = "rmi://" + hostname + "/" + servername + "#sampledata";
+ initialized = true;
+ }
+ }
+
+
+ /**
* Respond to a request for the servlet.
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@@ -648,41 +675,6 @@
/**
- * Gets the connection for the current session, creating it if it doesn't exist yet.
- * @param req The current request environment.
- * @return A connection that is tied to this HTTP session.
- * @throws IOException When an error occurs creating a new session.
- */
- private Connection getConnection(HttpServletRequest req) throws IOException, IllegalStateException {
- HttpSession httpSession = req.getSession();
- Connection connection = (Connection)httpSession.getAttribute(CONNECTION);
- if (connection == null) {
- try {
- connection = new SessionConnection(getSessionFactory().newSession(), null, null);
- } catch (QueryException qe) {
- throw new IOException("Unable to create a connection to the database. " + qe.getMessage());
- }
- httpSession.setAttribute(CONNECTION, connection);
- }
- return connection;
- }
-
-
- /**
- * This method allows us to put off getting a session factory until the server is
- * ready to provide one.
- * @return A new session factory.
- */
- private SessionFactory getSessionFactory() throws IllegalStateException {
- if (cachedSessionFactory == null) {
- cachedSessionFactory = server.getSessionFactory();
- if (cachedSessionFactory == null) throw new IllegalStateException("Server not yet ready. Try again soon.");
- }
- return cachedSessionFactory;
- }
-
-
- /**
* Get the name of the file to be used for the template.
* @return The absolute file path, with a root set at the resource directory.
*/
Modified: trunk/src/jar/web/java/org/mulgara/webquery/TutorialServlet.java
===================================================================
--- trunk/src/jar/web/java/org/mulgara/webquery/TutorialServlet.java 2009-02-17 16:10:35 UTC (rev 1517)
+++ trunk/src/jar/web/java/org/mulgara/webquery/TutorialServlet.java 2009-02-17 16:11:21 UTC (rev 1518)
@@ -19,6 +19,7 @@
import java.io.IOException;
import org.mulgara.server.AbstractServer;
+import org.mulgara.server.SessionFactoryProvider;
/**
* A tutorial Web UI for the server. This is almost the same as the standard
@@ -48,12 +49,19 @@
* @param server
* @throws IOException
*/
- public TutorialServlet(String hostname, String servername, AbstractServer server) throws IOException {
+ public TutorialServlet(String hostname, String servername, SessionFactoryProvider server) {
super(hostname, servername, server);
}
/**
+ * Creates the tutorial servlet in a managed environment.
+ */
+ public TutorialServlet() {
+ }
+
+
+ /**
* Provide a description for the servlet.
* @see javax.servlet.GenericServlet#getServletInfo()
*/
More information about the Mulgara-svn
mailing list