Writing your own deployer
by default, Chameleon deploys bundles and configuration from the runtime and application directories. However, your can extend this set with your own type of file.
Mechanism
Deployers are published within as OSGi services. They will be invoked by Chameleon when:
- the framework starts(or when they are initialized)
- when a new file is created
- when a file is modified
- when a file is deleted
- when the framework stops
Here is the Deployer interface:
public interface Deployer {
/**
* Does the current deployer accept the given file.
* The accept method must not check file existence,
* as it may disable notification on file deletion.
* @param file the file
* @return {@literal true} if the file is accepted by
* the current deployer,
* {@literal false} otherwise
*/
boolean accept(File file);
/**
* Callback called when an accepted file is created.
* @param file the new file
*/
void onFileCreate(File file);
/**
* Callback called when an accepted file is updated.
* @param file the updated file
*/
void onFileChange(File file);
/**
* Callback called when an accepted file is deleted.
* @param file the file
*/
void onFileDelete(File file);
/**
* Method called when the deployer is initialized.
* This method is called once per monitored directory,
* with a potentially empty set of files (if there are
* no file or if the directory does not contain any
* accepted files)
* @param files the set of accepted file currently present
* is a monitored directory.
*/
void open(Collection<File> files);
/**
* Callback called when the deployer is closed.
*/
void close();
}
The accept method
Deployers have an accept(File file)
method telling to Chameleon if the file is handled or not by the deployer. Obviously you can write your own accept method, but never forget to avoid checking the file existence. Indeed, your deployer would not be notified of file deletion (as the file is already deleted).
To help you, Chameleon provides two implementation of deployer you can extend:
org.ow2.chameleon.core.services.DirectoryBasedDeployer
- accept all files included in a specified directory (and any sub-directories)org.ow2.chameleon.core.services.ExtensionBasedDeployer
- accept all files having the specified extension (or set of extensions)
Writing your own deployer
Here is an example of a deployer managing txt
files. The DefaultDeployer
manages the extensions, and if not overriden, call onFileCreate
on each files given to the open
method.
package org.ow2.chameleon.core.activators;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.ow2.chameleon.core.services.ExtensionBasedDeployer;
import java.io.File;
import java.util.Collection;
/**
* A dummy deployer.
*/
@Component
@Instantiate
@Provides
public class TextDeployer extends ExtensionBasedDeployer {
public TextDeployer() {
super("txt");
}
@Override
public void onFileCreate(File file) {
System.out.println("New file " + file.getAbsolutePath());
}
@Override
public void onFileChange(File file) {
System.out.println("File content changed :" + file.getAbsolutePath());
}
@Override
public void onFileDelete(File file) {
System.out.println("File " + file.getAbsolutePath() + " deleted");
}
@Override
public void open(Collection<File> files) {
System.out.println(files.size() + " text files found");
}
@Override
public void close() {
System.out.println("Bye bye");
}
}
Notice that this deployer is an iPOJO component but it is not necessary.