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.

PortletUIServiceTrackerCustomizer.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.osgi.liferay;
  17. import java.util.Dictionary;
  18. import java.util.HashMap;
  19. import java.util.Hashtable;
  20. import java.util.Map;
  21. import java.util.Optional;
  22. import javax.portlet.Portlet;
  23. import org.osgi.framework.Bundle;
  24. import org.osgi.framework.BundleContext;
  25. import org.osgi.framework.ServiceObjects;
  26. import org.osgi.framework.ServiceReference;
  27. import org.osgi.framework.ServiceRegistration;
  28. import org.osgi.service.log.LogService;
  29. import org.osgi.util.tracker.ServiceTrackerCustomizer;
  30. import com.vaadin.osgi.resources.VaadinResourceService;
  31. import com.vaadin.ui.UI;
  32. /**
  33. * Tracks {@link UI UIs} registered as OSGi services.
  34. *
  35. * <p>
  36. * If the {@link UI} is annotated with
  37. * {@link VaadinLiferayPortletConfiguration}, a {@link Portlet} is created for
  38. * it.
  39. * <p>
  40. * This only applies to Liferay Portal 7+ with OSGi support.
  41. *
  42. * @author Sampsa Sohlman
  43. *
  44. * @since 8.1
  45. */
  46. class PortletUIServiceTrackerCustomizer
  47. implements ServiceTrackerCustomizer<UI, ServiceObjects<UI>> {
  48. private static final String RESOURCE_PATH_PREFIX = "/o/%s";
  49. private static final String DISPLAY_CATEGORY = PortletProperties.DISPLAY_CATEGORY;
  50. private static final String VAADIN_CATEGORY = "category.vaadin";
  51. private static final String PORTLET_NAME = PortletProperties.PORTLET_NAME;
  52. private static final String DISPLAY_NAME = PortletProperties.DISPLAY_NAME;
  53. private static final String PORTLET_SECURITY_ROLE = PortletProperties.PORTLET_SECURITY_ROLE;
  54. private static final String VAADIN_RESOURCE_PATH = "javax.portlet.init-param.vaadin.resources.path";
  55. private Map<ServiceReference<UI>, ServiceRegistration<Portlet>> portletRegistrations = new HashMap<ServiceReference<UI>, ServiceRegistration<Portlet>>();
  56. private VaadinResourceService service;
  57. private Optional<LogService> logService;
  58. PortletUIServiceTrackerCustomizer(VaadinResourceService service,
  59. LogService logService) {
  60. this.service = service;
  61. this.logService = Optional.ofNullable(logService);
  62. }
  63. @Override
  64. public ServiceObjects<UI> addingService(
  65. ServiceReference<UI> uiServiceReference) {
  66. Bundle bundle = uiServiceReference.getBundle();
  67. BundleContext bundleContext = bundle.getBundleContext();
  68. UI contributedUI = bundleContext.getService(uiServiceReference);
  69. try {
  70. Class<? extends UI> uiClass = contributedUI.getClass();
  71. VaadinLiferayPortletConfiguration portletConfiguration = uiClass
  72. .getAnnotation(VaadinLiferayPortletConfiguration.class);
  73. boolean isPortletUi = uiServiceReference
  74. .getProperty(PortletProperties.PORTLET_UI_PROPERTY) != null
  75. || portletConfiguration != null;
  76. if (isPortletUi) {
  77. return registerPortlet(uiServiceReference,
  78. portletConfiguration);
  79. } else {
  80. // No portlet configuration, ignore the UI
  81. return null;
  82. }
  83. } finally {
  84. bundleContext.ungetService(uiServiceReference);
  85. }
  86. }
  87. private ServiceObjects<UI> registerPortlet(ServiceReference<UI> reference,
  88. VaadinLiferayPortletConfiguration configuration) {
  89. Bundle bundle = reference.getBundle();
  90. BundleContext bundleContext = bundle.getBundleContext();
  91. ServiceObjects<UI> serviceObjects = bundleContext
  92. .getServiceObjects(reference);
  93. OsgiUIProvider uiProvider = new OsgiUIProvider(serviceObjects,
  94. logService);
  95. Dictionary<String, Object> properties = null;
  96. if (configuration != null) {
  97. properties = createPortletProperties(uiProvider, reference,
  98. configuration);
  99. } else {
  100. properties = createPortletProperties(reference);
  101. }
  102. OsgiVaadinPortlet portlet = new OsgiVaadinPortlet(uiProvider);
  103. ServiceRegistration<Portlet> serviceRegistration = bundleContext
  104. .registerService(Portlet.class, portlet, properties);
  105. portletRegistrations.put(reference, serviceRegistration);
  106. return serviceObjects;
  107. }
  108. private Dictionary<String, Object> createPortletProperties(
  109. OsgiUIProvider uiProvider, ServiceReference<UI> reference,
  110. VaadinLiferayPortletConfiguration configuration) {
  111. Hashtable<String, Object> properties = new Hashtable<String, Object>();
  112. String category = configuration.category();
  113. if (category.trim().isEmpty()) {
  114. category = VAADIN_CATEGORY;
  115. }
  116. copyProperty(reference, properties, DISPLAY_CATEGORY, category);
  117. String portletName = configuration.name();
  118. if (portletName.trim().isEmpty()) {
  119. portletName = uiProvider.getDefaultPortletName();
  120. }
  121. String displayName = configuration.displayName();
  122. if (displayName.trim().isEmpty()) {
  123. displayName = uiProvider.getDefaultDisplayName();
  124. }
  125. copyProperty(reference, properties, PORTLET_NAME, portletName);
  126. copyProperty(reference, properties, DISPLAY_NAME, displayName);
  127. copyProperty(reference, properties, PORTLET_SECURITY_ROLE,
  128. configuration.securityRole());
  129. String resourcesPath = String.format(RESOURCE_PATH_PREFIX,
  130. service.getResourcePathPrefix());
  131. copyProperty(reference, properties, VAADIN_RESOURCE_PATH,
  132. resourcesPath);
  133. return properties;
  134. }
  135. private void copyProperty(ServiceReference<UI> serviceReference,
  136. Dictionary<String, Object> properties, String key,
  137. Object defaultValue) {
  138. Object value = serviceReference.getProperty(key);
  139. if (value != null) {
  140. properties.put(key, value);
  141. } else if (value == null && defaultValue != null) {
  142. properties.put(key, defaultValue);
  143. }
  144. }
  145. private Dictionary<String, Object> createPortletProperties(
  146. ServiceReference<UI> reference) {
  147. Hashtable<String, Object> properties = new Hashtable<>();
  148. for (String key : reference.getPropertyKeys()) {
  149. properties.put(key, reference.getProperty(key));
  150. }
  151. String resourcesPath = String.format(RESOURCE_PATH_PREFIX,
  152. service.getResourcePathPrefix());
  153. properties.put(VAADIN_RESOURCE_PATH, resourcesPath);
  154. return properties;
  155. }
  156. @Override
  157. public void modifiedService(ServiceReference<UI> serviceReference,
  158. ServiceObjects<UI> ui) {
  159. /*
  160. * This service has been registered as a portlet at some point,
  161. * otherwise it wouldn't be tracked.
  162. *
  163. * This handles changes for Portlet related properties that are part of
  164. * the UI service to be passed to the Portlet service registration.
  165. */
  166. Dictionary<String, Object> newProperties = createPortletProperties(
  167. serviceReference);
  168. ServiceRegistration<Portlet> registration = portletRegistrations
  169. .get(serviceReference);
  170. if (registration != null) {
  171. registration.setProperties(newProperties);
  172. }
  173. }
  174. @Override
  175. public void removedService(ServiceReference<UI> reference,
  176. ServiceObjects<UI> ui) {
  177. ServiceRegistration<Portlet> portletRegistration = portletRegistrations
  178. .get(reference);
  179. portletRegistrations.remove(reference);
  180. portletRegistration.unregister();
  181. }
  182. void cleanPortletRegistrations() {
  183. for (ServiceRegistration<Portlet> registration : portletRegistrations
  184. .values()) {
  185. registration.unregister();
  186. }
  187. portletRegistrations.clear();
  188. portletRegistrations = null;
  189. }
  190. }