package workbench;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.jci.ReloadingClassLoader;
import org.apache.commons.jci.listeners.ReloadingListener;
import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
import org.apache.commons.jci.stores.FileResourceStore;
public class ReloadingTransformer {
private final ClassLoader loader;
private final String classNameDiagram;
private final String classNameKappa;
public ReloadingTransformer(String className,String className2){
this.classNameDiagram = className;
this.classNameKappa = className2;
final File directory = new File("/Users/fwb/work/urban/java/Urban/bin");
if (directory.exists()){
ReloadingClassLoader classLoader = new ReloadingClassLoader(ReloadingTransformer.class.getClassLoader());
classLoader.addResourceStore(new FileResourceStore(directory));
loader = classLoader;
ReloadingListener listener = new ReloadingListener();
listener.addReloadNotificationListener(classLoader);
FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor();
fam.addListener(directory, listener);
fam.start();
} else {
loader = ReloadingTransformer.class.getClassLoader();
}
}
@SuppressWarnings("unchecked")
public Reader transformToDiagram(String text) {
try {
String diagramText = (String)getDelegate(classNameDiagram).transform(text);
if (diagramText != null)
return DotUtility.layoutGraph(diagramText);
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public String transformToKappa(String text) {
return (String)getDelegate(classNameKappa).transform(text);
}
@SuppressWarnings("rawtypes")
Transformer getDelegate(String className) {
try {
return (Transformer) loader.loadClass(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}