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