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 7.2KB

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