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; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 23 | import org.osgi.framework.Bundle; |
| 24 | import org.osgi.framework.InvalidSyntaxException; |
| 25 | import org.osgi.service.cm.Configuration; |
| 26 | import org.osgi.service.cm.ConfigurationAdmin; |
| 27 | import org.osgi.service.cm.ConfigurationPermission; |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 28 | import org.osgi.service.log.LogService; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | /** |
| 32 | * The <code>ConfigurationAdminImpl</code> is the per-bundle frontend to the |
| 33 | * configuration manager. Instances of this class are created on-demand for |
| 34 | * each bundle trying to get hold of the <code>ConfigurationAdmin</code> |
| 35 | * service. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 36 | */ |
| 37 | public class ConfigurationAdminImpl implements ConfigurationAdmin |
| 38 | { |
| 39 | |
| 40 | // The configuration manager to which most of the tasks are delegated |
| 41 | private ConfigurationManager configurationManager; |
| 42 | |
| 43 | // The bundle for which this instance has been created |
| 44 | private Bundle bundle; |
| 45 | |
| 46 | |
| 47 | ConfigurationAdminImpl( ConfigurationManager configurationManager, Bundle bundle ) |
| 48 | { |
| 49 | this.configurationManager = configurationManager; |
| 50 | this.bundle = bundle; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void dispose() |
| 55 | { |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 56 | bundle = null; |
| 57 | configurationManager = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | Bundle getBundle() |
| 62 | { |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 63 | return bundle; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | //---------- ConfigurationAdmin interface --------------------------------- |
| 68 | |
| 69 | /* (non-Javadoc) |
| 70 | * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String) |
| 71 | */ |
| 72 | public Configuration createFactoryConfiguration( String factoryPid ) throws IOException |
| 73 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 74 | configurationManager.log( LogService.LOG_DEBUG, "createFactoryConfiguration(factoryPid={0})", new Object[] |
| 75 | { factoryPid } ); |
| 76 | |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 77 | return this.wrap( configurationManager.createFactoryConfiguration( this, factoryPid ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | /* (non-Javadoc) |
| 82 | * @see org.osgi.service.cm.ConfigurationAdmin#createFactoryConfiguration(java.lang.String, java.lang.String) |
| 83 | */ |
| 84 | public Configuration createFactoryConfiguration( String factoryPid, String location ) throws IOException |
| 85 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 86 | configurationManager.log( LogService.LOG_DEBUG, "createFactoryConfiguration(factoryPid={0}, location={1})", |
| 87 | new Object[] |
| 88 | { factoryPid, location } ); |
| 89 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 90 | // CM 1.4 / 104.13.2.3 |
| 91 | this.checkPermission( location ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 92 | |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 93 | return this.wrap( configurationManager.createFactoryConfiguration( factoryPid, location ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | /* (non-Javadoc) |
| 98 | * @see org.osgi.service.cm.ConfigurationAdmin#getConfiguration(java.lang.String) |
| 99 | */ |
| 100 | public Configuration getConfiguration( String pid ) throws IOException |
| 101 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 102 | configurationManager.log( LogService.LOG_DEBUG, "getConfiguration(pid={0})", new Object[] |
| 103 | { pid } ); |
| 104 | |
Felix Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 105 | ConfigurationImpl config = configurationManager.getConfiguration( pid, getBundle().getLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 106 | |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 107 | if ( config.getBundleLocation() == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 108 | { |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 109 | configurationManager.log( LogService.LOG_DEBUG, "Binding configuration {0} (isNew: {1}) to bundle {2}", |
| 110 | new Object[] |
| 111 | { config.getPid(), Boolean.valueOf( config.isNew() ), getBundle().getLocation() } ); |
| 112 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 113 | config.setStaticBundleLocation( this.getBundle().getLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 114 | } |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 115 | else if ( !config.getBundleLocation().equals( this.getBundle().getLocation() ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 116 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 117 | // CM 1.4 / 104.13.2.3 |
| 118 | this.checkPermission( config.getBundleLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 121 | return this.wrap( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | /* (non-Javadoc) |
| 126 | * @see org.osgi.service.cm.ConfigurationAdmin#getConfiguration(java.lang.String, java.lang.String) |
| 127 | */ |
| 128 | public Configuration getConfiguration( String pid, String location ) throws IOException |
| 129 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 130 | configurationManager.log( LogService.LOG_DEBUG, "getConfiguration(pid={0}, location={1})", new Object[] |
| 131 | { pid, location } ); |
| 132 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 133 | // CM 1.4 / 104.13.2.3 |
| 134 | this.checkPermission( location ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 135 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 136 | ConfigurationImpl config = configurationManager.getConfiguration( pid, location ); |
| 137 | if ( config.getBundleLocation() != null ) |
| 138 | { |
| 139 | // CM 1.4 / 104.13.2.3 |
| 140 | this.checkPermission( config.getBundleLocation() ); |
| 141 | } |
| 142 | |
| 143 | return this.wrap( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | /* (non-Javadoc) |
| 148 | * @see org.osgi.service.cm.ConfigurationAdmin#listConfigurations(java.lang.String) |
| 149 | */ |
| 150 | public Configuration[] listConfigurations( String filter ) throws IOException, InvalidSyntaxException |
| 151 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 152 | configurationManager.log( LogService.LOG_DEBUG, "listConfigurations(filter={0})", new Object[] |
| 153 | { filter } ); |
| 154 | |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 155 | ConfigurationImpl ci[] = configurationManager.listConfigurations( this, filter ); |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 156 | if ( ci == null || ci.length == 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 157 | { |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | Configuration[] cfgs = new Configuration[ci.length]; |
| 162 | for ( int i = 0; i < cfgs.length; i++ ) |
| 163 | { |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 164 | cfgs[i] = this.wrap( ci[i] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return cfgs; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | //---------- Security checks ---------------------------------------------- |
| 172 | |
| 173 | private Configuration wrap( ConfigurationImpl configuration ) |
| 174 | { |
| 175 | return new ConfigurationAdapter( this, configuration ); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 180 | * Returns <code>true</code> if the current access control context (call |
| 181 | * stack) has the CONFIGURE permission. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 182 | */ |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 183 | boolean hasPermission(String name) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 184 | { |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 185 | try |
| 186 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 187 | checkPermission(name); |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | catch ( SecurityException se ) |
| 191 | { |
| 192 | return false; |
| 193 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 198 | * Checks whether the current access control context (call stack) has |
| 199 | * the given permission for the given bundle location and throws a |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 200 | * <code>SecurityException</code> if this is not the case. |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 201 | * |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 202 | * @param name The bundle location to check for permission. If this |
| 203 | * is <code>null</code> or exactly matches the using bundle's |
| 204 | * location, permission is always granted. |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 205 | * |
| 206 | * @throws SecurityException if the access control context does not |
| 207 | * have the appropriate permission |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 208 | */ |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 209 | void checkPermission( String name ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 210 | { |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 211 | // the caller's permission must be checked |
| 212 | final SecurityManager sm = System.getSecurityManager(); |
| 213 | if ( sm != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 214 | { |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 215 | // CM 1.4 / 104.11.1 Implicit permission |
| 216 | if ( name != null && !name.equals( getBundle().getLocation() ) ) |
| 217 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 218 | try |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 219 | { |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 220 | sm.checkPermission( new ConfigurationPermission( name, ConfigurationPermission.CONFIGURE ) ); |
| 221 | |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 222 | configurationManager.log( LogService.LOG_DEBUG, |
| 223 | "Explicit Permission; grant CONFIGURE permission on configuration bound to {0} to bundle {1}", |
| 224 | new Object[] |
| 225 | { name, getBundle().getLocation() } ); |
| 226 | } |
Felix Meschberger | f0c5a3d | 2011-11-04 10:47:58 +0000 | [diff] [blame^] | 227 | catch ( SecurityException se ) |
| 228 | { |
| 229 | configurationManager |
| 230 | .log( |
| 231 | LogService.LOG_DEBUG, |
| 232 | "No Permission; denied CONFIGURE permission on configuration bound to {0} to bundle {1}; reason: {2}", |
| 233 | new Object[] |
| 234 | { name, getBundle().getLocation(), se.getMessage() } ); |
| 235 | throw se; |
| 236 | } |
Felix Meschberger | fb833e7 | 2011-10-27 06:12:37 +0000 | [diff] [blame] | 237 | } |
| 238 | else if ( configurationManager.isLogEnabled( LogService.LOG_DEBUG ) ) |
| 239 | { |
| 240 | configurationManager.log( LogService.LOG_DEBUG, |
| 241 | "Implicit Permission; grant CONFIGURE permission on configuration bound to {0} to bundle {1}", |
| 242 | new Object[] |
| 243 | { name, getBundle().getLocation() } ); |
| 244 | |
| 245 | } |
| 246 | } |
| 247 | else if ( configurationManager.isLogEnabled( LogService.LOG_DEBUG ) ) |
| 248 | { |
| 249 | configurationManager.log( LogService.LOG_DEBUG, |
| 250 | "No SecurityManager installed; grant CONFIGURE permission on configuration bound to {0} to bundle {1}", |
| 251 | new Object[] |
| 252 | { name, getBundle().getLocation() } ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | } |