Aspect Oriented Programming: Qi4j

Qi4j is an emerging framework for composition, injection and aspect oriented programming.

Its main asset is composition however it provides us with tools such as concerns and sideofs in order to apply aspect oriented programming.

I will use the same example with the employees as I did on the previous posts by using Spring and Java EE.
Each employee whether he is a supervisor or a worker will have a different behavior before entering the working area.

First let us start with the interface

 
package com.gkatzioura;

public interface Employee {

    public void enterWorkArea();

}

Also I will create the default implementation for the Employee interface

 
package com.gkatzioura;

import java.util.logging.Logger;

public class EmployeeMixin implements Employee {

    private static final Logger LOGGER = Logger.getLogger(EmployeeMixin.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("I will enter the work area");

    }

}

Now I will create two transient composites a worker and an employee.

 
package com.gkatzioura;

import org.qi4j.api.composite.TransientComposite;

public interface Worker extends Employee,TransientComposite {
}

 
package com.gkatzioura;

import org.qi4j.api.composite.TransientComposite;

public interface Supervisor extends Employee,TransientComposite {
}

And for each composite I will create a concern. Concern is the equivalent for MethodBeforeAdvice on spring.

 
package com.gkatzioura;

import org.qi4j.api.concern.ConcernOf;
import java.util.logging.Logger;

public class WorkerBeforeConcern extends ConcernOf<Employee> implements Employee{

    private static final Logger LOGGER = Logger.getLogger(WorkerBeforeConcern.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("Informing the supervisor that I have arrived");

        next.enterWorkArea();
    }
}
 
package com.gkatzioura;

import org.qi4j.api.concern.ConcernOf;
import java.util.logging.Logger;

public class SupervisorBeforeConcern extends ConcernOf<Employee> implements Employee {

    private static final Logger LOGGER = Logger.getLogger(SupervisorBeforeConcern.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("Check if everything is ok");

        next.enterWorkArea();
    }
}

The last class that remains is to assemble our environment.

 
package com.gkatzioura;

import org.qi4j.api.activation.ActivationException;
import org.qi4j.api.structure.Application;
import org.qi4j.api.structure.Module;
import org.qi4j.bootstrap.*;
import java.util.logging.Logger;

public class Main {

    private static Energy4Java qi4j;
    private static Application application;

    private static final Logger LOGGER = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) throws AssemblyException, ActivationException {

        qi4j = new Energy4Java();
        Application application = qi4j.newApplication(new ApplicationAssembler() {

            @Override
            public ApplicationAssembly assemble(ApplicationAssemblyFactory factory)
                    throws AssemblyException {

                ApplicationAssembly assembly = factory.newApplicationAssembly();

                LayerAssembly rootLayer = assembly.layer("layer");
                ModuleAssembly rootModule = rootLayer.module("module");

                rootModule.transients(Supervisor.class)
                        .withMixins(EmployeeMixin.class)
                        .withConcerns(SupervisorBeforeConcern.class);
                rootModule.transients(Worker.class)
                        .withMixins(EmployeeMixin.class).
                        withConcerns(WorkerBeforeConcern.class);

                return assembly;
            }
        });

        application.activate();

        Module module = application.findModule("layer", "module");

        Employee supervisor = module.newTransient(Supervisor.class);
        Worker worker = module.newTransient(Worker.class);

        supervisor.enterWorkArea();

        worker.enterWorkArea();

    }    
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s