Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 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 | */ |
| 19 | package org.apache.felix.cm.impl; |
| 20 | |
| 21 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.security.SecureRandom; |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 24 | import java.util.*; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 25 | |
| 26 | import org.apache.felix.cm.PersistenceManager; |
| 27 | import org.apache.felix.cm.file.FilePersistenceManager; |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 28 | import org.osgi.framework.*; |
| 29 | import org.osgi.service.cm.*; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 30 | import org.osgi.service.log.LogService; |
| 31 | import org.osgi.util.tracker.ServiceTracker; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * The <code>ConfigurationManager</code> is the central class in this |
| 36 | * implementation of the Configuration Admin Service Specification. As such it |
| 37 | * has the following tasks: |
| 38 | * <ul> |
| 39 | * <li>It is a <code>BundleActivator</code> which is called when the bundle |
| 40 | * is started and stopped. |
| 41 | * <li>It is a <code>BundleListener</code> which gets informed when the |
| 42 | * states of bundles change. Mostly this is needed to unbind any bound |
| 43 | * configuration in case a bundle is uninstalled. |
| 44 | * <li>It is a <code>ServiceListener</code> which gets informed when |
| 45 | * <code>ManagedService</code> and <code>ManagedServiceFactory</code> |
| 46 | * services are registered and unregistered. This is used to provide |
| 47 | * configuration to these services. As a service listener it also listens for |
| 48 | * {@link PersistenceManager} instances being registered to support different |
| 49 | * configuration persistence layers. |
| 50 | * <li>A {@link ConfigurationAdminFactory} instance is registered as the |
| 51 | * <code>ConfigurationAdmin</code> service. |
| 52 | * <li>A {@link FilePersistenceManager} instance is registered as a default |
| 53 | * {@link PersistenceManager}. |
| 54 | * <li>Last but not least this instance manages all tasks laid out in the |
| 55 | * specification such as maintaining configuration, taking care of configuration |
| 56 | * events, etc. |
| 57 | * </ul> |
| 58 | * <p> |
| 59 | * The default {@link FilePersistenceManager} is configured with a configuration |
| 60 | * location taken from the <code>felix.cm.dir</code> framework property. If |
| 61 | * this property is not set the <code>config</code> directory in the current |
| 62 | * working directory as specified in the <code>user.dir</code> system property |
| 63 | * is used. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 64 | */ |
| 65 | public class ConfigurationManager implements BundleActivator, BundleListener |
| 66 | { |
| 67 | |
| 68 | /** |
| 69 | * The name of the bundle context property defining the location for the |
| 70 | * configuration files (value is "felix.cm.dir"). |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 71 | * |
Carsten Ziegeler | 7853b9a | 2008-01-11 16:30:24 +0000 | [diff] [blame] | 72 | * @see #start(BundleContext) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 73 | */ |
| 74 | public static final String CM_CONFIG_DIR = "felix.cm.dir"; |
| 75 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 76 | /** |
| 77 | * The name of the bundle context property defining the maximum log level |
| 78 | * (value is "felix.cm.loglevel"). The log level setting is only used if |
| 79 | * there is no OSGi LogService available. Otherwise this setting is ignored. |
| 80 | * <p> |
| 81 | * This value of this property is expected to be an integer number |
| 82 | * corresponding to the log level values of the OSGi LogService. That is 1 |
| 83 | * for errors, 2 for warnings, 3 for informational messages and 4 for debug |
| 84 | * messages. The default value is 2, such that only warnings and errors are |
| 85 | * logged in the absence of a LogService. |
| 86 | */ |
| 87 | public static final String CM_LOG_LEVEL = "felix.cm.loglevel"; |
| 88 | |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 89 | // The name of the LogService (not using the class, which might be missing) |
| 90 | private static final String LOG_SERVICE_NAME = "org.osgi.service.log.LogService"; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 91 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 92 | private static final int CM_LOG_LEVEL_DEFAULT = 2; |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 93 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 94 | // random number generator to create configuration PIDs for factory |
| 95 | // configurations |
| 96 | private static SecureRandom numberGenerator; |
| 97 | |
| 98 | // comparator used to keep the ordered persistence manager map |
| 99 | private static final Comparator cmRankComp = new RankingComparator( true, ConfigurationPlugin.CM_RANKING ); |
| 100 | |
| 101 | // the BundleContext of the Configuration Admin Service bundle |
| 102 | private BundleContext bundleContext; |
| 103 | |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 104 | // the service registration of the configuration admin |
| 105 | private ServiceRegistration configurationAdminRegistration; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 106 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 107 | // the ServiceTracker to emit log services (see log(int, String, Throwable)) |
| 108 | private ServiceTracker logTracker; |
| 109 | |
| 110 | // the ConfigurationEvent listeners |
| 111 | private ServiceTracker configurationListenerTracker; |
| 112 | |
| 113 | // service tracker for managed services |
| 114 | private ServiceTracker managedServiceTracker; |
| 115 | |
| 116 | // service tracker for managed service factories |
| 117 | private ServiceTracker managedServiceFactoryTracker; |
| 118 | |
| 119 | // PersistenceManager services |
| 120 | private ServiceTracker persistenceManagerTracker; |
| 121 | |
| 122 | // the thread used to schedule tasks required to run asynchronously |
| 123 | private UpdateThread updateThread; |
| 124 | |
| 125 | /** |
| 126 | * The actual list of {@link PersistenceManager persistence managers} to use |
| 127 | * when looking for configuration data. This list is built from the |
| 128 | * {@link #persistenceManagerMap}, which is ordered according to the |
| 129 | * {@link RankingComparator}. |
| 130 | */ |
| 131 | private PersistenceManager[] persistenceManagers; |
| 132 | |
| 133 | // the persistenceManagerTracker.getTrackingCount when the |
| 134 | // persistenceManagers were last got |
| 135 | private int pmtCount; |
| 136 | |
| 137 | // the cache of Factory instances mapped by their factory PID |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 138 | private final Map factories = new HashMap(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 139 | |
| 140 | // the cache of Configuration instances mapped by their PID |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 141 | // have this always set to prevent NPE on bundle shutdown |
| 142 | private final Map configurations = new HashMap(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 143 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 144 | // the maximum log level when no LogService is available |
| 145 | private int logLevel = CM_LOG_LEVEL_DEFAULT; |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 146 | |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 147 | // flag indicating whether BundleChange events should be consumed (FELIX-979) |
| 148 | private volatile boolean handleBundleEvents; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 149 | |
| 150 | public void start( BundleContext bundleContext ) |
| 151 | { |
| 152 | // track the log service using a ServiceTracker |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 153 | logTracker = new ServiceTracker( bundleContext, LOG_SERVICE_NAME , null ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 154 | logTracker.open(); |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 155 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 156 | // assign the log level |
| 157 | String logLevelProp = bundleContext.getProperty( CM_LOG_LEVEL ); |
| 158 | if ( logLevelProp == null ) |
| 159 | { |
| 160 | logLevel = CM_LOG_LEVEL_DEFAULT; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | try |
| 165 | { |
| 166 | logLevel = Integer.parseInt( logLevelProp ); |
| 167 | } |
| 168 | catch ( NumberFormatException nfe ) |
| 169 | { |
| 170 | logLevel = CM_LOG_LEVEL_DEFAULT; |
| 171 | } |
| 172 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 173 | |
| 174 | // set up some fields |
| 175 | this.bundleContext = bundleContext; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 176 | |
| 177 | // configurationlistener support |
| 178 | configurationListenerTracker = new ServiceTracker( bundleContext, ConfigurationListener.class.getName(), null ); |
| 179 | configurationListenerTracker.open(); |
| 180 | |
| 181 | // initialize the asynchonous updater thread |
| 182 | this.updateThread = new UpdateThread( this ); |
| 183 | this.updateThread.start(); |
| 184 | |
| 185 | // set up the location (might throw IllegalArgumentException) |
| 186 | try |
| 187 | { |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 188 | FilePersistenceManager fpm = new FilePersistenceManager( bundleContext, bundleContext |
| 189 | .getProperty( CM_CONFIG_DIR ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 190 | Hashtable props = new Hashtable(); |
| 191 | props.put( Constants.SERVICE_PID, fpm.getClass().getName() ); |
| 192 | props.put( Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager" ); |
| 193 | props.put( Constants.SERVICE_VENDOR, "Apache Software Foundation" ); |
| 194 | props.put( Constants.SERVICE_RANKING, new Integer( Integer.MIN_VALUE ) ); |
| 195 | bundleContext.registerService( PersistenceManager.class.getName(), fpm, props ); |
| 196 | } |
| 197 | catch ( IllegalArgumentException iae ) |
| 198 | { |
| 199 | log( LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae ); |
| 200 | } |
| 201 | |
| 202 | // register as bundle and service listener |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 203 | handleBundleEvents = true; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 204 | bundleContext.addBundleListener( this ); |
| 205 | |
| 206 | // get all persistence managers to begin with |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 207 | pmtCount = 1; // make sure to get the persistence managers at least once |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 208 | persistenceManagerTracker = new ServiceTracker( bundleContext, PersistenceManager.class.getName(), null ); |
| 209 | persistenceManagerTracker.open(); |
| 210 | |
| 211 | // create and register configuration admin - start after PM tracker ... |
| 212 | ConfigurationAdminFactory caf = new ConfigurationAdminFactory( this ); |
| 213 | Hashtable props = new Hashtable(); |
| 214 | props.put( Constants.SERVICE_PID, "org.apache.felix.cm.ConfigurationAdmin" ); |
| 215 | props.put( Constants.SERVICE_DESCRIPTION, "Configuration Admin Service Specification 1.2 Implementation" ); |
| 216 | props.put( Constants.SERVICE_VENDOR, "Apache Software Foundation" ); |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 217 | configurationAdminRegistration = bundleContext.registerService( ConfigurationAdmin.class.getName(), caf, props ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 218 | |
| 219 | // start handling ManagedService[Factory] services |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 220 | managedServiceTracker = new ManagedServiceTracker(this); |
| 221 | managedServiceFactoryTracker = new ManagedServiceFactoryTracker(this); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
| 225 | public void stop( BundleContext bundleContext ) |
| 226 | { |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 227 | |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 228 | // stop handling bundle events immediately |
| 229 | handleBundleEvents = false; |
| 230 | |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 231 | // immediately unregister the Configuration Admin before cleaning up |
Felix Meschberger | 1056835 | 2009-01-15 08:57:11 +0000 | [diff] [blame] | 232 | // clearing the field before actually unregistering the service |
| 233 | // prevents IllegalStateException in getServiceReference() if |
| 234 | // the field is not null but the service already unregistered |
| 235 | if (configurationAdminRegistration != null) { |
| 236 | ServiceRegistration reg = configurationAdminRegistration; |
| 237 | configurationAdminRegistration = null; |
| 238 | reg.unregister(); |
| 239 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 240 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 241 | // stop handling ManagedService[Factory] services |
| 242 | managedServiceFactoryTracker.close(); |
| 243 | managedServiceTracker.close(); |
| 244 | |
| 245 | // don't care for PersistenceManagers any more |
| 246 | persistenceManagerTracker.close(); |
| 247 | |
| 248 | // stop listening for events |
| 249 | bundleContext.removeBundleListener( this ); |
| 250 | |
| 251 | if ( configurationListenerTracker != null ) |
| 252 | { |
| 253 | configurationListenerTracker.close(); |
| 254 | } |
| 255 | |
| 256 | if ( updateThread != null ) |
| 257 | { |
| 258 | // terminate asynchrounous updates |
| 259 | updateThread.terminate(); |
| 260 | |
| 261 | // wait for all updates to terminate |
| 262 | try |
| 263 | { |
| 264 | updateThread.join(); |
| 265 | } |
| 266 | catch ( InterruptedException ie ) |
| 267 | { |
| 268 | // don't really care |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if ( logTracker != null ) |
| 273 | { |
| 274 | logTracker.close(); |
| 275 | } |
| 276 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 277 | // just ensure the configuration cache is empty |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 278 | synchronized ( configurations ) |
| 279 | { |
| 280 | configurations.clear(); |
| 281 | } |
| 282 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 283 | // just ensure the factory cache is empty |
| 284 | synchronized ( factories ) |
| 285 | { |
| 286 | factories.clear(); |
| 287 | } |
| 288 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 289 | this.bundleContext = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | // ---------- Configuration caching support -------------------------------- |
| 294 | |
| 295 | ConfigurationImpl getCachedConfiguration( String pid ) |
| 296 | { |
| 297 | synchronized ( configurations ) |
| 298 | { |
| 299 | return ( ConfigurationImpl ) configurations.get( pid ); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 304 | ConfigurationImpl[] getCachedConfigurations() |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 305 | { |
| 306 | synchronized ( configurations ) |
| 307 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 308 | return ( ConfigurationImpl[] ) configurations.values().toArray( |
| 309 | new ConfigurationImpl[configurations.size()] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 314 | ConfigurationImpl cacheConfiguration( ConfigurationImpl configuration ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 315 | { |
| 316 | synchronized ( configurations ) |
| 317 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 318 | Object existing = configurations.get( configuration.getPid() ); |
| 319 | if ( existing != null ) |
| 320 | { |
| 321 | return ( ConfigurationImpl ) existing; |
| 322 | } |
| 323 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 324 | configurations.put( configuration.getPid(), configuration ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 325 | return configuration; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void removeConfiguration( ConfigurationImpl configuration ) |
| 331 | { |
| 332 | synchronized ( configurations ) |
| 333 | { |
| 334 | configurations.remove( configuration.getPid() ); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 339 | Factory getCachedFactory( String factoryPid ) |
| 340 | { |
| 341 | synchronized ( factories ) |
| 342 | { |
| 343 | return ( Factory ) factories.get( factoryPid ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | Factory[] getCachedFactories() |
| 349 | { |
| 350 | synchronized ( factories ) |
| 351 | { |
| 352 | return ( Factory[] ) factories.values().toArray( new Factory[factories.size()] ); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | |
| 357 | void cacheFactory( Factory factory ) |
| 358 | { |
| 359 | synchronized ( factories ) |
| 360 | { |
| 361 | factories.put( factory.getFactoryPid(), factory ); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 367 | // ---------- ConfigurationAdminImpl support ------------------------------- |
| 368 | |
| 369 | /* |
| 370 | * (non-Javadoc) |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 371 | * |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 372 | * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String) |
| 373 | */ |
| 374 | ConfigurationImpl createFactoryConfiguration( ConfigurationAdminImpl configurationAdmin, String factoryPid ) |
| 375 | throws IOException |
| 376 | { |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 377 | // check Persmission if factory is bound to another bundle |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 378 | Factory factory = getFactory( factoryPid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 379 | if ( factory.getBundleLocation() != null |
| 380 | && !factory.getBundleLocation().equals( configurationAdmin.getBundle().getLocation() ) ) |
| 381 | { |
| 382 | configurationAdmin.checkPermission(); |
| 383 | } |
| 384 | |
| 385 | // create the configuration |
| 386 | String pid = createPid( factoryPid ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 387 | ConfigurationImpl config = createConfiguration( pid, factoryPid, configurationAdmin.getBundle().getLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 388 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 389 | return config; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /* |
| 394 | * (non-Javadoc) |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 395 | * |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 396 | * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String, |
| 397 | * java.lang.String) |
| 398 | */ |
| 399 | ConfigurationImpl createFactoryConfiguration( String factoryPid, String location ) throws IOException |
| 400 | { |
| 401 | // create the configuration |
| 402 | String pid = createPid( factoryPid ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 403 | ConfigurationImpl config = createConfiguration( pid, factoryPid, location ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 404 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 405 | return config; |
| 406 | } |
| 407 | |
| 408 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 409 | ConfigurationImpl getExistingConfiguration( String pid ) throws IOException |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 410 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 411 | ConfigurationImpl config = getCachedConfiguration( pid ); |
| 412 | if ( config != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 413 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 414 | return config; |
| 415 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 416 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 417 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 418 | for ( int i = 0; i < pmList.length; i++ ) |
| 419 | { |
| 420 | if ( pmList[i].exists( pid ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 421 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 422 | Dictionary props = pmList[i].load( pid ); |
| 423 | config = new ConfigurationImpl( this, pmList[i], props ); |
| 424 | return cacheConfiguration( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 427 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 428 | // neither the cache nor any persistence manager has configuration |
| 429 | return null; |
| 430 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 431 | |
| 432 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 433 | ConfigurationImpl getConfiguration( String pid, String bundleLocation ) throws IOException |
| 434 | { |
| 435 | // check for existing (cached or persistent) configuration |
| 436 | ConfigurationImpl config = getExistingConfiguration( pid ); |
| 437 | if ( config != null ) |
| 438 | { |
| 439 | return config; |
| 440 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 441 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 442 | // else create new configuration also setting the bundle location |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 443 | // and cache the new configuration |
| 444 | config = createConfiguration( pid, null, bundleLocation ); |
| 445 | return cacheConfiguration( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
| 449 | ConfigurationImpl[] listConfigurations( ConfigurationAdminImpl configurationAdmin, String filterString ) |
| 450 | throws IOException, InvalidSyntaxException |
| 451 | { |
| 452 | Filter filter = null; |
| 453 | if ( filterString != null ) |
| 454 | { |
| 455 | filter = bundleContext.createFilter( filterString ); |
| 456 | } |
| 457 | |
| 458 | boolean unprivileged = configurationAdmin != null && !configurationAdmin.hasPermission(); |
| 459 | String location = unprivileged ? configurationAdmin.getBundle().getLocation() : null; |
| 460 | |
| 461 | List configList = new ArrayList(); |
| 462 | |
| 463 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 464 | for ( int i = 0; i < pmList.length; i++ ) |
| 465 | { |
| 466 | Enumeration configs = pmList[i].getDictionaries(); |
| 467 | while ( configs.hasMoreElements() ) |
| 468 | { |
| 469 | Dictionary config = ( Dictionary ) configs.nextElement(); |
| 470 | |
| 471 | // ignore non-Configuration dictionaries |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 472 | String pid = ( String ) config.get( Constants.SERVICE_PID ); |
| 473 | if ( pid == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 474 | { |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | // ignore this config if not privileged and not bound to bundle |
| 479 | if ( unprivileged ) |
| 480 | { |
| 481 | Object boundLocation = config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ); |
| 482 | if ( !location.equals( boundLocation ) ) |
| 483 | { |
| 484 | continue; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // check filter |
| 489 | if ( filter == null || filter.match( config ) ) |
| 490 | { |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 491 | // ensure the service.pid and returned a cached config if available |
| 492 | ConfigurationImpl cfg = getCachedConfiguration( pid ); |
| 493 | if ( cfg == null ) |
| 494 | { |
| 495 | cfg = new ConfigurationImpl( this, pmList[i], config ); |
| 496 | } |
| 497 | |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 498 | // FELIX-611: Ignore configuration objects without props |
| 499 | if ( !cfg.isNew() ) |
| 500 | { |
| 501 | configList.add( cfg ); |
| 502 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
Felix Meschberger | 8faceff | 2007-07-04 07:19:48 +0000 | [diff] [blame] | 507 | return ( ConfigurationImpl[] ) configList.toArray( new ConfigurationImpl[configList |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 508 | .size()] ); |
| 509 | } |
| 510 | |
| 511 | |
| 512 | void deleted( ConfigurationImpl config ) |
| 513 | { |
| 514 | // remove the configuration from the cache |
| 515 | removeConfiguration( config ); |
| 516 | updateThread.schedule( new DeleteConfiguration( config ) ); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | void updated( ConfigurationImpl config ) |
| 521 | { |
| 522 | updateThread.schedule( new UpdateConfiguration( config ) ); |
| 523 | } |
| 524 | |
| 525 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 526 | void fireConfigurationEvent( int type, String pid, String factoryPid ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 527 | { |
| 528 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 529 | updateThread.schedule( new FireConfigurationEvent( type, pid, factoryPid) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
| 533 | // ---------- BundleListener ----------------------------------------------- |
| 534 | |
| 535 | public void bundleChanged( BundleEvent event ) |
| 536 | { |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 537 | if ( event.getType() == BundleEvent.UNINSTALLED && handleBundleEvents ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 538 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 539 | final String location = event.getBundle().getLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 540 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 541 | // we only reset dynamic bindings, which are only present in |
| 542 | // cached configurations, hence only consider cached configs here |
| 543 | final ConfigurationImpl[] configs = getCachedConfigurations(); |
| 544 | for ( int i = 0; i < configs.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 545 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 546 | final ConfigurationImpl cfg = configs[i]; |
| 547 | if ( location.equals( cfg.getBundleLocation() ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 548 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 549 | // reset dynamic binding |
| 550 | cfg.setBundleLocation( null, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 553 | |
| 554 | // we only reset dynamic bindings, which are only present in |
| 555 | // cached factories, hence only consider cached factories here |
| 556 | final Factory[] factories = getCachedFactories(); |
| 557 | for ( int i = 0; i < factories.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 558 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 559 | final Factory factory = factories[i]; |
| 560 | if ( location.equals( factory.getBundleLocation() ) ) |
| 561 | { |
| 562 | // reset dynamic binding |
| 563 | factory.setBundleLocation( null, false ); |
| 564 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 565 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | |
| 570 | // ---------- internal ----------------------------------------------------- |
| 571 | |
| 572 | private PersistenceManager[] getPersistenceManagers() |
| 573 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 574 | int currentPmtCount = persistenceManagerTracker.getTrackingCount(); |
| 575 | if ( persistenceManagers == null || currentPmtCount > pmtCount ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 576 | { |
| 577 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 578 | List pmList = new ArrayList(); |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 579 | PersistenceManager[] pm; |
| 580 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 581 | ServiceReference[] refs = persistenceManagerTracker.getServiceReferences(); |
| 582 | if ( refs == null || refs.length == 0 ) |
| 583 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 584 | pm = new PersistenceManager[0]; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 585 | } |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 586 | else |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 587 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 588 | // sort the references according to the cmRanking property |
| 589 | SortedSet pms = new TreeSet( new RankingComparator( false ) ); |
| 590 | for ( int i = 0; i < refs.length; i++ ) |
| 591 | { |
| 592 | pms.add( refs[i] ); |
| 593 | } |
| 594 | |
| 595 | // create the service array from the sorted set of referenecs |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 596 | int pmIndex = 0; |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 597 | for ( Iterator pi = pms.iterator(); pi.hasNext(); pmIndex++ ) |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 598 | { |
| 599 | ServiceReference ref = ( ServiceReference ) pi.next(); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 600 | Object service = persistenceManagerTracker.getService( ref ); |
| 601 | if ( service != null ) |
| 602 | { |
| 603 | pmList.add( service ); |
| 604 | } |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 605 | } |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 606 | |
| 607 | pm = ( PersistenceManager[] ) pmList.toArray( new PersistenceManager[pmList.size()] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 610 | pmtCount = pm.length; |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 611 | persistenceManagers = pm; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return persistenceManagers; |
| 615 | } |
| 616 | |
| 617 | |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 618 | private ServiceReference getServiceReference() |
| 619 | { |
| 620 | ServiceRegistration reg = configurationAdminRegistration; |
| 621 | return ( reg != null ) ? reg.getReference() : null; |
| 622 | } |
| 623 | |
| 624 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 625 | private void configure( ServiceReference sr, ManagedService service ) |
| 626 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 627 | String[] pids = getServicePid( sr ); |
| 628 | if ( pids != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 629 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 630 | for ( int i = 0; i < pids.length; i++ ) |
| 631 | { |
| 632 | ManagedServiceUpdate update = new ManagedServiceUpdate( pids[i], sr, service ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 633 | updateThread.schedule( update ); |
| 634 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 635 | } |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 636 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 637 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 638 | |
| 639 | private void configure( ServiceReference sr, ManagedServiceFactory service ) |
| 640 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 641 | String[] pids = getServicePid( sr ); |
| 642 | if ( pids != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 643 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 644 | for ( int i = 0; i < pids.length; i++ ) |
| 645 | { |
| 646 | ManagedServiceFactoryUpdate update = new ManagedServiceFactoryUpdate( pids[i], sr, service ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 647 | updateThread.schedule( update ); |
| 648 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 649 | } |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 650 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 651 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 652 | |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 653 | /** |
| 654 | * Factory method to create a new configuration object. The configuration |
| 655 | * object returned is not stored in configuration cache and only persisted |
| 656 | * if the <code>factoryPid</code> parameter is <code>null</code>. |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 657 | * |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 658 | * @param pid |
| 659 | * The PID of the new configuration object. Must not be |
| 660 | * <code>null</code>. |
| 661 | * @param factoryPid |
| 662 | * The factory PID of the new configuration. Not |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 663 | * <code>null</code> if the new configuration object belongs to a |
| 664 | * factory. The configuration object will not be persisted if |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 665 | * this parameter is not <code>null</code>. |
| 666 | * @param bundleLocation |
| 667 | * The bundle location of the bundle to which the configuration |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 668 | * belongs or <code>null</code> if the configuration is not bound |
| 669 | * yet. |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 670 | * @return The new configuration object |
| 671 | * @throws IOException |
| 672 | * May be thrown if an error occurrs persisting the new |
| 673 | * configuration object. |
| 674 | */ |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 675 | ConfigurationImpl createConfiguration( String pid, String factoryPid, String bundleLocation ) throws IOException |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 676 | { |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 677 | return new ConfigurationImpl( this, getPersistenceManagers()[0], pid, factoryPid, bundleLocation ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
| 681 | Factory getFactory( String factoryPid ) throws IOException |
| 682 | { |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 683 | Factory factory = getCachedFactory( factoryPid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 684 | if ( factory != null ) |
| 685 | { |
| 686 | return factory; |
| 687 | } |
| 688 | |
| 689 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 690 | for ( int i = 0; i < pmList.length; i++ ) |
| 691 | { |
| 692 | if ( Factory.exists( pmList[i], factoryPid ) ) |
| 693 | { |
| 694 | factory = Factory.load( pmList[i], factoryPid ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 695 | cacheFactory( factory ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 696 | return factory; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // if getting here, there is no configuration yet, optionally create new |
| 701 | return createFactory( factoryPid ); |
| 702 | } |
| 703 | |
| 704 | |
| 705 | Factory createFactory( String factoryPid ) |
| 706 | { |
| 707 | Factory factory = new Factory( getPersistenceManagers()[0], factoryPid ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 708 | cacheFactory( factory ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 709 | return factory; |
| 710 | } |
| 711 | |
| 712 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 713 | /** |
| 714 | * Calls the registered configuration plugins on the given configuration |
| 715 | * object unless the configuration has just been created and not been |
| 716 | * updated yet. |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 717 | * |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 718 | * @param targetPid The identification of the configuration update used to |
| 719 | * select the plugins according to their cm.target service |
| 720 | * property |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 721 | * @param sr The service reference of the managed service (factory) which |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 722 | * is to be updated with configuration |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 723 | * @param cfg The configuration object whose properties have to be passed |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 724 | * through the plugins |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 725 | * @return The properties from the configuration object passed through the |
| 726 | * plugins or <code>null</code> if the configuration object has |
| 727 | * been newly created and no properties exist yet. |
| 728 | */ |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 729 | private Dictionary callPlugins( final String targetPid, final ServiceReference sr, final ConfigurationImpl cfg ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 730 | { |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 731 | // return a deep copy, since the plugins may tamper with the array |
| 732 | // and collection elements, which should not modify the internal data |
| 733 | Dictionary props = cfg.getProperties( true ); |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 734 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 735 | // guard against NPE for new configuration never updated |
| 736 | if (props == null) { |
| 737 | return null; |
| 738 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 739 | |
| 740 | ServiceReference[] plugins = null; |
| 741 | try |
| 742 | { |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 743 | String filter = "(|(!(cm.target=*))(cm.target=" + targetPid + "))"; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 744 | plugins = bundleContext.getServiceReferences( ConfigurationPlugin.class.getName(), filter ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 745 | } |
| 746 | catch ( InvalidSyntaxException ise ) |
| 747 | { |
| 748 | // no filter, no exception ... |
| 749 | } |
| 750 | |
| 751 | // abort early if there are no plugins |
| 752 | if ( plugins == null || plugins.length == 0 ) |
| 753 | { |
| 754 | return props; |
| 755 | } |
| 756 | |
| 757 | // sort the plugins by their service.cmRanking |
| 758 | SortedSet pluginSet = new TreeSet( cmRankComp ); |
Carsten Ziegeler | 7853b9a | 2008-01-11 16:30:24 +0000 | [diff] [blame] | 759 | for ( int i = 0; i < plugins.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 760 | { |
| 761 | pluginSet.add( plugins[i] ); |
| 762 | } |
| 763 | |
| 764 | // call the plugins in order |
| 765 | for ( Iterator pi = pluginSet.iterator(); pi.hasNext(); ) |
| 766 | { |
| 767 | ServiceReference pluginRef = ( ServiceReference ) pi.next(); |
| 768 | ConfigurationPlugin plugin = ( ConfigurationPlugin ) bundleContext.getService( pluginRef ); |
| 769 | try |
| 770 | { |
| 771 | plugin.modifyConfiguration( sr, props ); |
| 772 | } |
| 773 | catch ( Throwable t ) |
| 774 | { |
| 775 | log( LogService.LOG_ERROR, "Unexpected problem calling" + " configuration plugin", t ); |
| 776 | } |
| 777 | finally |
| 778 | { |
| 779 | // ensure ungetting the plugin |
| 780 | bundleContext.ungetService( pluginRef ); |
| 781 | } |
| 782 | cfg.setAutoProperties( props, false ); |
| 783 | } |
| 784 | |
| 785 | return props; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /** |
| 790 | * Creates a PID for the given factoryPid |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 791 | * |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 792 | * @param factoryPid |
| 793 | * @return |
| 794 | */ |
| 795 | private static String createPid( String factoryPid ) |
| 796 | { |
| 797 | SecureRandom ng = numberGenerator; |
| 798 | if ( ng == null ) |
| 799 | { |
| 800 | numberGenerator = ng = new SecureRandom(); |
| 801 | } |
| 802 | |
| 803 | byte[] randomBytes = new byte[16]; |
| 804 | ng.nextBytes( randomBytes ); |
| 805 | randomBytes[6] &= 0x0f; /* clear version */ |
| 806 | randomBytes[6] |= 0x40; /* set to version 4 */ |
| 807 | randomBytes[8] &= 0x3f; /* clear variant */ |
| 808 | randomBytes[8] |= 0x80; /* set to IETF variant */ |
| 809 | |
| 810 | StringBuffer buf = new StringBuffer( factoryPid.length() + 1 + 36 ); |
| 811 | |
| 812 | // prefix the new pid with the factory pid |
| 813 | buf.append( factoryPid ).append( "." ); |
| 814 | |
| 815 | // serialize the UUID into the buffer |
| 816 | for ( int i = 0; i < randomBytes.length; i++ ) |
| 817 | { |
| 818 | |
| 819 | if ( i == 4 || i == 6 || i == 8 || i == 10 ) |
| 820 | { |
| 821 | buf.append( '-' ); |
| 822 | } |
| 823 | |
| 824 | int val = randomBytes[i] & 0xff; |
| 825 | buf.append( Integer.toHexString( val >> 4 ) ); |
| 826 | buf.append( Integer.toHexString( val & 0xf ) ); |
| 827 | } |
| 828 | |
| 829 | return buf.toString(); |
| 830 | } |
| 831 | |
| 832 | |
| 833 | void log( int level, String message, Throwable t ) |
| 834 | { |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 835 | // log using the LogService if available |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 836 | Object log = logTracker.getService(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 837 | if ( log != null ) |
| 838 | { |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 839 | ( ( LogService ) log ).log( getServiceReference(), level, message, t ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
| 842 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 843 | // Otherwise only log if more serious than the configured level |
| 844 | if ( level <= logLevel ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 845 | { |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 846 | String code; |
| 847 | switch ( level ) |
| 848 | { |
| 849 | case LogService.LOG_INFO: |
| 850 | code = "*INFO *"; |
| 851 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 852 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 853 | case LogService.LOG_WARNING: |
| 854 | code = "*WARN *"; |
| 855 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 856 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 857 | case LogService.LOG_ERROR: |
| 858 | code = "*ERROR*"; |
| 859 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 860 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 861 | case LogService.LOG_DEBUG: |
| 862 | default: |
| 863 | code = "*DEBUG*"; |
| 864 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 865 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 866 | System.err.println( code + " " + message ); |
| 867 | if ( t != null ) |
| 868 | { |
| 869 | t.printStackTrace( System.err ); |
| 870 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 874 | |
| 875 | /** |
| 876 | * Returns the <code>service.pid</code> property of the service reference as |
| 877 | * an array of strings or <code>null</code> if the service reference does |
| 878 | * not have a service PID property. |
| 879 | * <p> |
| 880 | * The service.pid property may be a single string, in which case a single |
| 881 | * element array is returned. If the property is an array of string, this |
| 882 | * array is returned. If the property is a collection it is assumed to be a |
| 883 | * collection of strings and the collection is converted to an array to be |
| 884 | * returned. Otherwise (also if the property is not set) <code>null</code> |
| 885 | * is returned. |
| 886 | * |
| 887 | * @throws NullPointerException |
| 888 | * if reference is <code>null</code> |
| 889 | * @throws ArrayStoreException |
| 890 | * if the service pid is a collection and not all elements are |
| 891 | * strings. |
| 892 | */ |
| 893 | static String[] getServicePid( ServiceReference reference ) |
| 894 | { |
| 895 | Object pidObj = reference.getProperty( Constants.SERVICE_PID ); |
| 896 | if ( pidObj instanceof String ) |
| 897 | { |
| 898 | return new String[] |
| 899 | { ( String ) pidObj }; |
| 900 | } |
| 901 | else if ( pidObj instanceof String[] ) |
| 902 | { |
| 903 | return ( String[] ) pidObj; |
| 904 | } |
| 905 | else if ( pidObj instanceof Collection ) |
| 906 | { |
| 907 | Collection pidCollection = ( Collection ) pidObj; |
| 908 | return ( String[] ) pidCollection.toArray( new String[pidCollection.size()] ); |
| 909 | } |
| 910 | |
| 911 | return null; |
| 912 | } |
| 913 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 914 | // ---------- inner classes ------------------------------------------------ |
| 915 | |
| 916 | private class ManagedServiceUpdate implements Runnable |
| 917 | { |
| 918 | private String pid; |
| 919 | |
| 920 | private ServiceReference sr; |
| 921 | |
| 922 | private ManagedService service; |
| 923 | |
| 924 | |
| 925 | ManagedServiceUpdate( String pid, ServiceReference sr, ManagedService service ) |
| 926 | { |
| 927 | this.pid = pid; |
| 928 | this.sr = sr; |
| 929 | this.service = service; |
| 930 | } |
| 931 | |
| 932 | |
| 933 | public void run() |
| 934 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 935 | // get or load configuration for the pid |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 936 | ConfigurationImpl cfg; |
| 937 | try |
| 938 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 939 | cfg = getExistingConfiguration( pid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 940 | } |
| 941 | catch ( IOException ioe ) |
| 942 | { |
| 943 | log( LogService.LOG_ERROR, "Error loading configuration for " + pid, ioe ); |
| 944 | return; |
| 945 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 946 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 947 | // this will be set below to be given to the service |
| 948 | Dictionary dictionary; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 949 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 950 | // check configuration and call plugins if existing and not new |
| 951 | if ( cfg != null && !cfg.isNew() ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 952 | { |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 953 | |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 954 | if ( cfg.isDelivered() ) |
| 955 | { |
| 956 | log( LogService.LOG_DEBUG, "Configuration " + pid + " has already been delivered", null ); |
| 957 | return; |
| 958 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 959 | |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 960 | Bundle serviceBundle = sr.getBundle(); |
| 961 | if ( serviceBundle == null ) |
| 962 | { |
| 963 | log( LogService.LOG_INFO, "ServiceFactory for PID " + pid |
| 964 | + " seems to already have been unregistered, not updating with configuration", null ); |
| 965 | return; |
| 966 | } |
| 967 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 968 | // 104.3 Ignore duplicate PIDs from other bundles and report |
| 969 | // them to the log |
| 970 | // 104.4.1 No update call back for PID already bound to another |
| 971 | // bundle location |
| 972 | // 104.4.1 assign configuration to bundle if unassigned |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 973 | String bundleLocation = serviceBundle.getLocation(); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 974 | if ( cfg.getBundleLocation() == null ) |
| 975 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 976 | // dynamically bind to the location of the service if unbound |
| 977 | cfg.setBundleLocation( bundleLocation, false ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 978 | } |
| 979 | else if ( !bundleLocation.equals( cfg.getBundleLocation() ) ) |
| 980 | { |
| 981 | log( LogService.LOG_ERROR, "Cannot use configuration for " + pid + " requested by bundle " |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 982 | + serviceBundle.getLocation() + " but belongs to " + cfg.getBundleLocation(), null ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | |
| 986 | // 104.3 Report an error in the log if more than one service |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 987 | // with the same PID asks for the configuration |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 988 | if ( cfg.getServiceReference() != null && !sr.equals( cfg.getServiceReference() ) ) |
| 989 | { |
| 990 | log( LogService.LOG_ERROR, "Configuration for " + pid + " has already been used for service " |
| 991 | + cfg.getServiceReference() + " and will now also be given to " + sr, null ); |
| 992 | } |
| 993 | else |
| 994 | { |
| 995 | // assign the configuration to the service |
| 996 | cfg.setServiceReference( sr ); |
| 997 | } |
| 998 | |
| 999 | // prepare the configuration for the service (call plugins) |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1000 | dictionary = callPlugins( pid, sr, cfg ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1001 | } |
| 1002 | else |
| 1003 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1004 | // 104.5.3 ManagedService.updated must be called with null |
| 1005 | // if no configuration is available |
| 1006 | dictionary = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1009 | // update the service with the configuration |
| 1010 | try |
| 1011 | { |
| 1012 | service.updated( dictionary ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1013 | |
| 1014 | // if there is nothing to set, don't |
| 1015 | if ( cfg != null ) |
| 1016 | { |
| 1017 | cfg.setDelivered( true ); |
| 1018 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1019 | } |
| 1020 | catch ( ConfigurationException ce ) |
| 1021 | { |
| 1022 | if ( ce.getProperty() != null ) |
| 1023 | { |
| 1024 | log( LogService.LOG_ERROR, sr + ": Updating configuration property " + ce.getProperty() |
| 1025 | + " caused a problem: " + ce.getReason(), ce ); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | log( LogService.LOG_ERROR, sr + ": Updating configuration caused a problem: " + ce.getReason(), ce ); |
| 1030 | |
| 1031 | } |
| 1032 | } |
| 1033 | catch ( Throwable t ) |
| 1034 | { |
| 1035 | log( LogService.LOG_ERROR, sr + ": Unexpected problem updating configuration", t ); |
| 1036 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1037 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1038 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1039 | public String toString() |
| 1040 | { |
| 1041 | return "ManagedService Update: pid=" + pid; |
| 1042 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | private class ManagedServiceFactoryUpdate implements Runnable |
| 1046 | { |
| 1047 | private String factoryPid; |
| 1048 | |
| 1049 | private ServiceReference sr; |
| 1050 | |
| 1051 | private ManagedServiceFactory service; |
| 1052 | |
| 1053 | |
| 1054 | ManagedServiceFactoryUpdate( String factoryPid, ServiceReference sr, ManagedServiceFactory service ) |
| 1055 | { |
| 1056 | this.factoryPid = factoryPid; |
| 1057 | this.sr = sr; |
| 1058 | this.service = service; |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | public void run() |
| 1063 | { |
| 1064 | Factory factory; |
| 1065 | try |
| 1066 | { |
| 1067 | factory = getFactory( factoryPid ); |
| 1068 | } |
| 1069 | catch ( IOException ioe ) |
| 1070 | { |
| 1071 | log( LogService.LOG_ERROR, "Cannot get factory mapping for factory PID " + factoryPid, ioe ); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1075 | Bundle serviceBundle = sr.getBundle(); |
| 1076 | if ( serviceBundle == null ) |
| 1077 | { |
| 1078 | log( LogService.LOG_INFO, "ManagedServiceFactory for factory PID " + factoryPid |
| 1079 | + " seems to already have been unregistered, not updating with factory", null ); |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | String bundleLocation = serviceBundle.getLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1084 | if ( factory.getBundleLocation() == null ) |
| 1085 | { |
| 1086 | // bind to the location of the service if unbound |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1087 | factory.setBundleLocation( bundleLocation, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1088 | } |
| 1089 | else if ( !bundleLocation.equals( factory.getBundleLocation() ) ) |
| 1090 | { |
| 1091 | // factory PID is bound to another bundle |
| 1092 | log( LogService.LOG_ERROR, "Cannot use Factory configuration for " + factoryPid |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1093 | + " requested by bundle " + serviceBundle.getLocation() + " but belongs to " |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1094 | + factory.getBundleLocation(), null ); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
| 1098 | Set pids = factory.getPIDs(); |
| 1099 | |
| 1100 | for ( Iterator pi = pids.iterator(); pi.hasNext(); ) |
| 1101 | { |
| 1102 | String pid = ( String ) pi.next(); |
| 1103 | ConfigurationImpl cfg; |
| 1104 | try |
| 1105 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1106 | cfg = getExistingConfiguration( pid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1107 | } |
| 1108 | catch ( IOException ioe ) |
| 1109 | { |
| 1110 | log( LogService.LOG_ERROR, "Error loading configuration for " + pid, ioe ); |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
| 1114 | // sanity check on the configuration |
| 1115 | if ( cfg == null ) |
| 1116 | { |
| 1117 | log( LogService.LOG_ERROR, "Configuration " + pid + " referred to by factory " + factoryPid |
| 1118 | + " does not exist", null ); |
| 1119 | factory.removePID( pid ); |
| 1120 | factory.storeSilently(); |
| 1121 | continue; |
| 1122 | } |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1123 | else if ( cfg.isNew() ) |
| 1124 | { |
| 1125 | // Configuration has just been created but not yet updated |
| 1126 | // we currently just ignore it and have the update mechanism |
| 1127 | // provide the configuration to the ManagedServiceFactory |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 1128 | // As of FELIX-612 (not storing new factory configurations) |
| 1129 | // this should not happen. We keep this for added stability |
| 1130 | // but raise the logging level to error. |
| 1131 | log( LogService.LOG_ERROR, "Ignoring new configuration pid=" + pid, null ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1132 | continue; |
| 1133 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1134 | else if ( !factoryPid.equals( cfg.getFactoryPid() ) ) |
| 1135 | { |
| 1136 | log( LogService.LOG_ERROR, "Configuration " + pid + " referred to by factory " + factoryPid |
Felix Meschberger | bb04898 | 2007-08-06 08:56:50 +0000 | [diff] [blame] | 1137 | + " seems to belong to factory " + cfg.getFactoryPid(), null ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1138 | factory.removePID( pid ); |
| 1139 | factory.storeSilently(); |
| 1140 | continue; |
| 1141 | } |
| 1142 | |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1143 | // do not re-updated unmodified configuration |
| 1144 | if ( cfg.isDelivered() ) |
| 1145 | { |
| 1146 | log( LogService.LOG_DEBUG, "Configuration " + pid + " has already been updated", null ); |
| 1147 | continue; |
| 1148 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1149 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1150 | // check bundle location of configuration |
| 1151 | if ( cfg.getBundleLocation() == null ) |
| 1152 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1153 | // dynamically bind to the location of the service if unbound |
| 1154 | cfg.setBundleLocation( bundleLocation, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1155 | } |
| 1156 | else if ( !bundleLocation.equals( cfg.getBundleLocation() ) ) |
| 1157 | { |
| 1158 | // configuration is bound to another bundle |
| 1159 | log( LogService.LOG_ERROR, "Configuration " + pid + " (factory " + factoryPid |
| 1160 | + ") belongs to bundle " + cfg.getBundleLocation() + " but was requested for bundle " |
| 1161 | + bundleLocation, null ); |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | // prepare the configuration for the service (call plugins) |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1166 | // call the plugins with cm.target set to the service's factory PID |
| 1167 | // (clarification in Section 104.9.1 of Compendium 4.2) |
| 1168 | Dictionary dictionary = callPlugins( factoryPid, sr, cfg ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1169 | |
| 1170 | // update the service with the configuration |
| 1171 | try |
| 1172 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1173 | // only, if there is non-null configuration data |
| 1174 | if ( dictionary != null ) |
| 1175 | { |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1176 | log( LogService.LOG_DEBUG, sr + ": Updating configuration pid=" + pid, null ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1177 | service.updated( pid, dictionary ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1178 | cfg.setDelivered( true ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1179 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1180 | } |
| 1181 | catch ( ConfigurationException ce ) |
| 1182 | { |
| 1183 | if ( ce.getProperty() != null ) |
| 1184 | { |
| 1185 | log( LogService.LOG_ERROR, sr + ": Updating configuration property " + ce.getProperty() |
| 1186 | + " caused a problem: " + ce.getReason(), ce ); |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | log( LogService.LOG_ERROR, sr + ": Updating configuration caused a problem: " + ce.getReason(), |
| 1191 | ce ); |
| 1192 | |
| 1193 | } |
| 1194 | } |
| 1195 | catch ( Throwable t ) |
| 1196 | { |
| 1197 | log( LogService.LOG_ERROR, sr + ": Unexpected problem updating configuration", t ); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1201 | |
| 1202 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1203 | public String toString() |
| 1204 | { |
| 1205 | return "ManagedServiceFactory Update: factoryPid=" + factoryPid; |
| 1206 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | private class UpdateConfiguration implements Runnable |
| 1210 | { |
| 1211 | |
| 1212 | private ConfigurationImpl config; |
| 1213 | |
| 1214 | |
| 1215 | UpdateConfiguration( ConfigurationImpl config ) |
| 1216 | { |
| 1217 | this.config = config; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | public void run() |
| 1222 | { |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1223 | if ( config.isDelivered() ) |
| 1224 | { |
| 1225 | log( LogService.LOG_DEBUG, "Configuration " + config.getPid() + " has already been updated", null ); |
| 1226 | return; |
| 1227 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1228 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1229 | try |
| 1230 | { |
| 1231 | if ( config.getFactoryPid() == null ) |
| 1232 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1233 | final ServiceReference[] srList = bundleContext.getServiceReferences( ManagedService.class |
| 1234 | .getName(), "(" + Constants.SERVICE_PID + "=" + config.getPid() + ")" ); |
| 1235 | if ( srList != null && srList.length > 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1236 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1237 | final ServiceReference sr = srList[0]; |
| 1238 | final ManagedService srv = ( ManagedService ) bundleContext.getService( sr ); |
| 1239 | |
| 1240 | // 104.3 Report an error in the log if more than one service |
| 1241 | // with the same PID asks for the configuration |
| 1242 | if ( srList.length > 1 ) |
| 1243 | { |
| 1244 | for ( int i = 1; i < srList.length; i++ ) |
| 1245 | { |
| 1246 | log( LogService.LOG_ERROR, "Configuration for " + config.getPid() |
| 1247 | + " is used for service " + sr |
| 1248 | + "following services will not receive configuration: " + srList[i], null ); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1252 | try |
| 1253 | { |
| 1254 | // bind the configuration, fail if bound to another |
| 1255 | // bundle !! |
| 1256 | // check bundle location of configuration |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1257 | String bundleLocation = sr.getBundle().getLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1258 | if ( config.getBundleLocation() == null ) |
| 1259 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1260 | // dynamically bind to the location of the service if unbound |
| 1261 | config.setBundleLocation( bundleLocation, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1262 | } |
| 1263 | else if ( !bundleLocation.equals( config.getBundleLocation() ) ) |
| 1264 | { |
| 1265 | // configuration is bound to another bundle |
| 1266 | log( LogService.LOG_ERROR, "Configuration " + config.getPid() + " belongs to bundle " |
| 1267 | + config.getBundleLocation() + " but was requested for bundle " + bundleLocation, |
| 1268 | null ); |
| 1269 | return; |
| 1270 | } |
| 1271 | |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1272 | // record the delivery of the configuration |
| 1273 | if ( config.getServiceReference() == null ) |
| 1274 | { |
| 1275 | config.setServiceReference( sr ); |
| 1276 | } |
| 1277 | |
Felix Meschberger | bb04898 | 2007-08-06 08:56:50 +0000 | [diff] [blame] | 1278 | // prepare the configuration for the service (call plugins) |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1279 | Dictionary dictionary = callPlugins( config.getPid(), sr, config ); |
Felix Meschberger | bb04898 | 2007-08-06 08:56:50 +0000 | [diff] [blame] | 1280 | |
| 1281 | // update the ManagedService with the properties |
| 1282 | srv.updated( dictionary ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1283 | config.setDelivered( true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1284 | } |
| 1285 | finally |
| 1286 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1287 | bundleContext.ungetService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | else |
| 1292 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1293 | ServiceReference[] srList = bundleContext.getServiceReferences( ManagedServiceFactory.class |
| 1294 | .getName(), "(" + Constants.SERVICE_PID + "=" + config.getFactoryPid() + ")" ); |
| 1295 | if ( srList != null && srList.length > 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1296 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1297 | final ServiceReference sr = srList[0]; |
| 1298 | final ManagedServiceFactory srv = ( ManagedServiceFactory ) bundleContext.getService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1299 | try |
| 1300 | { |
| 1301 | // bind the configuration, fail if bound to another |
| 1302 | // bundle !! |
| 1303 | // check bundle location of configuration |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1304 | String bundleLocation = sr.getBundle().getLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1305 | if ( config.getBundleLocation() == null ) |
| 1306 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1307 | // dynamically bind to the location of the service if unbound |
| 1308 | config.setBundleLocation( bundleLocation, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1309 | } |
| 1310 | else if ( !bundleLocation.equals( config.getBundleLocation() ) ) |
| 1311 | { |
| 1312 | // configuration is bound to another bundle |
| 1313 | log( LogService.LOG_ERROR, "Configuration " + config.getPid() + " (factory " |
| 1314 | + config.getFactoryPid() + ") belongs to bundle " + config.getBundleLocation() |
| 1315 | + " but was requested for bundle " + bundleLocation, null ); |
| 1316 | return; |
| 1317 | } |
| 1318 | |
Felix Meschberger | bb04898 | 2007-08-06 08:56:50 +0000 | [diff] [blame] | 1319 | // prepare the configuration for the service (call plugins) |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1320 | // call the plugins with cm.target set to the service's factory PID |
| 1321 | // (clarification in Section 104.9.1 of Compendium 4.2) |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1322 | Dictionary dictionary = callPlugins( config.getFactoryPid(), sr, config ); |
Felix Meschberger | bb04898 | 2007-08-06 08:56:50 +0000 | [diff] [blame] | 1323 | |
| 1324 | // update the ManagedServiceFactory with the properties |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1325 | // only, if there is non-null configuration data |
| 1326 | if ( dictionary != null ) |
| 1327 | { |
| 1328 | srv.updated( config.getPid(), dictionary ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1329 | config.setDelivered( true ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1330 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1331 | } |
| 1332 | finally |
| 1333 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1334 | bundleContext.ungetService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | catch ( ConfigurationException ce ) |
| 1340 | { |
| 1341 | if ( ce.getProperty() != null ) |
| 1342 | { |
| 1343 | log( LogService.LOG_ERROR, "Updating configuration property " + ce.getProperty() |
| 1344 | + " caused a problem: " + ce.getReason(), ce ); |
| 1345 | } |
| 1346 | else |
| 1347 | { |
| 1348 | log( LogService.LOG_ERROR, "Updating configuration caused a problem: " + ce.getReason(), ce ); |
| 1349 | |
| 1350 | } |
| 1351 | } |
| 1352 | catch ( Throwable t ) |
| 1353 | { |
| 1354 | log( LogService.LOG_ERROR, "Unexpected problem updating configuration", t ); |
| 1355 | } |
| 1356 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1357 | fireConfigurationEvent( ConfigurationEvent.CM_UPDATED, config.getPid(), config.getFactoryPid() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1358 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1359 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1360 | public String toString() |
| 1361 | { |
| 1362 | return "Update: pid=" + config.getPid(); |
| 1363 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | private class DeleteConfiguration implements Runnable |
| 1367 | { |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1368 | |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1369 | private ConfigurationImpl config; |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1370 | private String pid; |
| 1371 | private String factoryPid; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1372 | |
| 1373 | |
| 1374 | DeleteConfiguration( ConfigurationImpl config ) |
| 1375 | { |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1376 | this.config = config; |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1377 | this.pid = config.getPid(); |
| 1378 | this.factoryPid = config.getFactoryPid(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | public void run() |
| 1383 | { |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1384 | if ( config.isDelivered() ) |
| 1385 | { |
| 1386 | log( LogService.LOG_DEBUG, "Deletion of configuration " + pid + " has already been delivered", null ); |
| 1387 | return; |
| 1388 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1389 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1390 | try |
| 1391 | { |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1392 | if ( factoryPid == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1393 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1394 | ServiceReference[] srList = bundleContext.getServiceReferences( ManagedService.class.getName(), "(" |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1395 | + Constants.SERVICE_PID + "=" + pid + ")" ); |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1396 | if ( srList != null && srList.length > 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1397 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1398 | final ServiceReference sr = srList[0]; |
| 1399 | final ManagedService srv = ( ManagedService ) bundleContext.getService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1400 | try |
| 1401 | { |
| 1402 | srv.updated( null ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1403 | config.setDelivered( true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1404 | } |
| 1405 | finally |
| 1406 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1407 | bundleContext.ungetService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | else |
| 1412 | { |
| 1413 | // remove the pid from the factory |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1414 | Factory factory = getFactory( factoryPid ); |
| 1415 | factory.removePID( pid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1416 | factory.store(); |
| 1417 | |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1418 | ServiceReference[] srList = bundleContext.getServiceReferences( ManagedServiceFactory.class |
| 1419 | .getName(), "(" + Constants.SERVICE_PID + "=" + factoryPid + ")" ); |
| 1420 | if ( srList != null && srList.length > 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1421 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1422 | final ServiceReference sr = srList[0]; |
| 1423 | final ManagedServiceFactory srv = ( ManagedServiceFactory ) bundleContext.getService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1424 | try |
| 1425 | { |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1426 | srv.deleted( pid ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1427 | config.setDelivered( true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1428 | } |
| 1429 | finally |
| 1430 | { |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 1431 | bundleContext.ungetService( sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | catch ( ConfigurationException ce ) |
| 1437 | { |
| 1438 | if ( ce.getProperty() != null ) |
| 1439 | { |
| 1440 | log( LogService.LOG_ERROR, "Updating configuration property " + ce.getProperty() |
| 1441 | + " caused a problem: " + ce.getReason(), ce ); |
| 1442 | } |
| 1443 | else |
| 1444 | { |
| 1445 | log( LogService.LOG_ERROR, "Updating configuration caused a problem: " + ce.getReason(), ce ); |
| 1446 | |
| 1447 | } |
| 1448 | } |
| 1449 | catch ( Throwable t ) |
| 1450 | { |
| 1451 | log( LogService.LOG_ERROR, "Unexpected problem updating configuration", t ); |
| 1452 | } |
| 1453 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1454 | fireConfigurationEvent( ConfigurationEvent.CM_DELETED, pid, factoryPid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1455 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1456 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1457 | public String toString() |
| 1458 | { |
| 1459 | return "Delete: pid=" + pid; |
| 1460 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | private class FireConfigurationEvent implements Runnable |
| 1464 | { |
| 1465 | private int type; |
| 1466 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1467 | private String pid; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1468 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1469 | private String factoryPid; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1470 | |
| 1471 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1472 | FireConfigurationEvent( int type, String pid, String factoryPid ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1473 | { |
| 1474 | this.type = type; |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1475 | this.pid = pid; |
| 1476 | this.factoryPid = factoryPid; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | public void run() |
| 1481 | { |
| 1482 | // get the listeners |
| 1483 | ServiceReference[] srs = configurationListenerTracker.getServiceReferences(); |
| 1484 | if ( srs == null || srs.length == 0 ) |
| 1485 | { |
| 1486 | return; |
| 1487 | } |
| 1488 | |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 1489 | ConfigurationEvent event = new ConfigurationEvent( getServiceReference(), type, factoryPid, pid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1490 | |
| 1491 | for ( int i = 0; i < srs.length; i++ ) |
| 1492 | { |
| 1493 | ConfigurationListener cl = ( ConfigurationListener ) configurationListenerTracker.getService( srs[i] ); |
| 1494 | try |
| 1495 | { |
| 1496 | cl.configurationEvent( event ); |
| 1497 | } |
| 1498 | catch ( Throwable t ) |
| 1499 | { |
| 1500 | log( LogService.LOG_ERROR, "Unexpected problem delivery configuration event to " + srs[i], t ); |
| 1501 | } |
| 1502 | } |
| 1503 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1504 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1505 | public String toString() |
| 1506 | { |
| 1507 | return "Fire ConfigurationEvent: pid=" + pid; |
| 1508 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1511 | private static abstract class AbstractManagedServiceTracker extends ServiceTracker |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1512 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1513 | protected final ConfigurationManager cm; |
| 1514 | |
| 1515 | AbstractManagedServiceTracker( ConfigurationManager cm, String className ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1516 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1517 | super( cm.bundleContext, className, null ); |
| 1518 | this.cm = cm; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1519 | open(); |
| 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | public void removedService( ServiceReference reference, Object service ) |
| 1524 | { |
| 1525 | // check whether we can take back the configuration object |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1526 | String[] pids = getServicePid( reference ); |
| 1527 | if ( pids != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1528 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1529 | for ( int i = 0; i < pids.length; i++ ) |
| 1530 | { |
| 1531 | ConfigurationImpl cfg = cm.getCachedConfiguration( pids[i] ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1532 | if ( cfg != null && reference.equals( cfg.getServiceReference() ) ) |
| 1533 | { |
| 1534 | cfg.setServiceReference( null ); |
| 1535 | cfg.setDelivered( false ); |
| 1536 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1537 | } |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1538 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1539 | |
| 1540 | super.removedService( reference, service ); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1544 | private static class ManagedServiceTracker extends AbstractManagedServiceTracker |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1545 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1546 | |
| 1547 | ManagedServiceTracker(ConfigurationManager cm) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1548 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1549 | super( cm, ManagedService.class.getName() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | public Object addingService( ServiceReference reference ) |
| 1554 | { |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1555 | Object serviceObject = super.addingService( reference ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1556 | |
| 1557 | // configure the managed service |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1558 | if ( serviceObject instanceof ManagedService ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1559 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1560 | cm.configure(reference, ( ManagedService ) serviceObject); |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1561 | } |
| 1562 | else |
| 1563 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1564 | cm.log( LogService.LOG_WARNING, "Service " + serviceObject + " is not a ManagedService", null ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1567 | return serviceObject; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1571 | private static class ManagedServiceFactoryTracker extends AbstractManagedServiceTracker |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1572 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1573 | ManagedServiceFactoryTracker(ConfigurationManager cm) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1574 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1575 | super( cm, ManagedServiceFactory.class.getName() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | public Object addingService( ServiceReference reference ) |
| 1580 | { |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1581 | Object serviceObject = super.addingService( reference ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1582 | |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1583 | // configure the managed service factory |
| 1584 | if ( serviceObject instanceof ManagedServiceFactory ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1585 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1586 | cm.configure( reference, ( ManagedServiceFactory ) serviceObject ); |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1587 | } |
| 1588 | else |
| 1589 | { |
Carsten Ziegeler | 4168398 | 2007-12-27 08:35:27 +0000 | [diff] [blame] | 1590 | cm.log( LogService.LOG_WARNING, "Service " + serviceObject + " is not a ManagedServiceFactory", null ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1593 | return serviceObject; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1594 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1595 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1596 | |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1597 | public void removedService( ServiceReference reference, Object service ) |
| 1598 | { |
| 1599 | // check whether we can take back the configuration objects |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1600 | String[] factoryPids = getServicePid( reference ); |
| 1601 | if ( factoryPids != null ) |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1602 | { |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1603 | for ( int i = 0; i < factoryPids.length; i++ ) |
| 1604 | { |
| 1605 | Factory factory = cm.getCachedFactory( factoryPids[i] ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1606 | if ( factory != null ) |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1607 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1608 | for ( Iterator pi = factory.getPIDs().iterator(); pi.hasNext(); ) |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1609 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1610 | String pid = ( String ) pi.next(); |
| 1611 | ConfigurationImpl cfg = cm.getCachedConfiguration( pid ); |
| 1612 | if ( cfg != null ) |
| 1613 | { |
| 1614 | cfg.setDelivered( false ); |
| 1615 | } |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
| 1618 | } |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame^] | 1619 | } |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1620 | |
| 1621 | super.removedService( reference, service ); |
| 1622 | } |
| 1623 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |