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.

LegacyVaadinServlet.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import javax.servlet.ServletConfig;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServletRequest;
  20. import com.vaadin.util.ReflectTools;
  21. public class LegacyVaadinServlet extends VaadinServlet {
  22. private static final UIProvider PROVIDER = new LegacyApplicationUIProvider() {
  23. @Override
  24. protected LegacyApplication createApplication() {
  25. VaadinServlet servlet = VaadinServlet.getCurrent();
  26. if (servlet instanceof LegacyVaadinServlet) {
  27. LegacyVaadinServlet legacyServlet = (LegacyVaadinServlet) servlet;
  28. HttpServletRequest request = VaadinServletService
  29. .getCurrentServletRequest();
  30. try {
  31. if (legacyServlet.shouldCreateApplication(request)) {
  32. return legacyServlet.getNewApplication(request);
  33. }
  34. } catch (ServletException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. return null;
  39. }
  40. };
  41. @Override
  42. public void init(ServletConfig servletConfig) throws ServletException {
  43. super.init(servletConfig);
  44. getService().addSessionInitListener((SessionInitEvent event) -> {
  45. try {
  46. onVaadinSessionStarted(event.getRequest(), event.getSession());
  47. } catch (ServletException e) {
  48. throw new ServiceException(e);
  49. }
  50. });
  51. }
  52. protected Class<? extends LegacyApplication> getApplicationClass()
  53. throws ClassNotFoundException {
  54. try {
  55. return ServletPortletHelper.getLegacyApplicationClass(getService());
  56. } catch (ServiceException e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. protected LegacyApplication getNewApplication(HttpServletRequest request)
  61. throws ServletException {
  62. try {
  63. Class<? extends LegacyApplication> applicationClass = getApplicationClass();
  64. return ReflectTools.createInstance(applicationClass);
  65. } catch (Exception e) {
  66. throw new ServletException(e);
  67. }
  68. }
  69. protected boolean shouldCreateApplication(HttpServletRequest request)
  70. throws ServletException {
  71. return true;
  72. }
  73. private void onVaadinSessionStarted(VaadinRequest request,
  74. VaadinSession session) throws ServletException {
  75. session.addUIProvider(PROVIDER);
  76. }
  77. }