Child pages
  • GBean Annotations
Skip to end of metadata
Go to start of metadata

Definition of annotations streamlines the definition of GBeanInfo. With the help of GBean annotations, you will be freed from adding attributes, operations, and references in GBeanInfo.

Annotations for GBeans are defined as follows, and are defined in the package org.apache.geronimo.gbean.annotation.

@GBean

Optional annotation defining the name and j2eeType of a GBean. If this annotation is not specified, then the default name is assumed to be the class (simple) name and the default j2eeType is GBean.

@Priority

Optional annotation defining the priority of a GBean.

@ParamSpecial

Annotation defining a GBean special attribute to be injected.

@ParamAttribute

Annotation defining the GBean attribute to be injected.

@ParamReference

Annotation defining the GBean reference to be injected.

@Persistent

Annotation for setter methods turning the corresponding GBean attributes into persistent attributes.

@Reference

Annotation for setter methods turning the corresponding GBean attribute into a GBean reference.

Note that at most one constructor must be annotated with @ParamSpecial, @ParamAttribute or @ParamReference.

Developing a GBean using Annotations

For developing a GBean with annotations, you at least have to go through the following steps:

  1. Write a POJO class
  2. Implement GbeanLifecycle inteface to use the Gbean Kernel lifecycle callback (Optional).

A sample GBean

Here we rewrite the sample simpleServerGBean provided in Developing and Deploying a Geronimo GBean, which references another GBean named EchoMessageGBean.

  • The SimpleServerGBean refers to another GBean EchoMessageInterface, which is described in Referencing another GBean.
  • The Plain Old Java Object (POJO) code comes with a different constructor annotated with @ParamSpecial, @ParamAttribute or @ParamReference.
  • There is no difference between the LifeCycle code implemented with and without annotations.
  • As the Geronimo kernel will recognize and invoke the attributes, operations, references and constructors defined with annotations, we have removed GBeanInfo implementation.
    SimpleServerGBean
    public class SimpleServerGBean implements GBeanLifecycle, InterfaceNamed {
        private final String gbeanName;
        private EchoMessageInterface echo;
        private int port;
    
        private boolean started = false;
        private ServerSocket serversocket;
    
       /* Different contructor */ 
       public SimpleServerGBean(@ParamAttribute(name = "gbeanName") String gbeanName, 
    			    @ParamAttribute(name = "port") int port,
    			    @ParamReference(name = "EchoMessageInterface") EchoMessageInterface echomsg){
    			                 
            this.gbeanName = gbeanName;
    	this.port = port;
    	this.echo= echomsg;
        }
    
        public String getName() {
            return this.gbeanName;
        }
    
        public boolean isStarted() {
            return started;
        }
    
        private void printConsoleLog(String log) {
            System.out.println(" LOG : " + log);
        }
    
    
        public void doFail() {
            started = false;
            printConsoleLog("GBean " + gbeanName + " failed");
        }
    
        public void doStart() throws Exception {
    
    	 serversocket = new ServerSocket(port);
    	 started = true;
    
    	 Thread simpleServerThread = new Thread(new Runnable() {
    
    	 Socket socket;
    	 InputStream is;
    	 OutputStream os;
    
    	 public void run() {
    
    	     while (started) {
    		 try {
    	             socket = serversocket.accept();
    		     is = socket.getInputStream();
    		     os = socket.getOutputStream();
    
    		     BufferedReader bufferedReader = new BufferedReader(
    		     new InputStreamReader(is));
    
    		     String appendMessage=echo.getEchoMessage();
    		     String responseMessage = appendMessage + bufferedReader.readLine();
    
    		     os.write(responseMessage.getBytes());
    
    		     bufferedReader.close();
    		     if (os != null) {
    		         os.close();
    		     }
    			
                         if (socket != null && !socket.isClosed()) {
    			 socket.close();
    		     }
    
    		 } catch (Exception e) {
    
    		     //ingore
    
    		    }
    	       }
    
    	 }
    
        });
    
         simpleServerThread.start();
    
         printConsoleLog("GBean " + gbeanName
    				+ " started and it's listening on port:" + port);
    
         }
    
         public void doStop() throws Exception {
    
    	 started = false;
    	 serversocket.close();
    	 printConsoleLog("GBean " + gbeanName + " stopped");
         }
    
    }
    

The dployment plan for SimpleServerGBean has to be modified.

Excerpt from simpleServer_deployment_plan.xml
<gbean name="echoserver" class="org.apache.geronimo.sample.SimpleServerGBean">
    <attribute name="port">7777</attribute>
    <attribute name="gbeanName">simpleServer</attribute>
    <reference name="EchoMessageInterface">
        <name>msgAppender</name>
    </reference>
</gbean>
	
<gbean name="msgAppender" class="org.apache.geronimo.sample.EchoMessageGBean">
    <attribute name="msg">[Echo from server 1]:</attribute>
</gbean>

Note: If you define the j2eeType attribute of the EchoMessageInterface GBean, you have to specify this attribute in the <reference> section of deployment plan so that Geronimo can recognize the GBean.

Further Reference

Two examples extracted from Geronimo source are presented here for your reference.

@GBean
public class AuthConfigProviderGBean implements GBeanLifecycle {

    private final String registrationID;

    public AuthConfigProviderGBean(@ParamAttribute(name = "config") String config, 
                                   @ParamSpecial(type = SpecialAttributeType.classLoader) ClassLoader classLoader) 
    throws AuthException, JAXBException, IOException, ParserConfigurationException, SAXException, XMLStreamException {

        ClassLoaderLookup classLoaderLookup = new ConstantClassLoaderLookup(classLoader);

        String messageLayer = null;
        String appContext = null;

        AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory();
        ConfigProviderType configProviderType = JaspiXmlUtil.loadConfigProvider(new StringReader(config));
        AuthConfigProvider authConfigProvider = JaspiUtil.wraptAuthConfigProvider(configProviderType, classLoaderLookup);
        registrationID = authConfigFactory.registerConfigProvider(authConfigProvider, messageLayer, appContext, null);
    }

    public void doStart() throws Exception {
    }

    public void doStop() throws Exception {
        AuthConfigFactory authConfigFactory = AuthConfigFactory.getFactory();
        authConfigFactory.removeRegistration(registrationID);
    }

    /**
     * Fails the GBean.  This informs the GBean that it is about to transition to the failed state.
     */
    public void doFail() {
    }
}
@GBean
public class JmxDiscoveryPublisher implements GBeanLifecycle {

    private final URI service;
    private final DiscoveryAgent discoveryAgent;

    public JmxDiscoveryPublisher(@ParamAttribute(name = "nodeName")String nodeName,
                                 @ParamAttribute(name = "clusterName")String clusterName,
                                 @ParamAttribute(name = "protocol")String protocol,
                                 @ParamAttribute(name = "urlPath")String urlPath,
                                 @ParamAttribute(name = "discoveryType")String discoveryType,
                                 @ParamReference(name = "DiscoveryAgent")DiscoveryAgent discoveryAgent,
                                 @ParamReference(name = "RMIRegistryService")RMIRegistryService rmiRegistryService
    ) throws URISyntaxException, IOException {
        this.discoveryAgent = discoveryAgent;
        String query = null;
        if (nodeName != null && nodeName.length() > 0) {
            query = "node=" + nodeName;
        }
        if (clusterName != null) {
            query = (query == null? "": query + "&") + "cluster=" + clusterName;
        }
        service = new URI(discoveryType + ":" + protocol, null,  rmiRegistryService.getHost(),
                          rmiRegistryService.getPort(), "/" + urlPath , query, null);
        discoveryAgent.registerService(service);
    }

    public void doStart() throws Exception {
    }

    public void doStop() throws Exception {
        discoveryAgent.unregisterService(service);
    }

    public void doFail() {
        try {
            doStop();
        } catch (Exception e) {
            //ignore
        }
    }
}
  • No labels