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