Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 1 | /* |
Carsten Ziegeler | 47b825b | 2008-01-29 09:01:39 +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 |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 9 | * |
Carsten Ziegeler | 47b825b | 2008-01-29 09:01:39 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 11 | * |
Carsten Ziegeler | 47b825b | 2008-01-29 09:01:39 +0000 | [diff] [blame] | 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. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.cm.impl; |
| 20 | |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.Dictionary; |
| 24 | |
| 25 | import org.osgi.service.cm.Configuration; |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 26 | import org.osgi.service.log.LogService; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | /** |
Felix Meschberger | 93c409a | 2009-01-19 10:47:59 +0000 | [diff] [blame] | 30 | * The <code>ConfigurationAdapter</code> is just an adapter to the internal |
| 31 | * configuration object. Instances of this class are returned as Configuration |
| 32 | * objects to the client, where each caller gets a fresh instance of this |
| 33 | * class while internal Configuration objects are shared. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 34 | */ |
| 35 | public class ConfigurationAdapter implements Configuration |
| 36 | { |
| 37 | |
Felix Meschberger | df26ae8 | 2009-08-14 19:50:43 +0000 | [diff] [blame] | 38 | private final ConfigurationAdminImpl configurationAdmin; |
| 39 | private final ConfigurationImpl delegatee; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | ConfigurationAdapter( ConfigurationAdminImpl configurationAdmin, ConfigurationImpl delegatee ) |
| 43 | { |
| 44 | this.configurationAdmin = configurationAdmin; |
| 45 | this.delegatee = delegatee; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 50 | * @see org.apache.felix.cm.impl.ConfigurationImpl#getPid() |
| 51 | */ |
| 52 | public String getPid() |
| 53 | { |
| 54 | checkDeleted(); |
| 55 | return delegatee.getPid(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 60 | * @see org.apache.felix.cm.impl.ConfigurationImpl#getFactoryPid() |
| 61 | */ |
| 62 | public String getFactoryPid() |
| 63 | { |
| 64 | checkDeleted(); |
| 65 | return delegatee.getFactoryPid(); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 70 | * @see org.apache.felix.cm.impl.ConfigurationImpl#getBundleLocation() |
| 71 | */ |
| 72 | public String getBundleLocation() |
| 73 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 74 | // CM 1.4 / 104.13.2.4 |
Felix Meschberger | fa6f013 | 2011-11-16 09:51:23 +0000 | [diff] [blame] | 75 | final String bundleLocation = delegatee.getBundleLocation(); |
Felix Meschberger | f768ef9 | 2012-01-29 13:59:32 +0000 | [diff] [blame] | 76 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "getBundleLocation() ==> {0}", new Object[] |
| 77 | { bundleLocation } ); |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 78 | checkActive(); |
Felix Meschberger | fa6f013 | 2011-11-16 09:51:23 +0000 | [diff] [blame] | 79 | configurationAdmin.checkPermission( ( bundleLocation == null ) ? "*" : bundleLocation ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 80 | checkDeleted(); |
Felix Meschberger | fa6f013 | 2011-11-16 09:51:23 +0000 | [diff] [blame] | 81 | return bundleLocation; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * @param bundleLocation |
| 87 | * @see org.apache.felix.cm.impl.ConfigurationImpl#setBundleLocation(java.lang.String) |
| 88 | */ |
| 89 | public void setBundleLocation( String bundleLocation ) |
| 90 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 91 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "setBundleLocation(bundleLocation={0})", |
| 92 | new Object[] |
| 93 | { bundleLocation } ); |
| 94 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 95 | // CM 1.4 / 104.13.2.4 |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 96 | checkActive(); |
Felix Meschberger | 565151a | 2011-11-16 12:13:39 +0000 | [diff] [blame] | 97 | final String configLocation = delegatee.getBundleLocation(); |
| 98 | configurationAdmin.checkPermission( ( configLocation == null ) ? "*" : configLocation ); |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 99 | configurationAdmin.checkPermission( ( bundleLocation == null ) ? "*" : bundleLocation ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 100 | checkDeleted(); |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 101 | delegatee.setStaticBundleLocation( bundleLocation ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * @throws IOException |
| 107 | * @see org.apache.felix.cm.impl.ConfigurationImpl#update() |
| 108 | */ |
| 109 | public void update() throws IOException |
| 110 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 111 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "update()", ( Throwable ) null ); |
| 112 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 113 | checkActive(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 114 | checkDeleted(); |
| 115 | delegatee.update(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * @param properties |
| 121 | * @throws IOException |
| 122 | * @see org.apache.felix.cm.impl.ConfigurationImpl#update(java.util.Dictionary) |
| 123 | */ |
| 124 | public void update( Dictionary properties ) throws IOException |
| 125 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 126 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "update(properties={0})", new Object[] |
| 127 | { properties } ); |
| 128 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 129 | checkActive(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 130 | checkDeleted(); |
| 131 | delegatee.update( properties ); |
| 132 | } |
| 133 | |
| 134 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 135 | public Dictionary getProperties() |
| 136 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 137 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "getProperties()", ( Throwable ) null ); |
| 138 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 139 | checkDeleted(); |
Felix Meschberger | 6a698df | 2009-08-16 18:38:46 +0000 | [diff] [blame] | 140 | |
Felix Meschberger | a0903df | 2009-01-19 10:40:28 +0000 | [diff] [blame] | 141 | // return a deep copy since the spec says, that modification of |
| 142 | // any value should not modify the internal, stored value |
| 143 | return delegatee.getProperties( true ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 147 | public long getChangeCount() |
| 148 | { |
| 149 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "getChangeCount()", ( Throwable ) null ); |
| 150 | |
| 151 | checkDeleted(); |
| 152 | |
| 153 | return delegatee.getRevision(); |
| 154 | } |
| 155 | |
| 156 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 157 | /** |
| 158 | * @throws IOException |
| 159 | * @see org.apache.felix.cm.impl.ConfigurationImpl#delete() |
| 160 | */ |
| 161 | public void delete() throws IOException |
| 162 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame] | 163 | delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "delete()", ( Throwable ) null ); |
| 164 | |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 165 | checkActive(); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 166 | checkDeleted(); |
| 167 | delegatee.delete(); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 172 | * @see org.apache.felix.cm.impl.ConfigurationImpl#hashCode() |
| 173 | */ |
| 174 | public int hashCode() |
| 175 | { |
| 176 | return delegatee.hashCode(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * @param obj |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 182 | * @see org.apache.felix.cm.impl.ConfigurationImpl#equals(java.lang.Object) |
| 183 | */ |
| 184 | public boolean equals( Object obj ) |
| 185 | { |
| 186 | return delegatee.equals( obj ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 191 | * @see org.apache.felix.cm.impl.ConfigurationImpl#toString() |
| 192 | */ |
| 193 | public String toString() |
| 194 | { |
| 195 | return delegatee.toString(); |
| 196 | } |
| 197 | |
| 198 | /** |
Felix Meschberger | 623f714 | 2012-01-31 07:13:37 +0000 | [diff] [blame] | 199 | * Checks whether this configuration object is backed by an active |
| 200 | * Configuration Admin Service (ConfigurationManager here). |
| 201 | * |
| 202 | * @throws IllegalStateException If this configuration object is not |
| 203 | * backed by an active ConfigurationManager |
| 204 | */ |
| 205 | private void checkActive() { |
| 206 | if (!delegatee.isActive()) { |
| 207 | throw new IllegalStateException( "Configuration " + delegatee.getPid() + " not backed by an active Configuration Admin Service" ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 212 | * Checks whether this configuration object has already been deleted. |
Carsten Ziegeler | 7853b9a | 2008-01-11 16:30:24 +0000 | [diff] [blame] | 213 | * |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 214 | * @throws IllegalStateException If this configuration object has been |
| 215 | * deleted. |
| 216 | */ |
| 217 | private void checkDeleted() { |
| 218 | if (delegatee.isDeleted()) { |
| 219 | throw new IllegalStateException( "Configuration " + delegatee.getPid() + " deleted" ); |
| 220 | } |
| 221 | } |
| 222 | } |