admin@glassfish.java.net

Re: Seeking Guidance

From: Bill Shannon <bill.shannon_at_sun.com>
Date: Thu, 15 Apr 2010 14:24:41 -0700

Jerome Dochez wrote on 04/15/2010 01:58 PM:
> On Apr 15, 2010, at 1:53 PM, Byron Nevins wrote:
>
>> Jerome, Bill
>>
>> I'm about to change a bunch of classes that assume the current server's name is "server".
>>
>> BEFORE:
>> @Param(optional=true)
>> String target = SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME;
>>
>> AFTER:
>> // add implements PostConstruct if necessary
>>
>> @Inject
>> private ServerEnvironment env;
>>
>> @Param(optional=true)
>> String target;
>>
>> public void postConstruct() {
>> if(target == null)
>> target = env.getInstanceName();
>> }
>>
>> ===============================
>>
>> I'm doing the more complicated postConstruct() technique because I don't think I can get away with doing the following with 100% certainty.
>>
>> @Inject
>> private ServerEnvironment env;
>>
>> @Param(optional=true)
>> String target = env.getInstanceName();
>>
>> =================================
>>
>> Is it guaranteed that the above will work (i.e. order of evaluation by HK2)?
>> This is my injection solution. I normally would just use the old-fashioned way of setting a system-wide final String with the instance's name.
>>
>> What do you recommend?
>
> In order to do 2, you need to ensure that @Inject is injected before @Param (which I think is the case), look in the AdminAdapter for the code. You also need to then change the AdminCommand javadocs to state the order of injection between the two annotations so it is an explicit contract.
>
> then you can use option 1.
>
> otherwise option 2 is clunky yet safe.

I'm pretty sure the order of injection will happen to give you what
you want, but it seems dangerous to depend on. I'm not convinced that
postConstruct will give you what you want. As I understand it, @Inject
is handled by the habitat when it creates the object. The habitat then
calls PostConstruct. The injection for @Param is done *after* the
habitat returns the object, and thus after postConstruct is called.

So I guess the question is whether to depend on this ordering, or to
just handle this in the code...

        public void execute(AdminCommandContext context) {
            if (target == null)
                target = env.getInstanceName();

or in the code that uses target...

            target != null ? target : env.getInstanceName()

How exactly will target be used? If it's commonly passed to another
method to get (e.g.) the Server object or something like that, maybe
*that* method should be enhanced so that null means "use the current
instance name"?