admin@glassfish.java.net

Re: Default Values and _at_Param

From: Bill Shannon <bill.shannon_at_oracle.com>
Date: Tue, 18 May 2010 11:15:50 -0700

Ludovic Champenois wrote on 05/18/10 11:02 AM:
>> I'm currently working on getting a prototype running of the Admin
>> Console using the REST API, and I'm having an issue with default
>> values. Using AMX, when we ask the system for the default values for a
>> JDBC Connection Pool, for steadyPoolSize, for example, we get 8. Via
>> REST, we get null. I glanced at the AMX code, and it appears that it
>> is asking JMX for the default value (how that works, specifically, I
>> really don't know). The REST API returns the value of the
>> "defaultValue" attribute on the Param annotation, which causes
>> problems for us, as that value is not always specified.
>>
>> Looking that the class CreateJdbcConnectionPool, we see this code:
>>
>> @Param(name="steadypoolsize", camelCaseName = "steadyPoolSize",
>> optional=true)
>> String steadypoolsize = "8";
>>
>> While defaultValue is not specified, there clearly exists a default,
>> as steadypoolsize is initialized to 8 when an instance of the class is
>> created.
>
> In the serverbeans itself, I see:
>
> @Attribute (defaultValue="8")
> @Min(value=0)
> @Max(value=Integer.MAX_VALUE)
> String getSteadyPoolSize();
>
> So I guess there is not relationship defined between a CLI param and a
> serverbean attribute, including for a default value...
> @Param has a defaultValue attribute itself....
>>
>> My question, then, is how should we handle this? Altering every Param
>> instance to specify a default value would work, though it would be
>> tedious,
> Agree.
>> and, unless we then removed the initialization values on the ivars, we
>> wouldn't be DRY, but removing that might break other code.
>>
>> Another approach might be to populate the defaultValue attribute
>> automatically when CommandModel.getParameters() is called. Ultimately,
>> that method call ends up calling CommandModelImpl.add():
>>
>> private void add(AnnotatedElement e) {
>> if (e.isAnnotationPresent(Param.class)) {
>> ParamModel model = new ParamModelImpl(e);
>> if (!params.containsKey(model.getName())) {
>> params.put(model.getName(), model);
>> }
>> }
>> }
>>
>> It would probably be pretty trivial to check for null and populate if
>> necessary here, but that would require instantiating an instance of
>> the AdminCommand, which may involve side effects and/or performance
>> issues that I can't predict as this is the first I've looked at the
>> code this deeply. :)
>
> Make sense to me...
>>
>> We could also put this second option at the REST layer, and query a
>> live object every time the default values are needed, if this type of
>> behavior is undesirable at the CommandModel level.
> The deeper, the better imo, so unless people disagree I would not want
> this in REST layer.

I looked into adding support to ParamModel for getting these default
values from the fields, but it was just too big of a change. Right
now the model has all the information it needs without reference to
an instance of the class, changing that started to get pretty ugly.
I wasn't happy with the result, so I gave up.

The simple answer is that you have to put the default value in the
annotation. I know, I know, I don't like it either, but I like it
better than the alternative I came up with. Maybe you can do better...