diff options
author | Jani Laakso <jani.laakso@itmill.com> | 2007-12-04 18:13:55 +0000 |
---|---|---|
committer | Jani Laakso <jani.laakso@itmill.com> | 2007-12-04 18:13:55 +0000 |
commit | 2bfeca0498c879c11f11a54a22ad87cccdaa78c2 (patch) | |
tree | 827aa1d49b3348f7fb3273ddb39db38cf211284f /src/com/itmill/toolkit/terminal | |
parent | 0df0319345e22146c3ff8b12802937b37f5dc06d (diff) | |
download | vaadin-framework-2bfeca0498c879c11f11a54a22ad87cccdaa78c2.tar.gz vaadin-framework-2bfeca0498c879c11f11a54a22ad87cccdaa78c2.zip |
Fixed #943 (Creating new applications without altering web.xml)
How to use
* Try http://localhost:8080/myContext/run/com.itmill.toolkit.demo.Calc
web.xml contains run/* servlet with applicationRunner=true parameter.
This should not affect Toolkit in any way if applicationRunner is not defined.
Currently it is defined only with run/*
This needs review. Reason why I did this is to
* skip one step more for newbies
* "how to start" flash demo is simpler (included XML is enough for any new application)
Side-effects
* UIDL request have part of URI path after their UIDL request, see firebug
svn changeset:3160/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/terminal')
-rw-r--r-- | src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java | 139 |
1 files changed, 104 insertions, 35 deletions
diff --git a/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java b/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java index 529d1f4229..c7d9f8357d 100644 --- a/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java +++ b/src/com/itmill/toolkit/terminal/gwt/server/ApplicationServlet.java @@ -146,6 +146,8 @@ public class ApplicationServlet extends HttpServlet { private String debugMode = ""; + private boolean applicationRunnerMode = false; + private ClassLoader classLoader; /** @@ -163,12 +165,17 @@ public class ApplicationServlet extends HttpServlet { throws javax.servlet.ServletException { super.init(servletConfig); - // Gets the application class name - String applicationClassName = servletConfig - .getInitParameter("application"); - if (applicationClassName == null) { - System.err - .println("Application not specified in servlet parameters"); + // Get applicationRunner + String applicationRunner = servletConfig + .getInitParameter("applicationRunner"); + if (applicationRunner != null) { + if ("true".equals(applicationRunner)) + applicationRunnerMode = true; + else if ("false".equals(applicationRunner)) + applicationRunnerMode = false; + else + throw new ServletException( + "If applicationRunner parameter is given for an application, it must be 'true' or 'false'"); } // Stores the application parameters into Properties object @@ -225,13 +232,23 @@ public class ApplicationServlet extends HttpServlet { // Loads the application class using the same class loader // as the servlet itself - try { - applicationClass = classLoader.loadClass(applicationClassName); - } catch (ClassNotFoundException e) { - throw new ServletException("Failed to load application class: " - + applicationClassName); + if (!applicationRunnerMode) { + // Gets the application class name + String applicationClassName = servletConfig + .getInitParameter("application"); + if (applicationClassName == null) + throw new ServletException( + "Application not specified in servlet parameters"); + try { + applicationClass = classLoader.loadClass(applicationClassName); + } catch (ClassNotFoundException e) { + throw new ServletException("Failed to load application class: " + + applicationClassName); + } + } else { + // This servlet is in application runner mode, it uses classloader + // later to create Applications based on URL } - } /** @@ -302,10 +319,19 @@ public class ApplicationServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if (request.getPathInfo() != null - && request.getPathInfo().startsWith("/ITMILL/")) { - serveStaticResourcesInITMILL(request, response); - return; + if (request.getPathInfo() != null) { + if (applicationRunnerMode + && (request.getPathInfo().indexOf("/", 1) != -1)) { + String resourceUrl = request.getPathInfo().substring( + request.getPathInfo().indexOf('/', 1)); + if (resourceUrl.startsWith("/ITMILL/")) { + serveStaticResourcesInITMILL(resourceUrl, response); + return; + } + } else if (request.getPathInfo().startsWith("/ITMILL/")) { + serveStaticResourcesInITMILL(request.getPathInfo(), response); + return; + } } Application application = null; @@ -342,10 +368,25 @@ public class ApplicationServlet extends HttpServlet { // Handles AJAX UIDL requests String resourceId = request.getPathInfo(); - if (resourceId != null && resourceId.startsWith(AJAX_UIDL_URI)) { - getApplicationManager(application).handleUidlRequest(request, - response); - return; + if (resourceId != null) { + if (applicationRunnerMode) { + if (resourceId.indexOf("/", 1) != -1) { + String resourceUrl = resourceId.substring(resourceId + .indexOf('/', 1)); + if (resourceId != null + && (resourceUrl.startsWith(AJAX_UIDL_URI))) { + getApplicationManager(application) + .handleUidlRequest(request, response); + return; + } + } + } else { + if (resourceId.startsWith(AJAX_UIDL_URI)) { + getApplicationManager(application).handleUidlRequest( + request, response); + return; + } + } } // Handles the URI if the application is still running @@ -427,9 +468,8 @@ public class ApplicationServlet extends HttpServlet { * @param response * @throws IOException */ - private void serveStaticResourcesInITMILL(HttpServletRequest request, + private void serveStaticResourcesInITMILL(String filename, HttpServletResponse response) throws IOException { - String filename = request.getPathInfo(); ServletContext sc = getServletContext(); InputStream is = sc.getResourceAsStream(filename); if (is == null) { @@ -498,17 +538,28 @@ public class ApplicationServlet extends HttpServlet { + "<script type=\"text/javascript\">\n" + " var itmill = {\n" + " appUri:'"); - String[] urlParts = getApplicationUrl(request).toString().split("\\/"); - String appUrl = ""; // don't use server and port in uri. It may cause problems with some // virtual server configurations which lose the server name - for (int i = 3; i < urlParts.length; i++) { - appUrl += "/" + urlParts[i]; - } - if (appUrl.endsWith("/")) { - appUrl = appUrl.substring(0, appUrl.length() - 1); + String appUrl = ""; + String[] urlParts; + if (applicationRunnerMode) { + String servletPath = request.getContextPath() + + request.getServletPath(); + if (request.getPathInfo().indexOf('/', 1) == -1) + servletPath += request.getPathInfo(); + else + servletPath += request.getPathInfo().substring(1, + request.getPathInfo().indexOf('/', 1)); + appUrl = servletPath; + } else { + urlParts = getApplicationUrl(request).toString().split("\\/"); + for (int i = 3; i < urlParts.length; i++) { + appUrl += "/" + urlParts[i]; + } + if (appUrl.endsWith("/")) { + appUrl = appUrl.substring(0, appUrl.length() - 1); + } } - page.write(appUrl); String widgetset = applicationProperties @@ -770,6 +821,8 @@ public class ApplicationServlet extends HttpServlet { .getServerPort() == 80) ? "" : ":" + request.getServerPort()) + request.getRequestURI()); + if (applicationRunnerMode) + return reqURL; String servletPath = request.getContextPath() + request.getServletPath(); if (servletPath.length() == 0 @@ -825,13 +878,19 @@ public class ApplicationServlet extends HttpServlet { if (servletPath.length() < aPath.length()) { servletPath += "/"; } - if (servletPath.equals(aPath)) { + if (applicationRunnerMode) { + if (request.getPathInfo().indexOf('/', 1) == -1) + servletPath += request.getPathInfo(); + else + servletPath += request.getPathInfo().substring(1, + request.getPathInfo().indexOf('/', 1)); + } + if (servletPath.equals(aPath)) { // Found a running application if (a.isRunning()) { return a; } - // Application has stopped, so remove it before creating a new // application WebApplicationContext.getApplicationContext(session) @@ -839,12 +898,23 @@ public class ApplicationServlet extends HttpServlet { break; } } - // Creates application, because a running one was not found WebApplicationContext context = WebApplicationContext .getApplicationContext(request.getSession()); URL applicationUrl = getApplicationUrl(request); + if (applicationRunnerMode) { + String applicationClassName = applicationUrl.getPath().substring( + applicationUrl.getPath().lastIndexOf('/') + 1); + try { + applicationClass = classLoader.loadClass(applicationClassName); + } catch (ClassNotFoundException e) { + throw new InstantiationException( + "Failed to load application class: " + + applicationClassName); + } + } + // Creates new application and start it try { Application application = (Application) applicationClass @@ -854,9 +924,8 @@ public class ApplicationServlet extends HttpServlet { // Sets initial locale from the request application.setLocale(request.getLocale()); - // Starts application and check license + // Starts application application.start(applicationUrl, applicationProperties, context); - return application; } catch (IllegalAccessException e) { |