Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 1 | /* |
| 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.scr.integration; |
| 20 | |
| 21 | |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 22 | import static org.ops4j.pax.exam.CoreOptions.junitBundles; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 23 | import static org.ops4j.pax.exam.CoreOptions.mavenBundle; |
| 24 | import static org.ops4j.pax.exam.CoreOptions.options; |
| 25 | import static org.ops4j.pax.exam.CoreOptions.provision; |
Felix Meschberger | 12d8c10 | 2009-12-18 12:47:44 +0000 | [diff] [blame] | 26 | import static org.ops4j.pax.exam.CoreOptions.systemProperty; |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 27 | import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle; |
| 28 | import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 29 | |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 30 | import java.io.File; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
David Jencks | 8584cf9 | 2012-06-16 03:56:11 +0000 | [diff] [blame^] | 33 | import java.io.PrintStream; |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 34 | import java.lang.reflect.Field; |
David Jencks | 8584cf9 | 2012-06-16 03:56:11 +0000 | [diff] [blame^] | 35 | import java.util.Arrays; |
| 36 | import java.util.Collections; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 37 | import java.util.Dictionary; |
| 38 | import java.util.Hashtable; |
David Jencks | 8584cf9 | 2012-06-16 03:56:11 +0000 | [diff] [blame^] | 39 | import java.util.Iterator; |
| 40 | import java.util.TreeSet; |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 41 | |
| 42 | import javax.inject.Inject; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 43 | import junit.framework.TestCase; |
| 44 | |
| 45 | import org.apache.felix.scr.Component; |
David Jencks | 8584cf9 | 2012-06-16 03:56:11 +0000 | [diff] [blame^] | 46 | import org.apache.felix.scr.Reference; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 47 | import org.apache.felix.scr.ScrService; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 48 | import org.junit.After; |
| 49 | import org.junit.Before; |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 50 | import org.ops4j.pax.exam.CoreOptions; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 51 | import org.ops4j.pax.exam.Option; |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 52 | import org.ops4j.pax.exam.OptionUtils; |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 53 | import org.ops4j.pax.exam.TestProbeBuilder; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 54 | import org.ops4j.pax.exam.junit.Configuration; |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 55 | import org.ops4j.pax.exam.junit.ProbeBuilder; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 56 | import org.osgi.framework.Bundle; |
| 57 | import org.osgi.framework.BundleContext; |
| 58 | import org.osgi.framework.BundleException; |
| 59 | import org.osgi.framework.Constants; |
| 60 | import org.osgi.framework.InvalidSyntaxException; |
| 61 | import org.osgi.service.cm.ConfigurationAdmin; |
| 62 | import org.osgi.util.tracker.ServiceTracker; |
| 63 | |
| 64 | |
| 65 | public abstract class ComponentTestBase |
| 66 | { |
| 67 | |
| 68 | @Inject |
| 69 | protected BundleContext bundleContext; |
| 70 | |
| 71 | protected Bundle bundle; |
| 72 | |
| 73 | protected ServiceTracker scrTracker; |
| 74 | |
| 75 | protected ServiceTracker configAdminTracker; |
| 76 | |
Felix Meschberger | 55e3b97 | 2009-10-12 07:16:08 +0000 | [diff] [blame] | 77 | // the name of the system property providing the bundle file to be installed and tested |
| 78 | protected static final String BUNDLE_JAR_SYS_PROP = "project.bundle.file"; |
| 79 | |
| 80 | // the default bundle jar file name |
| 81 | protected static final String BUNDLE_JAR_DEFAULT = "target/scr.jar"; |
| 82 | |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 83 | protected static final String PROP_NAME = "theValue"; |
| 84 | protected static final Dictionary<String, String> theConfig; |
| 85 | |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 86 | // the JVM option to set to enable remote debugging |
| 87 | protected static final String DEBUG_VM_OPTION = "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=30303"; |
| 88 | |
| 89 | // the actual JVM option set, extensions may implement a static |
| 90 | // initializer overwriting this value to have the configuration() |
| 91 | // method include it when starting the OSGi framework JVM |
| 92 | protected static String paxRunnerVmOption = null; |
| 93 | |
Felix Meschberger | f975aa5 | 2009-08-24 10:37:44 +0000 | [diff] [blame] | 94 | // the descriptor file to use for the installed test bundle |
| 95 | protected static String descriptorFile = "/integration_test_simple_components.xml"; |
| 96 | |
David Jencks | bad95fc | 2012-06-05 23:29:58 +0000 | [diff] [blame] | 97 | protected static boolean NONSTANDARD_COMPONENT_FACTORY_BEHAVIOR = false; |
| 98 | |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 99 | static |
| 100 | { |
| 101 | theConfig = new Hashtable<String, String>(); |
| 102 | theConfig.put( PROP_NAME, PROP_NAME ); |
| 103 | } |
| 104 | |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 105 | @ProbeBuilder |
| 106 | public TestProbeBuilder extendProbe(TestProbeBuilder builder) { |
David Jencks | a9cbe1b | 2012-06-15 21:37:30 +0000 | [diff] [blame] | 107 | builder.setHeader("Export-Package", "org.apache.felix.scr.integration.components,org.apache.felix.scr.integration.components.activatesignature,org.apache.felix.scr.integration.components.circular"); |
Felix Meschberger | 8422a30 | 2012-03-08 05:55:06 +0000 | [diff] [blame] | 108 | builder.setHeader("Import-Package", "org.apache.felix.scr,org.apache.felix.scr.component;mandatory:=\"status\"; status=\"provisional\""); |
| 109 | builder.setHeader("Bundle-ManifestVersion", "2"); |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 110 | return builder; |
| 111 | } |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 112 | |
| 113 | @Configuration |
| 114 | public static Option[] configuration() |
| 115 | { |
Felix Meschberger | 55e3b97 | 2009-10-12 07:16:08 +0000 | [diff] [blame] | 116 | final String bundleFileName = System.getProperty( BUNDLE_JAR_SYS_PROP, BUNDLE_JAR_DEFAULT ); |
| 117 | final File bundleFile = new File( bundleFileName ); |
| 118 | if ( !bundleFile.canRead() ) |
| 119 | { |
| 120 | throw new IllegalArgumentException( "Cannot read from bundle file " + bundleFileName + " specified in the " |
| 121 | + BUNDLE_JAR_SYS_PROP + " system property" ); |
| 122 | } |
| 123 | |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 124 | final Option[] base = options( |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 125 | provision( |
Felix Meschberger | 55e3b97 | 2009-10-12 07:16:08 +0000 | [diff] [blame] | 126 | CoreOptions.bundle( bundleFile.toURI().toString() ), |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 127 | mavenBundle( "org.ops4j.pax.tinybundles", "tinybundles", "1.0.0" ), |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 128 | mavenBundle( "org.apache.felix", "org.apache.felix.configadmin", "1.0.10" ) |
Felix Meschberger | 12d8c10 | 2009-12-18 12:47:44 +0000 | [diff] [blame] | 129 | ), |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 130 | junitBundles(), |
David Jencks | bad95fc | 2012-06-05 23:29:58 +0000 | [diff] [blame] | 131 | systemProperty( "ds.factory.enabled" ).value( Boolean.toString( NONSTANDARD_COMPONENT_FACTORY_BEHAVIOR ) ) |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 132 | |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 133 | ); |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 134 | final Option vmOption = ( paxRunnerVmOption != null ) ? CoreOptions.vmOption( paxRunnerVmOption ) : null; |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 135 | return OptionUtils.combine( base, vmOption ); |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | @Before |
| 140 | public void setUp() throws BundleException |
| 141 | { |
Felix Meschberger | f873dbe | 2011-02-04 10:50:18 +0000 | [diff] [blame] | 142 | scrTracker = new ServiceTracker( bundleContext, "org.apache.felix.scr.ScrService", null ); |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 143 | scrTracker.open(); |
Felix Meschberger | f873dbe | 2011-02-04 10:50:18 +0000 | [diff] [blame] | 144 | configAdminTracker = new ServiceTracker( bundleContext, "org.osgi.service.cm.ConfigurationAdmin", null ); |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 145 | configAdminTracker.open(); |
| 146 | |
Felix Meschberger | f975aa5 | 2009-08-24 10:37:44 +0000 | [diff] [blame] | 147 | bundle = installBundle( descriptorFile ); |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 148 | bundle.start(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | @After |
| 153 | public void tearDown() throws BundleException |
| 154 | { |
Felix Meschberger | f975aa5 | 2009-08-24 10:37:44 +0000 | [diff] [blame] | 155 | if ( bundle != null && bundle.getState() != Bundle.UNINSTALLED ) |
| 156 | { |
| 157 | bundle.uninstall(); |
| 158 | bundle = null; |
| 159 | } |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 160 | |
| 161 | configAdminTracker.close(); |
| 162 | configAdminTracker = null; |
| 163 | scrTracker.close(); |
| 164 | scrTracker = null; |
| 165 | } |
| 166 | |
| 167 | |
Felix Meschberger | ed21c03 | 2010-01-27 09:58:17 +0000 | [diff] [blame] | 168 | protected Component[] getComponents() |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 169 | { |
| 170 | ScrService scr = ( ScrService ) scrTracker.getService(); |
| 171 | if ( scr != null ) |
| 172 | { |
Felix Meschberger | ed21c03 | 2010-01-27 09:58:17 +0000 | [diff] [blame] | 173 | return scr.getComponents(); |
| 174 | } |
| 175 | |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | protected Component findComponentByName( String name ) |
| 181 | { |
Felix Meschberger | 5fa8b0e | 2010-08-03 08:21:13 +0000 | [diff] [blame] | 182 | Component[] components = findComponentsByName( name ); |
| 183 | if ( components != null && components.length > 0 ) |
Felix Meschberger | ed21c03 | 2010-01-27 09:58:17 +0000 | [diff] [blame] | 184 | { |
Felix Meschberger | 5fa8b0e | 2010-08-03 08:21:13 +0000 | [diff] [blame] | 185 | return components[0]; |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | protected Component[] findComponentsByName( String name ) |
| 193 | { |
Felix Meschberger | 5fa8b0e | 2010-08-03 08:21:13 +0000 | [diff] [blame] | 194 | ScrService scr = ( ScrService ) scrTracker.getService(); |
| 195 | if ( scr != null ) |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 196 | { |
Felix Meschberger | 5fa8b0e | 2010-08-03 08:21:13 +0000 | [diff] [blame] | 197 | return scr.getComponents( name ); |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | protected static void delay() |
| 205 | { |
| 206 | try |
| 207 | { |
| 208 | Thread.sleep( 300 ); |
| 209 | } |
| 210 | catch ( InterruptedException ie ) |
| 211 | { |
| 212 | // dont care |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | protected ConfigurationAdmin getConfigurationAdmin() |
| 218 | { |
| 219 | ConfigurationAdmin ca = ( ConfigurationAdmin ) configAdminTracker.getService(); |
| 220 | if ( ca == null ) |
| 221 | { |
| 222 | TestCase.fail( "Missing ConfigurationAdmin service" ); |
| 223 | } |
| 224 | return ca; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | protected void configure( String pid ) |
| 229 | { |
| 230 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 231 | try |
| 232 | { |
| 233 | org.osgi.service.cm.Configuration config = ca.getConfiguration( pid, null ); |
| 234 | config.update( theConfig ); |
| 235 | } |
| 236 | catch ( IOException ioe ) |
| 237 | { |
| 238 | TestCase.fail( "Failed updating configuration " + pid + ": " + ioe.toString() ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | protected void deleteConfig( String pid ) |
| 244 | { |
| 245 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 246 | try |
| 247 | { |
| 248 | org.osgi.service.cm.Configuration config = ca.getConfiguration( pid ); |
| 249 | config.delete(); |
| 250 | } |
| 251 | catch ( IOException ioe ) |
| 252 | { |
| 253 | TestCase.fail( "Failed deleting configuration " + pid + ": " + ioe.toString() ); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | protected String createFactoryConfiguration( String factoryPid ) |
| 259 | { |
| 260 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 261 | try |
| 262 | { |
| 263 | org.osgi.service.cm.Configuration config = ca.createFactoryConfiguration( factoryPid, null ); |
| 264 | config.update( theConfig ); |
| 265 | return config.getPid(); |
| 266 | } |
| 267 | catch ( IOException ioe ) |
| 268 | { |
| 269 | TestCase.fail( "Failed updating factory configuration " + factoryPid + ": " + ioe.toString() ); |
| 270 | return null; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | |
| 275 | protected void deleteFactoryConfigurations( String factoryPid ) |
| 276 | { |
| 277 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 278 | try |
| 279 | { |
| 280 | final String filter = "(service.factoryPid=" + factoryPid + ")"; |
| 281 | org.osgi.service.cm.Configuration[] configs = ca.listConfigurations( filter ); |
| 282 | if ( configs != null ) |
| 283 | { |
| 284 | for ( org.osgi.service.cm.Configuration configuration : configs ) |
| 285 | { |
| 286 | configuration.delete(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | catch ( InvalidSyntaxException ise ) |
| 291 | { |
| 292 | // unexpected |
| 293 | } |
| 294 | catch ( IOException ioe ) |
| 295 | { |
| 296 | TestCase.fail( "Failed deleting configurations " + factoryPid + ": " + ioe.toString() ); |
| 297 | } |
| 298 | } |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | protected static Class<?> getType( Object object, String desiredName ) |
| 302 | { |
| 303 | Class<?> ccImpl = object.getClass(); |
| 304 | while ( ccImpl != null && !desiredName.equals( ccImpl.getSimpleName() ) ) |
| 305 | { |
| 306 | ccImpl = ccImpl.getSuperclass(); |
| 307 | } |
| 308 | if ( ccImpl == null ) |
| 309 | { |
| 310 | TestCase.fail( "ComponentContext " + object + " is not a " + desiredName ); |
| 311 | } |
| 312 | |
| 313 | return ccImpl; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | protected static Object getFieldValue( Object object, String fieldName ) |
| 318 | { |
| 319 | try |
| 320 | { |
| 321 | final Field m_componentsField = getField( object.getClass(), fieldName ); |
| 322 | return m_componentsField.get( object ); |
| 323 | } |
| 324 | catch ( Throwable t ) |
| 325 | { |
| 326 | TestCase.fail( "Cannot get " + fieldName + " from " + object + ": " + t ); |
| 327 | return null; // keep the compiler happy |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | protected static Field getField( Class<?> type, String fieldName ) throws NoSuchFieldException |
| 333 | { |
Pierre De Rop | ce7ca4d | 2012-06-15 16:24:54 +0000 | [diff] [blame] | 334 | Class<?> clazz = type; |
| 335 | while (clazz != null) |
| 336 | { |
| 337 | Field[] fields = clazz.getDeclaredFields(); |
| 338 | for (int i = 0; i < fields.length; i++) |
| 339 | { |
| 340 | Field field = fields[i]; |
| 341 | if (field.getName().equals(fieldName)) |
| 342 | { |
| 343 | field.setAccessible( true ); |
| 344 | return field; |
| 345 | } |
| 346 | } |
| 347 | clazz = clazz.getSuperclass(); |
| 348 | } |
| 349 | throw new NoSuchFieldException(fieldName); |
Felix Meschberger | 2e88542 | 2009-08-21 11:35:00 +0000 | [diff] [blame] | 350 | } |
Felix Meschberger | f975aa5 | 2009-08-24 10:37:44 +0000 | [diff] [blame] | 351 | |
| 352 | |
| 353 | protected Bundle installBundle( final String descriptorFile ) throws BundleException |
| 354 | { |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 355 | final InputStream bundleStream = bundle() |
| 356 | .add("OSGI-INF/components.xml", getClass().getResource(descriptorFile)) |
| 357 | |
| 358 | .set(Constants.BUNDLE_SYMBOLICNAME, "simplecomponent") |
| 359 | .set(Constants.BUNDLE_VERSION, "0.0.11") |
| 360 | .set(Constants.IMPORT_PACKAGE, |
David Jencks | a9cbe1b | 2012-06-15 21:37:30 +0000 | [diff] [blame] | 361 | "org.apache.felix.scr.integration.components,org.apache.felix.scr.integration.components.activatesignature,org.apache.felix.scr.integration.components.circular") |
Felix Meschberger | 4f43c91 | 2012-03-08 04:37:56 +0000 | [diff] [blame] | 362 | .set("Service-Component", "OSGI-INF/components.xml") |
| 363 | .build(withBnd()); |
Felix Meschberger | f975aa5 | 2009-08-24 10:37:44 +0000 | [diff] [blame] | 364 | |
| 365 | try |
| 366 | { |
| 367 | final String location = "test:SimpleComponent/" + System.currentTimeMillis(); |
| 368 | return bundleContext.installBundle( location, bundleStream ); |
| 369 | } |
| 370 | finally |
| 371 | { |
| 372 | try |
| 373 | { |
| 374 | bundleStream.close(); |
| 375 | } |
| 376 | catch ( IOException ioe ) |
| 377 | { |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
David Jencks | 8584cf9 | 2012-06-16 03:56:11 +0000 | [diff] [blame^] | 382 | //Code copied from ScrCommand to make it easier to find out what your test components are actually doing. |
| 383 | // @Test |
| 384 | public void testDescription() |
| 385 | { |
| 386 | PrintStream out = System.out; |
| 387 | info( out ); |
| 388 | } |
| 389 | |
| 390 | void info( PrintStream out ) |
| 391 | { |
| 392 | Component[] components = getComponents(); |
| 393 | if ( components == null ) |
| 394 | { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | for ( int j = 0; j < components.length; j++ ) |
| 399 | { |
| 400 | Component component = components[j]; |
| 401 | out.print( "ID: " ); |
| 402 | out.println( component.getId() ); |
| 403 | out.print( "Name: " ); |
| 404 | out.println( component.getName() ); |
| 405 | out.print( "Bundle: " ); |
| 406 | out.println( component.getBundle().getSymbolicName() + " (" + component.getBundle().getBundleId() + ")" ); |
| 407 | out.print( "State: " ); |
| 408 | out.println( toStateString( component.getState() ) ); |
| 409 | out.print( "Default State: " ); |
| 410 | out.println( component.isDefaultEnabled() ? "enabled" : "disabled" ); |
| 411 | out.print( "Activation: " ); |
| 412 | out.println( component.isImmediate() ? "immediate" : "delayed" ); |
| 413 | |
| 414 | // DS 1.1 new features |
| 415 | out.print( "Configuration Policy: " ); |
| 416 | out.println( component.getConfigurationPolicy() ); |
| 417 | out.print( "Activate Method: " ); |
| 418 | out.print( component.getActivate() ); |
| 419 | if ( component.isActivateDeclared() ) |
| 420 | { |
| 421 | out.print( " (declared in the descriptor)" ); |
| 422 | } |
| 423 | out.println(); |
| 424 | out.print( "Deactivate Method: " ); |
| 425 | out.print( component.getDeactivate() ); |
| 426 | if ( component.isDeactivateDeclared() ) |
| 427 | { |
| 428 | out.print( " (declared in the descriptor)" ); |
| 429 | } |
| 430 | out.println(); |
| 431 | out.print( "Modified Method: " ); |
| 432 | if ( component.getModified() != null ) |
| 433 | { |
| 434 | out.print( component.getModified() ); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | out.print( "-" ); |
| 439 | } |
| 440 | out.println(); |
| 441 | |
| 442 | if ( component.getFactory() != null ) |
| 443 | { |
| 444 | out.print( "Factory: " ); |
| 445 | out.println( component.getFactory() ); |
| 446 | } |
| 447 | |
| 448 | String[] services = component.getServices(); |
| 449 | if ( services != null ) |
| 450 | { |
| 451 | out.print( "Services: " ); |
| 452 | out.println( services[0] ); |
| 453 | for ( int i = 1; i < services.length; i++ ) |
| 454 | { |
| 455 | out.print( " " ); |
| 456 | out.println( services[i] ); |
| 457 | } |
| 458 | out.print( "Service Type: " ); |
| 459 | out.println( component.isServiceFactory() ? "service factory" : "service" ); |
| 460 | } |
| 461 | |
| 462 | Reference[] refs = component.getReferences(); |
| 463 | if ( refs != null ) |
| 464 | { |
| 465 | for ( int i = 0; i < refs.length; i++ ) |
| 466 | { |
| 467 | out.print( "Reference: " ); |
| 468 | out.println( refs[i].getName() ); |
| 469 | out.print( " Satisfied: " ); |
| 470 | out.println( refs[i].isSatisfied() ? "satisfied" : "unsatisfied" ); |
| 471 | out.print( " Service Name: " ); |
| 472 | out.println( refs[i].getServiceName() ); |
| 473 | if ( refs[i].getTarget() != null ) |
| 474 | { |
| 475 | out.print( " Target Filter: " ); |
| 476 | out.println( refs[i].getTarget() ); |
| 477 | } |
| 478 | out.print( " Multiple: " ); |
| 479 | out.println( refs[i].isMultiple() ? "multiple" : "single" ); |
| 480 | out.print( " Optional: " ); |
| 481 | out.println( refs[i].isOptional() ? "optional" : "mandatory" ); |
| 482 | out.print( " Policy: " ); |
| 483 | out.println( refs[i].isStatic() ? "static" : "dynamic" ); |
| 484 | out.print( " Policy option: " ); |
| 485 | out.println( refs[i].isReluctant() ? "reluctant" : "greedy" ); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | Dictionary props = component.getProperties(); |
| 490 | if ( props != null ) |
| 491 | { |
| 492 | out.println( "Properties:" ); |
| 493 | TreeSet keys = new TreeSet( Collections.list( props.keys() ) ); |
| 494 | for ( Iterator ki = keys.iterator(); ki.hasNext(); ) |
| 495 | { |
| 496 | Object key = ki.next(); |
| 497 | out.print( " " ); |
| 498 | out.print( key ); |
| 499 | out.print( " = " ); |
| 500 | |
| 501 | Object prop = props.get( key ); |
| 502 | if ( prop.getClass().isArray() ) |
| 503 | { |
| 504 | prop = Arrays.asList( ( Object[] ) prop ); |
| 505 | } |
| 506 | out.print( prop ); |
| 507 | |
| 508 | out.println(); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private String toStateString( int state ) |
| 515 | { |
| 516 | switch ( state ) |
| 517 | { |
| 518 | case Component.STATE_DISABLED: |
| 519 | return "disabled"; |
| 520 | case Component.STATE_UNSATISFIED: |
| 521 | return "unsatisfied"; |
| 522 | case Component.STATE_ACTIVE: |
| 523 | return "active"; |
| 524 | case Component.STATE_REGISTERED: |
| 525 | return "registered"; |
| 526 | case Component.STATE_FACTORY: |
| 527 | return "factory"; |
| 528 | case Component.STATE_DISPOSED: |
| 529 | return "disposed"; |
| 530 | |
| 531 | case Component.STATE_ENABLING: |
| 532 | return "enabling"; |
| 533 | case Component.STATE_ENABLED: |
| 534 | return "enabled"; |
| 535 | case Component.STATE_ACTIVATING: |
| 536 | return "activating"; |
| 537 | case Component.STATE_DEACTIVATING: |
| 538 | return "deactivating"; |
| 539 | case Component.STATE_DISABLING: |
| 540 | return "disabling"; |
| 541 | case Component.STATE_DISPOSING: |
| 542 | return "disposing"; |
| 543 | default: |
| 544 | return String.valueOf( state ); |
| 545 | } |
| 546 | } |
| 547 | |
Felix Meschberger | a6ad976 | 2009-08-19 13:38:56 +0000 | [diff] [blame] | 548 | } |