Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
      /**
        * A Service whose service dependency "otherService" filter is configured from ConfigAdmin
        */
      @Service
      class X {
          private Dictionary m_config;
          
          /**
           * Initialize our service from config ... and store the config for later usage (from our init method)
           */ 
          @ConfigurationDependency(pid="MyPid")
          void configure(Dictionary conf) {
               m_config = config;
          }
     
          /**
           * All unnamed dependencies are injected: we can now configure other named
           * dependencies, using the already injected configuration.
           * The returned Map will be used to configure our "otherService" Dependency.
           */
          @Init
          Map init() {
              return new HashMap() {{
                  put("otherService.filter", m_config.get("filter"));
                  put("otherService.required", m_config.get("required"));
              }};
          } 

          /**
           * This named dependency filter/required flag will be configured by our init method (see above).
           */
          @ServiceDependency(name="otherService") 
          void bindOtherService(OtherService other) {
          }
          
          /**
           * All dependencies are injected and our service is now ready to be published.
           * Notice that you can also use the publisher service attribute if you need 
           * to take control on service exposition.
           */
          @Start
          void start() {
          }
      }

@ConfigurationDependency

A configuration dependency is always required, and allows you to depend on the availability of a valid configuration for your component. This dependency requires the OSGi Configuration Admin Service.

Annotation attributes:

  • pid: Returns the pid for a given service (by default, the pid is the service class name).
  • propagate: Returns true if the configuration properties must be published along with the service. Any additional service properties specified directly are merged with these.
  • heading: The label used to display the tab name (or section) where the properties are displayed. Example: "Printer Service".
  • description: A human readable description of the PID this annotation is associated with. Example: "Configuration for the PrinterService bundle".
  • metadata: an array of PropertyMetadaData[] annotation describing property types (see the FactoryConfigurationAdapterService section in the "Writing Components" section.

Usage Examples

In the following example, the "Printer" component depends on a configuration whose PID name is "org.apache.felix.sample.Printer". This service will initialize its ip/port number from the provided configuration:

Code Block

     package org.apache.felix.sample;
     
     @Component
     public class Printer {
         @ConfigurationDependency
         void updated(Dictionary config) {
             // load printer ip/port from the provided dictionary.
         }
     }

This other example shows how to specify a configuration dependency, as well as meta data used to customize the WebConsole GUI. Using these meta data, you can specify for example the default value for your configurations data, some descriptions, the cardinality of configuration values, etc ...

Code Block

     package org.apache.felix.sample;
     
     @Component
     public class Printer {
         @ConfigurationDependency(
             heading = "Printer Service",
             description = "Declare here parameters used to configure the Printer service", 
             metadata = { 
                 @PropertyMetaData(heading = "Ip Address", 
                                   description = "Enter the ip address for the Printer service",
                                   defaults = { "127.0.0.1" }, 
                                   type = String.class,
                                   id = "IPADDR", 
                                   cardinality = 0),
                 @PropertyMetaData(heading = "Port Number", 
                                   description = "Enter the port number for the Printer service",
                                   defaults = { "4444" }, 
                                   type = Integer.class,
                                   id = "PORTNUM", 
                                   cardinality = 0) 

             }
         )
         void updated(Dictionary config) {
             // load configuration from the provided dictionary.
         }

@BundleDependency

@ResourceDependency