admin@glassfish.java.net

Re: cli command ++

From: Carla Mott <carla.mott_at_oracle.com>
Date: Thu, 27 May 2010 23:33:57 -0700

I'm replying to this since I have the command working now and thought I
would share what I learned. I have not checked in this code yet because
I need to clean this up, update my workspace and run tests but this does
work.

First the xml that I'm trying to create in domain.xml looks like:

<nodes>
 <node name="node7" node-home="/export/glassfishv3" >
      <ssh-connector ssh-port="22" />
    </node>
</nodes>

The interesting part is I'm creating an element 'node' that has a
sub-element 'ssh-connector'

I create a class called Node that has the params and arguments that it
needs. I also want to add the sub-element and I do that by adding the
following:


    @Element // no Params is speicified here and if you look at
Cluster.java know that it is a special case
    List <SshConnector> getSshConnector();



I need an other class called SshConnector which will handle the
attributes for that sub-element. Finally I add a decorator to my Node
class which will create the sub-element ssh-connector. The code is
below. The decorator is called because in it is specified on the CRUD
command in my Nodes class. See Jerome's write up for details. The
values of the options for the command are automatically injected in the
decorator so you have access to what was on the commandline. There is a
'createChild' method on the instance which creates the sub-element and
an 'add' method which allows you to update the values in the newly
created sub-element. When you do an 'add' it is written to domain.xml.
The code below needs to be cleaned up some but it shows how to get an
element created and add values to that element.


   @Service
    @Scoped(PerLookup.class)
    class Decorator implements CreationDecorator<Node> {
        @Param(name="sshport",optional=true)
        String sshPort="22";

        @Inject
        Habitat habitat;

        @Inject
        ServerEnvironment env;

        @Inject
        Domain domain;

        /**
         * Decorates the newly CRUD created cluster configuration instance.
         * tasks :
         * - ensures that it references an existing configuration
         * - creates a new config from the default-config if no
config-ref
         * was provided.
         * - check for deprecated parameters.
         *
         * @param context administration command context
         * @param instance newly created configuration element
         * @throws TransactionFailure
         * @throws PropertyVetoException
         */
        @Override
        public void decorate(AdminCommandContext context, final Node
instance) throws TransactionFailure, PropertyVetoException {


            Logger logger = LogDomains.getLogger(Node.class,
LogDomains.ADMIN_LOGGER);
            LocalStringManagerImpl localStrings = new
LocalStringManagerImpl(Node.class);
           
            SshConnector sshC = instance.createChild(SshConnector.class);
            sshC.setsshPort(sshPort);
            instance.getSshConnector().add(sshC);

        }
    }



Carla


Carla Mott wrote:
> I've looked at the wiki page and still have questions. First is about
> the general architecture for building the new elements. I know that I
> can create a class called Node and add information that I need for
> that element and it is created through the use of the CRUD API you
> have explained. Now I need to add a sub-element to the node element.
> In the Cluster example it looks like a new element is added but added
> to the config element not to the cluster element and the structure is
> actually copied from an existing element in the domain.xml.
>
> Also in the Cluster class I think it creates a sub-element
> systemproperties witout using the Decorator. So I think what I need
> to do to add the sub-element to the node element is the following in
> the Node class
>
> @Element
> @Param(name="ssh-connector", optional=true)
> List<SSHConnector> getSSHConnector();
>
> and create a class called SSHConnector which has the necessary params
> and attributes. What I did:
>
> @Configured
> @SuppressWarnings("unused")
> public interface SSHConnector extends ConfigBeanProxy, Injectable,
> Named, ReferenceContainer, RefContainer {
>
> /**
> * Sets the node name
> * @param value node name
> * @throws PropertyVetoException if a listener vetoes the change
> */
> @Param(name="port", primary = true)
> public void setPort(String value) throws PropertyVetoException;
>
> /**
> * points to a named host.
> *
> * @return a named host name
> */
>
> @Attribute
> String getNodeHome();
>
> /**
> * Sets the value of the name property.
> *
> * @param value allowed object is
> * {_at_link String }
> * @throws PropertyVetoException if a listener vetoes the change
> */
> @Param(name="nodehome", optional=true)
> void setNodeHome(String value) throws PropertyVetoException;
>
>
> }
>
> Unfortunately when I run the command now I get a NullPointerException
> and no stack trace.
>
> Is the decorator is used to validate what the user has passed to the
> command? Is it used to create the sub-elements to the current
> element? It's not clear what I should be doing. I guess I can get
> the Node class using the habitat and make that the parent of the
> element I'm creating in the Decorator but I don't see how to create
> that sub-element exactly. The example is copying an existing element
> so the structure is already created. I see a createChild method on
> the instance but not sure how if that is something I should use.
>
> Thanks,
> Carla
>
> Carla Mott wrote:
>> ok. I know that cluster.java uses a decorator so I will look at that.
>>
>> Thanks,
>> Carla
>>
>> Jerome Dochez wrote:
>>> I am in the process of documenting all this, I will publish a wiki
>>> page tonight so you should look for answers there. I will send the
>>> link when it's ready.
>>> you will need a decorator since you want to add multiple elements in
>>> 1 command.
>>>
>>> On May 24, 2010, at 3:47 PM, Carla Mott wrote:
>>>
>>>
>>>> Hi,
>>>>
>>>> I'm working on a command where I want the resulting xml in
>>>> domain.xml to look like:
>>>> <nodes>
>>>> <node name="ssh_node" host="gf1.sfbay.sun.com"
>>>> glassfish-home="/export/gf">
>>>> <ssh-connector port="22">
>>>> <ssh-auth type="key" username="dipol" keyfile="~/.ssh/id_dsa" />
>>>> </ssh-connector>
>>>> </node>
>>>> </nodes>
>>>>
>>>> I can create nodes and node element but now am trying to figure out
>>>> how to add the ssh-connector and ssh-auth elements. Is there an
>>>> example that I can look at? I know there are other pieces of code
>>>> that do this but I'm not sure which one to use a a model.
>>>>
>>>> Thanks,
>>>> Carla
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: admin-unsubscribe_at_glassfish.dev.java.net
>>>> For additional commands, e-mail: admin-help_at_glassfish.dev.java.net
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: admin-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: admin-help_at_glassfish.dev.java.net
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: admin-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: admin-help_at_glassfish.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: admin-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: admin-help_at_glassfish.dev.java.net
>