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 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
27 import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
28 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
29 import org.eclipse.jetty.osgi.boot.ServiceProvider;
30 import org.eclipse.jetty.server.handler.ContextHandler;
31 import org.eclipse.jetty.util.log.Log;
32 import org.eclipse.jetty.util.log.Logger;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.osgi.util.tracker.ServiceTracker;
38 import org.osgi.util.tracker.ServiceTrackerCustomizer;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class ServiceWatcher implements ServiceTrackerCustomizer
52 {
53 private static Logger LOG = Log.getLogger(ServiceWatcher.class);
54
55 public static final String FILTER = "(objectclass=" + ServiceProvider.class.getName() + ")";
56
57
58
59 ServiceTracker _serviceTracker;
60
61
62
63
64
65
66
67 public ServiceWatcher() throws Exception
68 {
69
70 Bundle myBundle = FrameworkUtil.getBundle(this.getClass());
71 _serviceTracker = new ServiceTracker(myBundle.getBundleContext(), FrameworkUtil.createFilter(FILTER),null);
72 _serviceTracker.open();
73 }
74
75
76
77
78
79
80
81
82 public Map<ServiceReference, ServiceProvider> getDeployers(String managedServerName)
83 {
84 if (managedServerName == null)
85 managedServerName = OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME;
86
87 Map<ServiceReference, ServiceProvider> candidates = new HashMap<ServiceReference, ServiceProvider>();
88
89 ServiceReference[] references = _serviceTracker.getServiceReferences();
90 if (references != null)
91 {
92 for (ServiceReference ref:references)
93 {
94 String name = (String)ref.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME);
95 if (managedServerName.equalsIgnoreCase(name))
96 {
97 ServiceProvider candidate = (ServiceProvider)_serviceTracker.getService(ref);
98 if (candidate != null)
99 candidates.put(ref, candidate);
100 }
101 }
102 }
103 return candidates;
104 }
105
106
107
108
109
110
111
112
113
114 @Override
115 public Object addingService(ServiceReference reference)
116 {
117 BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext();
118 ContextHandler contextHandler = (ContextHandler) context.getService(reference);
119 return addService(context, contextHandler, reference);
120 }
121
122
123
124
125
126
127
128
129
130 @Override
131 public void modifiedService(ServiceReference reference, Object service)
132 {
133 BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext();
134 ContextHandler contextHandler = (ContextHandler) context.getService(reference);
135 removeService (context, contextHandler, reference);
136 addService (context, contextHandler, reference);
137 }
138
139
140
141
142
143
144
145 @Override
146 public void removedService(ServiceReference reference, Object service)
147 {
148 BundleContext context = FrameworkUtil.getBundle(JettyBootstrapActivator.class).getBundleContext();
149 ContextHandler contextHandler = (ContextHandler) context.getService(reference);
150 removeService (context, contextHandler, reference);
151 }
152
153
154
155
156
157
158
159
160
161 public Object addService (BundleContext context, ContextHandler contextHandler, ServiceReference reference)
162 {
163 if (contextHandler.getServer() != null)
164 {
165
166 return context.getService(reference);
167 }
168 String watermark = (String)reference.getProperty(OSGiWebappConstants.WATERMARK);
169 if (watermark != null && !"".equals(watermark))
170 return context.getService(reference);
171
172
173 String serverName = (String)reference.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME);
174 Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName);
175 if (candidates != null)
176 {
177 boolean added = false;
178 Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator();
179 while (!added && itor.hasNext())
180 {
181 Entry<ServiceReference, ServiceProvider> e = itor.next();
182 try
183 {
184 added = e.getValue().serviceAdded(reference, contextHandler);
185 if (added && LOG.isDebugEnabled())
186 LOG.debug("Provider "+e.getValue()+" deployed "+contextHandler);
187 }
188 catch (Exception x)
189 {
190 LOG.warn("Error deploying service representing jetty context", x);
191 }
192 }
193 }
194 return context.getService(reference);
195 }
196
197
198
199
200
201
202
203
204
205
206 public void removeService (BundleContext context, ContextHandler contextHandler, ServiceReference reference)
207 {
208
209
210 String serverName = (String)reference.getProperty(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME);
211 Map<ServiceReference, ServiceProvider> candidates = getDeployers(serverName);
212 if (candidates != null)
213 {
214 boolean removed = false;
215 Iterator<Entry<ServiceReference, ServiceProvider>> itor = candidates.entrySet().iterator();
216 while (!removed && itor.hasNext())
217 {
218 Entry<ServiceReference, ServiceProvider> e = itor.next();
219 try
220 {
221 removed = e.getValue().serviceRemoved(reference, contextHandler);
222 }
223 catch (Exception x)
224 {
225 LOG.warn("Error undeploying service representing jetty context ", x);
226 }
227 }
228 }
229 }
230 }