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

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