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