2

I have a component MyComponent that needs to access AConfiguration and BConfiguration:

my.MyComponent:

@Component(
    configurationPid = "[my.AConfiguration,my.BConfiguration]"
)
public class MyComponent {
    @Activate
    @Modified
    protected void activate(Map<String, Object> properties) {
        _aConfiguration = ConfigurableUtil
            .createConfigurable(AConfiguration.class, properties);
        _bConfiguration = ConfigurableUtil
            .createConfigurable(BConfiguration.class, properties);
    }

    public void hello() {
        System.out.println("A:" + _sConfiguration.valueA());
        System.out.println("B:" + _sConfiguration.valueB());
    }
}

my.AConfiguration:

@Meta.OCD(
    id = "my.AConfiguration"
)
public interface AConfiguration {
    @Meta.AD(deflt = "6")
    public long valueA();
}

my.BConfiguration:

@Meta.OCD(
    id = "my.BConfiguration"
)
public interface BConfiguration {
    @Meta.AD(deflt = "6")
    public long valueB();
}

Problem: Configuring valueA and valueB to 7 using Liferay's configuration UI has no effect, MyComponent.hello() still sees the default value 6.

What am I doing wrong?
What is the correct way to make my component use configuration information from several configuration interfaces?

Use case: My component does some business processing and saves results to a remote server. There is one configuration interface containing the business processing settings, and one configuration interface containing the URL to the remote server.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

1 Answers1

5

The format of the configurationPid attribute on the @Component annotation is wrong. It should be:

configurationPid = { "my.AConfiguration", "my.BConfiguration" }

This creates a String-array value with two entries, my.AConfiguration and my.BConfiguration. Instead you have used:

configurationPid = "[my.AConfiguration,my.BConfiguration]"

... which creates a single String with the literal value [my.AConfiguration,my.BConfiguration], which is almost certainly not what you want.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Oh, interesting! So using several configurations is OK, if using this syntax? Thanks! – Nicolas Raoul Dec 27 '17 at 09:43
  • 1
    Yes. This is the standard Java syntax for creating a String-array value in an annotation attribute. Unfortunately Java creates ambiguity because it allows for a single-entry array to be created without the braces. That is, `configurationPid = "foo"` is equivalent to `configurationPid = { "foo" }` – Neil Bartlett Dec 27 '17 at 09:54