Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +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 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.Dictionary; |
| 24 | import java.util.Hashtable; |
| 25 | |
| 26 | import org.apache.felix.cm.PersistenceManager; |
| 27 | import org.osgi.framework.Constants; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 28 | import org.osgi.service.cm.Configuration; |
| 29 | import org.osgi.service.cm.ConfigurationAdmin; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 30 | import org.osgi.service.log.LogService; |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 31 | |
Felix Meschberger | 93c409a | 2009-01-19 10:47:59 +0000 | [diff] [blame] | 32 | |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 33 | /** |
Felix Meschberger | 93c409a | 2009-01-19 10:47:59 +0000 | [diff] [blame] | 34 | * The <code>ConfigurationImpl</code> is the backend implementation of the |
| 35 | * Configuration Admin Service Specification <i>Configuration object</i> |
| 36 | * (section 104.4). Instances of this class are shared by multiple instances of |
| 37 | * the {@link ConfigurationAdapter} class, whose instances are actually returned |
| 38 | * to clients. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 39 | */ |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 40 | class ConfigurationImpl extends ConfigurationBase |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 41 | { |
| 42 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Concurrency note: There is a slight (but real) chance of a race condition |
| 45 | * between a configuration update and a ManagedService[Factory] registration. |
| 46 | * Per the specification a ManagedService must be called with configuration |
| 47 | * or null when registered and a ManagedService must be called with currently |
| 48 | * existing configuration when registered. Also the ManagedService[Factory] |
| 49 | * must be updated when the configuration is updated. |
| 50 | * |
| 51 | * Consider now this situation of two threads T1 and T2: |
| 52 | * |
| 53 | * T1. create and update configuration |
| 54 | * ConfigurationImpl.update persists configuration and sets field |
| 55 | * Thread preempted |
| 56 | * |
| 57 | * T2. ManagedServiceUpdate constructor reads configuration |
| 58 | * Uses configuration already persisted by T1 for update |
| 59 | * Schedules task to update service with the configuration |
| 60 | * |
| 61 | * T1. Runs again creating the UpdateConfiguration task with the |
| 62 | * configuration persisted before being preempted |
| 63 | * Schedules task to update service |
| 64 | * |
| 65 | * Update Thread: |
| 66 | * Updates ManagedService with configuration prepared by T2 |
| 67 | * Updates ManagedService with configuration prepared by T1 |
| 68 | * |
| 69 | * The correct behaviour would be here, that the second call to update |
| 70 | * would not take place. |
| 71 | * |
| 72 | * This concurrency safety is implemented with the help of the |
| 73 | * lastModificationTime field updated by the configure(Dictionary) method |
| 74 | * when setting the properties field and the lastUpdatedTime field updated |
| 75 | * in the Update Thread after calling the update(Dictionary) method of |
| 76 | * the ManagedService[Factory] service. |
| 77 | * |
| 78 | * The UpdateConfiguration task compares the lastModificationTime to the |
| 79 | * lastUpdateTime. If the configuration has been modified after being |
| 80 | * updated the last time, it is updated in the ManagedService[Factory]. If |
| 81 | * the configuration has already been updated since being modified (as in |
| 82 | * the case above), the UpdateConfiguration thread does not call the update |
| 83 | * method (but still sends the CM_UPDATED event). |
| 84 | * |
| 85 | * See also FELIX-1542. |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 86 | * |
| 87 | * FELIX-1545 provides further update to the concurrency situation defining |
| 88 | * three more failure cases: |
| 89 | * |
| 90 | * (1) System.currentTimeMillis() may be too coarse graind to protect |
| 91 | * against race condition. |
| 92 | * (2) ManagedService update sets last update time regardless of whether |
| 93 | * configuration was provided or not. This may cause a configuration |
| 94 | * update to be lost. |
| 95 | * (3) ManagedService update does not respect last update time which |
| 96 | * in turn may cause duplicate configuration delivery. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 97 | */ |
| 98 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 99 | /** |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 100 | * The name of a synthetic property stored in the persisted configuration |
| 101 | * data to indicate that the configuration data is new, that is created but |
| 102 | * never updated (value is "_felix_.cm.newConfiguration"). |
| 103 | * <p> |
| 104 | * This special property is stored by the |
| 105 | * {@link #ConfigurationImpl(ConfigurationManager, PersistenceManager, String, String, String)} |
| 106 | * constructor, when the configuration is first created and persisted and is |
| 107 | * interpreted by the |
| 108 | * {@link #ConfigurationImpl(ConfigurationManager, PersistenceManager, Dictionary)} |
| 109 | * method when the configuration data is loaded in a new object. |
| 110 | * <p> |
| 111 | * The goal of this property is to keep the information on whether |
| 112 | * configuration data is new (but persisted as per the spec) or has already |
| 113 | * been assigned with possible no data. |
| 114 | */ |
| 115 | private static final String CONFIGURATION_NEW = "_felix_.cm.newConfiguration"; |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 116 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 117 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 118 | * The factory serviceReference PID of this configuration or <code>null</code> if this |
| 119 | * is not a factory configuration. |
| 120 | */ |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 121 | private final String factoryPID; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 124 | * The configuration data of this configuration instance. This is a private |
| 125 | * copy of the properties of which a copy is made when the |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 126 | * {@link #getProperties()} method is called. This field is |
| 127 | * <code>null</code> if the configuration has been created and never been |
| 128 | * updated with acutal configuration properties. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 129 | */ |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 130 | private volatile CaseInsensitiveDictionary properties; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 131 | |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 132 | /** |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 133 | * Flag indicating that this configuration has been deleted. |
| 134 | * |
| 135 | * @see #isDeleted() |
Felix Meschberger | f463132 | 2008-03-10 12:32:35 +0000 | [diff] [blame] | 136 | */ |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 137 | private volatile boolean isDeleted; |
| 138 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 139 | /** |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 140 | * Current configuration modification counter. This field is incremented |
| 141 | * each time the {@link #properties} field is set (in the constructor or the |
| 142 | * {@link #configure(Dictionary)} method. field. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 143 | */ |
| 144 | private volatile long lastModificationTime; |
| 145 | |
| 146 | /** |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 147 | * Value of the {@link #lastModificationTime} counter at the time the non- |
| 148 | * <code>null</code> properties of this configuration have been updated to a |
| 149 | * ManagedService[Factory]. This field is initialized to -1 in the |
| 150 | * constructors and set to the value of the {@link #lastModificationTime} by |
| 151 | * the {@link #setLastUpdatedTime()} method called from the respective task |
| 152 | * updating the configuration. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 153 | * |
| 154 | * @see #lastModificationTime |
| 155 | */ |
| 156 | private volatile long lastUpdatedTime; |
| 157 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 158 | |
| 159 | ConfigurationImpl( ConfigurationManager configurationManager, PersistenceManager persistenceManager, |
| 160 | Dictionary properties ) |
| 161 | { |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 162 | super( configurationManager, persistenceManager, ( String ) properties.remove( Constants.SERVICE_PID ), |
| 163 | ( String ) properties.remove( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 164 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 165 | this.factoryPID = ( String ) properties.remove( ConfigurationAdmin.SERVICE_FACTORYPID ); |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 166 | this.isDeleted = false; |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 167 | this.lastUpdatedTime = -1; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 168 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 169 | // set the properties internally |
| 170 | configureFromPersistence( properties ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | ConfigurationImpl( ConfigurationManager configurationManager, PersistenceManager persistenceManager, String pid, |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 175 | String factoryPid, String bundleLocation ) throws IOException |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 176 | { |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 177 | super( configurationManager, persistenceManager, pid, bundleLocation ); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 178 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 179 | this.factoryPID = factoryPid; |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 180 | this.isDeleted = false; |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 181 | this.lastUpdatedTime = -1; |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 182 | |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 183 | // first "update" |
| 184 | this.properties = null; |
| 185 | setLastModificationTime(); |
| 186 | |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 187 | // this is a new configuration object, store immediately unless |
| 188 | // the new configuration object is created from a factory, in which |
| 189 | // case the configuration is only stored when first updated |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 190 | if ( factoryPid == null ) |
| 191 | { |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 192 | Dictionary props = new Hashtable(); |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 193 | setAutoProperties( props, true ); |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 194 | props.put( CONFIGURATION_NEW, Boolean.TRUE ); |
| 195 | persistenceManager.store( pid, props ); |
| 196 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 200 | public void delete() throws IOException |
| 201 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 202 | this.isDeleted = true; |
| 203 | getPersistenceManager().delete( this.getPid() ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 204 | getConfigurationManager().setDynamicBundleLocation( this.getPid(), null ); |
| 205 | getConfigurationManager().deleted( this ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 209 | public String getPid() |
| 210 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 211 | return getBaseId(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 215 | public String getFactoryPid() |
| 216 | { |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 217 | return factoryPID; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 221 | /** |
| 222 | * Returns an optionally deep copy of the properties of this configuration |
| 223 | * instance. |
| 224 | * <p> |
| 225 | * This method returns a copy of the internal dictionary. If the |
| 226 | * <code>deepCopy</code> parameter is true array and collection values are |
| 227 | * copied into new arrays or collections. Otherwise just a new dictionary |
| 228 | * referring to the same objects is returned. |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 229 | * |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 230 | * @param deepCopy |
| 231 | * <code>true</code> if a deep copy is to be returned. |
| 232 | * @return |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 233 | */ |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 234 | public Dictionary getProperties( boolean deepCopy ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 235 | { |
| 236 | // no properties yet |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 237 | if ( properties == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 238 | { |
| 239 | return null; |
| 240 | } |
| 241 | |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 242 | CaseInsensitiveDictionary props = new CaseInsensitiveDictionary( properties, deepCopy ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 243 | |
| 244 | // fix special properties (pid, factory PID, bundle location) |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 245 | setAutoProperties( props, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 246 | |
| 247 | return props; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /* (non-Javadoc) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 252 | * @see org.osgi.service.cm.Configuration#update() |
| 253 | */ |
| 254 | public void update() throws IOException |
| 255 | { |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 256 | PersistenceManager localPersistenceManager = getPersistenceManager(); |
| 257 | if ( localPersistenceManager != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 258 | { |
| 259 | // read configuration from persistence (again) |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 260 | Dictionary properties = localPersistenceManager.load( getPid() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 261 | |
| 262 | // ensure serviceReference pid |
| 263 | String servicePid = ( String ) properties.get( Constants.SERVICE_PID ); |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 264 | if ( servicePid != null && !getPid().equals( servicePid ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 265 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 266 | throw new IOException( "PID of configuration file does match requested PID; expected " + getPid() + ", got " |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 267 | + servicePid ); |
| 268 | } |
| 269 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 270 | configureFromPersistence( properties ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 271 | |
Felix Meschberger | 02f457d | 2009-08-20 06:27:17 +0000 | [diff] [blame] | 272 | // update the service but do not fire an CM_UPDATED event |
| 273 | getConfigurationManager().updated( this, false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /* (non-Javadoc) |
| 279 | * @see org.osgi.service.cm.Configuration#update(java.util.Dictionary) |
| 280 | */ |
| 281 | public void update( Dictionary properties ) throws IOException |
| 282 | { |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 283 | PersistenceManager localPersistenceManager = getPersistenceManager(); |
| 284 | if ( localPersistenceManager != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 285 | { |
| 286 | CaseInsensitiveDictionary newProperties = new CaseInsensitiveDictionary( properties ); |
| 287 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 288 | setAutoProperties( newProperties, true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 289 | |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 290 | // persist new configuration |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 291 | localPersistenceManager.store( getPid(), newProperties ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 292 | |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 293 | // if this is a factory configuration, update the factory with |
| 294 | String factoryPid = getFactoryPid(); |
| 295 | if ( factoryPid != null ) |
| 296 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 297 | // If this is a new factory configuration, we also have to add |
| 298 | // it to the configuration manager cache |
| 299 | if ( isNew() ) |
| 300 | { |
| 301 | getConfigurationManager().cacheConfiguration( this ); |
| 302 | } |
| 303 | |
| 304 | Factory factory = getConfigurationManager().getFactory( factoryPid ); |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 305 | if ( factory.addPID( getPid() ) ) |
| 306 | { |
| 307 | // only write back if the pid was not already registered |
| 308 | // with the factory |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 309 | try |
| 310 | { |
| 311 | factory.store(); |
| 312 | } |
| 313 | catch ( IOException ioe ) |
| 314 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 315 | getConfigurationManager().log( LogService.LOG_ERROR, |
| 316 | "Failure storing factory " + factoryPid + " with new configuration " + getPid(), ioe ); |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 317 | } |
Felix Meschberger | fbc7dc1 | 2008-08-06 08:25:58 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 320 | |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 321 | // finally assign the configuration for use |
| 322 | configure( newProperties ); |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 323 | |
Felix Meschberger | 02f457d | 2009-08-20 06:27:17 +0000 | [diff] [blame] | 324 | // update the service and fire an CM_UPDATED event |
| 325 | getConfigurationManager().updated( this, true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | //---------- Object overwrites -------------------------------------------- |
| 331 | |
| 332 | public boolean equals( Object obj ) |
| 333 | { |
| 334 | if ( obj == this ) |
| 335 | { |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | if ( obj instanceof Configuration ) |
| 340 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 341 | return getPid().equals( ( ( Configuration ) obj ).getPid() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | public int hashCode() |
| 349 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 350 | return getPid().hashCode(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | public String toString() |
| 355 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 356 | return "Configuration PID=" + getPid() + ", factoryPID=" + factoryPID + ", bundleLocation=" + getBundleLocation(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 360 | // ---------- private helper ----------------------------------------------- |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 361 | |
| 362 | void store() throws IOException |
| 363 | { |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 364 | // we don't need a deep copy, since we are not modifying |
| 365 | // any value in the dictionary itself. we are just adding |
| 366 | // properties to it, which are required for storing |
| 367 | Dictionary props = getProperties( false ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 368 | |
| 369 | // if this is a new configuration, we just use an empty Dictionary |
| 370 | if ( props == null ) |
| 371 | { |
| 372 | props = new Hashtable(); |
| 373 | |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 374 | // add automatic properties including the bundle location (if |
| 375 | // statically bound) |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 376 | setAutoProperties( props, true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 377 | } |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 378 | else |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 379 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 380 | replaceProperty( props, ConfigurationAdmin.SERVICE_BUNDLELOCATION, getStaticBundleLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // only store now, if this is not a new configuration |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 384 | getPersistenceManager().store( getPid(), props ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 388 | /** |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 389 | * Increments the last modification counter of this configuration to cause |
| 390 | * the ManagedService or ManagedServiceFactory subscribed to this |
| 391 | * configuration to be updated. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 392 | * <p> |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 393 | * This method is intended to only be called by the constructor(s) of this |
| 394 | * class and the {@link #update(Dictionary)} method to indicate to the |
| 395 | * update threads, the configuration is ready for distribution. |
| 396 | * <p> |
| 397 | * Setting the properties field and incrementing this counter should be |
| 398 | * done synchronized on this instance. |
| 399 | */ |
| 400 | void setLastModificationTime( ) |
| 401 | { |
| 402 | this.lastModificationTime++; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * Returns the modification counter of the last modification of the |
| 408 | * properties of this configuration object. |
| 409 | * <p> |
| 410 | * This value may be compared to the {@link #getLastUpdatedTime()} to decide |
| 411 | * whether to update the ManagedService[Factory] or not. |
| 412 | * <p> |
| 413 | * Getting the properties of this configuration and this counter should be |
| 414 | * done synchronized on this instance. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 415 | */ |
| 416 | long getLastModificationTime() |
| 417 | { |
| 418 | return lastModificationTime; |
| 419 | } |
| 420 | |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 421 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 422 | /** |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 423 | * Returns the modification counter of the last update of this configuration |
| 424 | * to the subscribing ManagedService or ManagedServiceFactory. This value |
| 425 | * may be compared to the {@link #getLastModificationTime()} to decide |
| 426 | * whether the configuration should be updated or not. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 427 | */ |
| 428 | long getLastUpdatedTime() |
| 429 | { |
| 430 | return lastUpdatedTime; |
| 431 | } |
| 432 | |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 433 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 434 | /** |
Felix Meschberger | 8d9851a | 2010-08-25 13:22:44 +0000 | [diff] [blame^] | 435 | * Sets the last update time field to the given value of the last |
| 436 | * modification time to indicate the version of configuration properties |
| 437 | * that have been updated in a ManagedService[Factory]. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 438 | * <p> |
| 439 | * This method should only be called from the Update Thread after supplying |
| 440 | * the configuration to the ManagedService[Factory]. |
Felix Meschberger | 8d9851a | 2010-08-25 13:22:44 +0000 | [diff] [blame^] | 441 | * |
| 442 | * @param lastModificationTime The value of the |
| 443 | * {@link #getLastModificationTime() last modification time field} at |
| 444 | * which the properties have been extracted from the configuration to |
| 445 | * be supplied to the service. |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 446 | */ |
Felix Meschberger | 8d9851a | 2010-08-25 13:22:44 +0000 | [diff] [blame^] | 447 | void setLastUpdatedTime( long lastModificationTime ) |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 448 | { |
Felix Meschberger | 8d9851a | 2010-08-25 13:22:44 +0000 | [diff] [blame^] | 449 | synchronized ( this ) |
| 450 | { |
| 451 | this.lastUpdatedTime = lastModificationTime; |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 452 | } |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Felix Meschberger | 8d9851a | 2010-08-25 13:22:44 +0000 | [diff] [blame^] | 455 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 456 | /** |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 457 | * Returns <code>false</code> if this configuration contains configuration |
| 458 | * properties. Otherwise <code>true</code> is returned and this is a |
| 459 | * newly creted configuration object whose {@link #update(Dictionary)} |
| 460 | * method has never been called. |
| 461 | */ |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 462 | boolean isNew() |
| 463 | { |
| 464 | return properties == null; |
| 465 | } |
| 466 | |
| 467 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 468 | /** |
| 469 | * Returns <code>true</code> if this configuration has already been deleted |
| 470 | * on the persistence. |
| 471 | */ |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 472 | boolean isDeleted() |
| 473 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 474 | return isDeleted; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 478 | private void configureFromPersistence( Dictionary properties ) |
| 479 | { |
| 480 | // if the this is not an empty/new configuration, accept the properties |
| 481 | // otherwise just set the properties field to null |
| 482 | if ( properties.get( CONFIGURATION_NEW ) == null ) |
| 483 | { |
| 484 | configure( properties ); |
| 485 | } |
| 486 | else |
| 487 | { |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 488 | configure( null ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 491 | |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 492 | private void configure( final Dictionary properties ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 493 | { |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 494 | final CaseInsensitiveDictionary newProperties; |
| 495 | if ( properties == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 496 | { |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 497 | newProperties = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 498 | } |
| 499 | else |
| 500 | { |
Felix Meschberger | a0e8e33 | 2009-08-18 07:52:51 +0000 | [diff] [blame] | 501 | // remove predefined properties |
| 502 | clearAutoProperties( properties ); |
| 503 | |
| 504 | // ensure CaseInsensitiveDictionary |
| 505 | if ( properties instanceof CaseInsensitiveDictionary ) |
| 506 | { |
| 507 | newProperties = ( CaseInsensitiveDictionary ) properties; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | newProperties = new CaseInsensitiveDictionary( properties ); |
| 512 | } |
| 513 | } |
| 514 | |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 515 | synchronized ( this ) |
| 516 | { |
| 517 | this.properties = newProperties; |
Felix Meschberger | f23eb07 | 2009-08-31 14:04:33 +0000 | [diff] [blame] | 518 | setLastModificationTime(); |
Felix Meschberger | fd52e31 | 2009-08-29 19:44:58 +0000 | [diff] [blame] | 519 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
| 523 | void setAutoProperties( Dictionary properties, boolean withBundleLocation ) |
| 524 | { |
| 525 | // set pid and factory pid in the properties |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 526 | replaceProperty( properties, Constants.SERVICE_PID, getPid() ); |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 527 | replaceProperty( properties, ConfigurationAdmin.SERVICE_FACTORYPID, factoryPID ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 528 | |
| 529 | // bundle location is not set here |
| 530 | if ( withBundleLocation ) |
| 531 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 532 | replaceProperty( properties, ConfigurationAdmin.SERVICE_BUNDLELOCATION, getStaticBundleLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 533 | } |
| 534 | else |
| 535 | { |
| 536 | properties.remove( ConfigurationAdmin.SERVICE_BUNDLELOCATION ); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 541 | static void clearAutoProperties( Dictionary properties ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 542 | { |
| 543 | properties.remove( Constants.SERVICE_PID ); |
| 544 | properties.remove( ConfigurationAdmin.SERVICE_FACTORYPID ); |
| 545 | properties.remove( ConfigurationAdmin.SERVICE_BUNDLELOCATION ); |
| 546 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 547 | } |