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.

LegacyVaadinPortlet.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2018 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.portlet.PortletConfig;
  18. import javax.portlet.PortletException;
  19. import javax.portlet.PortletRequest;
  20. import com.vaadin.util.ReflectTools;
  21. public class LegacyVaadinPortlet extends VaadinPortlet {
  22. private static final LegacyApplicationUIProvider PROVIDER = new LegacyApplicationUIProvider() {
  23. @Override
  24. protected LegacyApplication createApplication() {
  25. VaadinPortlet portlet = VaadinPortlet.getCurrent();
  26. if (portlet instanceof LegacyVaadinPortlet) {
  27. LegacyVaadinPortlet legacyPortlet = (LegacyVaadinPortlet) portlet;
  28. PortletRequest request = VaadinPortletService
  29. .getCurrentPortletRequest();
  30. if (legacyPortlet.shouldCreateApplication(request)) {
  31. try {
  32. return legacyPortlet.getNewApplication(request);
  33. } catch (PortletException e) {
  34. throw new RuntimeException(e);
  35. }
  36. }
  37. }
  38. return null;
  39. }
  40. };
  41. @Override
  42. public void init(PortletConfig portletConfig) throws PortletException {
  43. super.init(portletConfig);
  44. getService().addSessionInitListener((SessionInitEvent event) -> {
  45. try {
  46. onVaadinSessionStarted(
  47. (VaadinPortletRequest) event.getRequest(),
  48. (VaadinPortletSession) event.getSession());
  49. } catch (PortletException e) {
  50. throw new ServiceException(e);
  51. }
  52. });
  53. }
  54. protected Class<? extends LegacyApplication> getApplicationClass()
  55. throws ClassNotFoundException {
  56. try {
  57. return ServletPortletHelper.getLegacyApplicationClass(getService());
  58. } catch (ServiceException e) {
  59. throw new RuntimeException(e);
  60. }
  61. }
  62. protected LegacyApplication getNewApplication(PortletRequest request)
  63. throws PortletException {
  64. try {
  65. Class<? extends LegacyApplication> applicationClass = getApplicationClass();
  66. return ReflectTools.createInstance(applicationClass);
  67. } catch (Exception e) {
  68. throw new PortletException(e);
  69. }
  70. }
  71. private void onVaadinSessionStarted(VaadinPortletRequest request,
  72. VaadinPortletSession session) throws PortletException {
  73. session.addUIProvider(PROVIDER);
  74. }
  75. protected boolean shouldCreateApplication(PortletRequest request) {
  76. return true;
  77. }
  78. }