Aptvir the Java Compiler virus

October 4th, 2010

I’ve been using Annotation Processing Tools (APT) for a few months now, and I like it. It opens new capabilities for the Java platform, I exploited them to create an APT virus, called APTVir.

APTVir is a single class and a single file that replicate when it is available on the classpath n the compiled code. It relies on APT to execute itself and copy itself in the output produced by the compiler.

Instead of long explanations, I created a screencast that explains it :-) and the code is available as a GitHub repository.

Configuring easily a database for a JBoss AS cluster

September 8th, 2010

In GateIn project we sometime have to deal with the setup of a JBoss cluster. While JBoss clustering is quite easy to configure there are things you need to configure such as a shared database. In addition when doing tests you need to start from a fresh database. I will give you a tip I’m using when I’m setting up a GateIn cluster that can help you to save time for setting up a database.

By default GateIn uses the famous embedded database HSQLDB and it is very convenient because

  1. Deployed with GateIn, no need for extra setup
  2. Sufficient for doing development
  3. Fast
  4. Easy to erase the database and start from fresh
When dealing with a GateIn cluster (or any other application that requires a database), ultimately you need a database that will be used by the cluster nodes. Does it mean I have to give up HSQLSDB? the answer is no with an extra configuration step.
By default HSQLDB comes as an in process database, but it is possible to use other configuration modes
  1. Make the database open a port to accept remote connection
  2. Connect to a remote database
We can use those two setup an asymmetric configuration of two nodes. The first node datasource owns the database and the second node datasource uses the database setup by the first node remotely.

CRaSH 1.0.0-beta10 release

August 23rd, 2010

CRaSh 1.0.0-beta10 supports now line edition and cursor navigation for the telnet connector as its first contribution!

CRaSH 1.0.0-beta9 release

July 8th, 2010

CRaSh 1.0.0-beta9 minor feature and fix a few issues with GateIn 3.1:

  • export/import uses now the SSH username and password for accessing JCR for GateIn 3.1
  • connect now always require a password for GateIn 3.1
  • documentation now has a front cover page
enjoy!

Reflext improves its annotation introspection

June 3rd, 2010

So far the annotation API in Reflext was using the runtime API to provide annotation access. What I call the runtime API is what most of developers are used to when dealing with annotations.

When using the java.lang.reflect runtime, this works perfectly as the runtime provides obviously runtime annotation. Nothing special here. However at compile time, this runtime access is emulated by the compiler. The real API to access an annotation is the java.lang.model.element.AnnotationMirror interface that is a totally detyped access to an annotation. But the APT tries to be nice and provides an emulation of runtime annotation that works well until

  • the introspected annotation is being compiled and it is not yet available under its class form
  • the annotation is already compiled (coming likely from JDK or a dependency) but it has a Class parameter and that class is being compiled. In that situation the Class object is not yet available, consequently it fails.

For that matter there is now in Reflext an API that provides a detyped access to an annotation that works equally with runtime and compile time implementations (runtime annotation access is still possible of course) .Using such API is obviously less intuitive than using the annotation directly but when you don’t have the guarantee that you are the annotation type is already compiled it’s the only way to go.

CRaSH 1.0.0 Beta 8 released

May 27th, 2010

We have just released CRaSH 1.0.0 Beta 8 on Google Code forge, in that release we have worked on two usability aspects of the shell:

  • The capability for a command to prompt a value with or without echo of the value entered by the user. It is mainly used by the connect command to prompt the password when it is not entered via the -p option.
  • The second feature is the up/down arrow associated to recalling previously entered commands.
The prompt command implementation required a refactor of the CRaSH architecture, the main challenge coming from the following facts:
  • the shell is invoked by a term and the term has blocking IO
  • the execution of a command needs to be interruptible
The refactoring was quite fun to do, and lead to a few improvements in the architecture that consisted mostly in decoupling the various systems, now we can distinguish:
  1. The Shell that executes the commands and returns a result. The current implementation is connected to JCR to execute Groovy commands.
  2. The Shell Connector, a state machine executing commands in a synchronous or asynchronous manner, it depends on its configuration. The execution of a command can be cancelled.
  3. The Term: a state machine that translates IO into actions, managing also the command history.
  4. The TermIO: the input/output of the Term, that is implemented using Apache SSHD and Wimpi TelnetD
At the moment I am quite satisfied by the current features. I think it is still missing the command completion and I don’t have a clue if it is easy or complex to do and which system should manage it, I’ll leave it for later fun. Maybe it will be easy to do, or it could require some new important refactor to make it possible to happen, who knows?

CRaSH 1.0.0-beta3 release

February 16th, 2010

Just released the 1.0.0-beta3 release of CRaSH, you can get it there.

The first and foremost new features in that release are contributed by Patrice Lamarque that gave us very important commands such as cp to copy a JCR node, mv to move a JCR node and xpath to perform an xpath query in a similar way you can perform a SQL query.

Beside that, the pwd command was bugged in beta2 and is fixed in beta3.

Chromattic support for embedded types

February 8th, 2010

The embbeded type support for Chromattic brings much power to object modeling on top of Java Content Repository that allows to fully take advantage of how JCR models node types. JCR type model provides two features that don’t match the Java type mode.

The first feature is multiple inheritance that is supported natively by JCR which allows a type to extend several super types. The other feature is the mixin support which is pretty much similar to multiple inheritance except that it can be dynamic, meaning that a node can be assigned with a mixin type. Conversely a mixin type can be removed from a node.

Both features are difficult with classic Java inheritance and the most appropriate way for modeling those is to leverage object delegation. Actually often object delegation is preferable over inheritance as it decouples the object types and provides support for dynamicity.

Mixin type support

The following example is taken from GateIn and it shows how several java type can support templatization with the embedded support.

The Templatized class is a delegate object that provides a relationship to a Template type.

@MixinType(name="templatized")
public abstract class Templatized {

  @OneToMany(type=RelationshipType.PATH)
  @MappedBy("template")
  public abstract Template getTemplate();
  public abstract void setTemplate(Template template);

}

The Page class has an embedded relationship with a Templatized type.

@PrimaryType(name="page")
public abstract class Page {

  // Returns null if the mixin is not present
  @OneToOne(type=RelationshipType.EMBEDDED)
  public abstract Templatized getTemplatized();

  // Controls the life cycle of the mixin
  public abstract void setTemplatized(Templatized templatized);
}

At runtime this would likely be used in the following manner:

Page page = session.findByPath(Page.class, "page");
Templatized templatized = session.create(Templatized.class);
page.setTemplatized(templatized);
Template template = session.findByPath("template");
templatized.setTemplate(template);

Primary type support

Likewise Chromattic supports primary type, the only difference is that the dynamicity aspect does not occur therefore the relationship always exists between the types and it is obviously not possible to destroy it. This feature provides to my opinion a very good alternative for reusability.

Supports in GateIn

The embedded type feature has been added for GateIn in order to allow the Model Object for Portal (MOP). Indeed the model itself does not care about certain aspects and the portal needs to be able to make the base model richer than it is. With the embedded type we have the opportunity to keep the base model simple and have GateIn add its own mixins such as security, templatization, etc… Likewise eXo WCM that is built on top of GateIn will be able to add its own mixins for the purpose of web content management.

CRaSH 1.0.0 Beta 2 release

January 8th, 2010

The CRaSH 1.0.0 Beta 2 has been released, you can download it there and the documentation has been updated with the new features.

The most important feature brought by that release is the brand new support for SSH thanks to the Apache SSHD project. SSH access is more suitable for remote access on public networks and SCP can be used to import or export content from the repository.

Configuration is now possible via the web.xml file and the following can be configured:

  • SSH username, password, port and key file
  • Telnet port
Finally good work has been made on the release process by creating an archive containing the shell war file, the project sources and the javadocs.

Self-bounding Visitor

January 5th, 2010

I came across the generics self-bounding pattern, thanks to Olivier’s blog [in french] that uses it to describe fluent extensible interfaces. After a bit of research I found a very interesting forum thread that was talking about that pattern more in depth, in particular how it is possible to extend the pattern to a couple of classes.

Let’s just start with a simple example, a binary tree class:

abstract class Node<E extends Node<E>> {
   public E left;
   public E right;
}

class FooNode extends Node<FooNode> {
}

class BarNode extends FooNode {
}

class JuuNode extends Node<JuuNode> {
}

With that design, we constraint the Node class to be subclasses and have its children to be a subclass of its own subclass, and now we can write:

FooNode root = new FooNode();
root.left = new FooNode();
root.right = new BarNode();

But that is not possible:

FooNode root = new FooNode();
root.left = new JuuNode();

If you have carefully read the forum thread, someone posted an interesting extension of the pattern that uses two classes.

The Reflext framework I started to write a few months ago was using a visitor pattern to visit a set of types. There are several ways to visit a set of types, one of them is to visit the class hierarchy of super classes and implemented interfaces. There are many ways to implement the visitor pattern, one of them relies on decoupling the visitor from the code (or strategy) that takes care of doing the visit. The main interest is to reuse the visiting strategy many times and avoid the visitor to care about how visit is done, the visitor only cares about the overall strategy.

Based on this assumption, we can define two interfaces named Visitor and VisitorStrategy that will vary together, as it makes sense to use a hierarchy visitor with the hierarchy visitor strategy. Let’s start with the base interfaces. We use it a Type object as visited object, in the reflext framework it’s a TypeInfo interface that represents a type (which can be either runtime reflection or APT compile time reflection).

interface Visitor<V extends Visitor<V,S>, S extends Strategy<V,S>> {
  // The interface is empty as the base visitor does not provide an API
}
interface Strategy<V extends Visitor<V,S>, S extends Strategy<V,S>> {
  void visit(Type type, V visitor);
}

Now we can define the hierarchy visitor and its strategy

interface HierarchyVisitor<T extends HierarchyVisitor<T>> extends
  Visitor<T, HierarchyStrategy<T>> {

  // The API for hierarchy visit
  void enter(Type type);
  void leave(Type type);
}

abstract class HierarchyStrategy<T extends HierarchyVisitor<T>> extends
  Strategy<T, HierarchyStrategy<T>> {
  public void visit(Type type, T visitor) {

    // We only accept type which are java classes
    if (type instanceof Class) {
      if (accept(type)) {

        // Visitor callback
        enter(type);

        // Now visit the super class if we have one
        Class clazz = (Class)type;
        Class superClazz = clazz.getSuperClass();
        if (superClazz != null) {
          visit(superClazz, visitor);
        }

        // And now visit the interfaces
        for (Class itf : clazz.getInterfaces()) {
          visit(itf);
        }

        // Visitor callback
        leave(type);
      }
    }
  }
  abstract boolean accept(Type type);
}

We can see that the hierarchy visitor and its strategy vary together because they are constrained by design. The last piece missing is the to make vary the strategy (otherwise we would make the visitor and its strategy the same class…). We do it via an enum that provides several strategies

enum HierarchyScope {
  ALL() {
    public <T extends HierarchyVisitor<T>> HierarchyStrategy<T> get() {
      return new HierarchyStrategy<T>() {
        public boolean accept(Type type) {
          return true;
        }
      }
    }
  },

  ANCESTORS() {
    public <T extends HierarchyVisitor<T>> HierarchyStrategy<T> get() {
      return new VisitorStrategy<T>() {
        public boolean accept(Type type) {
          return (type instanceof Class) && !((Class)type).isInterface());
        }
      }
    }
  };

  public abstract <T extends HierarchyVisitor<T>> HierarchyStrategy<T> get();

}

Et voilà, now we can use safely the visitor. I’m not convinced that is a great piece of software, I’m just sure that it was very fun to write and research. Without the mentioned blogs and my favorite IDE (Intellij IDEA) it would have been nearly impossible to write correctly :-).

Self-bounding generics is a curious beast, it’s even called there “That Recursive Java Generics Thing”. Have fun!