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.internal.webapp;
20
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24
25 import org.eclipse.jetty.osgi.boot.BundleProvider;
26 import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
27 import org.eclipse.jetty.osgi.boot.utils.WebappRegistrationCustomizer;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleEvent;
32 import org.osgi.framework.FrameworkUtil;
33 import org.osgi.framework.ServiceReference;
34 import org.osgi.util.tracker.BundleTracker;
35 import org.osgi.util.tracker.BundleTrackerCustomizer;
36 import org.osgi.util.tracker.ServiceTracker;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class WebBundleTrackerCustomizer implements BundleTrackerCustomizer
52 {
53 private static final Logger LOG = Log.getLogger(WebBundleTrackerCustomizer.class);
54
55 public static Collection<WebappRegistrationCustomizer> JSP_REGISTRATION_HELPERS = new ArrayList<WebappRegistrationCustomizer>();
56 public static final String FILTER = "(&(objectclass=" + BundleProvider.class.getName() + ")"+
57 "("+OSGiServerConstants.MANAGED_JETTY_SERVER_NAME+"="+OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME+"))";
58
59 private ServiceTracker _serviceTracker;
60 private BundleTracker _bundleTracker;
61
62
63
64
65
66 public WebBundleTrackerCustomizer ()
67 throws Exception
68 {
69 Bundle myBundle = FrameworkUtil.getBundle(this.getClass());
70
71
72 _serviceTracker = new ServiceTracker(myBundle.getBundleContext(), FrameworkUtil.createFilter(FILTER),null) {
73 public Object addingService(ServiceReference reference) {
74 Object object = super.addingService(reference);
75 LOG.debug("Deployer registered {}", reference);
76 openBundleTracker();
77 return object;
78 }
79 };
80 _serviceTracker.open();
81
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public Object addingBundle(Bundle bundle, BundleEvent event)
107 {
108 if (bundle.getState() == Bundle.ACTIVE)
109 {
110 register(bundle);
111 }
112 else if (bundle.getState() == Bundle.STOPPING)
113 {
114 unregister(bundle);
115 }
116 else
117 {
118
119
120 }
121 return null;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object)
140 {
141
142
143 if (bundle.getState() == Bundle.STOPPING || bundle.getState() == Bundle.ACTIVE)
144 {
145 unregister(bundle);
146 }
147 if (bundle.getState() == Bundle.ACTIVE)
148 {
149 register(bundle);
150 }
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public void removedBundle(Bundle bundle, BundleEvent event, Object object)
169 {
170 unregister(bundle);
171 }
172
173
174
175
176
177
178
179 private boolean register(Bundle bundle)
180 {
181 if (bundle == null)
182 return false;
183
184
185 boolean deployed = false;
186 Object[] deployers = _serviceTracker.getServices();
187 if (deployers != null)
188 {
189 int i=0;
190 while (!deployed && i<deployers.length)
191 {
192
193 BundleProvider p = (BundleProvider)deployers[i];
194 try
195 {
196 deployed = p.bundleAdded(bundle);
197 }
198 catch (Exception x)
199 {
200 LOG.warn("Error deploying bundle for jetty context", x);
201 }
202 i++;
203 }
204 }
205
206 return deployed;
207 }
208
209
210
211
212
213 private void unregister(Bundle bundle)
214 {
215 Object[] deployers = _serviceTracker.getServices();
216 boolean undeployed = false;
217 if (deployers != null)
218 {
219 int i=0;
220 while (!undeployed && i<deployers.length)
221 {
222 try
223 {
224 undeployed = ((BundleProvider)deployers[i++]).bundleRemoved(bundle);
225 }
226 catch (Exception x)
227 {
228 LOG.warn("Error undeploying bundle for jetty context", x);
229 }
230 }
231 }
232 }
233
234 public void setAndOpenWebBundleTracker(BundleTracker bundleTracker) {
235 if(_bundleTracker == null) {
236 _bundleTracker = bundleTracker;
237 LOG.debug("Bundle tracker is set");
238 openBundleTracker();
239 }
240 }
241
242 private void openBundleTracker() {
243 if(_bundleTracker != null && _serviceTracker.getServices() != null &&
244 _serviceTracker.getServices().length > 0) {
245 _bundleTracker.open();
246 LOG.debug("Bundle tracker has been opened");
247 }
248 }
249
250 }