This is a library used to map filesystem resources to Java objects and vice-versa.
- mapping filesystem resources to Java objects,
- mapping Java objects to filesystem resources,
- registering custom type handlers.
Interface:
@FilesystemResource
interface PersonInfo {
@Read
@Path("first-name")
String getFirstName();
@Read
@Path("last-name")
String getLastName();
}
Filesystem:
persons
|- person1
| |- first-name
| \- last-name
\- person2
|- first-name
\- last-name
The following code maps the above filesystem into instances of PersonInfo
.
Path root = Paths.get("persons");
FilesystemMapper fsmapper = FilesystemMapper.instance();
FilesystemMapperTarget person1Target = fsmapper.getTarget(root.resolve("person1"));
FilesystemMapperTarget person2Target = fsmapper.getTarget(root.resolve("person2"));
PersonInfo person1 = person1Target.proxy(PersonInfo.class);
PersonInfo person2 = person2Target.proxy(PersonInfo.class);
// print contents of persons/person1/first-name
System.out.println(person1.getFirstName());
// print contents of persons/person1/last-name
System.out.println(person1.getLastName());
// print contents of persons/person2/first-name
System.out.println(person2.getFirstName());
// print contents of persons/person2/last-name
System.out.println(person2.getLastName());
Implementation:
class PersonInfoImpl implements PersonInfo {
public String getFirstName() {
return "John";
}
public String getLastName() {
return "Smith";
}
}
The following code maps the above implementation into the filesystem.
Path root = Paths.get("person");
FilesystemMapper fsmapper = FilesystemMapper.instance();
FilesystemMapperTarget personTarget = fsmapper.getTarget(root);
PersonInfo person = new PersonInfoImpl();
personTarget.mount(person);
The resulting filesystem:
person
|- first-name
\- last-name
The contents of person/first-name
and person/last-name
should be John
and Smith
respectively.