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; |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 24 | import java.text.MessageFormat; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 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; |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 32 | import java.util.LinkedList; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.Random; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 36 | |
| 37 | import org.apache.felix.cm.PersistenceManager; |
| 38 | import org.apache.felix.cm.file.FilePersistenceManager; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 39 | import org.apache.felix.cm.impl.helper.BaseTracker; |
| 40 | import org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker; |
| 41 | import org.apache.felix.cm.impl.helper.ManagedServiceTracker; |
| 42 | import org.apache.felix.cm.impl.helper.TargetedPID; |
| 43 | import org.osgi.framework.Bundle; |
| 44 | import org.osgi.framework.BundleActivator; |
| 45 | import org.osgi.framework.BundleContext; |
| 46 | import org.osgi.framework.BundleEvent; |
| 47 | import org.osgi.framework.BundleListener; |
| 48 | import org.osgi.framework.Constants; |
| 49 | import org.osgi.framework.Filter; |
| 50 | import org.osgi.framework.InvalidSyntaxException; |
| 51 | import org.osgi.framework.ServiceReference; |
| 52 | import org.osgi.framework.ServiceRegistration; |
| 53 | import org.osgi.service.cm.ConfigurationAdmin; |
| 54 | import org.osgi.service.cm.ConfigurationEvent; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 55 | import org.osgi.service.cm.ConfigurationListener; |
| 56 | import org.osgi.service.cm.ConfigurationPermission; |
| 57 | import org.osgi.service.cm.ConfigurationPlugin; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 58 | import org.osgi.service.cm.SynchronousConfigurationListener; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 59 | import org.osgi.service.log.LogService; |
| 60 | import org.osgi.util.tracker.ServiceTracker; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * The <code>ConfigurationManager</code> is the central class in this |
| 65 | * implementation of the Configuration Admin Service Specification. As such it |
| 66 | * has the following tasks: |
| 67 | * <ul> |
| 68 | * <li>It is a <code>BundleActivator</code> which is called when the bundle |
| 69 | * is started and stopped. |
| 70 | * <li>It is a <code>BundleListener</code> which gets informed when the |
| 71 | * states of bundles change. Mostly this is needed to unbind any bound |
| 72 | * configuration in case a bundle is uninstalled. |
| 73 | * <li>It is a <code>ServiceListener</code> which gets informed when |
| 74 | * <code>ManagedService</code> and <code>ManagedServiceFactory</code> |
| 75 | * services are registered and unregistered. This is used to provide |
| 76 | * configuration to these services. As a service listener it also listens for |
| 77 | * {@link PersistenceManager} instances being registered to support different |
| 78 | * configuration persistence layers. |
| 79 | * <li>A {@link ConfigurationAdminFactory} instance is registered as the |
| 80 | * <code>ConfigurationAdmin</code> service. |
| 81 | * <li>A {@link FilePersistenceManager} instance is registered as a default |
| 82 | * {@link PersistenceManager}. |
| 83 | * <li>Last but not least this instance manages all tasks laid out in the |
| 84 | * specification such as maintaining configuration, taking care of configuration |
| 85 | * events, etc. |
| 86 | * </ul> |
| 87 | * <p> |
| 88 | * The default {@link FilePersistenceManager} is configured with a configuration |
| 89 | * location taken from the <code>felix.cm.dir</code> framework property. If |
| 90 | * this property is not set the <code>config</code> directory in the current |
| 91 | * working directory as specified in the <code>user.dir</code> system property |
| 92 | * is used. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 93 | */ |
| 94 | public class ConfigurationManager implements BundleActivator, BundleListener |
| 95 | { |
| 96 | |
| 97 | /** |
| 98 | * The name of the bundle context property defining the location for the |
| 99 | * configuration files (value is "felix.cm.dir"). |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 100 | * |
Carsten Ziegeler | 7853b9a | 2008-01-11 16:30:24 +0000 | [diff] [blame] | 101 | * @see #start(BundleContext) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 102 | */ |
| 103 | public static final String CM_CONFIG_DIR = "felix.cm.dir"; |
| 104 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 105 | /** |
| 106 | * The name of the bundle context property defining the maximum log level |
| 107 | * (value is "felix.cm.loglevel"). The log level setting is only used if |
| 108 | * there is no OSGi LogService available. Otherwise this setting is ignored. |
| 109 | * <p> |
| 110 | * This value of this property is expected to be an integer number |
| 111 | * corresponding to the log level values of the OSGi LogService. That is 1 |
| 112 | * for errors, 2 for warnings, 3 for informational messages and 4 for debug |
| 113 | * messages. The default value is 2, such that only warnings and errors are |
| 114 | * logged in the absence of a LogService. |
| 115 | */ |
| 116 | public static final String CM_LOG_LEVEL = "felix.cm.loglevel"; |
| 117 | |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 118 | // The name of the LogService (not using the class, which might be missing) |
| 119 | private static final String LOG_SERVICE_NAME = "org.osgi.service.log.LogService"; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 120 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 121 | private static final int CM_LOG_LEVEL_DEFAULT = 2; |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 122 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 123 | // random number generator to create configuration PIDs for factory |
| 124 | // configurations |
Felix Meschberger | 417f66c | 2011-02-04 11:25:23 +0000 | [diff] [blame] | 125 | private static Random numberGenerator; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 126 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 127 | // the BundleContext of the Configuration Admin Service bundle |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 128 | BundleContext bundleContext; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 129 | |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 130 | // the service registration of the configuration admin |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 131 | private volatile ServiceRegistration configurationAdminRegistration; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 132 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 133 | // the ServiceTracker to emit log services (see log(int, String, Throwable)) |
| 134 | private ServiceTracker logTracker; |
| 135 | |
| 136 | // the ConfigurationEvent listeners |
| 137 | private ServiceTracker configurationListenerTracker; |
| 138 | |
| 139 | // service tracker for managed services |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 140 | private ManagedServiceTracker managedServiceTracker; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 141 | |
| 142 | // service tracker for managed service factories |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 143 | private ManagedServiceFactoryTracker managedServiceFactoryTracker; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 144 | |
| 145 | // PersistenceManager services |
| 146 | private ServiceTracker persistenceManagerTracker; |
| 147 | |
| 148 | // the thread used to schedule tasks required to run asynchronously |
| 149 | private UpdateThread updateThread; |
| 150 | |
Felix Meschberger | b68d1cf | 2010-08-26 08:46:36 +0000 | [diff] [blame] | 151 | // the thread used to schedule events to be dispatched asynchronously |
| 152 | private UpdateThread eventThread; |
| 153 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 154 | /** |
| 155 | * The actual list of {@link PersistenceManager persistence managers} to use |
| 156 | * when looking for configuration data. This list is built from the |
| 157 | * {@link #persistenceManagerMap}, which is ordered according to the |
| 158 | * {@link RankingComparator}. |
| 159 | */ |
| 160 | private PersistenceManager[] persistenceManagers; |
| 161 | |
| 162 | // the persistenceManagerTracker.getTrackingCount when the |
| 163 | // persistenceManagers were last got |
| 164 | private int pmtCount; |
| 165 | |
| 166 | // the cache of Factory instances mapped by their factory PID |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 167 | private final HashMap<String, Factory> factories = new HashMap<String, Factory>(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 168 | |
| 169 | // the cache of Configuration instances mapped by their PID |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 170 | // have this always set to prevent NPE on bundle shutdown |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 171 | private final HashMap<String, ConfigurationImpl> configurations = new HashMap<String, ConfigurationImpl>(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 172 | |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 173 | /** |
| 174 | * The map of dynamic configuration bindings. This maps the |
| 175 | * PID of the dynamically bound configuration or factory to its bundle |
| 176 | * location. |
| 177 | * <p> |
| 178 | * On bundle startup this map is loaded from persistence and validated |
| 179 | * against the locations of installed bundles: Entries pointing to bundle |
| 180 | * locations not currently installed are removed. |
| 181 | * <p> |
| 182 | * The map is written to persistence on each change. |
| 183 | */ |
| 184 | private DynamicBindings dynamicBindings; |
| 185 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 186 | // the maximum log level when no LogService is available |
| 187 | private int logLevel = CM_LOG_LEVEL_DEFAULT; |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 188 | |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 189 | // flag indicating whether BundleChange events should be consumed (FELIX-979) |
| 190 | private volatile boolean handleBundleEvents; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 191 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 192 | // flag indicating whether the manager is considered alive |
| 193 | private volatile boolean isActive; |
| 194 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 195 | public void start( BundleContext bundleContext ) |
| 196 | { |
| 197 | // track the log service using a ServiceTracker |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 198 | logTracker = new ServiceTracker( bundleContext, LOG_SERVICE_NAME , null ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 199 | logTracker.open(); |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 200 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 201 | // assign the log level |
| 202 | String logLevelProp = bundleContext.getProperty( CM_LOG_LEVEL ); |
| 203 | if ( logLevelProp == null ) |
| 204 | { |
| 205 | logLevel = CM_LOG_LEVEL_DEFAULT; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | try |
| 210 | { |
| 211 | logLevel = Integer.parseInt( logLevelProp ); |
| 212 | } |
| 213 | catch ( NumberFormatException nfe ) |
| 214 | { |
| 215 | logLevel = CM_LOG_LEVEL_DEFAULT; |
| 216 | } |
| 217 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 218 | |
| 219 | // set up some fields |
| 220 | this.bundleContext = bundleContext; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 221 | |
| 222 | // configurationlistener support |
| 223 | configurationListenerTracker = new ServiceTracker( bundleContext, ConfigurationListener.class.getName(), null ); |
| 224 | configurationListenerTracker.open(); |
| 225 | |
| 226 | // initialize the asynchonous updater thread |
Felix Meschberger | b68d1cf | 2010-08-26 08:46:36 +0000 | [diff] [blame] | 227 | ThreadGroup tg = new ThreadGroup( "Configuration Admin Service" ); |
| 228 | tg.setDaemon( true ); |
| 229 | this.updateThread = new UpdateThread( this, tg, "CM Configuration Updater" ); |
| 230 | this.eventThread = new UpdateThread( this, tg, "CM Event Dispatcher" ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 231 | |
| 232 | // set up the location (might throw IllegalArgumentException) |
| 233 | try |
| 234 | { |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 235 | FilePersistenceManager fpm = new FilePersistenceManager( bundleContext, bundleContext |
| 236 | .getProperty( CM_CONFIG_DIR ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 237 | Hashtable props = new Hashtable(); |
| 238 | props.put( Constants.SERVICE_PID, fpm.getClass().getName() ); |
| 239 | props.put( Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager" ); |
| 240 | props.put( Constants.SERVICE_VENDOR, "Apache Software Foundation" ); |
| 241 | props.put( Constants.SERVICE_RANKING, new Integer( Integer.MIN_VALUE ) ); |
| 242 | bundleContext.registerService( PersistenceManager.class.getName(), fpm, props ); |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 243 | |
| 244 | // setup dynamic configuration bindings |
| 245 | dynamicBindings = new DynamicBindings( bundleContext, fpm ); |
| 246 | } |
| 247 | catch ( IOException ioe ) |
| 248 | { |
| 249 | log( LogService.LOG_ERROR, "Failure setting up dynamic configuration bindings", ioe ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 250 | } |
| 251 | catch ( IllegalArgumentException iae ) |
| 252 | { |
| 253 | log( LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae ); |
| 254 | } |
| 255 | |
| 256 | // register as bundle and service listener |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 257 | handleBundleEvents = true; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 258 | bundleContext.addBundleListener( this ); |
| 259 | |
| 260 | // get all persistence managers to begin with |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 261 | pmtCount = 1; // make sure to get the persistence managers at least once |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 262 | persistenceManagerTracker = new ServiceTracker( bundleContext, PersistenceManager.class.getName(), null ); |
| 263 | persistenceManagerTracker.open(); |
| 264 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 265 | // consider alive now (before clients use Configuration Admin |
| 266 | // service registered in the next step) |
| 267 | isActive = true; |
| 268 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 269 | // create and register configuration admin - start after PM tracker ... |
| 270 | ConfigurationAdminFactory caf = new ConfigurationAdminFactory( this ); |
| 271 | Hashtable props = new Hashtable(); |
| 272 | props.put( Constants.SERVICE_PID, "org.apache.felix.cm.ConfigurationAdmin" ); |
| 273 | props.put( Constants.SERVICE_DESCRIPTION, "Configuration Admin Service Specification 1.2 Implementation" ); |
| 274 | props.put( Constants.SERVICE_VENDOR, "Apache Software Foundation" ); |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 275 | configurationAdminRegistration = bundleContext.registerService( ConfigurationAdmin.class.getName(), caf, props ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 276 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 277 | // start handling ManagedService[Factory] services |
| 278 | managedServiceTracker = new ManagedServiceTracker(this); |
| 279 | managedServiceFactoryTracker = new ManagedServiceFactoryTracker(this); |
| 280 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 281 | // start processing the event queues only after registering the service |
| 282 | // see FELIX-2813 for details |
| 283 | this.updateThread.start(); |
| 284 | this.eventThread.start(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | public void stop( BundleContext bundleContext ) |
| 289 | { |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 290 | |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 291 | // stop handling bundle events immediately |
| 292 | handleBundleEvents = false; |
| 293 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 294 | // stop handling ManagedService[Factory] services |
| 295 | managedServiceFactoryTracker.close(); |
| 296 | managedServiceTracker.close(); |
| 297 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 298 | // stop queue processing before unregistering the service |
| 299 | // see FELIX-2813 for details |
| 300 | if ( updateThread != null ) |
| 301 | { |
| 302 | updateThread.terminate(); |
| 303 | } |
| 304 | if ( eventThread != null ) |
| 305 | { |
| 306 | eventThread.terminate(); |
| 307 | } |
| 308 | |
Felix Meschberger | db90b1b | 2007-09-06 08:18:10 +0000 | [diff] [blame] | 309 | // immediately unregister the Configuration Admin before cleaning up |
Felix Meschberger | 1056835 | 2009-01-15 08:57:11 +0000 | [diff] [blame] | 310 | // clearing the field before actually unregistering the service |
| 311 | // prevents IllegalStateException in getServiceReference() if |
| 312 | // the field is not null but the service already unregistered |
| 313 | if (configurationAdminRegistration != null) { |
| 314 | ServiceRegistration reg = configurationAdminRegistration; |
| 315 | configurationAdminRegistration = null; |
| 316 | reg.unregister(); |
| 317 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 318 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 319 | // consider inactive after unregistering such that during |
| 320 | // unregistration the manager is still alive and can react |
| 321 | isActive = false; |
| 322 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 323 | // don't care for PersistenceManagers any more |
| 324 | persistenceManagerTracker.close(); |
| 325 | |
| 326 | // stop listening for events |
| 327 | bundleContext.removeBundleListener( this ); |
| 328 | |
| 329 | if ( configurationListenerTracker != null ) |
| 330 | { |
| 331 | configurationListenerTracker.close(); |
| 332 | } |
| 333 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 334 | if ( logTracker != null ) |
| 335 | { |
| 336 | logTracker.close(); |
| 337 | } |
| 338 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 339 | // just ensure the configuration cache is empty |
Felix Meschberger | 5d3e7ac | 2009-08-14 21:45:10 +0000 | [diff] [blame] | 340 | synchronized ( configurations ) |
| 341 | { |
| 342 | configurations.clear(); |
| 343 | } |
| 344 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 345 | // just ensure the factory cache is empty |
| 346 | synchronized ( factories ) |
| 347 | { |
| 348 | factories.clear(); |
| 349 | } |
| 350 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 351 | this.bundleContext = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 355 | /** |
| 356 | * Returns <code>true</code> if this manager is considered active. |
| 357 | */ |
| 358 | boolean isActive() |
| 359 | { |
| 360 | return isActive; |
| 361 | } |
| 362 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 363 | public BundleContext getBundleContext() |
| 364 | { |
| 365 | return bundleContext; |
| 366 | } |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 367 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 368 | // ---------- Configuration caching support -------------------------------- |
| 369 | |
| 370 | ConfigurationImpl getCachedConfiguration( String pid ) |
| 371 | { |
| 372 | synchronized ( configurations ) |
| 373 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 374 | return configurations.get( pid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 379 | ConfigurationImpl[] getCachedConfigurations() |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 380 | { |
| 381 | synchronized ( configurations ) |
| 382 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 383 | return configurations.values().toArray( |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 384 | new ConfigurationImpl[configurations.size()] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 389 | ConfigurationImpl cacheConfiguration( ConfigurationImpl configuration ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 390 | { |
| 391 | synchronized ( configurations ) |
| 392 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 393 | final String pid = configuration.getPidString(); |
| 394 | final Object existing = configurations.get( pid ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 395 | if ( existing != null ) |
| 396 | { |
| 397 | return ( ConfigurationImpl ) existing; |
| 398 | } |
| 399 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 400 | configurations.put( pid, configuration ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 401 | return configuration; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void removeConfiguration( ConfigurationImpl configuration ) |
| 407 | { |
| 408 | synchronized ( configurations ) |
| 409 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 410 | configurations.remove( configuration.getPidString() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 415 | Factory getCachedFactory( String factoryPid ) |
| 416 | { |
| 417 | synchronized ( factories ) |
| 418 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 419 | return factories.get( factoryPid ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | |
| 424 | Factory[] getCachedFactories() |
| 425 | { |
| 426 | synchronized ( factories ) |
| 427 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 428 | return factories.values().toArray( new Factory[factories.size()] ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | void cacheFactory( Factory factory ) |
| 434 | { |
| 435 | synchronized ( factories ) |
| 436 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 437 | factories.put( factory.getFactoryPidString(), factory ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 442 | // ---------- ConfigurationAdminImpl support |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 443 | |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 444 | void setDynamicBundleLocation( final String pid, final String location ) |
| 445 | { |
| 446 | if ( dynamicBindings != null ) |
| 447 | { |
| 448 | try |
| 449 | { |
| 450 | dynamicBindings.putLocation( pid, location ); |
| 451 | } |
| 452 | catch ( IOException ioe ) |
| 453 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 454 | log( LogService.LOG_ERROR, "Failed storing dynamic configuration binding for {0} to {1}", new Object[] |
| 455 | { pid, location, ioe } ); |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
| 461 | String getDynamicBundleLocation( final String pid ) |
| 462 | { |
| 463 | if ( dynamicBindings != null ) |
| 464 | { |
| 465 | return dynamicBindings.getLocation( pid ); |
| 466 | } |
| 467 | |
| 468 | return null; |
| 469 | } |
| 470 | |
| 471 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 472 | ConfigurationImpl createFactoryConfiguration( String factoryPid, String location ) throws IOException |
| 473 | { |
Felix Meschberger | b3bae58 | 2012-02-16 14:00:52 +0000 | [diff] [blame] | 474 | return cacheConfiguration( createConfiguration( createPid( factoryPid ), factoryPid, location ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 477 | /** |
| 478 | * Returns a targeted configuration for the given service PID and |
| 479 | * the reference target service. |
| 480 | * |
| 481 | * @param rawPid The raw service PID to get targeted configuration for. |
| 482 | * @param target The target <code>ServiceReference</code> to get |
| 483 | * configuration for. |
| 484 | * @return The best matching targeted configuration or <code>null</code> |
| 485 | * if there is no configuration at all. |
| 486 | * @throwss IOException if an error occurrs reading configurations |
| 487 | * from persistence. |
| 488 | */ |
| 489 | ConfigurationImpl getTargetedConfiguration( final String rawPid, final ServiceReference target ) throws IOException |
| 490 | { |
| 491 | final Bundle serviceBundle = target.getBundle(); |
| 492 | if ( serviceBundle != null ) |
| 493 | { |
| 494 | // for pre-1.5 API compatibility |
| 495 | final StringBuffer targetedPid = new StringBuffer( rawPid ); |
| 496 | int i = 3; |
| 497 | String[] names = new String[4]; |
| 498 | names[i--] = targetedPid.toString(); |
| 499 | targetedPid.append( '|' ).append( serviceBundle.getSymbolicName() ); |
| 500 | names[i--] = targetedPid.toString(); |
| 501 | targetedPid.append( '|' ).append( TargetedPID.getBundleVersion( serviceBundle ) ); |
| 502 | names[i--] = targetedPid.toString(); |
| 503 | targetedPid.append( '|' ).append( serviceBundle.getLocation() ); |
| 504 | names[i--] = targetedPid.toString(); |
| 505 | |
| 506 | for ( String candidate : names ) |
| 507 | { |
| 508 | ConfigurationImpl config = getConfiguration( candidate ); |
| 509 | if ( config != null ) |
| 510 | { |
| 511 | return config; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // service already unregistered, nothing to do really |
| 517 | return null; |
| 518 | } |
| 519 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 520 | |
Felix Meschberger | ad94987 | 2011-11-16 10:34:54 +0000 | [diff] [blame] | 521 | /** |
| 522 | * Returns the {@link ConfigurationImpl} with the given PID if |
| 523 | * available in the internal cache or from any persistence manager. |
| 524 | * Otherwise <code>null</code> is returned. |
| 525 | * |
| 526 | * @param pid The PID for which to return the configuration |
| 527 | * @return The configuration or <code>null</code> if non exists |
| 528 | * @throws IOException If an error occurrs reading from a persistence |
| 529 | * manager. |
| 530 | */ |
| 531 | ConfigurationImpl getConfiguration( String pid ) throws IOException |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 532 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 533 | ConfigurationImpl config = getCachedConfiguration( pid ); |
| 534 | if ( config != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 535 | { |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 536 | log( LogService.LOG_DEBUG, "Found cached configuration {0} bound to {1}", new Object[] |
| 537 | { pid, config.getBundleLocation() } ); |
Felix Meschberger | b3bae58 | 2012-02-16 14:00:52 +0000 | [diff] [blame] | 538 | |
| 539 | config.ensureFactoryConfigPersisted(); |
| 540 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 541 | return config; |
| 542 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 543 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 544 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 545 | for ( int i = 0; i < pmList.length; i++ ) |
| 546 | { |
| 547 | if ( pmList[i].exists( pid ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 548 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 549 | Dictionary props = pmList[i].load( pid ); |
| 550 | config = new ConfigurationImpl( this, pmList[i], props ); |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 551 | log( LogService.LOG_DEBUG, "Found existing configuration {0} bound to {1}", new Object[] |
| 552 | { pid, config.getBundleLocation() } ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 553 | return cacheConfiguration( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 556 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 557 | // neither the cache nor any persistence manager has configuration |
| 558 | return null; |
| 559 | } |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 560 | |
| 561 | |
Felix Meschberger | ad94987 | 2011-11-16 10:34:54 +0000 | [diff] [blame] | 562 | /** |
| 563 | * Creates a regular (non-factory) configuration for the given PID |
| 564 | * setting the bundle location accordingly. |
| 565 | * <p> |
| 566 | * This method assumes the configuration to not exist yet and will |
| 567 | * create it without further checking. |
| 568 | * |
| 569 | * @param pid The PID of the new configuration |
| 570 | * @param bundleLocation The location to set on the new configuration. |
| 571 | * This may be <code>null</code> to not bind the configuration |
| 572 | * yet. |
| 573 | * @return The new configuration persisted in the first persistence |
| 574 | * manager. |
| 575 | * @throws IOException If an error occurrs writing the configuration |
| 576 | * to the persistence. |
| 577 | */ |
| 578 | ConfigurationImpl createConfiguration( String pid, String bundleLocation ) throws IOException |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 579 | { |
| 580 | // check for existing (cached or persistent) configuration |
Felix Meschberger | ad94987 | 2011-11-16 10:34:54 +0000 | [diff] [blame] | 581 | ConfigurationImpl config = getConfiguration( pid ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 582 | if ( config != null ) |
| 583 | { |
| 584 | return config; |
| 585 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 586 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 587 | // else create new configuration also setting the bundle location |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 588 | // and cache the new configuration |
| 589 | config = createConfiguration( pid, null, bundleLocation ); |
| 590 | return cacheConfiguration( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | ConfigurationImpl[] listConfigurations( ConfigurationAdminImpl configurationAdmin, String filterString ) |
| 595 | throws IOException, InvalidSyntaxException |
| 596 | { |
| 597 | Filter filter = null; |
| 598 | if ( filterString != null ) |
| 599 | { |
| 600 | filter = bundleContext.createFilter( filterString ); |
| 601 | } |
| 602 | |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 603 | log( LogService.LOG_DEBUG, "Listing configurations matching {0}", new Object[] |
| 604 | { filterString } ); |
| 605 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 606 | List configList = new ArrayList(); |
| 607 | |
| 608 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 609 | for ( int i = 0; i < pmList.length; i++ ) |
| 610 | { |
| 611 | Enumeration configs = pmList[i].getDictionaries(); |
| 612 | while ( configs.hasMoreElements() ) |
| 613 | { |
| 614 | Dictionary config = ( Dictionary ) configs.nextElement(); |
| 615 | |
| 616 | // ignore non-Configuration dictionaries |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 617 | String pid = ( String ) config.get( Constants.SERVICE_PID ); |
| 618 | if ( pid == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 619 | { |
| 620 | continue; |
| 621 | } |
| 622 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 623 | // CM 1.4 / 104.13.2.3 Permission required |
| 624 | if ( !configurationAdmin.hasPermission( ( String ) config |
| 625 | .get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 626 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 627 | log( |
| 628 | LogService.LOG_DEBUG, |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 629 | "Omitting configuration {0}: No permission for bundle {1} on configuration bound to {2}", |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 630 | new Object[] |
| 631 | { config.get( Constants.SERVICE_PID ), configurationAdmin.getBundle().getLocation(), |
| 632 | config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 633 | continue; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // check filter |
| 637 | if ( filter == null || filter.match( config ) ) |
| 638 | { |
Felix Meschberger | 86a0d17 | 2007-07-04 07:15:01 +0000 | [diff] [blame] | 639 | // ensure the service.pid and returned a cached config if available |
| 640 | ConfigurationImpl cfg = getCachedConfiguration( pid ); |
| 641 | if ( cfg == null ) |
| 642 | { |
| 643 | cfg = new ConfigurationImpl( this, pmList[i], config ); |
| 644 | } |
| 645 | |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 646 | // FELIX-611: Ignore configuration objects without props |
| 647 | if ( !cfg.isNew() ) |
| 648 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 649 | log( LogService.LOG_DEBUG, "Adding configuration {0}", new Object[] |
| 650 | { config.get( Constants.SERVICE_PID ) } ); |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 651 | configList.add( cfg ); |
| 652 | } |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 653 | else |
| 654 | { |
| 655 | log( LogService.LOG_DEBUG, "Omitting configuration {0}: Is new", new Object[] |
| 656 | { config.get( Constants.SERVICE_PID ) } ); |
| 657 | } |
| 658 | } else { |
| 659 | log( LogService.LOG_DEBUG, "Omitting configuration {0}: Does not match filter", new Object[] |
| 660 | { config.get( Constants.SERVICE_PID ) } ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Felix Meschberger | 8faceff | 2007-07-04 07:19:48 +0000 | [diff] [blame] | 665 | return ( ConfigurationImpl[] ) configList.toArray( new ConfigurationImpl[configList |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 666 | .size()] ); |
| 667 | } |
| 668 | |
| 669 | |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 670 | void deleted( ConfigurationImpl config ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 671 | { |
| 672 | // remove the configuration from the cache |
| 673 | removeConfiguration( config ); |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 674 | fireConfigurationEvent( ConfigurationEvent.CM_DELETED, config.getPidString(), config.getFactoryPidString() ); |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 675 | updateThread.schedule( new DeleteConfiguration( config ) ); |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 676 | log( LogService.LOG_DEBUG, "DeleteConfiguration({0}) scheduled", new Object[] |
| 677 | { config.getPid() } ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Felix Meschberger | ce67d73 | 2009-08-20 06:26:35 +0000 | [diff] [blame] | 681 | void updated( ConfigurationImpl config, boolean fireEvent ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 682 | { |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 683 | if ( fireEvent ) |
| 684 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 685 | fireConfigurationEvent( ConfigurationEvent.CM_UPDATED, config.getPidString(), config.getFactoryPidString() ); |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 686 | } |
| 687 | updateThread.schedule( new UpdateConfiguration( config ) ); |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 688 | log( LogService.LOG_DEBUG, "UpdateConfiguration({0}) scheduled", new Object[] |
| 689 | { config.getPid() } ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 693 | void locationChanged( ConfigurationImpl config, String oldLocation ) |
| 694 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 695 | fireConfigurationEvent( ConfigurationEvent.CM_LOCATION_CHANGED, config.getPidString(), config.getFactoryPidString() ); |
Felix Meschberger | 35658d5 | 2011-10-20 21:43:58 +0000 | [diff] [blame] | 696 | if ( oldLocation != null && !config.isNew() ) |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 697 | { |
| 698 | updateThread.schedule( new LocationChanged( config, oldLocation ) ); |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 699 | log( LogService.LOG_DEBUG, "LocationChanged({0}, {1}=>{2}) scheduled", new Object[] |
| 700 | { config.getPid(), oldLocation, config.getBundleLocation() } ); |
Felix Meschberger | 35658d5 | 2011-10-20 21:43:58 +0000 | [diff] [blame] | 701 | } |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 702 | else |
Felix Meschberger | 35658d5 | 2011-10-20 21:43:58 +0000 | [diff] [blame] | 703 | { |
| 704 | log( LogService.LOG_DEBUG, |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 705 | "LocationChanged not scheduled for {0} (old location is null or configuration is new)", new Object[] |
| 706 | { config.getPid() } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 711 | void fireConfigurationEvent( int type, String pid, String factoryPid ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 712 | { |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 713 | FireConfigurationEvent event = new FireConfigurationEvent( type, pid, factoryPid ); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 714 | event.fireSynchronousEvents(); |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 715 | if ( event.hasConfigurationEventListeners() ) |
| 716 | { |
Felix Meschberger | b68d1cf | 2010-08-26 08:46:36 +0000 | [diff] [blame] | 717 | eventThread.schedule( event ); |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 718 | } |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 719 | else |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 720 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 721 | log( LogService.LOG_DEBUG, "No ConfigurationListeners to send {0} event to.", new Object[] |
| 722 | { event.getTypeName() } ); |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 723 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
| 727 | // ---------- BundleListener ----------------------------------------------- |
| 728 | |
| 729 | public void bundleChanged( BundleEvent event ) |
| 730 | { |
Felix Meschberger | 2c2a5be | 2009-03-26 16:47:01 +0000 | [diff] [blame] | 731 | if ( event.getType() == BundleEvent.UNINSTALLED && handleBundleEvents ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 732 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 733 | final String location = event.getBundle().getLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 734 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 735 | // we only reset dynamic bindings, which are only present in |
| 736 | // cached configurations, hence only consider cached configs here |
| 737 | final ConfigurationImpl[] configs = getCachedConfigurations(); |
| 738 | for ( int i = 0; i < configs.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 739 | { |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 740 | final ConfigurationImpl cfg = configs[i]; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 741 | if ( location.equals( cfg.getDynamicBundleLocation() ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 742 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 743 | cfg.setDynamicBundleLocation( null, true ); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 744 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 745 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | |
| 750 | // ---------- internal ----------------------------------------------------- |
| 751 | |
| 752 | private PersistenceManager[] getPersistenceManagers() |
| 753 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 754 | int currentPmtCount = persistenceManagerTracker.getTrackingCount(); |
| 755 | if ( persistenceManagers == null || currentPmtCount > pmtCount ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 756 | { |
| 757 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 758 | List pmList = new ArrayList(); |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 759 | PersistenceManager[] pm; |
| 760 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 761 | ServiceReference[] refs = persistenceManagerTracker.getServiceReferences(); |
| 762 | if ( refs == null || refs.length == 0 ) |
| 763 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 764 | pm = new PersistenceManager[0]; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 765 | } |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 766 | else |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 767 | { |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 768 | // sort the references according to the cmRanking property |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 769 | if ( refs.length > 1 ) |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 770 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 771 | Arrays.sort( refs, RankingComparator.SRV_RANKING ); |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // create the service array from the sorted set of referenecs |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 775 | for ( int i = 0; i < refs.length; i++ ) |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 776 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 777 | Object service = persistenceManagerTracker.getService( refs[i] ); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 778 | if ( service != null ) |
| 779 | { |
Felix Meschberger | a86cdfc | 2010-08-25 09:32:36 +0000 | [diff] [blame] | 780 | pmList.add( new CachingPersistenceManagerProxy( ( PersistenceManager ) service ) ); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 781 | } |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 782 | } |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 783 | |
| 784 | pm = ( PersistenceManager[] ) pmList.toArray( new PersistenceManager[pmList.size()] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 787 | pmtCount = pm.length; |
Felix Meschberger | d71f410 | 2007-07-06 15:34:32 +0000 | [diff] [blame] | 788 | persistenceManagers = pm; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return persistenceManagers; |
| 792 | } |
| 793 | |
| 794 | |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 795 | private ServiceReference getServiceReference() |
| 796 | { |
| 797 | ServiceRegistration reg = configurationAdminRegistration; |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 798 | if (reg != null) { |
| 799 | return reg.getReference(); |
| 800 | } |
| 801 | |
| 802 | // probably called for firing an event during service registration |
| 803 | // since we didn't get the service registration yet we use the |
| 804 | // service registry to get our service reference |
| 805 | BundleContext context = bundleContext; |
| 806 | if ( context != null ) |
| 807 | { |
| 808 | try |
| 809 | { |
| 810 | ServiceReference[] refs = context.getServiceReferences( ConfigurationAdmin.class.getName(), null ); |
| 811 | if ( refs != null ) |
| 812 | { |
| 813 | for ( int i = 0; i < refs.length; i++ ) |
| 814 | { |
| 815 | if ( refs[i].getBundle().getBundleId() == context.getBundle().getBundleId() ) |
| 816 | { |
| 817 | return refs[i]; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | catch ( InvalidSyntaxException e ) |
| 823 | { |
| 824 | // unexpected since there is no filter |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // service references |
| 829 | return null; |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
Felix Meschberger | 05d89e1 | 2011-11-03 23:37:10 +0000 | [diff] [blame] | 833 | /** |
| 834 | * Configures the ManagedService and returns the service.pid |
| 835 | * service property as a String[], which may be <code>null</code> if |
| 836 | * the ManagedService does not have such a property. |
| 837 | */ |
Felix Meschberger | 05d89e1 | 2011-11-03 23:37:10 +0000 | [diff] [blame] | 838 | /** |
| 839 | * Configures the ManagedServiceFactory and returns the service.pid |
| 840 | * service property as a String[], which may be <code>null</code> if |
| 841 | * the ManagedServiceFactory does not have such a property. |
| 842 | */ |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 843 | /** |
| 844 | * Schedules the configuration of the referenced service with |
| 845 | * configuration for the given PID. |
| 846 | * |
| 847 | * @param pid The service PID of the configuration to be provided |
| 848 | * to the referenced service. |
| 849 | * @param sr The <code>ServiceReference</code> to the service |
| 850 | * to be configured. |
| 851 | * @param factory <code>true</code> If the service is considered to |
| 852 | * be a <code>ManagedServiceFactory</code>. Otherwise the service |
| 853 | * is considered to be a <code>ManagedService</code>. |
| 854 | */ |
| 855 | public void configure( String pid, ServiceReference sr, final boolean factory ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 856 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 857 | Runnable r; |
| 858 | if ( factory ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 859 | { |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 860 | r = new ManagedServiceFactoryUpdate( pid, sr ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 861 | } |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 862 | else |
| 863 | { |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 864 | r = new ManagedServiceUpdate( pid, sr ); |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 865 | } |
| 866 | updateThread.schedule( r ); |
| 867 | log( LogService.LOG_DEBUG, "[{0}] scheduled", new Object[] |
| 868 | { r } ); |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame] | 869 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 870 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 871 | |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 872 | /** |
| 873 | * Factory method to create a new configuration object. The configuration |
| 874 | * object returned is not stored in configuration cache and only persisted |
| 875 | * if the <code>factoryPid</code> parameter is <code>null</code>. |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 876 | * |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 877 | * @param pid |
| 878 | * The PID of the new configuration object. Must not be |
| 879 | * <code>null</code>. |
| 880 | * @param factoryPid |
| 881 | * The factory PID of the new configuration. Not |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 882 | * <code>null</code> if the new configuration object belongs to a |
| 883 | * factory. The configuration object will not be persisted if |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 884 | * this parameter is not <code>null</code>. |
| 885 | * @param bundleLocation |
| 886 | * The bundle location of the bundle to which the configuration |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 887 | * belongs or <code>null</code> if the configuration is not bound |
| 888 | * yet. |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 889 | * @return The new configuration object |
| 890 | * @throws IOException |
| 891 | * May be thrown if an error occurrs persisting the new |
| 892 | * configuration object. |
| 893 | */ |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 894 | ConfigurationImpl createConfiguration( String pid, String factoryPid, String bundleLocation ) throws IOException |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 895 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 896 | log( LogService.LOG_DEBUG, "createConfiguration({0}, {1}, {2})", new Object[] |
| 897 | { pid, factoryPid, bundleLocation } ); |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 898 | return new ConfigurationImpl( this, getPersistenceManagers()[0], pid, factoryPid, bundleLocation ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 902 | /** |
| 903 | * Returns a list of {@link Factory} instances according to the |
| 904 | * Configuration Admin 1.5 specification for targeted PIDs (Section |
| 905 | * 104.3.2) |
| 906 | * |
| 907 | * @param rawFactoryPid The raw factory PID without any targetting. |
| 908 | * @param target The <code>ServiceReference</code> of the service to |
| 909 | * be supplied with targeted configuration. |
| 910 | * @return A list of {@link Factory} instances as listed above. This |
| 911 | * list will always at least include an instance for the |
| 912 | * <code>rawFactoryPid</code>. Other instances are only included |
| 913 | * if existing. |
| 914 | * @throws IOException If an error occurrs reading any of the |
| 915 | * {@link Factory} instances from persistence |
| 916 | */ |
| 917 | List<Factory> getTargetedFactories( final String rawFactoryPid, final ServiceReference target ) throws IOException |
| 918 | { |
| 919 | LinkedList<Factory> factories = new LinkedList<Factory>(); |
| 920 | |
| 921 | final Bundle serviceBundle = target.getBundle(); |
| 922 | if ( serviceBundle != null ) |
| 923 | { |
| 924 | // for pre-1.5 API compatibility |
| 925 | final StringBuffer targetedPid = new StringBuffer( rawFactoryPid ); |
| 926 | factories.add( getOrCreateFactory( targetedPid.toString() ) ); |
| 927 | |
| 928 | targetedPid.append( '|' ).append( serviceBundle.getSymbolicName() ); |
| 929 | Factory f = getFactory( targetedPid.toString() ); |
| 930 | if ( f != null ) |
| 931 | { |
| 932 | factories.add( 0, f ); |
| 933 | } |
| 934 | |
| 935 | targetedPid.append( '|' ).append( TargetedPID.getBundleVersion( serviceBundle ) ); |
| 936 | f = getFactory( targetedPid.toString() ); |
| 937 | if ( f != null ) |
| 938 | { |
| 939 | factories.add( 0, f ); |
| 940 | } |
| 941 | |
| 942 | targetedPid.append( '|' ).append( serviceBundle.getLocation() ); |
| 943 | f = getFactory( targetedPid.toString() ); |
| 944 | if ( f != null ) |
| 945 | { |
| 946 | factories.add( 0, f ); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | return factories; |
| 951 | } |
| 952 | |
| 953 | |
| 954 | /** |
| 955 | * Gets the factory with the exact identifier from the cached or from |
| 956 | * the persistence managers. If no factory exists already one is |
| 957 | * created and cached. |
| 958 | * |
| 959 | * @param factoryPid The PID of the {@link Factory} to return |
| 960 | * @return The existing or newly created {@link Factory} |
| 961 | * @throws IOException If an error occurrs reading the factory from |
| 962 | * a {@link PersistenceManager} |
| 963 | */ |
| 964 | Factory getOrCreateFactory( String factoryPid ) throws IOException |
| 965 | { |
| 966 | Factory factory = getFactory( factoryPid ); |
| 967 | if ( factory != null ) |
| 968 | { |
| 969 | return factory; |
| 970 | } |
| 971 | |
| 972 | return createFactory( factoryPid ); |
| 973 | } |
| 974 | |
| 975 | |
| 976 | /** |
| 977 | * Gets the factory with the exact identifier from the cached or from |
| 978 | * the persistence managers. If no factory exists <code>null</code> |
| 979 | * is returned. |
| 980 | * |
| 981 | * @param factoryPid The PID of the {@link Factory} to return |
| 982 | * @return The existing {@link Factory} or <code>null</code> |
| 983 | * @throws IOException If an error occurrs reading the factory from |
| 984 | * a {@link PersistenceManager} |
| 985 | */ |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 986 | Factory getFactory( String factoryPid ) throws IOException |
| 987 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 988 | // check for cached factory |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 989 | Factory factory = getCachedFactory( factoryPid ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 990 | if ( factory != null ) |
| 991 | { |
| 992 | return factory; |
| 993 | } |
| 994 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 995 | // try to load factory from persistence |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 996 | PersistenceManager[] pmList = getPersistenceManagers(); |
| 997 | for ( int i = 0; i < pmList.length; i++ ) |
| 998 | { |
| 999 | if ( Factory.exists( pmList[i], factoryPid ) ) |
| 1000 | { |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 1001 | factory = Factory.load( this, pmList[i], factoryPid ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1002 | cacheFactory( factory ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1003 | return factory; |
| 1004 | } |
| 1005 | } |
| 1006 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1007 | // no existing factory |
| 1008 | return null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1012 | /** |
| 1013 | * Creates a new factory with the given <code>factoryPid</code>. |
| 1014 | */ |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1015 | Factory createFactory( String factoryPid ) |
| 1016 | { |
Felix Meschberger | 3f9e4da | 2009-08-17 07:52:39 +0000 | [diff] [blame] | 1017 | Factory factory = new Factory( this, getPersistenceManagers()[0], factoryPid ); |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 1018 | cacheFactory( factory ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1019 | return factory; |
| 1020 | } |
| 1021 | |
| 1022 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1023 | /** |
| 1024 | * Calls the registered configuration plugins on the given configuration |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1025 | * properties from the given configuration object unless the configuration |
| 1026 | * has just been created and not been updated yet. |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1027 | * |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1028 | * @param props The configuraiton properties run through the registered |
| 1029 | * ConfigurationPlugin services. This may be <code>null</code> |
| 1030 | * in which case this method just immediately returns. |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1031 | * @param targetPid The identification of the configuration update used to |
| 1032 | * select the plugins according to their cm.target service |
| 1033 | * property |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1034 | * @param sr The service reference of the managed service (factory) which |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1035 | * is to be updated with configuration |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1036 | * @param cfg The configuration object whose properties have to be passed |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1037 | * through the plugins |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1038 | */ |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1039 | public void callPlugins( final Dictionary props, final String targetPid, final ServiceReference sr, |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1040 | final ConfigurationImpl cfg ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1041 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1042 | // guard against NPE for new configuration never updated |
| 1043 | if (props == null) { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1044 | return; |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1045 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1046 | |
| 1047 | ServiceReference[] plugins = null; |
| 1048 | try |
| 1049 | { |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1050 | String filter = "(|(!(cm.target=*))(cm.target=" + targetPid + "))"; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1051 | plugins = bundleContext.getServiceReferences( ConfigurationPlugin.class.getName(), filter ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1052 | } |
| 1053 | catch ( InvalidSyntaxException ise ) |
| 1054 | { |
| 1055 | // no filter, no exception ... |
| 1056 | } |
| 1057 | |
| 1058 | // abort early if there are no plugins |
| 1059 | if ( plugins == null || plugins.length == 0 ) |
| 1060 | { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1061 | return; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // sort the plugins by their service.cmRanking |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1065 | if ( plugins.length > 1 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1066 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1067 | Arrays.sort( plugins, RankingComparator.CM_RANKING ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // call the plugins in order |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1071 | for ( int i = 0; i < plugins.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1072 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1073 | ServiceReference pluginRef = plugins[i]; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1074 | ConfigurationPlugin plugin = ( ConfigurationPlugin ) bundleContext.getService( pluginRef ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1075 | if ( plugin != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1076 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1077 | try |
| 1078 | { |
| 1079 | plugin.modifyConfiguration( sr, props ); |
| 1080 | } |
| 1081 | catch ( Throwable t ) |
| 1082 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1083 | log( LogService.LOG_ERROR, "Unexpected problem calling configuration plugin {0}", new Object[] |
| 1084 | { toString( pluginRef ), t } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1085 | } |
| 1086 | finally |
| 1087 | { |
| 1088 | // ensure ungetting the plugin |
| 1089 | bundleContext.ungetService( pluginRef ); |
| 1090 | } |
| 1091 | cfg.setAutoProperties( props, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1092 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1093 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | /** |
| 1098 | * Creates a PID for the given factoryPid |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1099 | * |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1100 | * @param factoryPid |
| 1101 | * @return |
| 1102 | */ |
| 1103 | private static String createPid( String factoryPid ) |
| 1104 | { |
Felix Meschberger | 417f66c | 2011-02-04 11:25:23 +0000 | [diff] [blame] | 1105 | Random ng = numberGenerator; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1106 | if ( ng == null ) |
| 1107 | { |
Felix Meschberger | 417f66c | 2011-02-04 11:25:23 +0000 | [diff] [blame] | 1108 | // FELIX-2771 Secure Random not available on Mika |
| 1109 | try |
| 1110 | { |
| 1111 | ng = new SecureRandom(); |
| 1112 | } |
| 1113 | catch ( Throwable t ) |
| 1114 | { |
| 1115 | // fall back to Random |
| 1116 | ng = new Random(); |
| 1117 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | byte[] randomBytes = new byte[16]; |
| 1121 | ng.nextBytes( randomBytes ); |
| 1122 | randomBytes[6] &= 0x0f; /* clear version */ |
| 1123 | randomBytes[6] |= 0x40; /* set to version 4 */ |
| 1124 | randomBytes[8] &= 0x3f; /* clear variant */ |
| 1125 | randomBytes[8] |= 0x80; /* set to IETF variant */ |
| 1126 | |
| 1127 | StringBuffer buf = new StringBuffer( factoryPid.length() + 1 + 36 ); |
| 1128 | |
| 1129 | // prefix the new pid with the factory pid |
| 1130 | buf.append( factoryPid ).append( "." ); |
| 1131 | |
| 1132 | // serialize the UUID into the buffer |
| 1133 | for ( int i = 0; i < randomBytes.length; i++ ) |
| 1134 | { |
| 1135 | |
| 1136 | if ( i == 4 || i == 6 || i == 8 || i == 10 ) |
| 1137 | { |
| 1138 | buf.append( '-' ); |
| 1139 | } |
| 1140 | |
| 1141 | int val = randomBytes[i] & 0xff; |
| 1142 | buf.append( Integer.toHexString( val >> 4 ) ); |
| 1143 | buf.append( Integer.toHexString( val & 0xf ) ); |
| 1144 | } |
| 1145 | |
| 1146 | return buf.toString(); |
| 1147 | } |
| 1148 | |
| 1149 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1150 | public boolean isLogEnabled( int level ) |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1151 | { |
| 1152 | return level <= logLevel; |
| 1153 | } |
| 1154 | |
| 1155 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1156 | public void log( int level, String format, Object[] args ) |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1157 | { |
| 1158 | if ( isLogEnabled( level ) ) |
| 1159 | { |
| 1160 | Throwable throwable = null; |
| 1161 | String message = format; |
| 1162 | |
| 1163 | if ( args != null && args.length > 0 ) |
| 1164 | { |
| 1165 | if ( args[args.length - 1] instanceof Throwable ) |
| 1166 | { |
| 1167 | throwable = ( Throwable ) args[args.length - 1]; |
| 1168 | } |
| 1169 | message = MessageFormat.format( format, args ); |
| 1170 | } |
| 1171 | |
| 1172 | log( level, message, throwable ); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1177 | public void log( int level, String message, Throwable t ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1178 | { |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1179 | // log using the LogService if available |
Felix Meschberger | 85b355d | 2007-08-31 07:17:38 +0000 | [diff] [blame] | 1180 | Object log = logTracker.getService(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1181 | if ( log != null ) |
| 1182 | { |
Felix Meschberger | b4f83e4 | 2009-01-15 08:53:36 +0000 | [diff] [blame] | 1183 | ( ( LogService ) log ).log( getServiceReference(), level, message, t ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1187 | // Otherwise only log if more serious than the configured level |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1188 | if ( isLogEnabled( level ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1189 | { |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1190 | String code; |
| 1191 | switch ( level ) |
| 1192 | { |
| 1193 | case LogService.LOG_INFO: |
| 1194 | code = "*INFO *"; |
| 1195 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1196 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1197 | case LogService.LOG_WARNING: |
| 1198 | code = "*WARN *"; |
| 1199 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1200 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1201 | case LogService.LOG_ERROR: |
| 1202 | code = "*ERROR*"; |
| 1203 | break; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1204 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1205 | case LogService.LOG_DEBUG: |
| 1206 | default: |
| 1207 | code = "*DEBUG*"; |
| 1208 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1209 | |
Felix Meschberger | 08282c3 | 2009-01-28 07:01:55 +0000 | [diff] [blame] | 1210 | System.err.println( code + " " + message ); |
| 1211 | if ( t != null ) |
| 1212 | { |
| 1213 | t.printStackTrace( System.err ); |
| 1214 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Felix Meschberger | 851c641 | 2009-08-16 18:43:26 +0000 | [diff] [blame] | 1218 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1219 | public static String toString( ServiceReference ref ) |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1220 | { |
| 1221 | String[] ocs = ( String[] ) ref.getProperty( "objectClass" ); |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1222 | StringBuffer buf = new StringBuffer( "[" ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1223 | for ( int i = 0; i < ocs.length; i++ ) |
| 1224 | { |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1225 | buf.append( ocs[i] ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1226 | if ( i < ocs.length - 1 ) |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1227 | buf.append( ", " ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Felix Meschberger | dc8a553 | 2011-02-18 13:11:03 +0000 | [diff] [blame] | 1230 | buf.append( ", id=" ).append( ref.getProperty( Constants.SERVICE_ID ) ); |
| 1231 | |
| 1232 | Bundle provider = ref.getBundle(); |
| 1233 | if ( provider != null ) |
| 1234 | { |
| 1235 | buf.append( ", bundle=" ).append( provider.getBundleId() ); |
Felix Meschberger | 0135b32 | 2011-11-16 12:30:22 +0000 | [diff] [blame] | 1236 | buf.append( '/' ).append( provider.getLocation() ); |
Felix Meschberger | dc8a553 | 2011-02-18 13:11:03 +0000 | [diff] [blame] | 1237 | } |
| 1238 | else |
| 1239 | { |
| 1240 | buf.append( ", unregistered" ); |
| 1241 | } |
| 1242 | |
| 1243 | buf.append( "]" ); |
| 1244 | return buf.toString(); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Felix Meschberger | ce67d73 | 2009-08-20 06:26:35 +0000 | [diff] [blame] | 1247 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1248 | /** |
| 1249 | * Checks whether the bundle is allowed to receive the configuration |
| 1250 | * with the given location binding. |
| 1251 | * <p> |
| 1252 | * This method implements the logic defined CM 1.4 / 104.4.1: |
| 1253 | * <ul> |
| 1254 | * <li>If the location is <code>null</code> (the configuration is not |
| 1255 | * bound yet), assume the bundle is allowed</li> |
| 1256 | * <li>If the location is a single location (no leading "?"), require |
| 1257 | * the bundle's location to match</li> |
| 1258 | * <li>If the location is a multi-location (leading "?"), assume the |
| 1259 | * bundle is allowed if there is no security manager. If there is a |
| 1260 | * security manager, check whether the bundle has "target" permission |
| 1261 | * on this location.</li> |
| 1262 | * </ul> |
| 1263 | */ |
| 1264 | boolean canReceive( final Bundle bundle, final String location ) |
| 1265 | { |
| 1266 | if ( location == null ) |
| 1267 | { |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1268 | log( LogService.LOG_DEBUG, "canReceive=true; bundle={0}; configuration=(unbound)", new Object[] |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1269 | { bundle.getLocation() } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1270 | return true; |
| 1271 | } |
| 1272 | else if ( location.startsWith( "?" ) ) |
| 1273 | { |
| 1274 | // multi-location |
| 1275 | if ( System.getSecurityManager() != null ) |
| 1276 | { |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1277 | final boolean hasPermission = bundle.hasPermission( new ConfigurationPermission( location, |
| 1278 | ConfigurationPermission.TARGET ) ); |
| 1279 | log( LogService.LOG_DEBUG, "canReceive={0}: bundle={1}; configuration={2} (SecurityManager check)", |
| 1280 | new Object[] |
| 1281 | { new Boolean( hasPermission ), bundle.getLocation(), location } ); |
| 1282 | return hasPermission; |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1283 | } |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1284 | |
| 1285 | log( LogService.LOG_DEBUG, "canReceive=true; bundle={0}; configuration={1} (no SecurityManager)", |
| 1286 | new Object[] |
| 1287 | { bundle.getLocation(), location } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1288 | return true; |
| 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | // single location, must match |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1293 | final boolean hasPermission = location.equals( bundle.getLocation() ); |
| 1294 | log( LogService.LOG_DEBUG, "canReceive={0}: bundle={1}; configuration={2}", new Object[] |
| 1295 | { new Boolean( hasPermission ), bundle.getLocation(), location } ); |
| 1296 | return hasPermission; |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1300 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1301 | // ---------- inner classes |
| 1302 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1303 | /** |
| 1304 | * The <code>ManagedServiceUpdate</code> updates a freshly registered |
| 1305 | * <code>ManagedService</code> with a specific configuration. If a |
| 1306 | * ManagedService is registered with multiple PIDs an instance of this |
| 1307 | * class is used for each registered PID. |
| 1308 | */ |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1309 | private class ManagedServiceUpdate implements Runnable |
| 1310 | { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1311 | private final String pid; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1312 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1313 | private final ServiceReference sr; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1314 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1315 | private final ConfigurationImpl config; |
| 1316 | |
| 1317 | private final Dictionary rawProperties; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1318 | |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1319 | private final long revision; |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1320 | |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1321 | ManagedServiceUpdate( String pid, ServiceReference sr ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1322 | { |
| 1323 | this.pid = pid; |
| 1324 | this.sr = sr; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1325 | |
| 1326 | // get or load configuration for the pid |
| 1327 | ConfigurationImpl config = null; |
| 1328 | Dictionary rawProperties = null; |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1329 | long revision = -1; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1330 | try |
| 1331 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1332 | config = getTargetedConfiguration( pid, sr ); |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1333 | if ( config != null ) |
| 1334 | { |
| 1335 | synchronized ( config ) |
| 1336 | { |
| 1337 | rawProperties = config.getProperties( true ); |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1338 | revision = config.getRevision(); |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1339 | } |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | catch ( IOException ioe ) |
| 1343 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1344 | log( LogService.LOG_ERROR, "Error loading configuration for {0}", new Object[] |
| 1345 | { pid, ioe } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | this.config = config; |
| 1349 | this.rawProperties = rawProperties; |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1350 | this.revision = revision; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | public void run() |
| 1355 | { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1356 | Dictionary properties = rawProperties; |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1357 | |
| 1358 | // check configuration and call plugins if existing |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1359 | if ( config != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1360 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1361 | log( LogService.LOG_DEBUG, "Updating service {0} to with configuration {1}@{2}", new Object[] |
| 1362 | { pid, this.config.getPid(), new Long( revision ) } ); |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1363 | |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1364 | Bundle serviceBundle = sr.getBundle(); |
| 1365 | if ( serviceBundle == null ) |
| 1366 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1367 | log( LogService.LOG_INFO, |
| 1368 | "Service for PID {0} seems to already have been unregistered, not updating with configuration", |
| 1369 | new Object[] |
| 1370 | { pid } ); |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1371 | return; |
| 1372 | } |
| 1373 | |
Felix Meschberger | 27689c1 | 2011-11-16 08:52:04 +0000 | [diff] [blame] | 1374 | if ( canReceive( serviceBundle, config.getBundleLocation() ) ) |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1375 | { |
Felix Meschberger | 27689c1 | 2011-11-16 08:52:04 +0000 | [diff] [blame] | 1376 | // 104.4.2 Dynamic Binding |
| 1377 | config.tryBindLocation( serviceBundle.getLocation() ); |
Felix Meschberger | 27689c1 | 2011-11-16 08:52:04 +0000 | [diff] [blame] | 1378 | } |
| 1379 | else |
| 1380 | { |
| 1381 | // CM 1.4 / 104.13.2.2 / 104.5.3 |
| 1382 | // act as if there is no configuration |
| 1383 | log( |
| 1384 | LogService.LOG_DEBUG, |
| 1385 | "Cannot use configuration {0} for {1}: No visibility to configuration bound to {2}; calling with null", |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1386 | new Object[] |
| 1387 | { pid, ConfigurationManager.toString( sr ), config.getBundleLocation() } ); |
Felix Meschberger | 27689c1 | 2011-11-16 08:52:04 +0000 | [diff] [blame] | 1388 | |
| 1389 | // CM 1.4 / 104.5.3 ManagedService.updated must be |
| 1390 | // called with null if configuration is no visible |
| 1391 | properties = null; |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1394 | } |
| 1395 | else |
| 1396 | { |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1397 | // 104.5.3 ManagedService.updated must be called with null |
| 1398 | // if no configuration is available |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1399 | properties = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1402 | managedServiceTracker.provideConfiguration( sr, config, properties ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1403 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1404 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1405 | public String toString() |
| 1406 | { |
| 1407 | return "ManagedService Update: pid=" + pid; |
| 1408 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1411 | /** |
| 1412 | * The <code>ManagedServiceFactoryUpdate</code> updates a freshly |
| 1413 | * registered <code>ManagedServiceFactory</code> with a specific |
| 1414 | * configuration. If a ManagedServiceFactory is registered with |
| 1415 | * multiple PIDs an instance of this class is used for each registered |
| 1416 | * PID. |
| 1417 | */ |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1418 | private class ManagedServiceFactoryUpdate implements Runnable |
| 1419 | { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1420 | private final String factoryPid; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1421 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1422 | private final ServiceReference sr; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1423 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1424 | private final Map configs; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1425 | |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1426 | private final Map revisions; |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1427 | |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1428 | ManagedServiceFactoryUpdate( String factoryPid, ServiceReference sr ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1429 | { |
| 1430 | this.factoryPid = factoryPid; |
| 1431 | this.sr = sr; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1432 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1433 | List<Factory> factories = null; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1434 | Map configs = null; |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1435 | Map revisions = null; |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1436 | try |
| 1437 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1438 | factories = getTargetedFactories( factoryPid, sr ); |
| 1439 | for (Factory factory : factories) { |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1440 | configs = new HashMap(); |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1441 | revisions = new HashMap(); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1442 | for ( Iterator pi = factory.getPIDs().iterator(); pi.hasNext(); ) |
| 1443 | { |
| 1444 | final String pid = ( String ) pi.next(); |
| 1445 | ConfigurationImpl cfg; |
| 1446 | try |
| 1447 | { |
Felix Meschberger | ad94987 | 2011-11-16 10:34:54 +0000 | [diff] [blame] | 1448 | cfg = getConfiguration( pid ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1449 | } |
| 1450 | catch ( IOException ioe ) |
| 1451 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1452 | log( LogService.LOG_ERROR, "Error loading configuration for {0}", new Object[] |
| 1453 | { pid, ioe } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1454 | continue; |
| 1455 | } |
| 1456 | |
| 1457 | // sanity check on the configuration |
| 1458 | if ( cfg == null ) |
| 1459 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1460 | log( LogService.LOG_ERROR, "Configuration {0} referred to by factory {1} does not exist", |
| 1461 | new Object[] |
| 1462 | { pid, factoryPid } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1463 | factory.removePID( pid ); |
| 1464 | factory.storeSilently(); |
| 1465 | continue; |
| 1466 | } |
| 1467 | else if ( cfg.isNew() ) |
| 1468 | { |
| 1469 | // Configuration has just been created but not yet updated |
| 1470 | // we currently just ignore it and have the update mechanism |
| 1471 | // provide the configuration to the ManagedServiceFactory |
| 1472 | // As of FELIX-612 (not storing new factory configurations) |
| 1473 | // this should not happen. We keep this for added stability |
| 1474 | // but raise the logging level to error. |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1475 | log( LogService.LOG_ERROR, "Ignoring new configuration pid={0}", new Object[] |
| 1476 | { pid } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1477 | continue; |
| 1478 | } |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1479 | /* |
| 1480 | * this code would catch targeted factory PIDs; |
| 1481 | * since this is not expected any way, we can |
| 1482 | * leave this out |
| 1483 | */ |
| 1484 | /* |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1485 | else if ( !factoryPid.equals( cfg.getFactoryPid() ) ) |
| 1486 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1487 | log( LogService.LOG_ERROR, |
| 1488 | "Configuration {0} referred to by factory {1} seems to belong to factory {2}", |
| 1489 | new Object[] |
| 1490 | { pid, factoryPid, cfg.getFactoryPid() } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1491 | factory.removePID( pid ); |
| 1492 | factory.storeSilently(); |
| 1493 | continue; |
| 1494 | } |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1495 | */ |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1496 | |
| 1497 | // get the configuration properties for later |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1498 | synchronized ( cfg ) |
| 1499 | { |
| 1500 | configs.put( cfg, cfg.getProperties( true ) ); |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1501 | revisions.put( cfg, new Long( cfg.getRevision() ) ); |
Felix Meschberger | cb46b9e | 2009-08-31 14:33:15 +0000 | [diff] [blame] | 1502 | } |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | catch ( IOException ioe ) |
| 1507 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1508 | log( LogService.LOG_ERROR, "Cannot get factory mapping for factory PID {0}", new Object[] |
| 1509 | { factoryPid, ioe } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1512 | this.configs = configs; |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1513 | this.revisions = revisions; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | public void run() |
| 1518 | { |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1519 | Bundle serviceBundle = sr.getBundle(); |
| 1520 | if ( serviceBundle == null ) |
| 1521 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1522 | log( |
| 1523 | LogService.LOG_INFO, |
| 1524 | "ManagedServiceFactory for factory PID {0} seems to already have been unregistered, not updating with factory", |
| 1525 | new Object[] |
| 1526 | { factoryPid } ); |
Felix Meschberger | 4124319 | 2009-01-14 19:59:58 +0000 | [diff] [blame] | 1527 | return; |
| 1528 | } |
| 1529 | |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1530 | if ( configs == null || configs.isEmpty() ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1531 | { |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1532 | log( LogService.LOG_DEBUG, "No configuration with factory PID {0}; not updating ManagedServiceFactory", |
| 1533 | new Object[] |
| 1534 | { factoryPid } ); |
| 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | for ( Iterator ci = configs.entrySet().iterator(); ci.hasNext(); ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1539 | { |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1540 | final Map.Entry entry = ( Map.Entry ) ci.next(); |
| 1541 | final ConfigurationImpl cfg = ( ConfigurationImpl ) entry.getKey(); |
| 1542 | final Dictionary properties = ( Dictionary ) entry.getValue(); |
| 1543 | final long revision = ( ( Long ) revisions.get( cfg ) ).longValue(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1544 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1545 | log( LogService.LOG_DEBUG, "Updating service {0} with configuration {1}/{2}@{3}", new Object[] |
| 1546 | { this.factoryPid, cfg.getFactoryPid(), cfg.getPid(), new Long( revision ) } ); |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1547 | |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1548 | // CM 1.4 / 104.13.2.1 |
| 1549 | if ( !canReceive( serviceBundle, cfg.getBundleLocation() ) ) |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1550 | { |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1551 | log( LogService.LOG_ERROR, |
| 1552 | "Cannot use configuration {0} for {1}: No visibility to configuration bound to {2}", |
| 1553 | new Object[] |
| 1554 | { cfg.getPid(), ConfigurationManager.toString( sr ), cfg.getBundleLocation() } ); |
| 1555 | continue; |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 1556 | } |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1557 | |
| 1558 | // 104.4.2 Dynamic Binding |
| 1559 | cfg.tryBindLocation( serviceBundle.getLocation() ); |
| 1560 | |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1561 | // update the service with the configuration (if non-null) |
| 1562 | if ( properties != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1563 | { |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 1564 | log( LogService.LOG_DEBUG, "{0}: Updating configuration pid={1}", new Object[] |
| 1565 | { ConfigurationManager.toString( sr ), cfg.getPid() } ); |
Felix Meschberger | 382a19b | 2012-07-03 09:45:14 +0000 | [diff] [blame] | 1566 | managedServiceFactoryTracker.provideConfiguration( sr, cfg, properties ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1567 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1571 | |
| 1572 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1573 | public String toString() |
| 1574 | { |
| 1575 | return "ManagedServiceFactory Update: factoryPid=" + factoryPid; |
| 1576 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1579 | private abstract class ConfigurationProvider<T> implements Runnable |
| 1580 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1581 | |
| 1582 | protected final ConfigurationImpl config; |
| 1583 | protected final long revision; |
| 1584 | protected final Dictionary<String, ?> properties; |
| 1585 | protected final BaseTracker<T> helper; |
| 1586 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1587 | |
| 1588 | protected ConfigurationProvider( final ConfigurationImpl config ) |
| 1589 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1590 | synchronized ( config ) |
| 1591 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1592 | this.config = config; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1593 | this.revision = config.getRevision(); |
| 1594 | this.properties = config.getProperties( true ); |
| 1595 | } |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1596 | this.helper = ( BaseTracker<T> ) ( ( config.getFactoryPid() == null ) ? managedServiceTracker |
| 1597 | : managedServiceFactoryTracker ); |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1600 | |
| 1601 | protected TargetedPID getTargetedServicePid() |
| 1602 | { |
| 1603 | final TargetedPID factoryPid = this.config.getFactoryPid(); |
| 1604 | if ( factoryPid != null ) |
| 1605 | { |
| 1606 | return factoryPid; |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1607 | } |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1608 | return this.config.getPid(); |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1609 | } |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1610 | } |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1611 | |
| 1612 | /** |
| 1613 | * The <code>UpdateConfiguration</code> is used to update |
| 1614 | * <code>ManagedService[Factory]</code> services with the configuration |
| 1615 | * they are subscribed to. This may cause the configuration to be |
| 1616 | * supplied to multiple services. |
| 1617 | */ |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1618 | private class UpdateConfiguration extends ConfigurationProvider |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1619 | { |
| 1620 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1621 | UpdateConfiguration( final ConfigurationImpl config ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1622 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1623 | super( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | public void run() |
| 1628 | { |
Felix Meschberger | 6120723 | 2011-11-17 10:06:45 +0000 | [diff] [blame] | 1629 | log( LogService.LOG_DEBUG, "Updating configuration {0} to revision #{1}", new Object[] |
| 1630 | { config.getPid(), new Long( revision ) } ); |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1631 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1632 | final List<ServiceReference<?>> srList = this.helper.getServices( getTargetedServicePid() ); |
| 1633 | if ( !srList.isEmpty() ) |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1634 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1635 | // optionally bind dynamically to the first service |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1636 | config.tryBindLocation( srList.get( 0 ).getBundle().getLocation() ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1637 | |
| 1638 | final String configBundleLocation = config.getBundleLocation(); |
| 1639 | |
| 1640 | // provide configuration to all services from the |
| 1641 | // correct bundle |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1642 | for (ServiceReference<?> ref : srList) |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1643 | { |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1644 | final Bundle refBundle = ref.getBundle(); |
| 1645 | if ( refBundle == null ) |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1646 | { |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1647 | log( LogService.LOG_DEBUG, |
| 1648 | "Service {0} seems to be unregistered concurrently (not providing configuration)", |
| 1649 | new Object[] |
| 1650 | { ConfigurationManager.toString( ref ) } ); |
| 1651 | } |
| 1652 | else if ( canReceive( refBundle, configBundleLocation ) ) |
| 1653 | { |
Felix Meschberger | accd901 | 2012-07-02 15:18:08 +0000 | [diff] [blame] | 1654 | helper.provideConfiguration( ref, this.config, this.properties ); |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | // CM 1.4 / 104.13.2.2 |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1659 | log( LogService.LOG_ERROR, |
| 1660 | "Cannot use configuration {0} for {1}: No visibility to configuration bound to {2}", |
| 1661 | new Object[] |
| 1662 | { config.getPid(), ConfigurationManager.toString( ref ), configBundleLocation } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1665 | } |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 1666 | } |
Felix Meschberger | 137ee72 | 2011-11-16 09:51:59 +0000 | [diff] [blame] | 1667 | else if ( isLogEnabled( LogService.LOG_DEBUG ) ) |
| 1668 | { |
Felix Meschberger | 1ee888d | 2011-11-16 12:42:57 +0000 | [diff] [blame] | 1669 | log( LogService.LOG_DEBUG, "No ManagedService[Factory] registered for updates to configuration {0}", |
Felix Meschberger | 137ee72 | 2011-11-16 09:51:59 +0000 | [diff] [blame] | 1670 | new Object[] |
| 1671 | { config.getPid() } ); |
| 1672 | } |
Felix Meschberger | 41cce52 | 2009-08-19 05:54:40 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1676 | public String toString() |
| 1677 | { |
| 1678 | return "Update: pid=" + config.getPid(); |
| 1679 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1682 | |
| 1683 | /** |
| 1684 | * The <code>DeleteConfiguration</code> class is used to inform |
| 1685 | * <code>ManagedService[Factory]</code> services of a configuration |
| 1686 | * being deleted. |
| 1687 | */ |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1688 | private class DeleteConfiguration extends ConfigurationProvider |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1689 | { |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1690 | |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 1691 | private final String configLocation; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1692 | |
| 1693 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1694 | DeleteConfiguration( ConfigurationImpl config ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1695 | { |
Felix Meschberger | ce67d73 | 2009-08-20 06:26:35 +0000 | [diff] [blame] | 1696 | /* |
| 1697 | * NOTE: We keep the configuration because it might be cleared just |
| 1698 | * after calling this method. The pid and factoryPid fields are |
| 1699 | * final and cannot be reset. |
| 1700 | */ |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1701 | super(config); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1702 | this.configLocation = config.getBundleLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | public void run() |
| 1707 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1708 | List<ServiceReference<?>> srList = this.helper.getServices( getTargetedServicePid() ); |
| 1709 | if ( !srList.isEmpty() ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1710 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1711 | for (ServiceReference<?> sr : srList) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1712 | { |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1713 | final Bundle srBundle = sr.getBundle(); |
| 1714 | if ( srBundle == null ) |
| 1715 | { |
| 1716 | log( LogService.LOG_DEBUG, |
| 1717 | "Service {0} seems to be unregistered concurrently (not removing configuration)", |
| 1718 | new Object[] |
| 1719 | { ConfigurationManager.toString( sr ) } ); |
| 1720 | } |
| 1721 | else if ( canReceive( srBundle, configLocation ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1722 | { |
Felix Meschberger | accd901 | 2012-07-02 15:18:08 +0000 | [diff] [blame] | 1723 | this.helper.removeConfiguration( sr, this.config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1724 | } |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1725 | else |
| 1726 | { |
| 1727 | // CM 1.4 / 104.13.2.2 |
| 1728 | log( LogService.LOG_ERROR, |
| 1729 | "Cannot remove configuration {0} for {1}: No visibility to configuration bound to {2}", |
| 1730 | new Object[] |
| 1731 | { config.getPid(), ConfigurationManager.toString( sr ), configLocation } ); |
| 1732 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1733 | } |
| 1734 | } |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1735 | |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1736 | final TargetedPID factoryPid = config.getFactoryPid(); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1737 | if ( factoryPid != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1738 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1739 | // remove the pid from the factory |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1740 | final String pid = config.getPidString(); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1741 | try |
| 1742 | { |
Felix Meschberger | c0894f3 | 2012-07-04 08:09:55 +0000 | [diff] [blame^] | 1743 | Factory factory = getOrCreateFactory( factoryPid.toString() ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1744 | factory.removePID( pid ); |
| 1745 | factory.store(); |
| 1746 | } |
| 1747 | catch ( IOException ioe ) |
| 1748 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1749 | log( LogService.LOG_ERROR, "Failed removing {0} from the factory {1}", new Object[] |
| 1750 | { pid, factoryPid, ioe } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1751 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1752 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1753 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1754 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1755 | public String toString() |
| 1756 | { |
Felix Meschberger | ce67d73 | 2009-08-20 06:26:35 +0000 | [diff] [blame] | 1757 | return "Delete: pid=" + config.getPid(); |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1758 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1761 | private class LocationChanged extends ConfigurationProvider |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1762 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1763 | private final String oldLocation; |
| 1764 | |
| 1765 | |
| 1766 | LocationChanged( ConfigurationImpl config, String oldLocation ) |
| 1767 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1768 | super( config ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1769 | this.oldLocation = oldLocation; |
| 1770 | } |
| 1771 | |
| 1772 | |
| 1773 | public void run() |
| 1774 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1775 | List<ServiceReference<?>> srList = helper.getServices( getTargetedServicePid() ); |
| 1776 | if ( !srList.isEmpty() ) |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1777 | { |
Felix Meschberger | 63e7ab1 | 2012-07-02 14:10:43 +0000 | [diff] [blame] | 1778 | for (final ServiceReference<?> sr : srList) |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1779 | { |
Felix Meschberger | 2444da6 | 2011-11-17 11:17:50 +0000 | [diff] [blame] | 1780 | final Bundle srBundle = sr.getBundle(); |
| 1781 | if ( srBundle == null ) |
| 1782 | { |
| 1783 | log( LogService.LOG_DEBUG, |
| 1784 | "Service {0} seems to be unregistered concurrently (not processing)", new Object[] |
| 1785 | { ConfigurationManager.toString( sr ) } ); |
| 1786 | continue; |
| 1787 | } |
| 1788 | |
| 1789 | final boolean wasVisible = canReceive( srBundle, oldLocation ); |
| 1790 | final boolean isVisible = canReceive( srBundle, config.getBundleLocation() ); |
| 1791 | |
| 1792 | // make sure the config is dynamically bound to the first |
| 1793 | // service if the config has been unbound causing this update |
| 1794 | if ( isVisible ) |
| 1795 | { |
| 1796 | config.tryBindLocation( srBundle.getLocation() ); |
| 1797 | } |
| 1798 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1799 | if ( wasVisible && !isVisible ) |
| 1800 | { |
| 1801 | // call deleted method |
Felix Meschberger | accd901 | 2012-07-02 15:18:08 +0000 | [diff] [blame] | 1802 | helper.removeConfiguration( sr, this.config ); |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1803 | log( LogService.LOG_DEBUG, "Configuration {0} revoked from {1} (no more visibility)", |
| 1804 | new Object[] |
| 1805 | { config.getPid(), ConfigurationManager.toString( sr ) } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1806 | } |
| 1807 | else if ( !wasVisible && isVisible ) |
| 1808 | { |
| 1809 | // call updated method |
Felix Meschberger | accd901 | 2012-07-02 15:18:08 +0000 | [diff] [blame] | 1810 | helper.provideConfiguration( sr, this.config, this.properties ); |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1811 | log( LogService.LOG_DEBUG, "Configuration {0} provided to {1} (new visibility)", new Object[] |
| 1812 | { config.getPid(), ConfigurationManager.toString( sr ) } ); |
Felix Meschberger | 35658d5 | 2011-10-20 21:43:58 +0000 | [diff] [blame] | 1813 | } |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1814 | else |
Felix Meschberger | 35658d5 | 2011-10-20 21:43:58 +0000 | [diff] [blame] | 1815 | { |
| 1816 | // same visibility as before |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame] | 1817 | log( LogService.LOG_DEBUG, "Unmodified visibility to configuration {0} for {1}", new Object[] |
| 1818 | { config.getPid(), ConfigurationManager.toString( sr ) } ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | public String toString() |
| 1826 | { |
| 1827 | return "Location Changed (pid=" + config.getPid() + "): " + oldLocation + " ==> " |
| 1828 | + config.getBundleLocation(); |
| 1829 | } |
| 1830 | } |
| 1831 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1832 | private class FireConfigurationEvent implements Runnable |
| 1833 | { |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1834 | private final int type; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1835 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1836 | private final String pid; |
Felix Meschberger | 2fd5b58 | 2007-12-10 10:32:29 +0000 | [diff] [blame] | 1837 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1838 | private final String factoryPid; |
| 1839 | |
| 1840 | private final ServiceReference[] listenerReferences; |
| 1841 | |
| 1842 | private final ConfigurationListener[] listeners; |
| 1843 | |
| 1844 | private final Bundle[] listenerProvider; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1845 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 1846 | private ConfigurationEvent event; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1847 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1848 | private FireConfigurationEvent( final int type, final String pid, final String factoryPid) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1849 | { |
| 1850 | this.type = type; |
Felix Meschberger | 6642333 | 2007-08-22 08:46:34 +0000 | [diff] [blame] | 1851 | this.pid = pid; |
| 1852 | this.factoryPid = factoryPid; |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1853 | |
| 1854 | final ServiceReference[] srs = configurationListenerTracker.getServiceReferences(); |
| 1855 | if ( srs == null || srs.length == 0 ) |
| 1856 | { |
| 1857 | this.listenerReferences = null; |
| 1858 | this.listeners = null; |
| 1859 | this.listenerProvider = null; |
| 1860 | } |
| 1861 | else |
| 1862 | { |
| 1863 | this.listenerReferences = srs; |
| 1864 | this.listeners = new ConfigurationListener[srs.length]; |
| 1865 | this.listenerProvider = new Bundle[srs.length]; |
| 1866 | for ( int i = 0; i < srs.length; i++ ) |
| 1867 | { |
| 1868 | this.listeners[i] = ( ConfigurationListener ) configurationListenerTracker.getService( srs[i] ); |
| 1869 | this.listenerProvider[i] = srs[i].getBundle(); |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 1875 | void fireSynchronousEvents() |
| 1876 | { |
| 1877 | if ( hasConfigurationEventListeners() && getServiceReference() != null ) |
| 1878 | { |
| 1879 | for ( int i = 0; i < this.listeners.length; i++ ) |
| 1880 | { |
| 1881 | if ( this.listeners[i] instanceof SynchronousConfigurationListener ) |
| 1882 | { |
| 1883 | sendEvent( i ); |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1890 | boolean hasConfigurationEventListeners() |
| 1891 | { |
| 1892 | return this.listenerReferences != null; |
| 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | String getTypeName() |
| 1897 | { |
| 1898 | switch ( type ) |
| 1899 | { |
| 1900 | case ConfigurationEvent.CM_DELETED: |
| 1901 | return "CM_DELETED"; |
| 1902 | case ConfigurationEvent.CM_UPDATED: |
| 1903 | return "CM_UPDATED"; |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 1904 | case ConfigurationEvent.CM_LOCATION_CHANGED: |
| 1905 | return "CM_LOCATION_CHANGED"; |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1906 | default: |
| 1907 | return "<UNKNOWN(" + type + ")>"; |
| 1908 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | |
| 1912 | public void run() |
| 1913 | { |
Felix Meschberger | 9967fbb | 2010-08-25 18:13:25 +0000 | [diff] [blame] | 1914 | for ( int i = 0; i < listeners.length; i++ ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1915 | { |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 1916 | sendEvent( i ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
Felix Meschberger | 5dfaa96 | 2009-08-14 19:26:01 +0000 | [diff] [blame] | 1919 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 1920 | |
Felix Meschberger | 432e387 | 2008-03-07 14:58:57 +0000 | [diff] [blame] | 1921 | public String toString() |
| 1922 | { |
| 1923 | return "Fire ConfigurationEvent: pid=" + pid; |
| 1924 | } |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 1925 | |
| 1926 | |
| 1927 | private ConfigurationEvent getConfigurationEvent() |
| 1928 | { |
| 1929 | if ( event == null ) |
| 1930 | { |
| 1931 | this.event = new ConfigurationEvent( getServiceReference(), type, factoryPid, pid ); |
| 1932 | } |
| 1933 | return event; |
| 1934 | } |
| 1935 | |
| 1936 | |
| 1937 | private void sendEvent( final int serviceIndex ) |
| 1938 | { |
| 1939 | if ( listenerProvider[serviceIndex].getState() == Bundle.ACTIVE && this.listeners[serviceIndex] != null ) |
| 1940 | { |
| 1941 | log( LogService.LOG_DEBUG, "Sending {0} event for {1} to {2}", new Object[] |
| 1942 | { getTypeName(), pid, ConfigurationManager.toString( listenerReferences[serviceIndex] ) } ); |
| 1943 | |
| 1944 | try |
| 1945 | { |
| 1946 | listeners[serviceIndex].configurationEvent( getConfigurationEvent() ); |
| 1947 | } |
| 1948 | catch ( Throwable t ) |
| 1949 | { |
| 1950 | log( LogService.LOG_ERROR, "Unexpected problem delivering configuration event to {0}", new Object[] |
| 1951 | { ConfigurationManager.toString( listenerReferences[serviceIndex] ), t } ); |
| 1952 | } |
| 1953 | finally |
| 1954 | { |
| 1955 | this.listeners[serviceIndex] = null; |
| 1956 | } |
| 1957 | } |
| 1958 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1959 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 1960 | } |