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