package org.cipres.kepler.registry; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.net.URL; import java.net.URLDecoder; import javax.swing.JOptionPane; import org.exolab.castor.xml.Unmarshaller; import org.cipres.communication.Facilitator; import org.cipres.guigen.ServiceCommandPanel; import org.cipres.helpers.CipresRegistry; import org.cipres.helpers.RegistryEntryWrapper; import org.cipres.properties.PropertyManager; public class CipresKeplerRegistry { private int groupID = 0; // Application properties stored in registry.xml private String defaultInputFileDir; private String defaultStdOutDir; private String defaultStdErrDir; private ActorInfo[] actors; private boolean appPathsChecked = false; /** * @param actorName * @return Actor */ public ActorInfo getActor(String actorName) { if (!appPathsChecked) { checkActorPaths(); } for (int i = 0; i < actors.length; i++) { if (actors[i].getActorName().equalsIgnoreCase(actorName)) { return actors[i]; } } return null; } /** * @return Returns the defaultInputFileDir. */ public String getDefaultInputFileDir() { return defaultInputFileDir; } /** * @param defaultInputFileDir * The defaultInputFileDir to set. */ public void setDefaultInputFileDir(String defaultInputFileDir) throws Exception { //if not yet set create one in base dir String baseDir = getBaseDir(); String sep = System.getProperty("file.separator"); File f; if(defaultInputFileDir==null || defaultInputFileDir.trim().length()==0){ //make necessary dirs f = new File(baseDir + sep + "sample_data"); f.mkdirs(); } else { f = new File(defaultInputFileDir); } if (!f.exists() || !f.isDirectory()) { if (!f.mkdir()) { throw new Exception( "Unable to locate or create defaultInputFileDir at " + defaultInputFileDir); } } this.defaultInputFileDir = f.getAbsolutePath() + sep; } /** * @return Returns the defaultStdErrDir. */ public String getDefaultStdErrDir() { return defaultStdErrDir; } /** * @param defaultStdErrDir * The defaultStdErrDir to set. */ public void setDefaultStdErrDir(String defaultStdErrDir) throws Exception { //if not yet set create one in user home dir String homeDir = System.getProperty("user.home"); String sep = System.getProperty("file.separator"); File f; if(defaultStdErrDir==null || defaultStdErrDir.trim().length()==0){ //make necessary dirs f = new File(homeDir + sep + ".kepler" + sep + "cipres" + sep + "temp"); f.mkdirs(); } else { f = new File(defaultStdErrDir); } if (!f.exists() || !f.isDirectory()) { if (!f.mkdir()) { throw new Exception( "Unable to locate or create defaultStdErrDir at " + defaultStdErrDir); } } this.defaultStdErrDir = f.getAbsolutePath() + sep; } /** * @return Returns the defaultStdOutDir. */ public String getDefaultStdOutDir() { return defaultStdOutDir; } /** * @param defaultStdOutDir * The defaultStdOutDir to set. */ public void setDefaultStdOutDir(String defaultStdOutDir) throws Exception { //if not yet set create one in user home dir String homeDir = System.getProperty("user.home"); String sep = System.getProperty("file.separator"); File f; if(defaultStdOutDir==null || defaultStdOutDir.trim().length()==0){ //make necessary dirs f = new File(homeDir + sep + ".kepler" + sep + "cipres" + sep + "temp"); f.mkdirs(); } else { f = new File(defaultStdOutDir); } if (!f.exists() || !f.isDirectory()) { if (!f.mkdir()) { throw new Exception( "Unable to locate or create defaultStdOutDir at " + defaultStdOutDir); } } this.defaultStdOutDir = f.getAbsolutePath() + sep; } /** * @return Returns the Actors. */ public ActorInfo[] getActors() { return actors; } /** * @param actors * The Actors to set. */ public void setActors(ActorInfo[] actors) { this.actors = actors; } protected static CipresKeplerRegistry deserialize(File serializedFile, String defaultDir) throws Exception { FileReader reader = null; try { reader = new FileReader(serializedFile); CipresKeplerRegistry ckr = (CipresKeplerRegistry) Unmarshaller .unmarshal(CipresKeplerRegistry.class, reader); ckr.initCipresRegistry(); return ckr; } catch (Exception e) { throw e; } finally { reader.close(); } } public static CipresKeplerRegistry EditRegistry() throws Exception { ServiceCommandPanel cmdpnl = new ServiceCommandPanel(new File(Globals .getInstance().getGuiXmlDir() + "RegistryEditor.xml")); cmdpnl.setServiceDisplayName("Registry Editor"); cmdpnl.setNotifyButtonDisplayMode(ServiceCommandPanel.NOTIFY_BUTTON_DISPLAY_MODE_SETPARAMS); CipresKeplerRegistry reg = Globals.getInstance().getRegistry(); // set values in gui to current settings for (int i = 0; i < reg.getActors().length; i++) { if (reg.getActors()[i].getAppName().equalsIgnoreCase("paup")) { cmdpnl.setParameterValue("paup", reg.getActors()[i].getAppPath()); i = reg.getActors().length; } } cmdpnl.setParameterValue("defaultInputFileDir", reg.getDefaultInputFileDir()); cmdpnl.setParameterValue("defaultStdOutDir", reg.getDefaultStdOutDir()); cmdpnl.setParameterValue("defaultStdErrDir", reg.getDefaultStdErrDir()); cmdpnl.showServicePanelAsDialog(); reg.setDefaultInputFileDir(cmdpnl.getParameterValue("defaultInputFileDir")); reg.setDefaultStdOutDir(cmdpnl.getParameterValue("defaultStdOutDir")); reg.setDefaultStdErrDir(cmdpnl.getParameterValue("defaultStdErrDir")); // set values of paup actors to that specified in gui String paupLocation = cmdpnl.getParameterValue("paup"); for (int i = 0; i < reg.getActors().length; i++) { if (reg.getActors()[i].getAppName().equalsIgnoreCase("paup")) reg.getActors()[i].setAppPath(paupLocation); } // serialize to xml FileWriter writer = null; writer = new FileWriter(Globals.getInstance().getRegistryXmlFilepath()); org.exolab.castor.xml.Marshaller.marshal(reg, writer); return reg; } private void checkActorPaths() { // if path has not been set for any actors alert user that // app paths need to be set boolean appPathMissing = false; StringBuffer sb = new StringBuffer(); File appFile = null; String fname = null; for (int i = 0; i < this.getActors().length; i++) { fname = this.getActors()[i].getAppPath(); if (fname != null) { appFile = new File(this.getActors()[i].getAppPath()); } if (fname == null || !appFile.exists()) { sb.append(" -" + this.getActors()[i].getActorName() + "\n"); appPathMissing = true; } } if (appPathMissing) { JOptionPane.showMessageDialog(null, "Paths for the following " + "applications have not been set:\n" + sb.toString() + " Click 'Ok' to bring up the dialog to set their paths"); try { CipresKeplerRegistry.EditRegistry(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "The following error " + "occured when updating the application information: " + e.getMessage()); e.printStackTrace(); } } // now checck cipres registry to see that app paths have been set // This will throw a RegistryConfigError if paup or python paths are // needed but not set. String[] args = new String[0]; try { // this method will attempt to configure the Property and Registry services, // and will display a gui if needed to allow the user to configure things // manually. If manual configuration is needed, but the user declines to // do so, just exit out of the application if (Facilitator.initializeAndConfigure("CipresKeplerRegistry", args, null) == false) System.exit(-1); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } appPathsChecked = true; } /* * Determines application root by finding CipresKeplerRegistry.jar via * the classpath. It should be found in "application root"/lib/jar/cipres */ private String getBaseDir() throws Exception { ClassLoader cl = CipresKeplerRegistry.class.getClassLoader(); URL url =cl.getResource("jar/cipres/CipresKeplerRegistry.jar"); if (url == null) throw new Exception("Unable to locate the base directory of the distribution"); String jarFilePath = URLDecoder.decode(url.getPath(), "UTF-8"); File jarFile = new File(jarFilePath); if (!jarFile.exists()) throw new Exception("Unable to find the CipresKeplerRegistry.jar "); return jarFile.getParentFile().getParentFile().getParentFile().getParent(); } /** * * @throws Exception */ private void initCipresRegistry() throws Exception { final String appName = "Kepler"; Facilitator.initialize(appName); groupID = CipresRegistry.createCipresServiceGroup(appName); final String binDir = PropertyManager.getInstance().getProperty("cipres.bin.path"); final String separator = System.getProperty("file.separator"); final String scriptName = System.getProperty("os.name").startsWith("Windows") ? "cipres_services.bat" : "cipres_services"; Runtime.getRuntime().addShutdownHook(new RegistryShutdown(binDir + separator + scriptName + " -kpr")); } /** * * @param idlInterface * @return */ public RegistryEntryWrapper getCipresService(final Class idlInterface) throws Exception { return CipresRegistry.getCipresServiceWrapper(idlInterface, null, null, groupID); } /** * a shutdown hook */ private class RegistryShutdown extends Thread { private final String killCommand; public RegistryShutdown(final String command) { killCommand = command; } //@Override public void run() { try { Runtime.getRuntime().exec(killCommand); } catch (final Exception err) { err.printStackTrace(); } } } }