Plexus

This document describes functionality that has not yet been released yet. Although stable enough to be documented, it may change slightly from publication to release.

Plexus is an inversion-of-control (IoC) or dependency injection framework similar to the Spring Framework. Although not well known on it’s own, it’s the framework which underpins Apache Maven.

The retepMicroKernel supports Plexus as an option for deploying components within an application since 10.9.

Due to the modularity of the kernel, components deployed by plexus are accessible from spring based applications, and visa-versa as the integration fully supports the ResourceResolver API provided by the kernel.

Adding plexus to your application

To add plexus to your application is as simple as adding a single dependency to the module.

        <dependency>
            <groupId>uk.org.retep.microkernel.plexus</groupId>
            <artifactId>plexus</artifactId>
            <version>10.9-SNAPSHOT</version>
        </dependency>

Also because it uses the plexus component annotations to declare the components, we also require an additional maven plugin to parse the source to generate the component.xml file from those annotations:

<plugin>
    <groupId>org.codehaus.plexus</groupId>
    <artifactId>plexus-component-metadata</artifactId>
    <version>1.5.4</version>
    <executions>
        <execution>
            <id>process-classes</id>
            <goals>
                <goal>generate-metadata</goal>
            </goals>
        </execution>
        <execution>
            <id>process-test-classes</id>
            <goals>
                <goal>generate-test-metadata</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Defining your components

A component consists of an implementing class, it’s role and optionally a roleHint. Usually (advised), the role is an interface the class actually implements, so from the good tutorial from Sonatype:

An example role:

package example;

public interface Cheese
{
    /**
    * Slices the cheese for apportioning onto crackers.
    * @param slices the number of slices
    */
    void slice( int slices );

    /**
    * Get the description of the aroma of the cheese.
    * @return the aroma
    */
    String getAroma();
}

Next an implementation:

package example;

import org.codehaus.plexus.component.annotations.Component;

/**
 * All we need to tell Plexus that this is a component.
 */
@Component( role = Cheese.class, hint = "parmesan" )
public class ParmesanCheese
    implements Cheese
{
    public void slice( int slices )
    {
        throw new UnsupportedOperationException( "No can do" );
    }

    public String getAroma()
    {
        return "strong";
    }
}

Injecting dependencies

Just in Sonatype’s example, injecting dependencies is the same, just add n @Requirement annotation to a field and that’s it.

@Requirement( hint = "parmesan" )
private Cheese parmesanCheese;

Deploying the Plexus Application

This is where we diverge slightly. Although you have your components defined they will not get deployed until something references them. If nothing does, then you’ll find your application will appear to do nothing.

To start a plexus application you therefore have two options.

@DeployOnStartup

The DeployOnStartup annotation is provided by the kernel and allows you to annotate a component stating that when the application starts then this component should also start. All the kernel then does is in the start phase of the application it performs a lookup against that component and plexus then starts it.

<h2>@DeployOnStartup</h2>
@Component( role = App.class )
public class App implements Startable
{
    @Requirement( hint = "parmesan"  )
    private Cheese cheese;

    @Override
    public void start() throws StartingException
    {
System.out.println( "parmesanCheese smells : " + parmesanCheese.getAroma() );
    }
}

Leave a Reply

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

Gravatar
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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,397 other followers