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
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 }