View Javadoc
1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package com.acme.osgi;
20  
21  import java.util.Dictionary;
22  import java.util.Hashtable;
23  
24  import org.eclipse.jetty.server.Server;
25  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
26  import org.osgi.framework.BundleActivator;
27  import org.osgi.framework.BundleContext;
28  import org.osgi.framework.ServiceRegistration;
29  
30  /**
31   * Bootstrap a Server
32   * 
33   * 
34   */
35  public class Activator implements BundleActivator
36  {
37  
38      private ServiceRegistration _sr;
39      
40      /**
41       * 
42       * @param context
43       */
44      public void start(BundleContext context) throws Exception
45      {    
46          Server server = new Server(9999);
47          ContextHandlerCollection contexts = new ContextHandlerCollection();
48          server.setHandler(contexts);
49  
50          Dictionary serverProps = new Hashtable();
51          //define the unique name of the server instance
52          serverProps.put("managedServerName", "fooServer");
53          //serverProps.put("jetty.http.port", "9999");
54          //register as an OSGi Service for Jetty to find 
55          _sr = context.registerService(Server.class.getName(), server, serverProps);
56      }
57  
58      /**
59       * Stop the activator.
60       * 
61       * @see
62       * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
63       */
64      public void stop(BundleContext context) throws Exception
65      {
66          _sr.unregister();
67      }
68  }