View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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;
20  
21  import java.util.Dictionary;
22  import java.util.Hashtable;
23  
24  import org.eclipse.jetty.deploy.App;
25  import org.eclipse.jetty.deploy.AppProvider;
26  import org.eclipse.jetty.deploy.DeploymentManager;
27  import org.eclipse.jetty.server.handler.ContextHandler;
28  import org.osgi.framework.Bundle;
29  import org.osgi.framework.FrameworkUtil;
30  import org.osgi.framework.ServiceRegistration;
31  
32  
33  
34  /**
35   * AbstractOSGiApp
36   *
37   * Base class representing info about a webapp/ContextHandler that is deployed into Jetty.
38   * 
39   */
40  public abstract class AbstractOSGiApp extends App
41  {      
42      protected Bundle _bundle;
43      protected Dictionary _properties;
44      protected ServiceRegistration _registration;
45  
46      /* ------------------------------------------------------------ */
47      public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, String originId)
48      {
49          super(manager, provider, originId);
50          _properties = bundle.getHeaders();
51          _bundle = bundle;
52      }
53      /* ------------------------------------------------------------ */
54      public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String originId)
55      {
56          super(manager, provider, originId);
57          _properties = properties;
58          _bundle = bundle;
59      }
60      
61      /* ------------------------------------------------------------ */
62      public String getBundleSymbolicName()
63      {
64          return _bundle.getSymbolicName();
65      }
66      
67      /* ------------------------------------------------------------ */
68      public String getBundleVersionAsString()
69      {
70         if (_bundle.getVersion() == null)
71             return null;
72         return _bundle.getVersion().toString();
73      }
74      
75      /* ------------------------------------------------------------ */
76      public Bundle getBundle()
77      {
78          return _bundle;
79      }
80      
81      /* ------------------------------------------------------------ */
82      public void setRegistration (ServiceRegistration registration)
83      {
84          _registration = registration;
85      }
86      
87      /* ------------------------------------------------------------ */
88      public ServiceRegistration getRegistration ()
89      {
90          return _registration;
91      }
92      
93      
94      /* ------------------------------------------------------------ */
95      public void registerAsOSGiService() throws Exception
96      {
97          if (_registration == null)
98          {
99              Dictionary<String,String> properties = new Hashtable<String,String>();
100             properties.put(OSGiWebappConstants.WATERMARK, OSGiWebappConstants.WATERMARK);
101             if (getBundleSymbolicName() != null)
102                 properties.put(OSGiWebappConstants.OSGI_WEB_SYMBOLICNAME, getBundleSymbolicName());
103             if (getBundleVersionAsString() != null)
104                 properties.put(OSGiWebappConstants.OSGI_WEB_VERSION, getBundleVersionAsString());
105             properties.put(OSGiWebappConstants.OSGI_WEB_CONTEXTPATH, getContextPath());
106             ServiceRegistration rego = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(ContextHandler.class.getName(), getContextHandler(), properties);
107             setRegistration(rego);
108         }
109     }
110 
111     /* ------------------------------------------------------------ */
112     protected void deregisterAsOSGiService() throws Exception
113     {
114         if (_registration == null)
115             return;
116 
117         _registration.unregister();
118         _registration = null;
119     }
120 
121 }