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