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.rest.internal;
19  
20  import java.util.Dictionary;
21  import java.util.Hashtable;
22  import org.apache.log4j.Logger;
23  import org.osgi.framework.BundleActivator;
24  import org.osgi.framework.BundleContext;
25  import org.osgi.framework.ServiceRegistration;
26  import at.ac.tuwien.infosys.sm4all.copal.rest.Service;
27  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLContextEventTypeReader;
28  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLContextEventTypesWriter;
29  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLContextListenersWriter;
30  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLContextProcessorsWriter;
31  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLContextPublishersWriter;
32  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLProcessedEventQueriesWriter;
33  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLEventReader;
34  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLProcessedEventQueryReader;
35  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLRESTfulListenerReader;
36  import at.ac.tuwien.infosys.sm4all.copal.rest.provider.XMLRESTfulPublisherReader;
37  
38  /**
39   * The OSGi {@link BundleActivator} of COPAL REST service.
40   * 
41   * @author sanjin
42   */
43  public class Activator implements BundleActivator {
44  
45      private static final Logger LOGGER = Logger.getLogger(Activator.class);
46      private static final Class<?>[] PROVIDERS = {
47              XMLContextListenersWriter.class, XMLContextProcessorsWriter.class,
48              XMLContextPublishersWriter.class, XMLContextEventTypeReader.class,
49              XMLContextEventTypesWriter.class, XMLProcessedEventQueriesWriter.class,
50              XMLProcessedEventQueryReader.class,
51              XMLRESTfulPublisherReader.class, XMLEventReader.class,
52              XMLRESTfulListenerReader.class };
53  
54      private final ServiceImpl service = new ServiceImpl();
55      private final ContextEventTypeRegistryTracker eventTypeRegistryTracker = new ContextEventTypeRegistryTracker(
56              this.service);
57      private final ContextPublisherRegistryTracker publisherRegistryTracker = new ContextPublisherRegistryTracker(
58              this.service);
59      private final ContextProcessorRegistryTracker processorRegistryTracker = new ContextProcessorRegistryTracker(
60              this.service);
61      private final ContextQueryFactoryTracker queryFactoryTracker = new ContextQueryFactoryTracker(
62              this.service);
63      private ServiceRegistration serviceRegistration;
64  
65      @Override
66      public void start(final BundleContext bundleContext) {
67          final Dictionary<String, Object> properties = new Hashtable<String, Object>();
68          properties.put("service.exported.interfaces", Service.class.getName());
69          properties.put("service.exported.configs", "org.apache.cxf.rs");
70          properties.put("service.exported.intents", "HTTP");
71          properties.put("org.apache.cxf.rs.httpservice.context", "/copal");
72  
73          final StringBuilder providers = new StringBuilder();
74          for (final Class<?> provider : PROVIDERS)
75              providers.append(provider.getName()).append(",");
76          providers.deleteCharAt(providers.length() - 1);
77          properties.put("org.apache.cxf.rs.provider", providers.toString());
78  
79          this.serviceRegistration = bundleContext.registerService(
80                  Service.class.getName(), this.service, properties);
81          if (this.serviceRegistration == null)
82              LOGGER.info("Failed to register REST service!");
83          else {
84              this.eventTypeRegistryTracker.start(bundleContext);
85              this.publisherRegistryTracker.start(bundleContext);
86              this.processorRegistryTracker.start(bundleContext);
87              this.queryFactoryTracker.start(bundleContext);
88  
89              LOGGER.info("Successfully registered REST service!");
90          }
91      }
92  
93      @Override
94      public void stop(final BundleContext bundleContext) {
95          if (this.serviceRegistration != null) {
96              this.queryFactoryTracker.stop(bundleContext);
97              this.processorRegistryTracker.stop(bundleContext);
98              this.publisherRegistryTracker.stop(bundleContext);
99              this.eventTypeRegistryTracker.stop(bundleContext);
100 
101             this.serviceRegistration.unregister();
102             LOGGER.info("Successfully stopped REST services!");
103         }
104     }
105 }