Monday Jan 04, 2010

REST based hybrid(OSGI + JavaEE) - OSGI Framework Controller

GlassFish V3 comes with an OSGI Web Container, which provides the support for development of hybrid applications.

A hybrid application is a combination of Java EE and OSGI. More information about developing a simple hybrid application can be found here.

This blog entry describes about a REST based Web Application Bundle to control OSGI Framework.

Steps for the setup:

1. Download and install latest nightly build of glassfish-v3 from here.
2. Start the server using : asadmin start-domain
3. Download the OSGI Web Container bundle form here.
       Install it by copying the downloaded jar file to glassfishv3/glassfish/domains/domain1/autodeploy-bundles directory.
4. Get the ready to install REST based OSGI Framework controller OR

The complete netbeans project is available here. The project built with netbeans generates a plain war file, to make it an OSGI bundle, we need to update the MANIFEST.MF entry. Update the generated war by using the following command:
       jar -uvfm REST_WAB.war MANIFEST.MF

5. Install the application by copying it to glassfishv3/glassfish/domains/domain1/autodeploy-bundles directory. Server log will indicate the success of installation.
6. Use your browser to access the URL : http://localhost:8080/REST_WAB
       Click on View Bundles to get the status of modules in GlassFish v3. The framework controller allows for the install/uninstall, start/stop of the OSGI bundles.

rest framework controller
OSGI Framework Controller

Getting the OSGI Framework Bundle Context in a REST Application:

-- get the servlet context as:
        @Context ServletContext ctx;

-- getting the org.osgi.framework.BundleContext:
         BundleContext bundleContext = (BundleContext) ctx.getAttribute("osgi-bundlecontext");

The bundleContext can be used for running the OSGI Framework related commands.

Thursday Dec 25, 2008

Event Notification Made Easy - GlassFish V3

Tired of writing the complex event notifiers and the event listeners, you should no longer be.

GlassFish v3, HK2 kernel has a clean and easy way to implement the event notification mechanism. Lets start with a simple Application Event Listener. This Listener will listen to any application deployed, changed or undeployed events.

The object which we are listening should be marked as @Configured :-

@Configured

public interface SomeInterface ...{

}

Now the Listener Should implement the ConfigListener Interface.

The Listener for Application Events will look like:

@Service
public class Listener implements ConfigListener {

The object for which we are implementing the listener.
    @Inject
    private Applications allApplications; 

The Following function provides the notifications of the Event.

public UnprocessedChangeEvents changed(PropertyChangeEvent[] events) {
    final UnprocessedChangeEvents unprocessed = ConfigSupport.sortAndDispatch(events, new Changed() {

    public <T extends ConfigBeanProxy> NotProcessed changed(TYPE type, Class<T> changedType, T changedInstance) {
                switch (type) {
                    case ADD: //
changedInstance was added to the parent
                        break;
                    case CHANGE: //
changedInstance has mutated
                        break;
                    case REMOVE: //
changedInstance was removed from the parent
                        break;
                    default:
                        break;
                }
            }
}

Whenever some Application Event Occurs all the implementors of ConfigListener which are injecting the Applications interface are notified.

About

Mohit Gupta

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today