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.io.File;
22 import java.net.URL;
23 import java.util.Dictionary;
24 import java.util.Hashtable;
25
26 import org.eclipse.jetty.deploy.App;
27 import org.eclipse.jetty.deploy.AppProvider;
28 import org.eclipse.jetty.deploy.DeploymentManager;
29 import org.eclipse.jetty.server.handler.ContextHandler;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.log.Logger;
32 import org.eclipse.jetty.util.resource.Resource;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.FrameworkUtil;
35 import org.osgi.framework.ServiceRegistration;
36
37
38
39
40
41
42
43
44
45 public abstract class AbstractOSGiApp extends App
46 {
47 private static final Logger LOG = Log.getLogger(AbstractOSGiApp.class);
48
49 protected Bundle _bundle;
50 protected Dictionary<?,?> _properties;
51 protected ServiceRegistration _registration;
52
53
54 public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, String originId)
55 {
56 this (manager, provider, bundle, bundle.getHeaders(), originId);
57 }
58
59 public AbstractOSGiApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary<?,?> properties, String originId)
60 {
61 super(manager, provider, originId);
62 _properties = properties;
63 _bundle = bundle;
64 }
65
66
67 public String getBundleSymbolicName()
68 {
69 return _bundle.getSymbolicName();
70 }
71
72
73 public String getBundleVersionAsString()
74 {
75 if (_bundle.getVersion() == null)
76 return null;
77 return _bundle.getVersion().toString();
78 }
79
80
81 public Bundle getBundle()
82 {
83 return _bundle;
84 }
85
86
87 public void setRegistration (ServiceRegistration registration)
88 {
89 _registration = registration;
90 }
91
92
93 public ServiceRegistration getRegistration ()
94 {
95 return _registration;
96 }
97
98
99
100 public void registerAsOSGiService() throws Exception
101 {
102 if (_registration == null)
103 {
104 Dictionary<String,String> properties = new Hashtable<String,String>();
105 properties.put(OSGiWebappConstants.WATERMARK, OSGiWebappConstants.WATERMARK);
106 if (getBundleSymbolicName() != null)
107 properties.put(OSGiWebappConstants.OSGI_WEB_SYMBOLICNAME, getBundleSymbolicName());
108 if (getBundleVersionAsString() != null)
109 properties.put(OSGiWebappConstants.OSGI_WEB_VERSION, getBundleVersionAsString());
110 properties.put(OSGiWebappConstants.OSGI_WEB_CONTEXTPATH, getContextPath());
111 ServiceRegistration rego = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(ContextHandler.class.getName(), getContextHandler(), properties);
112 setRegistration(rego);
113 }
114 }
115
116
117 protected void deregisterAsOSGiService() throws Exception
118 {
119 if (_registration == null)
120 return;
121
122 _registration.unregister();
123 _registration = null;
124 }
125
126 protected Resource getFileAsResource (String dir, String file)
127 {
128 Resource r = null;
129 try
130 {
131 File asFile = new File (dir, file);
132 if (asFile.exists())
133 r = Resource.newResource(asFile);
134 }
135 catch (Exception e)
136 {
137 r = null;
138 }
139 return r;
140 }
141
142 protected Resource getFileAsResource (String file)
143 {
144 Resource r = null;
145 try
146 {
147 File asFile = new File (file);
148 if (asFile.exists())
149 r = Resource.newResource(asFile);
150 }
151 catch (Exception e)
152 {
153 r = null;
154 }
155 return r;
156 }
157
158 protected Resource findFile (String fileName, String jettyHome, String bundleOverrideLocation, Bundle containingBundle)
159 {
160 Resource res = null;
161
162
163 if (fileName.startsWith("/"))
164 res = getFileAsResource(fileName);
165 if (res != null)
166 return res;
167
168
169 if (jettyHome != null)
170 {
171 if (jettyHome.startsWith("\"") || jettyHome.startsWith("'"))
172 jettyHome = jettyHome.substring(1);
173 if (jettyHome.endsWith("\"") || (jettyHome.endsWith("'")))
174 jettyHome = jettyHome.substring(0,jettyHome.length()-1);
175
176 res = getFileAsResource(jettyHome, fileName);
177 }
178 if (res != null)
179 return res;
180
181
182
183 if (bundleOverrideLocation != null)
184 {
185 try(Resource location=Resource.newResource(bundleOverrideLocation))
186 {
187 res=location.addPath(fileName);
188 }
189 catch (Exception e)
190 {
191 LOG.warn(e);
192 }
193 }
194 if (res != null)
195 return res;
196
197
198 if (containingBundle != null)
199 {
200 if (fileName.startsWith("./"))
201 fileName = fileName.substring(1);
202
203 if (!fileName.startsWith("/"))
204 fileName = "/" + fileName;
205
206 URL entry = _bundle.getEntry(fileName);
207 if (entry != null)
208 res = Resource.newResource(entry);
209 }
210
211 return res;
212 }
213 }