You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicationRunnerServlet.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import javax.servlet.ServletConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import com.vaadin.Application;
  13. @SuppressWarnings("serial")
  14. public class ApplicationRunnerServlet extends AbstractApplicationServlet {
  15. /**
  16. * The name of the application class currently used. Only valid within one
  17. * request.
  18. */
  19. private String[] defaultPackages;
  20. private ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
  21. @Override
  22. public void init(ServletConfig servletConfig) throws ServletException {
  23. super.init(servletConfig);
  24. String initParameter = servletConfig
  25. .getInitParameter("defaultPackages");
  26. if (initParameter != null) {
  27. defaultPackages = initParameter.split(",");
  28. }
  29. }
  30. @Override
  31. protected void service(HttpServletRequest request,
  32. HttpServletResponse response) throws ServletException, IOException {
  33. this.request.set(request);
  34. super.service(request, response);
  35. this.request.set(null);
  36. }
  37. @Override
  38. URL getApplicationUrl(HttpServletRequest request)
  39. throws MalformedURLException {
  40. URL url = super.getApplicationUrl(request);
  41. String path = url.toString();
  42. path += getApplicationRunnerApplicationClassName(request);
  43. path += "/";
  44. return new URL(path);
  45. }
  46. @Override
  47. protected Application getNewApplication(HttpServletRequest request)
  48. throws ServletException {
  49. // Creates a new application instance
  50. try {
  51. final Application application = (Application) getApplicationClass()
  52. .newInstance();
  53. return application;
  54. } catch (final IllegalAccessException e) {
  55. throw new ServletException(e);
  56. } catch (final InstantiationException e) {
  57. throw new ServletException(e);
  58. } catch (final ClassNotFoundException e) {
  59. throw new ServletException(
  60. new InstantiationException(
  61. "Failed to load application class: "
  62. + getApplicationRunnerApplicationClassName(request)));
  63. }
  64. }
  65. private String getApplicationRunnerApplicationClassName(
  66. HttpServletRequest request) {
  67. return getApplicationRunnerURIs(request).applicationClassname;
  68. }
  69. private static class URIS {
  70. String staticFilesPath;
  71. String applicationURI;
  72. String context;
  73. String runner;
  74. String applicationClassname;
  75. }
  76. /**
  77. * Parses application runner URIs.
  78. *
  79. * If request URL is e.g.
  80. * http://localhost:8080/vaadin/run/com.vaadin.demo.Calc then
  81. * <ul>
  82. * <li>context=vaadin</li>
  83. * <li>Runner servlet=run</li>
  84. * <li>Vaadin application=com.vaadin.demo.Calc</li>
  85. * </ul>
  86. *
  87. * @param request
  88. * @return string array containing widgetset URI, application URI and
  89. * context, runner, application classname
  90. */
  91. private static URIS getApplicationRunnerURIs(HttpServletRequest request) {
  92. final String[] urlParts = request.getRequestURI().toString()
  93. .split("\\/");
  94. String context = null;
  95. String runner = null;
  96. URIS uris = new URIS();
  97. String applicationClassname = null;
  98. String contextPath = request.getContextPath();
  99. if (urlParts[1].equals(contextPath.replaceAll("\\/", ""))) {
  100. // class name comes after web context and runner application
  101. context = urlParts[1];
  102. runner = urlParts[2];
  103. if (urlParts.length == 3) {
  104. throw new IllegalArgumentException("No application specified");
  105. }
  106. applicationClassname = urlParts[3];
  107. uris.staticFilesPath = "/" + context;
  108. uris.applicationURI = "/" + context + "/" + runner + "/"
  109. + applicationClassname;
  110. uris.context = context;
  111. uris.runner = runner;
  112. uris.applicationClassname = applicationClassname;
  113. } else {
  114. // no context
  115. context = "";
  116. runner = urlParts[1];
  117. if (urlParts.length == 2) {
  118. throw new IllegalArgumentException("No application specified");
  119. }
  120. applicationClassname = urlParts[2];
  121. uris.staticFilesPath = "/";
  122. uris.applicationURI = "/" + runner + "/" + applicationClassname;
  123. uris.context = context;
  124. uris.runner = runner;
  125. uris.applicationClassname = applicationClassname;
  126. }
  127. return uris;
  128. }
  129. // @Override
  130. @Override
  131. protected Class getApplicationClass() throws ClassNotFoundException {
  132. // TODO use getClassLoader() ?
  133. Class<? extends Application> appClass = null;
  134. String baseName = getApplicationRunnerApplicationClassName(request
  135. .get());
  136. try {
  137. appClass = (Class<? extends Application>) getClass()
  138. .getClassLoader().loadClass(baseName);
  139. return appClass;
  140. } catch (Exception e) {
  141. //
  142. if (defaultPackages != null) {
  143. for (int i = 0; i < defaultPackages.length; i++) {
  144. try {
  145. appClass = (Class<? extends Application>) getClass()
  146. .getClassLoader().loadClass(
  147. defaultPackages[i] + "." + baseName);
  148. } catch (ClassNotFoundException ee) {
  149. // Ignore as this is expected for many packages
  150. } catch (Exception e2) {
  151. // TODO: handle exception
  152. e2.printStackTrace();
  153. }
  154. if (appClass != null) {
  155. return appClass;
  156. }
  157. }
  158. }
  159. }
  160. throw new ClassNotFoundException();
  161. }
  162. @Override
  163. String getRequestPathInfo(HttpServletRequest request) {
  164. String path = request.getPathInfo();
  165. if (path == null) {
  166. return null;
  167. }
  168. path = path.substring(1 + getApplicationRunnerApplicationClassName(
  169. request).length());
  170. return path;
  171. }
  172. @Override
  173. protected String getStaticFilesLocation(HttpServletRequest request) {
  174. URIS uris = getApplicationRunnerURIs(request);
  175. String staticFilesPath = uris.staticFilesPath;
  176. if (staticFilesPath.equals("/")) {
  177. staticFilesPath = "";
  178. }
  179. return staticFilesPath;
  180. }
  181. }