View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.core.internal;
19  
20  import org.apache.log4j.Logger;
21  import at.ac.tuwien.infosys.sm4all.copal.api.helpers.AbstractGenericActivator;
22  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextEventTypeRegistry;
23  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextPublisherRegistry;
24  import at.ac.tuwien.infosys.sm4all.copal.service.event.PublishingService;
25  
26  /**
27   * Automatic activator of {@link ContextPublisherRegistry} service. This service
28   * tracker registers and unregisters {@link ContextPublisherRegistry} service as
29   * {@link PublishingService} and {@link ContextEventTypeRegistry} services, on
30   * which it depends, change their availability.
31   * 
32   * @author sanjin
33   */
34  public class ContextPublisherRegistryActivator extends AbstractGenericActivator {
35  
36      private static final Logger LOGGER = Logger.getLogger(ContextPublisherRegistryActivator.class);
37  
38      private ContextPublisherRegistryImpl contextPublisherRegistry;
39  
40      /**
41       * Creates instance of OSGi Activator for {@link ContextPublisherRegistry}
42       * service.
43       */
44      public ContextPublisherRegistryActivator() {
45          super(ContextEventTypeRegistry.class.getName(),
46                  PublishingService.class.getName());
47      }
48  
49      @Override
50      protected void start() {
51          final ContextEventTypeRegistry eventTypeRegistry = getDependency(ContextEventTypeRegistry.class.getName());
52          final PublishingService publishingService = getDependency(PublishingService.class.getName());
53  
54          this.contextPublisherRegistry = new ContextPublisherRegistryImpl(
55                  eventTypeRegistry, publishingService);
56          if (register(ContextPublisherRegistry.class,
57                  this.contextPublisherRegistry))
58              LOGGER.info("Successfully registered ContextPublisherRegistry service!");
59          else {
60              LOGGER.error("Failed to register ContextPublisherRegistry service!");
61              this.contextPublisherRegistry.shutdown();
62              this.contextPublisherRegistry = null;
63          }
64      }
65  
66      @Override
67      protected void stop() {
68          if (this.contextPublisherRegistry != null) {
69              unregister(ContextPublisherRegistry.class);
70              LOGGER.info("Successfully unregistered ContextPublisherRegistry service!");
71  
72              this.contextPublisherRegistry.shutdown();
73              this.contextPublisherRegistry = null;
74          }
75      }
76  }