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

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