blob: c61693854181cf846b0687972c41b6b21bcb452b [file] [log] [blame]
Felix Meschberger6a698df2009-08-16 18:38:46 +00001/*
Carsten Ziegeler47b825b2008-01-29 09:01:39 +00002 * 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 Meschbergeradd2b4a2007-04-11 18:12:33 +00009 *
Carsten Ziegeler47b825b2008-01-29 09:01:39 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000011 *
Carsten Ziegeler47b825b2008-01-29 09:01:39 +000012 * 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 Meschbergeradd2b4a2007-04-11 18:12:33 +000018 */
19package org.apache.felix.cm.impl;
20
21
22import java.io.IOException;
23import java.util.Dictionary;
24
25import org.osgi.service.cm.Configuration;
Felix Meschbergerf0c5a3d2011-11-04 10:47:58 +000026import org.osgi.service.log.LogService;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000027
28
29/**
Felix Meschberger93c409a2009-01-19 10:47:59 +000030 * 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 Meschbergeradd2b4a2007-04-11 18:12:33 +000034 */
35public class ConfigurationAdapter implements Configuration
36{
37
Felix Meschbergerdf26ae82009-08-14 19:50:43 +000038 private final ConfigurationAdminImpl configurationAdmin;
39 private final ConfigurationImpl delegatee;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000040
41
42 ConfigurationAdapter( ConfigurationAdminImpl configurationAdmin, ConfigurationImpl delegatee )
43 {
44 this.configurationAdmin = configurationAdmin;
45 this.delegatee = delegatee;
46 }
47
48
49 /**
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000050 * @see org.apache.felix.cm.impl.ConfigurationImpl#getPid()
51 */
52 public String getPid()
53 {
54 checkDeleted();
Felix Meschbergerc0894f32012-07-04 08:09:55 +000055 return delegatee.getPidString();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000056 }
57
58
59 /**
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000060 * @see org.apache.felix.cm.impl.ConfigurationImpl#getFactoryPid()
61 */
62 public String getFactoryPid()
63 {
64 checkDeleted();
Felix Meschbergerc0894f32012-07-04 08:09:55 +000065 return delegatee.getFactoryPidString();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000066 }
67
68
69 /**
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000070 * @see org.apache.felix.cm.impl.ConfigurationImpl#getBundleLocation()
71 */
72 public String getBundleLocation()
73 {
Felix Meschberger007c50e2011-10-20 12:39:38 +000074 // CM 1.4 / 104.13.2.4
Felix Meschbergerfa6f0132011-11-16 09:51:23 +000075 final String bundleLocation = delegatee.getBundleLocation();
Felix Meschbergerf768ef92012-01-29 13:59:32 +000076 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "getBundleLocation() ==> {0}", new Object[]
77 { bundleLocation } );
Felix Meschberger623f7142012-01-31 07:13:37 +000078 checkActive();
Felix Meschberger274aa242012-12-31 19:46:31 +000079 configurationAdmin.checkPermission( delegatee.getConfigurationManager(), ( bundleLocation == null ) ? "*" : bundleLocation );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000080 checkDeleted();
Felix Meschbergerfa6f0132011-11-16 09:51:23 +000081 return bundleLocation;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000082 }
83
84
85 /**
86 * @param bundleLocation
Felix Meschberger54e1f5b2012-10-19 13:56:03 +000087 * @see org.apache.felix.cm.impl.ConfigurationImpl#setStaticBundleLocation(String)
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000088 */
89 public void setBundleLocation( String bundleLocation )
90 {
Felix Meschbergerf0c5a3d2011-11-04 10:47:58 +000091 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "setBundleLocation(bundleLocation={0})",
92 new Object[]
93 { bundleLocation } );
94
Felix Meschberger007c50e2011-10-20 12:39:38 +000095 // CM 1.4 / 104.13.2.4
Felix Meschberger623f7142012-01-31 07:13:37 +000096 checkActive();
Felix Meschberger565151a2011-11-16 12:13:39 +000097 final String configLocation = delegatee.getBundleLocation();
Felix Meschberger274aa242012-12-31 19:46:31 +000098 configurationAdmin.checkPermission( delegatee.getConfigurationManager(), ( configLocation == null ) ? "*" : configLocation );
99 configurationAdmin.checkPermission( delegatee.getConfigurationManager(), ( bundleLocation == null ) ? "*" : bundleLocation );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000100 checkDeleted();
Felix Meschbergeref470042009-08-19 05:52:41 +0000101 delegatee.setStaticBundleLocation( bundleLocation );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000102 }
103
104
105 /**
106 * @throws IOException
107 * @see org.apache.felix.cm.impl.ConfigurationImpl#update()
108 */
109 public void update() throws IOException
110 {
Felix Meschbergerf0c5a3d2011-11-04 10:47:58 +0000111 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "update()", ( Throwable ) null );
112
Felix Meschberger623f7142012-01-31 07:13:37 +0000113 checkActive();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000114 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 Meschbergerf0c5a3d2011-11-04 10:47:58 +0000126 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "update(properties={0})", new Object[]
127 { properties } );
128
Felix Meschberger623f7142012-01-31 07:13:37 +0000129 checkActive();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000130 checkDeleted();
131 delegatee.update( properties );
132 }
133
134
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000135 public Dictionary getProperties()
136 {
Felix Meschbergerf0c5a3d2011-11-04 10:47:58 +0000137 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "getProperties()", ( Throwable ) null );
138
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000139 checkDeleted();
Felix Meschberger6a698df2009-08-16 18:38:46 +0000140
Felix Meschbergera0903df2009-01-19 10:40:28 +0000141 // 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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000144 }
145
146
Felix Meschberger0770cad2012-06-11 12:36:52 +0000147 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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000157 /**
158 * @throws IOException
159 * @see org.apache.felix.cm.impl.ConfigurationImpl#delete()
160 */
161 public void delete() throws IOException
162 {
Felix Meschbergerf0c5a3d2011-11-04 10:47:58 +0000163 delegatee.getConfigurationManager().log( LogService.LOG_DEBUG, "delete()", ( Throwable ) null );
164
Felix Meschberger623f7142012-01-31 07:13:37 +0000165 checkActive();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000166 checkDeleted();
167 delegatee.delete();
168 }
169
170
171 /**
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000172 * @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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000182 * @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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000191 * @see org.apache.felix.cm.impl.ConfigurationImpl#toString()
192 */
193 public String toString()
194 {
195 return delegatee.toString();
196 }
197
198 /**
Felix Meschberger623f7142012-01-31 07:13:37 +0000199 * 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 */
Felix Meschberger274aa242012-12-31 19:46:31 +0000205 private void checkActive()
206 {
207 if ( !delegatee.isActive() )
208 {
209 throw new IllegalStateException( "Configuration " + delegatee.getPid()
210 + " not backed by an active Configuration Admin Service" );
Felix Meschberger623f7142012-01-31 07:13:37 +0000211 }
212 }
213
Felix Meschberger274aa242012-12-31 19:46:31 +0000214
Felix Meschberger623f7142012-01-31 07:13:37 +0000215 /**
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000216 * Checks whether this configuration object has already been deleted.
Carsten Ziegeler7853b9a2008-01-11 16:30:24 +0000217 *
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000218 * @throws IllegalStateException If this configuration object has been
219 * deleted.
220 */
Felix Meschberger274aa242012-12-31 19:46:31 +0000221 private void checkDeleted()
222 {
223 if ( delegatee.isDeleted() )
224 {
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000225 throw new IllegalStateException( "Configuration " + delegatee.getPid() + " deleted" );
226 }
227 }
228}