package de.mpg.mpiz.koeln.anyname;
import java.util.Random;
import de.kerner.commons.osgi.utils.ServiceNotAvailabeException;
import de.mpg.mpiz.koeln.anna.abstractstep.AbstractAnnaStep;
import de.mpg.mpiz.koeln.anna.data.DataBean;
import de.mpg.mpiz.koeln.anna.server.data.DataModifier;
import de.mpg.mpiz.koeln.anna.server.data.DataProxy;
import de.mpg.mpiz.koeln.anna.server.data.simple.SimpleDataServer;
import de.mpg.mpiz.koeln.anna.step.StepExecutionException;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
public class Test extends AbstractAnnaStep<DataBean> {
private volatile ServiceTracker tracker;
@Override
public boolean canBeSkipped(DataProxy<DataBean> proxy) throws Throwable {
return false;
}
@Override
public DataProxy<DataBean> getDataProxy()
throws ServiceNotAvailabeException {
SimpleDataServer server = (SimpleDataServer) tracker.getService();
if (server == null)
throw new ServiceNotAvailabeException("could not find data server");
return server;
}
@Override
protected synchronized void init(BundleContext context)
throws StepExecutionException {
super.init(context);
tracker = new ServiceTracker(context, SimpleDataServer.class.getName(),
null);
tracker.open();
}
@Override
protected void finalize() throws Throwable {
this.tracker.close();
tracker = null;
super.finalize();
}
@Override
public boolean requirementsSatisfied(DataProxy<DataBean> proxy)
throws Throwable {
return true;
}
@Override
public boolean run(DataProxy<DataBean> proxy) throws Throwable {
final int number = new Random().nextInt();
proxy.modifiyData(new DataModifier<DataBean>() {
public void modifiyData(DataBean v) {
v.addCustom("number", number);
}
});
// Just to verify //
logger.info(proxy.viewData().getCustom("number"));
return true;
}
public boolean isCyclic() {
return false;
}
} |