View Javadoc
1   /*
2    * Copyright (C) 2007-2014 JMPEAX.
3    *
4    *     This program is free software: you can redistribute it and/or modify
5    *     it under the terms of the GNU General Public License as published by
6    *     the Free Software Foundation, either version 3 of the License, or
7    *     (at your option) any later version.
8    *
9    *     This program is distributed in the hope that it will be useful,
10   *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *     GNU General Public License for more details.
13   *
14   *     You should have received a copy of the GNU General Public License
15   *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package com.jmpeax.osgi.undertow.http;
19  
20  import com.jmpeax.osgi.undertow.http.impl.UndertowHttpServer;
21  import com.jmpeax.osgi.undertow.http.impl.UndertowHttpService;
22  import org.osgi.framework.BundleActivator;
23  import org.osgi.framework.BundleContext;
24  import org.osgi.framework.ServiceRegistration;
25  import org.osgi.service.http.HttpService;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.io.IOException;
30  import java.util.Dictionary;
31  import java.util.Properties;
32  
33  /**
34   * Bundle activator for Undertow http service implementation.
35   * @since 1.0.0
36   * @author Carlos Ortiz
37   */
38  public class UndertowHttpServiceActivator implements BundleActivator {
39  
40      /**
41       * Internal Logger.
42       */
43      private Logger log = LoggerFactory.getLogger(UndertowHttpServiceActivator.class);
44      /**
45       * Properties of the service.
46       */
47      private Dictionary<String, ?> configuration;
48      private ServiceRegistration<?> httpService;
49      private UndertowHttpServer server;
50  
51      @Override
52      public void start(final BundleContext context) throws Exception {
53          defaults();
54          server = new UndertowHttpServer(configuration);
55          httpService = context.registerService(HttpService.class, new UndertowHttpService(server), null);
56          server.startServer();
57      }
58  
59      @Override
60      public void stop(final BundleContext context) throws Exception {
61          if (httpService != null) {
62              log.debug("Unregister Http service");
63              httpService.unregister();
64          }
65          if (server != null) {
66              log.debug("Stopping Http Server");
67              server.stopServer();
68          }
69      }
70  
71      private Dictionary defaults() throws IOException {
72          Properties properties = new Properties();
73          properties.load(getClass().getResourceAsStream("/undertow-defaults.properties"));
74          return properties;
75      }
76  }