Friday, 6 March 2009

Using TypeLoaders in Fuse to load Objects

The default loader supports String content.

If you want to load objects from the content it is necessary to create a custom TypeLoader.

For example, if we have the following resource file:

testfuse2.resources

TestFuse2.person=Jon,Doe


and a Person POJO defined as follows:

Person.java

public class Person
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

public String toString()
{
return this.getClass().getName()+"[firstName="+firstName+", lastName="+lastName+"]";
}
}


Then we need to create our own typeloader to parse the Strign Jon,Doe to return a Person object.

Our TypeLoader looks like this:

PersonLoader.java

import java.util.Map;
import org.jdesktop.fuse.TypeLoader;

public class PersonLoader extends TypeLoader<Person>
{
@SuppressWarnings("unchecked")
public PersonLoader()
{
super(Person.class);
}

@Override
public Person loadType(String name, String value, Class resolver, Map map)
{
String[] parts = value.split(",");

return new Person(parts[0],parts[1]);
}
}



Now all we need to do, is modify our original code from my last post to register the new typeloader.

TestFuse2.java

import org.jdesktop.fuse.InjectedResource;
import org.jdesktop.fuse.ResourceInjector;
import org.jdesktop.fuse.TypeLoaderFactory;

public class TestFuse2
{
@InjectedResource
private Person person;

public String toString()
{
return person.toString();
}

public static void main(String[] args)
{
ResourceInjector.get().load(TestFuse2.class.getResource("testfuse2.resources"));
TypeLoaderFactory.addTypeLoader(new PersonLoader());

TestFuse2 testFuse2 = new TestFuse2();
ResourceInjector.get().inject(testFuse2);

System.out.println(testFuse2);
}
}

Using Fuse for Dependency Injection in Swing

The fuse project for dependency injection can be found at https://fuse.dev.java.net.

The overview is quite simple and can be found at https://fuse.dev.java.net/gettingstarted.html.

in short, Fuse uses a text file containing resources and values to be injected into the object at run time.

The following code shows a very simple example of how to use fuse:

1: Create the Resource File

create the package test and create the file testfuse.resources in that package.

add the following line to testfuse.resources

TestFuse.name=Jon Doe

When this resource file is applied by fuse, it will inject the value Jon Doe into the member name in the class TestFuse

2: Create the Java code to inject the resources

Create the class TestFuse.java to read the resource file and use fuse to inject the contents.

package test;
import org.jdesktop.fuse.InjectedResource;
import org.jdesktop.fuse.ResourceInjector;

public class TestFuse
{
@InjectedResource
private String name;

public String toString()
{
return this.getClass().getName()+"[name="+name+"]";
}

public static void main(String[] args)
{
ResourceInjector.get().load(TestFuse.class.getResource("testfuse.resources"));

TestFuse testFuse = new TestFuse();
ResourceInjector.get().inject(testFuse);

System.out.println(testFuse);
}
}