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; |
| 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 Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 35 | */ |
| 36 | public 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 Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 55 | bundle = null; |
| 56 | configurationManager = null; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | Bundle getBundle() |
| 61 | { |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 62 | return bundle; |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 63 | } |
| 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 Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 73 | return this.wrap( configurationManager.createFactoryConfiguration( this, factoryPid ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 74 | } |
| 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 Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 82 | // CM 1.4 / 104.13.2.3 |
| 83 | this.checkPermission( location ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 84 | |
Carsten Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 85 | return this.wrap( configurationManager.createFactoryConfiguration( factoryPid, location ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 86 | } |
| 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 Meschberger | 2941ef9 | 2007-08-20 13:15:16 +0000 | [diff] [blame] | 94 | ConfigurationImpl config = configurationManager.getConfiguration( pid, getBundle().getLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 95 | |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 96 | if ( config.getBundleLocation() == null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 97 | { |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 98 | config.setStaticBundleLocation( this.getBundle().getLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 99 | } |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 100 | else if ( !config.getBundleLocation().equals( this.getBundle().getLocation() ) ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 101 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 102 | // CM 1.4 / 104.13.2.3 |
| 103 | this.checkPermission( config.getBundleLocation() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 106 | return this.wrap( config ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 107 | } |
| 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 Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 115 | // CM 1.4 / 104.13.2.3 |
| 116 | this.checkPermission( location ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 117 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 118 | 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 Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 126 | } |
| 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 Ziegeler | eb80d51 | 2007-08-15 11:17:41 +0000 | [diff] [blame] | 134 | ConfigurationImpl ci[] = configurationManager.listConfigurations( this, filter ); |
Felix Meschberger | 0c4e704 | 2008-08-06 07:41:48 +0000 | [diff] [blame] | 135 | if ( ci == null || ci.length == 0 ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 136 | { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | Configuration[] cfgs = new Configuration[ci.length]; |
| 141 | for ( int i = 0; i < cfgs.length; i++ ) |
| 142 | { |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 143 | cfgs[i] = this.wrap( ci[i] ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 144 | } |
| 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 Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 159 | * Returns <code>true</code> if the current access control context (call |
| 160 | * stack) has the CONFIGURE permission. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 161 | */ |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 162 | boolean hasPermission(String name) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 163 | { |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 164 | try |
| 165 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 166 | checkPermission(name); |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | catch ( SecurityException se ) |
| 170 | { |
| 171 | return false; |
| 172 | } |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 177 | * Checks whether the current access control context (call stack) has |
| 178 | * the given permission for the given bundle location and throws a |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 179 | * <code>SecurityException</code> if this is not the case. |
Carsten Ziegeler | 6c6f25b | 2007-08-15 10:04:02 +0000 | [diff] [blame] | 180 | * |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 181 | * @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 Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 189 | */ |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 190 | void checkPermission( String name ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 191 | { |
Felix Meschberger | 35ab36a | 2009-08-27 20:11:12 +0000 | [diff] [blame] | 192 | // the caller's permission must be checked |
| 193 | final SecurityManager sm = System.getSecurityManager(); |
| 194 | if ( sm != null ) |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 195 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 196 | if (name == null) { |
| 197 | name = "*"; |
| 198 | } |
| 199 | |
| 200 | sm.checkPermission( new ConfigurationPermission( name, ConfigurationPermission.CONFIGURE ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | } |