Java Reference
In-Depth Information
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
public class HistoryDecorator implements Command,
History, FrameworkListener, BundleListener {
private final List<String> m_history =
Collections.synchronizedList(new ArrayList<String>());
private final Command m_next;
public HistoryDecorator(Command next, List<String> history) {
m_next = next;
m_history.addAll(history);
}
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
try {
m_next.exec(args, out, err);
} finally {
m_history.add(args);
}
}
public List<String> get() {
return new ArrayList<String>(m_history);
}
public void frameworkEvent(FrameworkEvent event) {
m_history.add("\tFrameworkEvent(type=" + event.getType() +
",bundle=" + event.getBundle() +
",source=" + event.getSource() +
",throwable=" + event.getThrowable() + ")");
}
public void bundleChanged(BundleEvent event) {
m_history.add("\tBundleEvent(type=" + event.getType() +
",bundle=" + event.getBundle() +
",source=" + event.getSource() + ")");
}
}
You use an interceptor pattern to wrap the commands so you can record the issued
commands. The wrapper also records any events in the history by implementing the
BundleListener and FrameworkListener interfaces. You maintain a list of all issued
commands and received events in the m_history member defined at B . The history
wrapper command forwards the command execution to the command C and stores it
in the history list.
The wrapper implements the single FrameworkListener.frameworkEvent() .
Here, you record the event information in the history list. The most important part of
the event is its type. Framework events are of one of the following types:
FrameworkEvent.STARTED —Indicates the framework has performed all initial-
ization and has finished starting up.
FrameworkEvent.INFO —Indicates some information of general interest in vari-
ous situations.
B
Defines
m_history
member
Forwards command
execution
C
 
Search WWH ::




Custom Search