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.0KB

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