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