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.ContextProcessorRegistry;
24  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextPublisherRegistry;
25  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextQueryFactory;
26  
27  /**
28   * Automatic activator of {@link ContextProcessorRegistry} service. This service
29   * tracker registers and unregisters {@link ContextProcessorRegistry} service as
30   * {@link ContextEventTypeRegistry}, {@link ContextQueryFactory}, and
31   * {@link ContextPublisherRegistry} services, on which it depends, change their
32   * availability.
33   * 
34   * @author sanjin
35   */
36  public class ContextProcessorRegistryActivator extends AbstractGenericActivator {
37  
38      private static final Logger LOGGER = Logger.getLogger(ContextProcessorRegistryActivator.class);
39  
40      private ContextProcessorRegistryImpl contextProcessorRegistry;
41  
42      /**
43       * Creates instance of OSGi Activator for {@link ContextProcessorRegistry}
44       * service.
45       */
46      public ContextProcessorRegistryActivator() {
47          super(ContextEventTypeRegistry.class.getName(),
48                  ContextQueryFactory.class.getName(),
49                  ContextPublisherRegistry.class.getName());
50      }
51  
52      @Override
53      protected void start() {
54          final ContextEventTypeRegistry eventTypeRegistry = getDependency(ContextEventTypeRegistry.class.getName());
55          final ContextQueryFactory queryFactory = getDependency(ContextQueryFactory.class.getName());
56          final ContextPublisherRegistry publisherRegistry = getDependency(ContextPublisherRegistry.class.getName());
57  
58          this.contextProcessorRegistry = new ContextProcessorRegistryImpl(
59                  eventTypeRegistry, publisherRegistry, queryFactory);
60          if (register(ContextProcessorRegistry.class,
61                  this.contextProcessorRegistry))
62              LOGGER.info("Successfully registered ContextProcessorRegistry service!");
63          else {
64              LOGGER.error("Failed to register ContextProcessorRegistry service!");
65              this.contextProcessorRegistry.shutdown();
66              this.contextProcessorRegistry = null;
67          }
68      }
69  
70      @Override
71      protected void stop() {
72          if (this.contextProcessorRegistry != null) {
73              unregister(ContextProcessorRegistry.class);
74              LOGGER.info("Successfully unregistered ContextProcessorRegistry service!");
75  
76              this.contextProcessorRegistry.shutdown();
77              this.contextProcessorRegistry = null;
78          }
79      }
80  }