blob: 73bf15a88000a2907d9224d5e79a46adf0d02f9c [file] [log] [blame]
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +00001/*
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +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
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 */
19package org.apache.felix.cm.impl;
20
21
22import java.io.IOException;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000023import org.osgi.framework.Bundle;
24import org.osgi.framework.InvalidSyntaxException;
25import org.osgi.service.cm.Configuration;
26import org.osgi.service.cm.ConfigurationAdmin;
27import org.osgi.service.cm.ConfigurationPermission;
28
29
30/**
31 * The <code>ConfigurationAdminImpl</code> is the per-bundle frontend to the
32 * configuration manager. Instances of this class are created on-demand for
33 * each bundle trying to get hold of the <code>ConfigurationAdmin</code>
34 * service.
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000035 */
36public class ConfigurationAdminImpl implements ConfigurationAdmin
37{
38
39 // The configuration manager to which most of the tasks are delegated
40 private ConfigurationManager configurationManager;
41
42 // The bundle for which this instance has been created
43 private Bundle bundle;
44
45
46 ConfigurationAdminImpl( ConfigurationManager configurationManager, Bundle bundle )
47 {
48 this.configurationManager = configurationManager;
49 this.bundle = bundle;
50 }
51
52
53 void dispose()
54 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +000055 bundle = null;
56 configurationManager = null;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000057 }
58
59
60 Bundle getBundle()
61 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +000062 return bundle;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000063 }
64
65
66 //---------- ConfigurationAdmin interface ---------------------------------
67
68 /* (non-Javadoc)
69 * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String)
70 */
71 public Configuration createFactoryConfiguration( String factoryPid ) throws IOException
72 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +000073 return this.wrap( configurationManager.createFactoryConfiguration( this, factoryPid ) );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000074 }
75
76
77 /* (non-Javadoc)
78 * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String, java.lang.String)
79 */
80 public Configuration createFactoryConfiguration( String factoryPid, String location ) throws IOException
81 {
Felix Meschberger007c50e2011-10-20 12:39:38 +000082 // CM 1.4 / 104.13.2.3
83 this.checkPermission( location );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000084
Carsten Ziegelereb80d512007-08-15 11:17:41 +000085 return this.wrap( configurationManager.createFactoryConfiguration( factoryPid, location ) );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000086 }
87
88
89 /* (non-Javadoc)
90 * @see org.osgi.service.cm.ConfigurationAdmin#getConfiguration(java.lang.String)
91 */
92 public Configuration getConfiguration( String pid ) throws IOException
93 {
Felix Meschberger2941ef92007-08-20 13:15:16 +000094 ConfigurationImpl config = configurationManager.getConfiguration( pid, getBundle().getLocation() );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000095
Felix Meschbergerca3cd982009-08-19 19:27:58 +000096 if ( config.getBundleLocation() == null )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000097 {
Felix Meschbergeref470042009-08-19 05:52:41 +000098 config.setStaticBundleLocation( this.getBundle().getLocation() );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000099 }
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000100 else if ( !config.getBundleLocation().equals( this.getBundle().getLocation() ) )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000101 {
Felix Meschberger007c50e2011-10-20 12:39:38 +0000102 // CM 1.4 / 104.13.2.3
103 this.checkPermission( config.getBundleLocation() );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000104 }
105
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000106 return this.wrap( config );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000107 }
108
109
110 /* (non-Javadoc)
111 * @see org.osgi.service.cm.ConfigurationAdmin#getConfiguration(java.lang.String, java.lang.String)
112 */
113 public Configuration getConfiguration( String pid, String location ) throws IOException
114 {
Felix Meschberger007c50e2011-10-20 12:39:38 +0000115 // CM 1.4 / 104.13.2.3
116 this.checkPermission( location );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000117
Felix Meschberger007c50e2011-10-20 12:39:38 +0000118 ConfigurationImpl config = configurationManager.getConfiguration( pid, location );
119 if ( config.getBundleLocation() != null )
120 {
121 // CM 1.4 / 104.13.2.3
122 this.checkPermission( config.getBundleLocation() );
123 }
124
125 return this.wrap( config );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000126 }
127
128
129 /* (non-Javadoc)
130 * @see org.osgi.service.cm.ConfigurationAdmin#listConfigurations(java.lang.String)
131 */
132 public Configuration[] listConfigurations( String filter ) throws IOException, InvalidSyntaxException
133 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +0000134 ConfigurationImpl ci[] = configurationManager.listConfigurations( this, filter );
Felix Meschberger0c4e7042008-08-06 07:41:48 +0000135 if ( ci == null || ci.length == 0 )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000136 {
137 return null;
138 }
139
140 Configuration[] cfgs = new Configuration[ci.length];
141 for ( int i = 0; i < cfgs.length; i++ )
142 {
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000143 cfgs[i] = this.wrap( ci[i] );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000144 }
145
146 return cfgs;
147 }
148
149
150 //---------- Security checks ----------------------------------------------
151
152 private Configuration wrap( ConfigurationImpl configuration )
153 {
154 return new ConfigurationAdapter( this, configuration );
155 }
156
157
158 /**
Felix Meschberger35ab36a2009-08-27 20:11:12 +0000159 * Returns <code>true</code> if the current access control context (call
160 * stack) has the CONFIGURE permission.
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000161 */
Felix Meschberger007c50e2011-10-20 12:39:38 +0000162 boolean hasPermission(String name)
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000163 {
Felix Meschberger35ab36a2009-08-27 20:11:12 +0000164 try
165 {
Felix Meschberger007c50e2011-10-20 12:39:38 +0000166 checkPermission(name);
Felix Meschberger35ab36a2009-08-27 20:11:12 +0000167 return true;
168 }
169 catch ( SecurityException se )
170 {
171 return false;
172 }
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000173 }
174
175
176 /**
Felix Meschberger007c50e2011-10-20 12:39:38 +0000177 * Checks whether the current access control context (call stack) has
178 * the given permission for the given bundle location and throws a
Felix Meschberger35ab36a2009-08-27 20:11:12 +0000179 * <code>SecurityException</code> if this is not the case.
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000180 *
Felix Meschberger007c50e2011-10-20 12:39:38 +0000181 * @param name The bundle location to check for permission. If
182 * <code>null</code> assumes <code>*</code>.
183 * @param permission The actual permission to check. This must be one
184 * of the constants defined in the
185 * <code>ConfigurationPermission</code> class.
186 *
187 * @throws SecurityException if the access control context does not
188 * have the appropriate permission
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000189 */
Felix Meschberger007c50e2011-10-20 12:39:38 +0000190 void checkPermission( String name )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000191 {
Felix Meschberger35ab36a2009-08-27 20:11:12 +0000192 // the caller's permission must be checked
193 final SecurityManager sm = System.getSecurityManager();
194 if ( sm != null )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000195 {
Felix Meschberger007c50e2011-10-20 12:39:38 +0000196 if (name == null) {
197 name = "*";
198 }
199
200 sm.checkPermission( new ConfigurationPermission( name, ConfigurationPermission.CONFIGURE ) );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000201 }
202 }
203
204}