1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39 public abstract class AbstractOSGiApp extends App
40 {
41 protected Bundle _bundle;
42 protected Dictionary _properties;
43 protected ServiceRegistration _registration;
44
45
46 public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, String originId)
47 {
48 super(manager, provider, originId);
49 _properties = bundle.getHeaders();
50 _bundle = bundle;
51 }
52
53 public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String originId)
54 {
55 super(manager, provider, originId);
56 _properties = properties;
57 _bundle = bundle;
58 }
59
60
61 public String getBundleSymbolicName()
62 {
63 return _bundle.getSymbolicName();
64 }
65
66
67 public String getBundleVersionAsString()
68 {
69 if (_bundle.getVersion() == null)
70 return null;
71 return _bundle.getVersion().toString();
72 }
73
74
75 public Bundle getBundle()
76 {
77 return _bundle;
78 }
79
80
81 public void setRegistration (ServiceRegistration registration)
82 {
83 _registration = registration;
84 }
85
86
87 public ServiceRegistration getRegistration ()
88 {
89 return _registration;
90 }
91
92
93
94 public void registerAsOSGiService() throws Exception
95 {
96 if (_registration == null)
97 {
98 Dictionary<String,String> properties = new Hashtable<String,String>();
99 properties.put(OSGiWebappConstants.WATERMARK, OSGiWebappConstants.WATERMARK);
100 if (getBundleSymbolicName() != null)
101 properties.put(OSGiWebappConstants.OSGI_WEB_SYMBOLICNAME, getBundleSymbolicName());
102 if (getBundleVersionAsString() != null)
103 properties.put(OSGiWebappConstants.OSGI_WEB_VERSION, getBundleVersionAsString());
104 properties.put(OSGiWebappConstants.OSGI_WEB_CONTEXTPATH, getContextPath());
105 ServiceRegistration rego = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(ContextHandler.class.getName(), getContextHandler(), properties);
106 setRegistration(rego);
107 }
108 }
109
110
111 protected void deregisterAsOSGiService() throws Exception
112 {
113 if (_registration == null)
114 return;
115
116 _registration.unregister();
117 _registration = null;
118 }
119
120 }