Child pages
  • GBeansArticle1

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Content

Table of Contents

Geronimo GBean Architecture

...

The concepts that provide the base for the GBeans have much longer history in the sense that the Geronimo Kernel is an inversion of Control, IOC framework where the IOC is also known as the dependency injection. The essence of IOC or the dependency injection is to create a loosely coupled architecture where the dependencies among components are managed by the framework. When a component has a dependency on other component the IOC framework will find the correct component and make it available to the first component. The dependency injection name comes from the fact that the framework automatically injects the dependencies to the component. Most simple form of IOC or the Dependency injection is to by system wide known values. For an instance let us assume this is Web Service that is to deployed inside Axis, do not worry about Axis but just assume that the Axis will load the following class and call the doit() method.

No Format

public class WS {
    public WS(MesageContext msgctx) {
    }
    
    public void doit() {
    }
}

...

But Axis has something called unmigrated-wiki-markup

!<ac:link><ri:page ri:content-title="MessageContext" /></ac:link> that has all the configuration information. In order to give a reference of !<ac:link><ri:page ri:content-title="MessageContext" /></ac:link> to the WS class Axis can say "if you have a Constructor like XX(!

MessageContext) or you have a method like setMessageContext(!MessageContext msgctx) I will inject the !MessageContext to you".

...

The Idea of IOC is to define all the relationships among the components in terms of the framework level configurations and each component is injected to the other components that refer the service when the first component is available. For an example let us consider the how a GBean G1 obtains a reference to a Configuration Store. The G1 is configured specifying the patterns of the reference it is expected.

No Format

e.g. bean.setReferencePatterns("configStore,*:type=configStore,*");

...

When a another GBean whose name match the pattern ":type=configStore," is started it is automatically injected in to the G1. This is more like Aspect Oriented programming, AOP where the developer says to the framework what is need to be done rather than how to do it.

...

A GBean with one attribute "val" will look like this:

...

...

We can start our GBean with the following code. The GBean implementation is done on top of JMX and the GBeanMBean accepts a GBean info and converts it into an Mbean.

No Format

GBeanMBean gmb = new GBeanMBean(MyGBean.GBEAN_INFO);
gmb.setAttribute("value","To be or not to be that is the question");
ObjectName myGbeanName = ObjectName.newInstance("Geronimo.my:name=mine");
kernel.loadGBean(myGbeanName,gmb);
kernel.startGBean(myGbeanName);
//dowork
kernel.stopGBean(myGbeanName);
kernel.unloadGBean(myGbeanName);

...

Dependency Injection

There are two types of relationships

...

Following is a simple GBean that uses constructor injection.

...

...


public class MyGBean implements GBeanLifecycle {
    private final String GBean1 bean1;
    static {
        GBeanInfoBuilder infoFactory =  ...
        // attributes
        infoFactory.addReferance("Bean1", GBean1.class, true); ---------(A)
        // operations
        infoFactory.setConstructor(new String[]{"Bean1"}); -------------(B)
        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public ConstructorInjectionGbean(GBean1 bean1) {-----------(C)
        this.bean1 = bean1;
    }
...
}

...

Here the Line (A) said that there is a reference to the GBean1 and line (B) registered the matching constructor (C). In the Following code the developer specified the reference instance of GBean1 and start GBeans.

No Format

ObjectName GBean1= ObjectName.newInstance("Geronimo.my:name=gb1");
... //start the GBean 1
GBeanMBean gmb = new GBeanMBean(MyGBean.GBEAN_INFO);
gmb.setReferencePatterns("GBean1", GBean1Name);

ObjectName myGbeanName = ObjectName.newInstance("Geronimo.my:name=mine");
kernel.loadGBean(myGbeanName,gmb);
kernel.startGBean(myGbeanName);
//dowork

...

When the MyGBean is started an instance of GBean1 is injected via the constructor.

...

Only difference when the Reference collection come in to play is replacing the GBean1 references in the above example with Collection and when specifying the name of the referenced GBean it is specified as a pattern with "*" and "?".

No Format

bean.setReferencePatterns("Geronimo.my:*");

...

The code asks the framework to make available all the GBeans with the Domain name "Geronimo.my:". When a GBean that whose matches the patterns is started it is automatically injected to the referee GBean.

...

GBeans leave the handling of the logic more or less in the form of usual java code and the Methods in the GBean class can be register to the GBean by adding a

No Format

infoFactory.addOperation("doit",
new Object[]{val1,val2},new String[]{
val1.getClass().getName(),
val2.getClass().getName()}); --------- (A)

...

Kernel supports kernel.invoke("objectName","methodName",...) using which one can call any method in any other GBean. But use of this is discouraged and accepted way to do it is to register reference pattern and obtain the instance of the other GBean injected in and code simple Java style on the code.

E.g. Say GBean G1 need to call the doit() on GBean G2 the crude way to do it is to

...

...

Correct way to do it is write G1 like

...

...

At line (A) defining there is a reference to G2 and at line (B) adding a constructor that inject the G2 and G1 obtain a reference to the G2. Then logic is implemented using simple Java code. GBeans have interfaces that make the GBean implements a given method. By making the GBean class implements the interface and putting a entry infoFactory.addInterface("interfaceName") will automatically add the all the methods in the interface to the GBean and the getters and setter will be mapped to the attributes.

...