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.

OsgiVaadinResources.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.resources;
  17. import org.osgi.framework.BundleActivator;
  18. import org.osgi.framework.BundleContext;
  19. import org.osgi.framework.ServiceReference;
  20. import org.osgi.util.tracker.ServiceTracker;
  21. /**
  22. * {@link BundleActivator} used to provide access to the
  23. * {@link VaadinResourceService} singleton for publishing themes, widgetsets and
  24. * other necessary resources.
  25. *
  26. * @author Vaadin Ltd.
  27. *
  28. * @since 8.1
  29. * @deprecated use OSGi DS services to bind a instance of
  30. * {@link VaadinResourceService}
  31. */
  32. public class OsgiVaadinResources implements BundleActivator {
  33. /**
  34. * Thrown if a method is called when the Resource bundle is not active.
  35. *
  36. * @author Vaadin Ltd.
  37. *
  38. * @since 8.1
  39. */
  40. @SuppressWarnings("serial")
  41. public static class ResourceBundleInactiveException extends Exception {
  42. public ResourceBundleInactiveException(String message) {
  43. super(message);
  44. }
  45. }
  46. private static OsgiVaadinResources instance;
  47. private ServiceTracker<VaadinResourceService, VaadinResourceService> vaadinResourceTracker;
  48. private VaadinResourceService service;
  49. /**
  50. * Returns the {@link VaadinResourceService} instance. Always returns
  51. * non-null.
  52. *
  53. * @return the {@link VaadinResourceService resource service} to use for
  54. * publishing themes, widgetsets and other necessary resources
  55. * @throws ResourceBundleInactiveException
  56. * if the bundle is not active
  57. */
  58. public static VaadinResourceService getService()
  59. throws ResourceBundleInactiveException {
  60. if (instance == null) {
  61. throw new ResourceBundleInactiveException(
  62. "Vaadin Shared is not active!");
  63. }
  64. return instance.service;
  65. }
  66. @Override
  67. public void start(BundleContext context) throws Exception {
  68. vaadinResourceTracker = new ServiceTracker<VaadinResourceService, VaadinResourceService>(
  69. context, VaadinResourceService.class, null) {
  70. @Override
  71. public VaadinResourceService addingService(
  72. ServiceReference<VaadinResourceService> reference) {
  73. VaadinResourceService vaadinService = super.addingService(
  74. reference);
  75. service = vaadinService;
  76. return vaadinService;
  77. }
  78. @Override
  79. public void removedService(
  80. ServiceReference<VaadinResourceService> reference,
  81. VaadinResourceService service) {
  82. super.removedService(reference, service);
  83. if (OsgiVaadinResources.this.service == service) {
  84. OsgiVaadinResources.this.service = null;
  85. }
  86. }
  87. };
  88. vaadinResourceTracker.open();
  89. instance = this;
  90. }
  91. @Override
  92. public void stop(BundleContext context) throws Exception {
  93. if (vaadinResourceTracker != null) {
  94. vaadinResourceTracker.close();
  95. }
  96. vaadinResourceTracker = null;
  97. instance = null;
  98. service = null;
  99. }
  100. }