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.

OsgiUIProvider.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.osgi.liferay;
  17. import java.util.Optional;
  18. import org.osgi.framework.Constants;
  19. import org.osgi.framework.ServiceObjects;
  20. import org.osgi.framework.ServiceReference;
  21. import org.osgi.service.log.LogService;
  22. import com.vaadin.server.UIClassSelectionEvent;
  23. import com.vaadin.server.UICreateEvent;
  24. import com.vaadin.server.UIProvider;
  25. import com.vaadin.ui.UI;
  26. /**
  27. * Vaadin {@link com.vaadin.server.UIProvider} that provides a single {@link UI}
  28. * class provided through the registration of a {@link UI} as an OSGi service.
  29. * <p>
  30. * This only applies to Liferay Portal 7+ with OSGi support.
  31. *
  32. * @author Sampsa Sohlman
  33. *
  34. * @since 8.1
  35. */
  36. @SuppressWarnings("serial")
  37. public class OsgiUIProvider extends UIProvider {
  38. private Class<UI> uiClass;
  39. private ServiceObjects<UI> serviceObjects;
  40. private boolean prototype;
  41. private Optional<LogService> logService;
  42. @SuppressWarnings("unchecked")
  43. public OsgiUIProvider(ServiceObjects<UI> serviceObjects,
  44. Optional<LogService> logService) {
  45. super();
  46. this.serviceObjects = serviceObjects;
  47. this.logService = logService;
  48. UI ui = serviceObjects.getService();
  49. ServiceReference<UI> reference = serviceObjects.getServiceReference();
  50. Object property = reference.getProperty(Constants.SERVICE_SCOPE);
  51. prototype = Constants.SCOPE_PROTOTYPE.equals(property);
  52. uiClass = (Class<UI>) ui.getClass();
  53. serviceObjects.ungetService(ui);
  54. }
  55. @Override
  56. public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
  57. return uiClass;
  58. }
  59. @Override
  60. public UI createInstance(UICreateEvent event) {
  61. if (prototype) {
  62. UI ui = serviceObjects.getService();
  63. ui.addDetachListener(event2 -> serviceObjects.ungetService(ui));
  64. return ui;
  65. }
  66. logService.ifPresent(log -> log.log(LogService.LOG_WARNING,
  67. "UI services should have a prototype scope! Creating UI instance using the default constructor!"));
  68. return super.createInstance(event);
  69. }
  70. public String getDefaultPortletName() {
  71. return uiClass.getName();
  72. }
  73. public String getDefaultDisplayName() {
  74. return uiClass.getSimpleName();
  75. }
  76. }