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