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