Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.impl; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 20 | |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.Field; |
| 23 | import java.lang.reflect.InvocationTargetException; |
| 24 | import java.lang.reflect.Method; |
| 25 | import java.lang.reflect.Proxy; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Dictionary; |
| 28 | import java.util.Enumeration; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.Hashtable; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Properties; |
| 35 | |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 36 | import org.apache.felix.dm.DependencyManager; |
| 37 | import org.apache.felix.dm.dependencies.Dependency; |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 38 | import org.apache.felix.dm.impl.dependencies.DependencyActivation; |
| 39 | import org.apache.felix.dm.impl.dependencies.DependencyService; |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 40 | import org.apache.felix.dm.management.ServiceComponent; |
| 41 | import org.apache.felix.dm.management.ServiceComponentDependency; |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 42 | import org.apache.felix.dm.service.Service; |
| 43 | import org.apache.felix.dm.service.ServiceStateListener; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 44 | import org.osgi.framework.BundleContext; |
| 45 | import org.osgi.framework.ServiceRegistration; |
| 46 | |
| 47 | /** |
| 48 | * Service implementation. |
| 49 | * |
| 50 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 51 | */ |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 52 | public class ServiceImpl implements Service, DependencyService, ServiceComponent { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 53 | private static final Class[] VOID = new Class[] {}; |
| 54 | private static final ServiceRegistration NULL_REGISTRATION; |
| 55 | private static final ServiceStateListener[] SERVICE_STATE_LISTENER_TYPE = new ServiceStateListener[] {}; |
| 56 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 57 | private final Object SYNC = new Object(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 58 | private final BundleContext m_context; |
| 59 | private final DependencyManager m_manager; |
| 60 | |
| 61 | // configuration (static) |
| 62 | private String m_callbackInit; |
| 63 | private String m_callbackStart; |
| 64 | private String m_callbackStop; |
| 65 | private String m_callbackDestroy; |
| 66 | private Object m_serviceName; |
| 67 | private Object m_implementation; |
| 68 | |
| 69 | // configuration (dynamic, but does not affect state) |
| 70 | private Dictionary m_serviceProperties; |
| 71 | |
| 72 | // configuration (dynamic, and affects state) |
| 73 | private ArrayList m_dependencies = new ArrayList(); |
| 74 | |
| 75 | // runtime state (calculated from dependencies) |
| 76 | private State m_state; |
| 77 | |
| 78 | // runtime state (changes because of state changes) |
| 79 | private Object m_serviceInstance; |
| 80 | private ServiceRegistration m_registration; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 81 | private boolean m_isBound; |
| 82 | private boolean m_isInstantiated; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 83 | |
| 84 | // service state listeners |
| 85 | private final List m_stateListeners = new ArrayList(); |
| 86 | |
| 87 | // work queue |
| 88 | private final SerialExecutor m_executor = new SerialExecutor(); |
| 89 | |
| 90 | // instance factory |
| 91 | private Object m_instanceFactory; |
| 92 | private String m_instanceFactoryCreateMethod; |
| 93 | |
| 94 | // composition manager |
| 95 | private Object m_compositionManager; |
| 96 | private String m_compositionManagerGetMethod; |
| 97 | private Object m_compositionManagerInstance; |
| 98 | |
| 99 | // internal logging |
| 100 | private final Logger m_logger; |
| 101 | private ServiceRegistration m_serviceRegistration; |
| 102 | private Map m_autoConfig = new HashMap(); |
| 103 | private Map m_autoConfigInstance = new HashMap(); |
| 104 | |
| 105 | public ServiceImpl(BundleContext context, DependencyManager manager, Logger logger) { |
| 106 | m_logger = logger; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 107 | m_state = new State((List) m_dependencies.clone(), false, false, false); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 108 | m_context = context; |
| 109 | m_manager = manager; |
| 110 | m_callbackInit = "init"; |
| 111 | m_callbackStart = "start"; |
| 112 | m_callbackStop = "stop"; |
| 113 | m_callbackDestroy = "destroy"; |
| 114 | m_implementation = null; |
| 115 | m_autoConfig.put(BundleContext.class, Boolean.TRUE); |
| 116 | m_autoConfig.put(ServiceRegistration.class, Boolean.TRUE); |
| 117 | m_autoConfig.put(DependencyManager.class, Boolean.TRUE); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 118 | m_autoConfig.put(Service.class, Boolean.TRUE); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 121 | private void calculateStateChanges() { |
| 122 | // see if any of the things we did caused a further change of state |
| 123 | State oldState, newState; |
| 124 | synchronized (m_dependencies) { |
| 125 | oldState = m_state; |
| 126 | newState = new State((List) m_dependencies.clone(), !oldState.isInactive(), m_isInstantiated, m_isBound); |
| 127 | m_state = newState; |
| 128 | } |
| 129 | calculateStateChanges(oldState, newState); |
| 130 | } |
| 131 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 132 | private void calculateStateChanges(final State oldState, final State newState) { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 133 | if (oldState.isInactive() && (newState.isTrackingOptional())) { |
| 134 | m_executor.enqueue(new Runnable() { |
| 135 | public void run() { |
| 136 | activateService(newState); |
| 137 | }}); |
| 138 | } |
| 139 | if (oldState.isInactive() && (newState.isWaitingForRequired())) { |
| 140 | m_executor.enqueue(new Runnable() { |
| 141 | public void run() { |
| 142 | startTrackingRequired(newState); |
| 143 | }}); |
| 144 | } |
| 145 | if (oldState.isWaitingForRequired() && newState.isTrackingOptional()) { |
| 146 | m_executor.enqueue(new Runnable() { |
| 147 | public void run() { |
| 148 | activateService(newState); |
| 149 | }}); |
| 150 | } |
| 151 | if ((oldState.isWaitingForRequired()) && newState.isInactive()) { |
| 152 | m_executor.enqueue(new Runnable() { |
| 153 | public void run() { |
| 154 | stopTrackingRequired(oldState); |
| 155 | }}); |
| 156 | } |
| 157 | if (oldState.isTrackingOptional() && newState.isWaitingForRequiredInstantiated()) { |
| 158 | m_executor.enqueue(new Runnable() { |
| 159 | public void run() { |
| 160 | // TODO as far as I can see there is nothing left to do here |
| 161 | }}); |
| 162 | } |
| 163 | if (oldState.isTrackingOptional() && newState.isWaitingForRequired()) { |
| 164 | m_executor.enqueue(new Runnable() { |
| 165 | public void run() { |
| 166 | deactivateService(oldState); |
| 167 | }}); |
| 168 | } |
| 169 | if (oldState.isTrackingOptional() && newState.isBound()) { |
| 170 | m_executor.enqueue(new Runnable() { |
| 171 | public void run() { |
| 172 | bindService(oldState); |
| 173 | }}); |
| 174 | } |
| 175 | if (oldState.isTrackingOptional() && newState.isInactive()) { |
| 176 | m_executor.enqueue(new Runnable() { |
| 177 | public void run() { |
| 178 | deactivateService(oldState); |
| 179 | stopTrackingRequired(oldState); |
| 180 | }}); |
| 181 | } |
| 182 | if (oldState.isWaitingForRequiredInstantiated() && newState.isWaitingForRequired()) { |
| 183 | m_executor.enqueue(new Runnable() { |
| 184 | public void run() { |
| 185 | deactivateService(oldState); |
| 186 | }}); |
| 187 | } |
| 188 | if (oldState.isWaitingForRequiredInstantiated() && newState.isInactive()) { |
| 189 | m_executor.enqueue(new Runnable() { |
| 190 | public void run() { |
| 191 | deactivateService(oldState); |
| 192 | stopTrackingRequired(oldState); |
| 193 | }}); |
| 194 | } |
| 195 | if (oldState.isWaitingForRequiredInstantiated() && newState.isBound()) { |
| 196 | m_executor.enqueue(new Runnable() { |
| 197 | public void run() { |
| 198 | bindService(oldState); |
| 199 | }}); |
| 200 | } |
| 201 | if (oldState.isBound() && newState.isWaitingForRequiredInstantiated()) { |
| 202 | m_executor.enqueue(new Runnable() { |
| 203 | public void run() { |
| 204 | unbindService(oldState); |
| 205 | }}); |
| 206 | } |
| 207 | if (oldState.isBound() && newState.isWaitingForRequired()) { |
| 208 | m_executor.enqueue(new Runnable() { |
| 209 | public void run() { |
| 210 | unbindService(oldState); |
| 211 | deactivateService(oldState); |
| 212 | }}); |
| 213 | } |
| 214 | if (oldState.isBound() && newState.isInactive()) { |
| 215 | m_executor.enqueue(new Runnable() { |
| 216 | public void run() { |
| 217 | unbindService(oldState); |
| 218 | deactivateService(oldState); |
| 219 | stopTrackingRequired(oldState); |
| 220 | }}); |
| 221 | } |
| 222 | m_executor.execute(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 223 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 224 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 225 | public Service add(final Dependency dependency) { |
| 226 | State oldState, newState; |
| 227 | synchronized (m_dependencies) { |
| 228 | oldState = m_state; |
| 229 | m_dependencies.add(dependency); |
| 230 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 231 | if (oldState.isAllRequiredAvailable() || (oldState.isWaitingForRequired() && dependency.isRequired())) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 232 | ((DependencyActivation) dependency).start(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 233 | } |
| 234 | synchronized (m_dependencies) { |
Marcel Offermans | 306c252 | 2009-12-17 13:57:58 +0000 | [diff] [blame^] | 235 | // starting the dependency above might have triggered another state change, so |
| 236 | // we have to fetch the current state again |
| 237 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 238 | newState = new State((List) m_dependencies.clone(), !oldState.isInactive(), m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 239 | m_state = newState; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 240 | } |
| 241 | calculateStateChanges(oldState, newState); |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | public Service add(List dependencies) { |
| 246 | // TODO review if this can be done more smartly |
| 247 | for (int i = 0; i < dependencies.size(); i++) { |
| 248 | add((Dependency) dependencies.get(i)); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 249 | } |
| 250 | return this; |
| 251 | } |
| 252 | |
| 253 | public Service remove(Dependency dependency) { |
| 254 | State oldState, newState; |
| 255 | synchronized (m_dependencies) { |
| 256 | oldState = m_state; |
| 257 | m_dependencies.remove(dependency); |
| 258 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 259 | if (oldState.isAllRequiredAvailable() || (oldState.isWaitingForRequired() && dependency.isRequired())) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 260 | ((DependencyActivation) dependency).stop(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 261 | } |
| 262 | synchronized (m_dependencies) { |
Marcel Offermans | 306c252 | 2009-12-17 13:57:58 +0000 | [diff] [blame^] | 263 | // starting the dependency above might have triggered another state change, so |
| 264 | // we have to fetch the current state again |
| 265 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 266 | newState = new State((List) m_dependencies.clone(), !oldState.isInactive(), m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 267 | m_state = newState; |
| 268 | } |
| 269 | calculateStateChanges(oldState, newState); |
| 270 | return this; |
| 271 | } |
| 272 | |
| 273 | public List getDependencies() { |
| 274 | synchronized (m_dependencies) { |
| 275 | return (List) m_dependencies.clone(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | public ServiceRegistration getServiceRegistration() { |
| 280 | return m_registration; |
| 281 | } |
| 282 | |
| 283 | public Object getService() { |
| 284 | return m_serviceInstance; |
| 285 | } |
| 286 | |
| 287 | public void dependencyAvailable(final Dependency dependency) { |
| 288 | State oldState, newState; |
| 289 | synchronized (m_dependencies) { |
| 290 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 291 | newState = new State((List) m_dependencies.clone(), !oldState.isInactive(), m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 292 | m_state = newState; |
| 293 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 294 | if (newState.isAllRequiredAvailable()) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 295 | m_executor.enqueue(new Runnable() { |
| 296 | public void run() { |
| 297 | updateInstance(dependency); |
| 298 | } |
Marcel Offermans | 306c252 | 2009-12-17 13:57:58 +0000 | [diff] [blame^] | 299 | public String toString() { |
| 300 | return "update instance " + dependency; |
| 301 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 302 | }); |
| 303 | m_executor.execute(); |
| 304 | } |
Marcel Offermans | 306c252 | 2009-12-17 13:57:58 +0000 | [diff] [blame^] | 305 | calculateStateChanges(oldState, newState); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | public void dependencyChanged(final Dependency dependency) { |
| 309 | State state; |
| 310 | synchronized (m_dependencies) { |
| 311 | state = m_state; |
| 312 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 313 | if (state.isAllRequiredAvailable()) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 314 | m_executor.enqueue(new Runnable() { |
| 315 | public void run() { |
| 316 | updateInstance(dependency); |
| 317 | } |
| 318 | }); |
| 319 | m_executor.execute(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | public void dependencyUnavailable(final Dependency dependency) { |
| 324 | State oldState, newState; |
| 325 | synchronized (m_dependencies) { |
| 326 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 327 | newState = new State((List) m_dependencies.clone(), !oldState.isInactive(), m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 328 | m_state = newState; |
| 329 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 330 | if (newState.isAllRequiredAvailable()) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 331 | m_executor.enqueue(new Runnable() { |
| 332 | public void run() { |
| 333 | updateInstance(dependency); |
| 334 | } |
| 335 | }); |
| 336 | m_executor.execute(); |
| 337 | } |
Marcel Offermans | 306c252 | 2009-12-17 13:57:58 +0000 | [diff] [blame^] | 338 | calculateStateChanges(oldState, newState); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | public synchronized void start() { |
| 342 | m_serviceRegistration = m_context.registerService(ServiceComponent.class.getName(), this, null); |
| 343 | State oldState, newState; |
| 344 | synchronized (m_dependencies) { |
| 345 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 346 | newState = new State((List) m_dependencies.clone(), true, m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 347 | m_state = newState; |
| 348 | } |
| 349 | calculateStateChanges(oldState, newState); |
| 350 | } |
| 351 | |
| 352 | public synchronized void stop() { |
| 353 | State oldState, newState; |
| 354 | synchronized (m_dependencies) { |
| 355 | oldState = m_state; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 356 | newState = new State((List) m_dependencies.clone(), false, m_isInstantiated, m_isBound); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 357 | m_state = newState; |
| 358 | } |
| 359 | calculateStateChanges(oldState, newState); |
| 360 | m_serviceRegistration.unregister(); |
| 361 | } |
| 362 | |
| 363 | public synchronized Service setInterface(String serviceName, Dictionary properties) { |
| 364 | ensureNotActive(); |
| 365 | m_serviceName = serviceName; |
| 366 | m_serviceProperties = properties; |
| 367 | return this; |
| 368 | } |
| 369 | |
| 370 | public synchronized Service setInterface(String[] serviceName, Dictionary properties) { |
| 371 | ensureNotActive(); |
| 372 | m_serviceName = serviceName; |
| 373 | m_serviceProperties = properties; |
| 374 | return this; |
| 375 | } |
| 376 | |
| 377 | public synchronized Service setCallbacks(String init, String start, String stop, String destroy) { |
| 378 | ensureNotActive(); |
| 379 | m_callbackInit = init; |
| 380 | m_callbackStart = start; |
| 381 | m_callbackStop = stop; |
| 382 | m_callbackDestroy = destroy; |
| 383 | return this; |
| 384 | } |
| 385 | |
| 386 | public synchronized Service setImplementation(Object implementation) { |
| 387 | ensureNotActive(); |
| 388 | m_implementation = implementation; |
| 389 | return this; |
| 390 | } |
| 391 | |
| 392 | public synchronized Service setFactory(Object factory, String createMethod) { |
| 393 | ensureNotActive(); |
| 394 | m_instanceFactory = factory; |
| 395 | m_instanceFactoryCreateMethod = createMethod; |
| 396 | return this; |
| 397 | } |
| 398 | |
| 399 | public synchronized Service setFactory(String createMethod) { |
| 400 | return setFactory(null, createMethod); |
| 401 | } |
| 402 | |
| 403 | public synchronized Service setComposition(Object instance, String getMethod) { |
| 404 | ensureNotActive(); |
| 405 | m_compositionManager = instance; |
| 406 | m_compositionManagerGetMethod = getMethod; |
| 407 | return this; |
| 408 | } |
| 409 | |
| 410 | public synchronized Service setComposition(String getMethod) { |
| 411 | return setComposition(null, getMethod); |
| 412 | } |
| 413 | |
| 414 | public String toString() { |
| 415 | return "ServiceImpl[" + m_serviceName + " " + m_implementation + "]"; |
| 416 | } |
| 417 | |
| 418 | public synchronized Dictionary getServiceProperties() { |
| 419 | if (m_serviceProperties != null) { |
| 420 | return (Dictionary) ((Hashtable) m_serviceProperties).clone(); |
| 421 | } |
| 422 | return null; |
| 423 | } |
| 424 | |
| 425 | public synchronized void setServiceProperties(Dictionary serviceProperties) { |
| 426 | m_serviceProperties = serviceProperties; |
Marcel Offermans | 937ab4f | 2009-12-10 10:53:01 +0000 | [diff] [blame] | 427 | if ((m_registration != null) && (m_serviceName != null)) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 428 | m_registration.setProperties(calculateServiceProperties()); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // service state listener methods |
| 433 | public void addStateListener(ServiceStateListener listener) { |
| 434 | synchronized (m_stateListeners) { |
| 435 | m_stateListeners.add(listener); |
| 436 | } |
| 437 | // when we register as a listener and the service is already started |
| 438 | // make sure we invoke the right callbacks so the listener knows |
| 439 | State state; |
| 440 | synchronized (m_dependencies) { |
| 441 | state = m_state; |
| 442 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 443 | if (state.isAllRequiredAvailable()) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 444 | listener.starting(this); |
| 445 | listener.started(this); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | public void removeStateListener(ServiceStateListener listener) { |
| 450 | synchronized (m_stateListeners) { |
| 451 | m_stateListeners.remove(listener); |
| 452 | } |
| 453 | } |
| 454 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 455 | public void removeStateListeners() { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 456 | synchronized (m_stateListeners) { |
| 457 | m_stateListeners.clear(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | private void stateListenersStarting() { |
| 462 | ServiceStateListener[] list = getListeners(); |
| 463 | for (int i = 0; i < list.length; i++) { |
| 464 | try { |
| 465 | list[i].starting(this); |
| 466 | } |
| 467 | catch (Throwable t) { |
| 468 | m_logger.log(Logger.LOG_ERROR, "Error invoking listener starting method.", t); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | private void stateListenersStarted() { |
| 474 | ServiceStateListener[] list = getListeners(); |
| 475 | for (int i = 0; i < list.length; i++) { |
| 476 | try { |
| 477 | list[i].started(this); |
| 478 | } |
| 479 | catch (Throwable t) { |
| 480 | m_logger.log(Logger.LOG_ERROR, "Error invoking listener started method.", t); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private void stateListenersStopping() { |
| 486 | ServiceStateListener[] list = getListeners(); |
| 487 | for (int i = 0; i < list.length; i++) { |
| 488 | try { |
| 489 | list[i].stopping(this); |
| 490 | } |
| 491 | catch (Throwable t) { |
| 492 | m_logger.log(Logger.LOG_ERROR, "Error invoking listener stopping method.", t); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private void stateListenersStopped() { |
| 498 | ServiceStateListener[] list = getListeners(); |
| 499 | for (int i = 0; i < list.length; i++) { |
| 500 | try { |
| 501 | list[i].stopped(this); |
| 502 | } |
| 503 | catch (Throwable t) { |
| 504 | m_logger.log(Logger.LOG_ERROR, "Error invoking listener stopped method.", t); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | private ServiceStateListener[] getListeners() { |
| 510 | synchronized (m_stateListeners) { |
| 511 | return (ServiceStateListener[]) m_stateListeners.toArray(SERVICE_STATE_LISTENER_TYPE); |
| 512 | } |
| 513 | } |
| 514 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 515 | private void activateService(State state) { |
| 516 | String init; |
| 517 | synchronized (this) { |
| 518 | init = m_callbackInit; |
| 519 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 520 | // service activation logic, first we initialize the service instance itself |
| 521 | // meaning it is created if necessary and the bundle context is set |
| 522 | initService(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 523 | // now is the time to configure the service, meaning all required |
| 524 | // dependencies will be set and any callbacks called |
| 525 | configureService(state); |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame] | 526 | // flag that our instance has been created |
| 527 | m_isInstantiated = true; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 528 | // then we invoke the init callback so the service can further initialize |
| 529 | // itself |
| 530 | invoke(init); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 531 | // see if any of this caused further state changes |
| 532 | calculateStateChanges(); |
| 533 | } |
| 534 | |
| 535 | private void bindService(State state) { |
| 536 | String start; |
| 537 | synchronized (this) { |
| 538 | start = m_callbackStart; |
| 539 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 540 | // inform the state listeners we're starting |
| 541 | stateListenersStarting(); |
| 542 | // invoke the start callback, since we're now ready to be used |
| 543 | invoke(start); |
| 544 | // start tracking optional services |
| 545 | startTrackingOptional(state); |
| 546 | // register the service in the framework's service registry |
| 547 | registerService(); |
| 548 | // inform the state listeners we've started |
| 549 | stateListenersStarted(); |
| 550 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 551 | |
| 552 | private void unbindService(State state) { |
| 553 | String stop; |
| 554 | synchronized (this) { |
| 555 | stop = m_callbackStop; |
| 556 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 557 | // service deactivation logic, first inform the state listeners |
| 558 | // we're stopping |
| 559 | stateListenersStopping(); |
| 560 | // then, unregister the service from the framework |
| 561 | unregisterService(); |
| 562 | // stop tracking optional services |
| 563 | stopTrackingOptional(state); |
| 564 | // invoke the stop callback |
| 565 | invoke(stop); |
| 566 | // inform the state listeners we've stopped |
| 567 | stateListenersStopped(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | private void deactivateService(State state) { |
| 571 | String destroy; |
| 572 | synchronized (this) { |
| 573 | destroy = m_callbackDestroy; |
| 574 | } |
Marcel Offermans | 78e5dfc | 2009-12-10 13:52:49 +0000 | [diff] [blame] | 575 | // flag that our instance was destroyed |
| 576 | m_isInstantiated = false; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 577 | // invoke the destroy callback |
| 578 | invoke(destroy); |
| 579 | // destroy the service instance |
| 580 | destroyService(state); |
| 581 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 582 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 583 | private void invoke(String name) { |
| 584 | if (name != null) { |
| 585 | // invoke method if it exists |
| 586 | try { |
| 587 | Class clazz = m_serviceInstance.getClass(); |
| 588 | while (clazz != null) { |
| 589 | try { |
| 590 | Method method = clazz.getDeclaredMethod(name, null); |
| 591 | if (method != null) { |
| 592 | method.setAccessible(true); |
| 593 | try { |
| 594 | method.invoke(m_serviceInstance, null); |
| 595 | } |
| 596 | catch (InvocationTargetException e) { |
| 597 | m_logger.log(Logger.LOG_ERROR, "Exception while invoking method " + method + ".", e); |
| 598 | } |
| 599 | return; |
| 600 | } |
| 601 | } |
| 602 | catch (NoSuchMethodException e) { |
| 603 | // ignore this, we keep searching if the method does not exist |
| 604 | } |
| 605 | clazz = clazz.getSuperclass(); |
| 606 | } |
| 607 | } |
| 608 | catch (Exception e) { |
| 609 | m_logger.log(Logger.LOG_ERROR, "Error trying to invoke method named " + name + ".", e); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | private void startTrackingOptional(State state) { |
| 615 | Iterator i = state.getDependencies().iterator(); |
| 616 | while (i.hasNext()) { |
| 617 | Dependency dependency = (Dependency) i.next(); |
| 618 | if (!dependency.isRequired()) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 619 | ((DependencyActivation) dependency).start(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | private void stopTrackingOptional(State state) { |
| 625 | Iterator i = state.getDependencies().iterator(); |
| 626 | while (i.hasNext()) { |
| 627 | Dependency dependency = (Dependency) i.next(); |
| 628 | if (!dependency.isRequired()) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 629 | ((DependencyActivation) dependency).stop(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | private void startTrackingRequired(State state) { |
| 635 | Iterator i = state.getDependencies().iterator(); |
| 636 | while (i.hasNext()) { |
| 637 | Dependency dependency = (Dependency) i.next(); |
| 638 | if (dependency.isRequired()) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 639 | ((DependencyActivation) dependency).start(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | private void stopTrackingRequired(State state) { |
| 645 | Iterator i = state.getDependencies().iterator(); |
| 646 | while (i.hasNext()) { |
| 647 | Dependency dependency = (Dependency) i.next(); |
| 648 | if (dependency.isRequired()) { |
Pierre De Rop | d2ec395 | 2009-12-04 22:40:05 +0000 | [diff] [blame] | 649 | ((DependencyActivation) dependency).stop(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | private Object createInstance(Class clazz) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
| 655 | Constructor constructor = clazz.getConstructor(VOID); |
| 656 | constructor.setAccessible(true); |
| 657 | return clazz.newInstance(); |
| 658 | } |
| 659 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 660 | public void initService() { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 661 | if (m_serviceInstance == null) { |
| 662 | if (m_implementation instanceof Class) { |
| 663 | // instantiate |
| 664 | try { |
| 665 | m_serviceInstance = createInstance((Class) m_implementation); |
| 666 | } |
| 667 | catch (Exception e) { |
| 668 | m_logger.log(Logger.LOG_ERROR, "Could not create service instance of class " + m_implementation + ".", e); |
| 669 | } |
| 670 | } |
| 671 | else { |
| 672 | if (m_instanceFactoryCreateMethod != null) { |
| 673 | Object factory = null; |
| 674 | if (m_instanceFactory != null) { |
| 675 | if (m_instanceFactory instanceof Class) { |
| 676 | try { |
| 677 | factory = createInstance((Class) m_instanceFactory); |
| 678 | } |
| 679 | catch (Exception e) { |
| 680 | m_logger.log(Logger.LOG_ERROR, "Could not create factory instance of class " + m_instanceFactory + ".", e); |
| 681 | } |
| 682 | } |
| 683 | else { |
| 684 | factory = m_instanceFactory; |
| 685 | } |
| 686 | } |
| 687 | else { |
| 688 | // TODO review if we want to try to default to something if not specified |
| 689 | // for now the JavaDoc of setFactory(method) reflects the fact that we need |
| 690 | // to review it |
| 691 | } |
| 692 | if (factory == null) { |
| 693 | m_logger.log(Logger.LOG_ERROR, "Factory cannot be null."); |
| 694 | } |
| 695 | else { |
| 696 | try { |
| 697 | Method m = factory.getClass().getDeclaredMethod(m_instanceFactoryCreateMethod, null); |
| 698 | m_serviceInstance = m.invoke(factory, null); |
| 699 | } |
| 700 | catch (Exception e) { |
| 701 | m_logger.log(Logger.LOG_ERROR, "Could not create service instance using factory " + factory + " method " + m_instanceFactoryCreateMethod + ".", e); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | if (m_implementation == null) { |
| 706 | m_logger.log(Logger.LOG_ERROR, "Implementation cannot be null."); |
| 707 | } |
| 708 | if (m_serviceInstance == null) { |
| 709 | m_serviceInstance = m_implementation; |
| 710 | } |
| 711 | } |
| 712 | // configure the bundle context |
| 713 | if (((Boolean) m_autoConfig.get(BundleContext.class)).booleanValue()) { |
| 714 | configureImplementation(BundleContext.class, m_context, (String) m_autoConfigInstance.get(BundleContext.class)); |
| 715 | } |
| 716 | if (((Boolean) m_autoConfig.get(ServiceRegistration.class)).booleanValue()) { |
| 717 | configureImplementation(ServiceRegistration.class, NULL_REGISTRATION, (String) m_autoConfigInstance.get(ServiceRegistration.class)); |
| 718 | } |
| 719 | if (((Boolean) m_autoConfig.get(DependencyManager.class)).booleanValue()) { |
| 720 | configureImplementation(DependencyManager.class, m_manager, (String) m_autoConfigInstance.get(DependencyManager.class)); |
| 721 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 722 | if (((Boolean) m_autoConfig.get(Service.class)).booleanValue()) { |
| 723 | configureImplementation(Service.class, this, (String) m_autoConfigInstance.get(Service.class)); |
| 724 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
| 728 | public void setAutoConfig(Class clazz, boolean autoConfig) { |
| 729 | m_autoConfig.put(clazz, Boolean.valueOf(autoConfig)); |
| 730 | } |
| 731 | |
| 732 | public void setAutoConfig(Class clazz, String instanceName) { |
| 733 | m_autoConfig.put(clazz, Boolean.valueOf(instanceName != null)); |
| 734 | m_autoConfigInstance.put(clazz, instanceName); |
| 735 | } |
| 736 | |
| 737 | private void configureService(State state) { |
| 738 | // configure all services (the optional dependencies might be configured |
| 739 | // as null objects but that's what we want at this point) |
| 740 | configureServices(state); |
| 741 | } |
| 742 | |
| 743 | private void destroyService(State state) { |
| 744 | unconfigureServices(state); |
| 745 | m_serviceInstance = null; |
| 746 | } |
| 747 | |
| 748 | private void registerService() { |
| 749 | if (m_serviceName != null) { |
| 750 | ServiceRegistrationImpl wrapper = new ServiceRegistrationImpl(); |
| 751 | m_registration = wrapper; |
| 752 | if (((Boolean) m_autoConfig.get(ServiceRegistration.class)).booleanValue()) { |
| 753 | configureImplementation(ServiceRegistration.class, m_registration, (String) m_autoConfigInstance.get(ServiceRegistration.class)); |
| 754 | } |
| 755 | |
| 756 | // service name can either be a string or an array of strings |
| 757 | ServiceRegistration registration; |
| 758 | |
| 759 | // determine service properties |
| 760 | Dictionary properties = calculateServiceProperties(); |
| 761 | |
| 762 | // register the service |
| 763 | try { |
| 764 | if (m_serviceName instanceof String) { |
| 765 | registration = m_context.registerService((String) m_serviceName, m_serviceInstance, properties); |
| 766 | } |
| 767 | else { |
| 768 | registration = m_context.registerService((String[]) m_serviceName, m_serviceInstance, properties); |
| 769 | } |
| 770 | wrapper.setServiceRegistration(registration); |
| 771 | } |
| 772 | catch (IllegalArgumentException iae) { |
| 773 | m_logger.log(Logger.LOG_ERROR, "Could not register service " + m_serviceInstance, iae); |
| 774 | // set the registration to an illegal state object, which will make all invocations on this |
| 775 | // wrapper fail with an ISE (which also occurs when the SR becomes invalid) |
| 776 | wrapper.setIllegalState(); |
| 777 | } |
| 778 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 779 | m_isBound = true; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | private Dictionary calculateServiceProperties() { |
| 783 | Dictionary properties = new Properties(); |
| 784 | addTo(properties, m_serviceProperties); |
| 785 | for (int i = 0; i < m_dependencies.size(); i++) { |
| 786 | Dependency d = (Dependency) m_dependencies.get(i); |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 787 | if (d.isPropagated()) { |
| 788 | Dictionary dict = d.getProperties(); |
| 789 | addTo(properties, dict); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | if (properties.size() == 0) { |
| 793 | properties = null; |
| 794 | } |
| 795 | return properties; |
| 796 | } |
| 797 | |
| 798 | private void addTo(Dictionary properties, Dictionary additional) { |
| 799 | if (properties == null) { |
| 800 | throw new IllegalArgumentException("Dictionary to add to cannot be null."); |
| 801 | } |
| 802 | if (additional != null) { |
| 803 | Enumeration e = additional.keys(); |
| 804 | while (e.hasMoreElements()) { |
| 805 | Object key = e.nextElement(); |
| 806 | properties.put(key, additional.get(key)); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | private void unregisterService() { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 812 | m_isBound = false; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 813 | if (m_serviceName != null) { |
| 814 | m_registration.unregister(); |
| 815 | configureImplementation(ServiceRegistration.class, NULL_REGISTRATION); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | private void updateInstance(Dependency dependency) { |
Marcel Offermans | 937ab4f | 2009-12-10 10:53:01 +0000 | [diff] [blame] | 820 | if (dependency.isAutoConfig()) { |
| 821 | configureImplementation(dependency.getAutoConfigType(), dependency.getAutoConfigInstance(), dependency.getAutoConfigName()); |
| 822 | if (dependency.isPropagated() && m_registration != null) { |
| 823 | m_registration.setProperties(calculateServiceProperties()); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Configure a field in the service implementation. The service implementation |
| 830 | * is searched for fields that have the same type as the class that was specified |
| 831 | * and for each of these fields, the specified instance is filled in. |
| 832 | * |
| 833 | * @param clazz the class to search for |
| 834 | * @param instance the instance to fill in |
| 835 | * @param instanceName the name of the instance to fill in, or <code>null</code> if not used |
| 836 | */ |
| 837 | private void configureImplementation(Class clazz, Object instance, String instanceName) { |
| 838 | Object[] instances = getCompositionInstances(); |
| 839 | if (instances != null) { |
| 840 | for (int i = 0; i < instances.length; i++) { |
| 841 | Object serviceInstance = instances[i]; |
| 842 | Class serviceClazz = serviceInstance.getClass(); |
| 843 | while (serviceClazz != null) { |
| 844 | Field[] fields = serviceClazz.getDeclaredFields(); |
| 845 | for (int j = 0; j < fields.length; j++) { |
| 846 | if (fields[j].getType().equals(clazz) && (instanceName == null || fields[j].getName().equals(instanceName))) { |
| 847 | try { |
| 848 | fields[j].setAccessible(true); |
| 849 | // synchronized makes sure the field is actually written to immediately |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 850 | synchronized (SYNC) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 851 | fields[j].set(serviceInstance, instance); |
| 852 | } |
| 853 | } |
| 854 | catch (Exception e) { |
| 855 | m_logger.log(Logger.LOG_ERROR, "Could not set field " + fields[j], e); |
| 856 | return; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | serviceClazz = serviceClazz.getSuperclass(); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | public Object[] getCompositionInstances() { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 867 | Object[] instances = null; |
| 868 | if (m_compositionManagerGetMethod != null) { |
| 869 | if (m_compositionManager != null) { |
| 870 | m_compositionManagerInstance = m_compositionManager; |
| 871 | } |
| 872 | else { |
| 873 | m_compositionManagerInstance = m_serviceInstance; |
| 874 | } |
| 875 | if (m_compositionManagerInstance != null) { |
| 876 | try { |
| 877 | Method m = m_compositionManagerInstance.getClass().getDeclaredMethod(m_compositionManagerGetMethod, null); |
| 878 | m.setAccessible(true); |
| 879 | instances = (Object[]) m.invoke(m_compositionManagerInstance, null); |
| 880 | } |
| 881 | catch (Exception e) { |
| 882 | m_logger.log(Logger.LOG_ERROR, "Could not obtain instances from the composition manager.", e); |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 883 | instances = m_serviceInstance == null ? new Object[] {} : new Object[] { m_serviceInstance }; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
| 887 | else { |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 888 | instances = m_serviceInstance == null ? new Object[] {} : new Object[] { m_serviceInstance }; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 889 | } |
| 890 | return instances; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | private void configureImplementation(Class clazz, Object instance) { |
| 894 | configureImplementation(clazz, instance, null); |
| 895 | } |
| 896 | |
| 897 | private void configureServices(State state) { |
| 898 | Iterator i = state.getDependencies().iterator(); |
| 899 | while (i.hasNext()) { |
| 900 | Dependency dependency = (Dependency) i.next(); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 901 | if (dependency.isAutoConfig()) { |
| 902 | configureImplementation(dependency.getAutoConfigType(), dependency.getAutoConfigInstance(), dependency.getAutoConfigName()); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 903 | } |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 904 | if (dependency.isRequired()) { |
| 905 | dependency.invokeAdded(this); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 906 | } |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 907 | |
| 908 | |
| 909 | // if (dependency instanceof ServiceDependencyImpl) { |
| 910 | // ServiceDependencyImpl sd = (ServiceDependencyImpl) dependency; |
| 911 | // if (sd.isAutoConfig()) { |
| 912 | // if (sd.isRequired()) { |
| 913 | // configureImplementation(sd.getInterface(), sd.getService(), sd.getAutoConfigName()); |
| 914 | // } |
| 915 | // else { |
| 916 | // // for optional services, we do an "ad-hoc" lookup to inject the service if it is |
| 917 | // // already available even though the tracker has not yet been started |
| 918 | // configureImplementation(sd.getInterface(), sd.lookupService(), sd.getAutoConfigName()); |
| 919 | // } |
| 920 | // } |
| 921 | // // for required dependencies, we invoke any callbacks here |
| 922 | // if (sd.isRequired()) { |
| 923 | // sd.invokeAdded(this, sd.lookupServiceReference(), sd.lookupService()); |
| 924 | // } |
| 925 | // } |
| 926 | // else if (dependency instanceof BundleDependencyImpl) { |
| 927 | // BundleDependencyImpl bd = (BundleDependencyImpl) dependency; |
| 928 | // if (bd.isAutoConfig()) { |
| 929 | // if (bd.isRequired()) { |
| 930 | // configureImplementation(Bundle.class, bd.getBundle()); // TODO AutoConfigName support |
| 931 | // } |
| 932 | // else { |
| 933 | // // for optional services, we do an "ad-hoc" lookup to inject the service if it is |
| 934 | // // already available even though the tracker has not yet been started |
| 935 | // |
| 936 | // // TODO !!! configureImplementation(sd.getInterface(), sd.lookupService(), sd.getAutoConfigName()); |
| 937 | // } |
| 938 | // } |
| 939 | // // for required dependencies, we invoke any callbacks here |
| 940 | // if (bd.isRequired()) { |
| 941 | // bd.invokeAdded(this, bd.getBundle()); |
| 942 | // } |
| 943 | // } |
| 944 | // else if (dependency instanceof ResourceDependencyImpl) { |
| 945 | // ResourceDependencyImpl bd = (ResourceDependencyImpl) dependency; |
| 946 | // if (bd.isAutoConfig()) { |
| 947 | // if (bd.isRequired()) { |
| 948 | // configureImplementation(Resource.class, bd.getResource()); // TODO AutoConfigName support |
| 949 | // } |
| 950 | // else { |
| 951 | // // for optional services, we do an "ad-hoc" lookup to inject the service if it is |
| 952 | // // already available even though the tracker has not yet been started |
| 953 | // |
| 954 | // // TODO !!! configureImplementation(sd.getInterface(), sd.lookupService(), sd.getAutoConfigName()); |
| 955 | // } |
| 956 | // } |
| 957 | // // for required dependencies, we invoke any callbacks here |
| 958 | // if (bd.isRequired()) { |
| 959 | // bd.invokeAdded(this, bd.getResource()); |
| 960 | // } |
| 961 | // } |
| 962 | // else if (dependency instanceof ConfigurationDependencyImpl) { |
| 963 | // ConfigurationDependencyImpl cd = (ConfigurationDependencyImpl) dependency; |
| 964 | // // for configuration dependencies, we invoke updated |
| 965 | // try { |
| 966 | // cd.invokeUpdate(this, this.getService(), cd.getConfiguration()); |
| 967 | // } |
| 968 | // catch (ConfigurationException e) { |
| 969 | // // if this happens, it's definitely an inconsistency |
| 970 | // // when sharing configuration dependencies between services, all implementations |
| 971 | // // should accept the same configurations |
| 972 | // e.printStackTrace(); |
| 973 | // } |
| 974 | // } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
| 978 | private void unconfigureServices(State state) { |
| 979 | Iterator i = state.getDependencies().iterator(); |
| 980 | while (i.hasNext()) { |
| 981 | Dependency dependency = (Dependency) i.next(); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 982 | if (dependency.isRequired()) { |
| 983 | dependency.invokeRemoved(this); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 984 | } |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 985 | // if (dependency instanceof ServiceDependencyImpl) { |
| 986 | // ServiceDependencyImpl sd = (ServiceDependencyImpl) dependency; |
| 987 | // // for required dependencies, we invoke any callbacks here |
| 988 | // if (sd.isRequired()) { |
| 989 | // sd.invokeRemoved(this, sd.lookupServiceReference(), sd.lookupService()); |
| 990 | // } |
| 991 | // } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
| 995 | private void ensureNotActive() { |
| 996 | State state; |
| 997 | synchronized (m_dependencies) { |
| 998 | state = m_state; |
| 999 | } |
| 1000 | if (!state.isInactive()) { |
| 1001 | throw new IllegalStateException("Cannot modify state while active."); |
| 1002 | } |
| 1003 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 1004 | |
| 1005 | public boolean isRegistered() { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 1006 | State state; |
| 1007 | synchronized (m_dependencies) { |
| 1008 | state = m_state; |
| 1009 | } |
Marcel Offermans | 0f6605e | 2009-12-10 10:31:35 +0000 | [diff] [blame] | 1010 | return (state.isAllRequiredAvailable()); |
| 1011 | } |
| 1012 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 1013 | // ServiceComponent interface |
| 1014 | |
| 1015 | static class SCDImpl implements ServiceComponentDependency { |
| 1016 | private final String m_name; |
| 1017 | private final int m_state; |
| 1018 | private final String m_type; |
| 1019 | |
| 1020 | public SCDImpl(String name, int state, String type) { |
| 1021 | m_name = name; |
| 1022 | m_state = state; |
| 1023 | m_type = type; |
| 1024 | } |
| 1025 | |
| 1026 | public String getName() { |
| 1027 | return m_name; |
| 1028 | } |
| 1029 | |
| 1030 | public int getState() { |
| 1031 | return m_state; |
| 1032 | } |
| 1033 | |
| 1034 | public String getType() { |
| 1035 | return m_type; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | public ServiceComponentDependency[] getComponentDependencies() { |
| 1040 | List deps = getDependencies(); |
| 1041 | if (deps != null) { |
| 1042 | ServiceComponentDependency[] result = new ServiceComponentDependency[deps.size()]; |
| 1043 | for (int i = 0; i < result.length; i++) { |
| 1044 | Dependency dep = (Dependency) deps.get(i); |
| 1045 | if (dep instanceof ServiceComponentDependency) { |
| 1046 | result[i] = (ServiceComponentDependency) dep; |
| 1047 | } |
| 1048 | else { |
| 1049 | result[i] = new SCDImpl(dep.toString(), (dep.isAvailable() ? 1 : 0) + (dep.isRequired() ? 2 : 0), dep.getClass().getName()); |
| 1050 | } |
| 1051 | } |
| 1052 | return result; |
| 1053 | } |
| 1054 | return null; |
| 1055 | } |
| 1056 | |
| 1057 | public String getName() { |
| 1058 | if (m_serviceName instanceof String[]) { |
| 1059 | StringBuffer sb = new StringBuffer(); |
| 1060 | String[] names = (String[]) m_serviceName; |
| 1061 | for (int i = 0; i < names.length; i++) { |
| 1062 | if (i > 0) { |
| 1063 | sb.append(", "); |
| 1064 | } |
| 1065 | sb.append(names[i]); |
| 1066 | } |
| 1067 | return sb.toString(); |
| 1068 | } |
| 1069 | else if (m_serviceName instanceof String) { |
| 1070 | return m_serviceName.toString(); |
| 1071 | } |
| 1072 | else { |
| 1073 | return m_implementation.toString(); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | public int getState() { |
| 1078 | return (isRegistered() ? 1 : 0); |
| 1079 | } |
| 1080 | |
| 1081 | static { |
| 1082 | NULL_REGISTRATION = (ServiceRegistration) Proxy.newProxyInstance(ServiceImpl.class.getClassLoader(), new Class[] {ServiceRegistration.class}, new DefaultNullObject()); |
| 1083 | } |
| 1084 | } |