Index Changes
Fuji Home


What are they :

Aspects are nice to have add-ons or features that help your business logic without being part of the business logic.

Aspects are OSGi bundles:

All the aspects are OSGi bundles and thus offer the ability of their life cycle management, say through management console. Aspect bundles are like libraries.

aspects-api is the collection of all aspects apis. Every aspect has its own OSGi bundle, aspects-<aspect-name>, e.g. aspects-cache.

The composite application/service assembly/application depends on aspects-api and thus could make use of any aspect functionality. Every aspect, depends on aspects-api. This way your application only depends on one bundle, aspects-api and only the aspects of your interest can be running as per your choice.

Aspects and Interceptors:

Aspects need be handled separately from that of business functionality as they really are not part of that logic.

Any component starts its functionality immediately after that component receives the message to process. Pre-message and Post-message handlers within the message handler are one way to achieve aspects. To start with, this approach loosely couples with your business logic. But then, adding control logic to choose when to handle the aspects raises the coupling. And there is no way to stop receiving the message, if that message in-deed not required to be received like in cases of Caching, Content-based-routing.

Interceptors of Fuji provides a niche platform to handle Aspects. Actually, Aspects can be treated as one use-case for interceptors. Handling aspects within interceptors, decouples the business logic. Interceptor scoping of choosing providers and consumers and message flow controls provide all the right tools to handle several aspects including Caching, Content-based-routing.

An example - Cache

A prototype Cache aspect implementation is available in Fuji. Here are the steps to use it.

  • Build Aspects
    • Modify <FujiSourceCodeBase>/trunk/core/core/pom.xml default-profile to add <module>aspects</module> so that aspects can be built.
    • Build Fuji
  • Add aspects-api dependency to the composite application
    • You can do it either of the following ways:
      • Add as a library: In Netbeans, under your project, Right click on Libraries and Click on Add Library...
      • Edit pom: In Netbeans, under your project, Project Files, pom.xml
  • Create a Bundle Activator class (Workaround) - ExampleActivator
    • BundleContext instance is needed to access other OSGi bundles and this work is in progress. Here is a work around.
    • Create a POJO in your composite application and you may call it as ExampleActivator
package com.sun.jbi.aspects.interceptors.examples;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
 * @author rdara
public class ExampleActivator implements BundleActivator {
    static BundleContext bc;
    public ExampleActivator() {
    public static BundleContext getBundleContext() {
        return bc;
    public void start(BundleContext arg0) throws Exception {
        bc = arg0;
        System.out.println("myXSLT  bundle started and bc = " + bc);

    public void stop(BundleContext arg0) throws Exception {
  • Edit MANIFEST.MF to include the following. Please be aware the 71 character limitation of MANIFESTS.
Bundle-Activator: com.sun.jbi.aspects.interceptors.examples.ExampleAc
  • Create a POJO -
package com.sun.jbi.aspects.interceptors.examples;

import com.sun.jbi.aspects.api.Cache;
import com.sun.jbi.interceptors.Intercept;
import javax.jbi.messaging.MessageExchange;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

 * @author rdara
public class Example {

    static Cache myCacheService;

    public Example() {

     * Unconditional interceptor
     * @param exchange 
     * @return Cached reply or the original exchange
    public MessageExchange myInterceptorMethod(MessageExchange exchange) {
        if (myCacheService == null) {
            myCacheService = getCacheService();
        return myCacheService != null ? myCacheService.cache(exchange) : exchange;

     * Obtain Cache service through the bundle context's service reference
    private static Cache getCacheService() {
        if (myCacheService == null) {
            BundleContext bc = ExampleActivator.getBundleContext();
            if (bc != null) {
                ServiceReference sr = bc.getServiceReference(Cache.class.getName());
                if (sr != null) {
                    myCacheService = (Cache) bc.getService(sr);
        return myCacheService;
  • Edit MANIFEST to add package dependency
Import-Package: com.sun.jbi.aspects.api
  • Build and run the project
    • Install aspects-api
install file:<FujiCodeBase>\trunk\core\aspects\api\target\aspects-api-1.0-M2-SNAPSHOT.jar
    • Install aspects-cahce
install file:<FujiCodeBase>\trunk\core\aspects\api\target\aspects-cache-1.0-M2-SNAPSHOT.jar
    • Start all the bundles and Cache aspect is in place.

JSPWiki v2.4.100
« Home Index Changes Prefs
This page (revision-1) was last changed on 12-Feb-09 14:16 PM, -0800 by Nikita