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 org.eclipse.jetty.osgi.boot.warurl;
20  
21  import java.util.Dictionary;
22  import java.util.Hashtable;
23  
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.osgi.framework.ServiceRegistration;
27  import org.osgi.service.url.URLConstants;
28  import org.osgi.service.url.URLStreamHandlerService;
29  
30  /**
31   * Register the factory to handle the war scheme specified by rfc66
32   * when the bundle is activated.
33   */
34  public class WarUrlActivator implements BundleActivator
35  {
36      
37      private ServiceRegistration _reg;
38  
39      /**
40       * Register the url stream handler factory.
41       * 
42       * @param context
43       */
44      @SuppressWarnings("unchecked")
45      public void start(BundleContext context) throws Exception
46      {
47          Dictionary props = new Hashtable();
48          props.put(URLConstants.URL_HANDLER_PROTOCOL,new String[] {"war"});
49          context.registerService(URLStreamHandlerService.class.getName(),
50                  new WarUrlStreamHandler(), props);
51      }
52      
53      /**
54       * Remove the url stream handler. (probably not required,
55       * as osgi might shutdown every registered service
56       * by default: need test)
57       */
58      public void stop(BundleContext context) throws Exception
59      {
60          try
61          {
62              if (_reg != null)
63              {
64                  _reg.unregister();
65              }
66          }
67          catch (Exception e)
68          {
69              e.printStackTrace();
70          }
71      }
72      
73  }