diff --git a/WorkbenchProject/Cache/hostname.encrypted b/WorkbenchProject/Cache/hostname.encrypted new file mode 100644 index 0000000..adee18b Binary files /dev/null and b/WorkbenchProject/Cache/hostname.encrypted differ diff --git a/WorkbenchProject/Cache/username.encrypted b/WorkbenchProject/Cache/username.encrypted new file mode 100644 index 0000000..addafc8 Binary files /dev/null and b/WorkbenchProject/Cache/username.encrypted differ diff --git a/WorkbenchProject/Key b/WorkbenchProject/Key new file mode 100644 index 0000000..2a9788f Binary files /dev/null and b/WorkbenchProject/Key differ diff --git a/WorkbenchProject/LastSimulation/message b/WorkbenchProject/LastSimulation/message new file mode 100644 index 0000000..78ad942 Binary files /dev/null and b/WorkbenchProject/LastSimulation/message differ diff --git a/WorkbenchProject/LastSimulation/model.ttl b/WorkbenchProject/LastSimulation/model.ttl new file mode 100644 index 0000000..ca92a10 --- /dev/null +++ b/WorkbenchProject/LastSimulation/model.ttl @@ -0,0 +1,108 @@ +@prefix rdf: . +@prefix xsd: . +@prefix rdfs: . +@prefix remote: . +@prefix prov: . +@prefix local: . + + + a prov:Entity ; + rdfs:label "simulationConfigurationFile" ; + prov:wasDerivedFrom ; + prov:wasGeneratedBy . + + + a prov:Entity ; + rdfs:label "nlist" ; + prov:wasDerivedFrom ; + prov:wasGeneratedBy . + + + a prov:Activity ; + rdfs:label "upload" ; + prov:endedAtTime "2023-03-01T00:04:26.626Z"^^ ; + prov:generated ; + prov:startedAtTime "2023-03-01T00:04:26.298Z"^^ ; + prov:used ; + prov:wasAssociatedWith . + + + a prov:Entity ; + rdfs:label "nlist" . + + + a prov:Entity ; + rdfs:label "nlist" ; + prov:wasDerivedFrom ; + prov:wasGeneratedBy . + + + a prov:Entity ; + rdfs:label "nlist" . + + a prov:Activity ; + rdfs:label "upload" ; + prov:endedAtTime "2023-03-01T00:04:25.333Z"^^ ; + prov:generated ; + prov:startedAtTime "2023-03-01T00:04:25.026Z"^^ ; + prov:used ; + prov:wasAssociatedWith . + + a prov:SoftwareAgent . + + + a prov:Activity ; + rdfs:label "upload" ; + prov:endedAtTime "2023-03-01T00:04:25.99Z"^^ ; + prov:generated ; + prov:startedAtTime "2023-03-01T00:04:25.682Z"^^ ; + prov:used ; + prov:wasAssociatedWith . + + + a prov:Activity ; + rdfs:label "upload" ; + prov:endedAtTime "2023-03-01T00:04:26.298Z"^^ ; + prov:generated ; + prov:startedAtTime "2023-03-01T00:04:25.991Z"^^ ; + prov:used ; + prov:wasAssociatedWith . + + + a prov:Entity ; + rdfs:label "nlist" . + + + a prov:Activity ; + rdfs:label "upload" ; + prov:endedAtTime "2023-03-01T00:04:25.682Z"^^ ; + prov:generated ; + prov:startedAtTime "2023-03-01T00:04:25.333Z"^^ ; + prov:used ; + prov:wasAssociatedWith . + + + a prov:Activity ; + prov:generated ; + prov:wasAssociatedWith . + + + a prov:Entity ; + rdfs:label "simulationConfigurationFile" ; + prov:wasGeneratedBy . + + + a prov:Entity ; + rdfs:label "nlist" ; + prov:wasDerivedFrom ; + prov:wasGeneratedBy . + + + a prov:Entity ; + rdfs:label "script" . + + + a prov:Entity ; + rdfs:label "script" ; + prov:wasDerivedFrom ; + prov:wasGeneratedBy . diff --git a/WorkbenchProject/LastSimulation/simName b/WorkbenchProject/LastSimulation/simName new file mode 100644 index 0000000..ccff4fa Binary files /dev/null and b/WorkbenchProject/LastSimulation/simName differ diff --git a/WorkbenchProject/LastSimulation/simdir b/WorkbenchProject/LastSimulation/simdir new file mode 100644 index 0000000..a3876cd Binary files /dev/null and b/WorkbenchProject/LastSimulation/simdir differ diff --git a/WorkbenchProject/LastSimulation/simulation b/WorkbenchProject/LastSimulation/simulation new file mode 100644 index 0000000..cb7015f Binary files /dev/null and b/WorkbenchProject/LastSimulation/simulation differ diff --git a/WorkbenchProject/LastSimulation/uri b/WorkbenchProject/LastSimulation/uri new file mode 100644 index 0000000..28fee28 --- /dev/null +++ b/WorkbenchProject/LastSimulation/uri @@ -0,0 +1,3 @@ +C:\Users\Administrator\WorkbenchProjects\Default\trial404\provenance\trial404.ttl +DESKTOP-3N8B16A@205.175.118.10# +null diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/provenance/ProvMgr.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/provenance/ProvMgr.java index 9627ed1..acd0529 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/provenance/ProvMgr.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/provenance/ProvMgr.java @@ -6,6 +6,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; +import java.io.Serializable; import java.net.InetAddress; import java.net.URL; import java.net.URLConnection; @@ -50,7 +51,7 @@ * @author Del Davis, Extended by Joseph Conquest * @version 1.2 */ -public class ProvMgr { +public class ProvMgr implements Serializable { // private static final Logger LOG = Logger.getLogger(ProvMgr.class.getName()); @@ -63,14 +64,60 @@ public class ProvMgr { public static final String LOCAL_NS_PREFIX = "local"; /* URIs used to describe the provenance */ - private String provOutputFileURI; - private String localNameSpaceURI; - private String remoteNameSpaceURI; + private String provOutputFileUri; + private String localNameSpaceUri; + private String remoteNameSpaceUri; /* RDF in-memory representation of the provenance */ private Model model; // + /** + * The getter for provURI. + * + * @return return the provURI. + * + */ + public String getProvUri() { + return provOutputFileUri; + } + + /** + * The getter for LocalURI. + * + * @return return the LocalURI. + * + */ + public String getLocalUri() { + return localNameSpaceUri; + } + + /** + * The getter for RemoteURI. + * + * @return return the RemoteURI. + * + */ + public String getRemoteUri() { + return remoteNameSpaceUri; + } + + /** + * The constructor used by resume Lastsimulation. + * + * @param provUri the provURI + * @param localUri the localURI + * @param remoteUri the remoteURI + * @param model the model + */ + + public ProvMgr(String provUri, String localUri, String remoteUri, Model model) { + this.provOutputFileUri = provUri; + this.localNameSpaceUri = localUri; + this.remoteNameSpaceUri = remoteUri; + this.model = model; + } + // /** * Constructs the Provenance Constructor object from a previously recorded provenance file. @@ -97,7 +144,7 @@ public ProvMgr(Simulation simulation, boolean load) throws IOException, RiotNotF private void init(Simulation simulation) { // create RDF model model = ModelFactory.createDefaultModel(); - provOutputFileURI = simulation.getProvLocation() + provOutputFileUri = simulation.getProvLocation() .resolve(simulation.getName() + ".ttl").toString(); // set prefixes for... // RDF syntax @@ -108,9 +155,9 @@ private void init(Simulation simulation) { model.setNsPrefix("prov", ProvOntology.getPROVNameSpaceURI()); // XML schema model.setNsPrefix("xsd", ProvOntology.getXSDNameSpaceURI()); - localNameSpaceURI = getLocalNameSpaceURI(); + localNameSpaceUri = getLocalNameSpaceURI(); // Graphitti Prov - model.setNsPrefix(LOCAL_NS_PREFIX, localNameSpaceURI); + model.setNsPrefix(LOCAL_NS_PREFIX, localNameSpaceUri); } /** @@ -120,11 +167,11 @@ private void init(Simulation simulation) { */ private void load(Simulation simulation) throws RiotNotFoundException { String name = simulation.getName(); - provOutputFileURI = simulation.getProvLocation().resolve(name + ".ttl").toString(); - model = RDFDataMgr.loadModel(provOutputFileURI); - localNameSpaceURI = getLocalNameSpaceURI(); - model.setNsPrefix(LOCAL_NS_PREFIX, localNameSpaceURI); - trimRemoteNS(); + provOutputFileUri = simulation.getProvLocation().resolve(name + ".ttl").toString(); + model = RDFDataMgr.loadModel(provOutputFileUri); + localNameSpaceUri = getLocalNameSpaceURI(); + model.setNsPrefix(LOCAL_NS_PREFIX, localNameSpaceUri); + trimRemoteNS(); } /** @@ -137,8 +184,8 @@ private void trimRemoteNS() { String nameSpace = entry.getKey(); String uri = entry.getValue(); if (nameSpace.startsWith(REMOTE_NS_PREFIX)) { - remoteNameSpaceURI = uri.substring(uri.lastIndexOf('/') + 1); - model.setNsPrefix(nameSpace, remoteNameSpaceURI); + remoteNameSpaceUri = uri.substring(uri.lastIndexOf('/') + 1); + model.setNsPrefix(nameSpace, remoteNameSpaceUri); } } } @@ -166,9 +213,9 @@ public void setNsPrefix(String prefix, String uri) { * * @return The URI of the provenance output file */ - public String getProvFileURI() { - return provOutputFileURI; - } + public String getProvFileUri() { + return provOutputFileUri; + } /** * Gets the RDF model maintained by the manager. diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/FileManager.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/FileManager.java index da2a779..b945dc1 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/FileManager.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/FileManager.java @@ -175,14 +175,23 @@ public static Path getCurrentProjectDirectory() { return getProjectsDirectory().resolve(WorkbenchManager.getInstance().getProjectName()); } + private static String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } + /** * Provides the working directory of the current user. * * @return The working directory of the current user */ - public static Path getUserDir() { - return Paths.get(System.getProperty("user.dir")); - } + public static Path getUserDir() { + return Paths.get(workingDir()); + } /** * Provides the home directory of the current user. diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/WorkbenchManager.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/WorkbenchManager.java index 2710e34..c41ace3 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/WorkbenchManager.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/WorkbenchManager.java @@ -4,7 +4,10 @@ import com.jcraft.jsch.JSchException; import com.jcraft.jsch.SftpException; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; @@ -14,7 +17,6 @@ import javafx.scene.control.TextInputDialog; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; - import edu.uwb.braingrid.general.LoggerHelper; import edu.uwb.braingrid.provenance.ProvMgr; import edu.uwb.braingrid.workbench.model.Project; @@ -56,6 +58,14 @@ public final class WorkbenchManager { private Project project; private Simulation simulation; private ProvMgr prov; + + public void simulationSetter(Simulation inputSimulation) { + this.simulation = inputSimulation; + } + + public void provMgrSetter(ProvMgr inputProv) { + this.prov = inputProv; + } // // @@ -516,26 +526,44 @@ public boolean generateScript() { * @return True if all files were uploaded/copied successfully and the script was started, * otherwise false */ - public boolean runScript() { - boolean success = false; - ScriptManager sm = new ScriptManager(); - try { - String simulationName = simulation.getName(); - String scriptPath = simulation.getScriptFilePath(); - String[] neuronLists = FileManager.getNeuronListFilenames(simulationName); - success = sm.runScript(prov, simulation.getSimSpec(), simulationName, scriptPath, - neuronLists, simulation.getSimConfigFilename()); - simulation.setScriptRan(success); - simulation.setScriptStartedAt(); - messageAccumulator += sm.getOutstandingMessages(); - } catch (JSchException | SftpException | IOException | NullPointerException e) { - messageAccumulator += "\n" + "Script did not run do to " - + e.getClass() + "...\n"; - messageAccumulator += "Exception message: " + e.getMessage(); - } - - return success; - } + public boolean runScript() { + boolean success = false; + ScriptManager sm = new ScriptManager(); + try { + String simulationName = simulation.getName(); + String scriptPath = simulation.getScriptFilePath(); + String[] neuronLists = FileManager.getNeuronListFilenames(simulationName); + success = sm.runScript(prov, simulation.getSimSpec(), simulationName, scriptPath, + neuronLists, simulation.getSimConfigFilename()); + simulation.setScriptRan(success); + simulation.setScriptStartedAt(); + if (success) { + File simulationFile = new File(workingDir() + "\\LastSimulation\\simulation"); + FileOutputStream simOut = new FileOutputStream(simulationFile); + ObjectOutputStream simObjOut = new ObjectOutputStream(simOut); + simObjOut.writeObject(simulation); + } + + //save the simulation here + messageAccumulator += sm.getOutstandingMessages(); + } catch (JSchException | SftpException | IOException | NullPointerException e) { + e.printStackTrace(); + messageAccumulator += "\n" + "Script did not run do to " + + e.getClass() + "...\n"; + messageAccumulator += "Exception message: " + e.getMessage(); + } + + return success; + } + + private static String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } /** * Analyzes the redirected provenance output from an executed script. @@ -611,7 +639,7 @@ private void persistProvenance() { prov.persist(simulation); messageAccumulator += "\n" + "Provenance persisted to: " - + prov.getProvFileURI() + "\n"; + + prov.getProvFileUri() + "\n"; } catch (IOException e) { messageAccumulator += "\n" + "Unable to persist provenance\n" @@ -735,6 +763,15 @@ public String getMessages() { return messageAccumulator; } + /** + * Set the message for message Accumulator, used for remember last simulation. + * + * @param msg msg to set + */ + public void setMessages(String msg) { + messageAccumulator = msg; + } + /** * Clears the accumulated messages for this manager. */ diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/comm/SecureFileTransfer.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/comm/SecureFileTransfer.java index 6d4efd0..7fb9424 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/comm/SecureFileTransfer.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/comm/SecureFileTransfer.java @@ -1,5 +1,6 @@ package edu.uwb.braingrid.workbench.comm; +import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; @@ -7,6 +8,14 @@ import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpProgressMonitor; +import edu.uwb.braingrid.workbench.WorkbenchManager; +import edu.uwb.braingrid.workbench.ui.SimulationRuntimeDialog; +import java.io.InputStreamReader; +import javafx.scene.control.TextArea; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import riotcmd.infer; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -273,4 +282,82 @@ private boolean readInputStream(InputStream in, ChannelExec cExec) { return success; } // + + /** + * Connects to the last simulation. + * + * @param hostname The name of the host machine to connect to. + * @param username The user's login username. + * @param password The user's login password. + * @param simName The last simulation to connect to. + * @param manager The temp place holder. + */ + + public void checkLastSim(String hostname, String username, + String password, String simName, WorkbenchManager manager) { + JSch jsch = new JSch(); + try { + session = jsch.getSession(username, hostname, PORT); + session.setPassword(password); + session.setConfig("StrictHostKeyChecking", "no"); //optional + session.connect(); + + Channel channel = session.openChannel("exec"); + ((ChannelExec) channel).setInputStream(null); + ((ChannelExec) channel).setCommand("cd WorkbenchSimulations/ && ls"); + channel.connect(); + + InputStream in; + try { + in = channel.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String files; + while ((files = reader.readLine()) != null) { + if (files.equals(simName)) { + Channel channel2 = session.openChannel("sftp"); + channel2.connect(); + //((ChannelSftp) channel2).setInputStream(null); + try { + ((ChannelSftp) channel2).cd( + "WorkbenchSimulations//" + simName + "//Output//Debug"); + InputStream workBenchLog = ((ChannelSftp) channel2).get("workbench.txt"); + BufferedReader readLog = new BufferedReader(new InputStreamReader(workBenchLog)); + String line; + String lastline = ""; + while ((line = readLog.readLine()) != null) { + lastline = line; + } + String completion = "Complete"; + lastline = lastline.substring( + lastline.length() - completion.length(), lastline.length()); + if (lastline.equals("Complete")) { + displayDownloadFrame(channel2, manager); + break; + } + } catch (SftpException e) { + e.printStackTrace(); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + } catch (JSchException e) { + e.printStackTrace(); + } + } + + private void displayDownloadFrame(Channel channel, WorkbenchManager manager) { + JFrame frame = new JFrame(); + String[] options = {"Download", "Cancel"}; + int option = JOptionPane.showOptionDialog(frame, + "Last simlation completed, do you want to download?", "Last Simulation Completed", + JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, + null, options, options[0]); + if (option == JOptionPane.YES_NO_OPTION) { + SimulationRuntimeDialog srd = new SimulationRuntimeDialog( + new TextArea(manager.getMessages())); + } + } } diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/ScriptHistory.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/ScriptHistory.java index d1f6069..66e5beb 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/ScriptHistory.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/ScriptHistory.java @@ -1,5 +1,7 @@ package edu.uwb.braingrid.workbench.model; +import java.io.Serializable; + import edu.uwb.braingrid.workbench.utils.DateTime; /** @@ -7,7 +9,7 @@ * * @author Aaron Conrad */ -public class ScriptHistory { +public class ScriptHistory implements Serializable { // private long startedAt; diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/Simulation.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/Simulation.java index 1ab69e7..23e633b 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/Simulation.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/Simulation.java @@ -1,5 +1,6 @@ package edu.uwb.braingrid.workbench.model; +import java.io.Serializable; import java.nio.file.Path; import java.util.logging.Logger; @@ -13,7 +14,7 @@ * * @author Steven Leighton */ -public class Simulation { +public class Simulation implements Serializable { // private static final Logger LOG = Logger.getLogger(Simulation.class.getName()); diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/SimulationSpecification.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/SimulationSpecification.java index e9f58f5..cb4e4a5 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/SimulationSpecification.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/model/SimulationSpecification.java @@ -1,6 +1,7 @@ package edu.uwb.braingrid.workbench.model; import com.fasterxml.jackson.annotation.JsonIgnore; +import java.io.Serializable; import java.util.Objects; /** @@ -10,7 +11,7 @@ * * @author Del Davis */ -public class SimulationSpecification { +public class SimulationSpecification implements Serializable { /** * Description of the execution model for a simulation (in particular, these values indicate the diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/script/ScriptManager.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/script/ScriptManager.java index 123f616..955a945 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/script/ScriptManager.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/script/ScriptManager.java @@ -1,11 +1,19 @@ package edu.uwb.braingrid.workbench.script; +import com.fasterxml.jackson.databind.ObjectMapper; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.SftpException; +import edu.uwb.braingrid.workbench.Workbench; +import edu.uwb.braingrid.workbench.WorkbenchManager; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -16,8 +24,8 @@ import java.util.logging.Logger; import javax.xml.parsers.ParserConfigurationException; import org.apache.jena.rdf.model.Resource; +import org.apache.jena.sparql.function.library.e; import org.xml.sax.SAXException; - import edu.uwb.braingrid.provenance.ProvMgr; import edu.uwb.braingrid.provenance.WorkbenchOperationRecorder; import edu.uwb.braingrid.workbench.FileManager; @@ -251,126 +259,186 @@ public boolean runScript(ProvMgr provMgr, SimulationSpecification simSpec, return success; } - private boolean runRemoteScript(ProvMgr provMgr, String hostname, String simulationName, - String scriptPath, String[] nListFilenames, String simConfigFilename) - throws JSchException, FileNotFoundException, SftpException { - Long functionStartTime = System.currentTimeMillis(); - Long accumulatedTime = 0L; - char[] password = null; - boolean success = true; - // get username and password - LoginCredentialsDialog lcd = new LoginCredentialsDialog(hostname, true); - if (lcd.okClicked()) { - SecureFileTransfer sft = new SecureFileTransfer(); - password = lcd.getPassword(); - lcd.clearPassword(); - /* Create Simulation Directory and Subdirectories */ - String simDir = FileManager.getSimulationsDirectory() + "/" + simulationName; - String configDir = simDir + "/configfiles"; - String nListDir = simDir + "/configfiles/NList"; - String remoteScriptPath = simDir + "/" + FileManager.getSimpleFilename(scriptPath); - String cmd = "mkdir -p " + nListDir; - sft.executeCommand(cmd, hostname, lcd.getUsername(), password, true); - /* Upload Script */ - Date uploadStartTime = new Date(); - if (sft.uploadFile(scriptPath, simDir, hostname, lcd.getUsername(), password, null)) { - // record provenance of upload - if (provMgr != null) { - Long startTime = System.currentTimeMillis(); - WorkbenchOperationRecorder.recordFile(provMgr, scriptPath, remoteScriptPath, - "script", hostname, "uploadScript", uploadStartTime, new Date()); - accumulatedTime = DateTime.sumProvTiming(startTime, accumulatedTime); - } - outstandingMessages += "\n" + scriptPath + "\nuploaded to " + hostname + "\n"; - /* Upload Neuron List Files */ - boolean loopSuccess; - if (nListFilenames != null) { - for (String nListFilename : nListFilenames) { - String filename = FileManager.getSimpleFilename(nListFilename); - outstandingMessages += "\n" + "Uploaded " + nListFilename + "\nto " - + hostname + "\n"; - uploadStartTime = new Date(); - loopSuccess = sft.uploadFile(nListFilename, nListDir, hostname, - lcd.getUsername(), password, null); - if (!loopSuccess) { - success = false; - outstandingMessages += "\n" + "Problem uploading " + nListFilename - + "\nto " + hostname + "\n"; - break; - } else { - outstandingMessages += "\n" + filename + "\nuploaded to " + hostname - + "\n"; - // record upload provenance - if (provMgr != null) { - Long startTime = System.currentTimeMillis(); - String nlType = ""; - try { - nlType = InputAnalyzer.getInputType( - new File(nListFilename)).toString(); - } catch (ParserConfigurationException | SAXException - | IOException ex) { - } - WorkbenchOperationRecorder.recordFile(provMgr, nListFilename, - nListDir + FileManager.getSimpleFilename(nListFilename), - "nlist", hostname, "upload_" + nlType + "_NList", - uploadStartTime, new Date()); - accumulatedTime = DateTime.sumProvTiming(startTime, - accumulatedTime); - } - } - } - } - /* Upload Simulation Configuration File */ - if (success) { - uploadStartTime = new Date(); - success = sft.uploadFile(simConfigFilename, configDir, hostname, - lcd.getUsername(), password, null); - if (success) { - if (provMgr != null) { - Long startTime = System.currentTimeMillis(); - WorkbenchOperationRecorder.recordFile(provMgr, simConfigFilename, - configDir + FileManager.getSimpleFilename(simConfigFilename), - "simulationConfigurationFile", hostname, "upload_SimConfig", - uploadStartTime, new Date()); - accumulatedTime = DateTime.sumProvTiming(startTime, accumulatedTime); - } - outstandingMessages += "\n" - + FileManager.getSimpleFilename(simConfigFilename) - + "\nuploaded to " + hostname + "\n"; - } else { - outstandingMessages += "\n" + "Problem uploading " - + FileManager.getSimpleFilename(simConfigFilename) - + "\nto " + hostname + "\n"; - } - } - /* Execute Script */ - if (success) { - cmd = "nohup sh " + remoteScriptPath + " &"; - outstandingMessages += "\n" + "Executing " + cmd - + "\nat " - + hostname + "\n"; - sft.executeCommand(cmd, hostname, lcd.getUsername(), password, false); - success = true; - } + private static String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } + + private String lastSimLocation() { + return workingDir() + "\\LastSimulation"; + } + + private void saveSimDir(String simDir) { + try { + FileOutputStream simDirLocation = new FileOutputStream( + new File(makeLastSimulationDir() + "\\simdir")); + ObjectOutputStream writeSimDir = new ObjectOutputStream(simDirLocation); + writeSimDir.writeObject(simDir); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveSimName(String simName) { + FileOutputStream simNameLocation; + try { + simNameLocation = new FileOutputStream( + new File(lastSimLocation() + "\\simName")); + ObjectOutputStream writeSimName = new ObjectOutputStream(simNameLocation); + writeSimName.writeObject(simName); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private boolean runRemoteScript(ProvMgr provMgr, String hostname, String simulationName, + String scriptPath, String[] nListFilenames, String simConfigFilename) + throws JSchException, FileNotFoundException, SftpException { + Long functionStartTime = System.currentTimeMillis(); + Long accumulatedTime = 0L; + char[] password = null; + boolean success = true; + // get username and password + LoginCredentialsDialog lcd = new LoginCredentialsDialog(hostname, true); + if (lcd.okClicked()) { + SecureFileTransfer sft = new SecureFileTransfer(); + password = lcd.getPassword(); + lcd.clearPassword(); + /* Create Simulation Directory and Subdirectories */ + String simDir = FileManager.getSimulationsDirectory() + "/" + simulationName; + String configDir = simDir + "/configfiles"; + String nListDir = simDir + "/configfiles/NList"; + String remoteScriptPath = simDir + "/" + FileManager.getSimpleFilename(scriptPath); + String cmd = "mkdir -p " + nListDir; + sft.executeCommand(cmd, hostname, lcd.getUsername(), password, true); + saveSimDir(simDir); + saveSimName(simulationName); + /* Upload Script */ + Date uploadStartTime = new Date(); + if (sft.uploadFile(scriptPath, simDir, hostname, lcd.getUsername(), password, null)) { + // record provenance of upload + if (provMgr != null) { + Long startTime = System.currentTimeMillis(); + WorkbenchOperationRecorder.recordFile(provMgr, scriptPath, remoteScriptPath, + "script", hostname, "uploadScript", uploadStartTime, new Date()); + accumulatedTime = DateTime.sumProvTiming(startTime, accumulatedTime); + } + outstandingMessages += "\n" + scriptPath + "\nuploaded to " + hostname + "\n"; + /* Upload Neuron List Files */ + boolean loopSuccess; + if (nListFilenames != null) { + for (String nListFilename : nListFilenames) { + String filename = FileManager.getSimpleFilename(nListFilename); + outstandingMessages += "\n" + "Uploaded " + nListFilename + "\nto " + + hostname + "\n"; + uploadStartTime = new Date(); + loopSuccess = sft.uploadFile(nListFilename, nListDir, hostname, + lcd.getUsername(), password, null); + if (!loopSuccess) { + success = false; + outstandingMessages += "\n" + "Problem uploading " + nListFilename + + "\nto " + hostname + "\n"; + break; } else { - outstandingMessages += "\n" + "Failed to upload script to " + hostname + "\n"; + outstandingMessages += "\n" + filename + "\nuploaded to " + hostname + + "\n"; + // record upload provenance + if (provMgr != null) { + Long startTime = System.currentTimeMillis(); + String nlType = ""; + try { + nlType = InputAnalyzer.getInputType( + new File(nListFilename)).toString(); + } catch (ParserConfigurationException | SAXException + | IOException ex) { + ex.printStackTrace(); + } + WorkbenchOperationRecorder.recordFile(provMgr, nListFilename, + nListDir + FileManager.getSimpleFilename(nListFilename), + "nlist", hostname, "upload_" + nlType + "_NList", + uploadStartTime, new Date()); + accumulatedTime = DateTime.sumProvTiming(startTime, + accumulatedTime); + } } - } else { - outstandingMessages += "\n" + "\nRemote Credentials Specification Cancelled\n"; + } } - outstandingMessages += "\n" + "Remote Operations Completed: " + new Date() + "\n"; - - if (password != null) { - Arrays.fill(password, '0'); + /* Upload Simulation Configuration File */ + if (success) { + uploadStartTime = new Date(); + success = sft.uploadFile(simConfigFilename, configDir, hostname, + lcd.getUsername(), password, null); + if (success) { + if (provMgr != null) { + Long startTime = System.currentTimeMillis(); + WorkbenchOperationRecorder.recordFile(provMgr, simConfigFilename, + configDir + FileManager.getSimpleFilename(simConfigFilename), + "simulationConfigurationFile", hostname, "upload_SimConfig", + uploadStartTime, new Date()); + accumulatedTime = DateTime.sumProvTiming(startTime, accumulatedTime); + } + outstandingMessages += "\n" + + FileManager.getSimpleFilename(simConfigFilename) + + "\nuploaded to " + hostname + "\n"; + } else { + outstandingMessages += "\n" + "Problem uploading " + + FileManager.getSimpleFilename(simConfigFilename) + + "\nto " + hostname + "\n"; + } } + /* Execute Script */ + if (success) { + cmd = "nohup sh " + remoteScriptPath + " &"; + outstandingMessages += "\n" + "Executing " + cmd + + "\nat " + + hostname + "\n"; + sft.executeCommand(cmd, hostname, lcd.getUsername(), password, false); + success = true; + } + } else { + outstandingMessages += "\n" + "Failed to upload script to " + hostname + "\n"; + } + } else { + outstandingMessages += "\n" + "\nRemote Credentials Specification Cancelled\n"; + } + outstandingMessages += "\n" + "Remote Operations Completed: " + new Date() + "\n"; + + if (password != null) { + Arrays.fill(password, '0'); + } DateTime.recordFunctionExecutionTime("ScriptManager", "runRemoteScript", System.currentTimeMillis() - functionStartTime, provMgr != null); if (provMgr != null) { DateTime.recordAccumulatedProvTiming("ScriptManager", "runRemoteScript", accumulatedTime); } - return success; + if (success) { + String simLocation = workingDir() + "\\LastSimulation"; + File messageFile = new File(simLocation + "\\message"); + File uriFile = new File(simLocation + "\\uri"); + FileOutputStream messageOut = new FileOutputStream(messageFile); + try { + ObjectOutputStream messageOutObj = new ObjectOutputStream(messageOut); + messageOutObj.writeObject(WorkbenchManager.getInstance().getMessages()); + provMgr.getModel().write( + new FileOutputStream(new File( + workingDir() + "\\LastSimulation\\model.ttl")), "TURTLE"); + FileWriter uriWriter = new FileWriter(uriFile); + uriWriter.write(provMgr.getProvUri() + "\n"); + uriWriter.write(provMgr.getLocalUri() + "\n"); + uriWriter.write(provMgr.getRemoteUri() + "\n"); + uriWriter.close(); + messageOutObj.close(); + } catch (IOException e) { + e.printStackTrace(); + } } + return success; + } private boolean runLocalScript(ProvMgr provMgr, String simulationName, String scriptLocation, String[] inputFilenames, String simConfigFilename) throws IOException { @@ -645,6 +713,15 @@ public long analyzeScriptOutput(SimulationSpecification simSpec, Simulation simu return timeCompleted; } + private String makeLastSimulationDir() { + String lastSim = workingDir() + "\\LastSimulation"; + File lastSimDirectory = new File(lastSim); + if (!lastSimDirectory.exists() || !lastSimDirectory.isDirectory()) { + lastSimDirectory.mkdir(); + } + return lastSim; + } + private String fetchScriptOutputFiles(Simulation simulation, SimulationSpecification simSpec, Path scriptOutputFolder) throws JSchException, SftpException, IOException { String filename = null; diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/LoginCredentialsDialog.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/LoginCredentialsDialog.java index 7b7c48d..4d7dea9 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/LoginCredentialsDialog.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/LoginCredentialsDialog.java @@ -16,6 +16,15 @@ */ public class LoginCredentialsDialog extends javax.swing.JDialog { + private String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } + // /** * This method is called from within the constructor to initialize the form. @@ -41,8 +50,8 @@ private void initComponents() { hostnameLabel.setText("Hostname: "); usernameLabel.setText("Username: "); - File inputFile = new File(System.getProperty("user.dir") + "\\Cache\\username.encrypted"); - File key = new File(System.getProperty("user.dir") + "\\Key"); + File inputFile = new File(workingDir() + "\\Cache\\username.encrypted"); + File key = new File(workingDir() + "\\Key"); try { FileInputStream keyInput = new FileInputStream(key); ObjectInputStream keyObj; diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/SimulationSpecificationDialog.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/SimulationSpecificationDialog.java index c50bd93..8bca0d3 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/SimulationSpecificationDialog.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbench/ui/SimulationSpecificationDialog.java @@ -588,7 +588,7 @@ private void setRemoteRelatedComponentsEnabled(boolean enabled) { usernameTextField.setEnabled(enabled); passwordField.setEnabled(enabled); //if the cache file exists, autofill the textfield; - File key = new File(System.getProperty("user.dir") + "\\Key"); + File key = new File(workingDir() + "\\Key"); String userPostfix = "\\Cache\\username.encrypted"; String hostnamePostfix = "\\Cache\\hostname.encrypted"; boolean usernameFilled = tryFillTextField(key, userPostfix, "username", usernameTextField); @@ -596,9 +596,18 @@ private void setRemoteRelatedComponentsEnabled(boolean enabled) { hostnamePostfix, "hostname", hostAddressTextField); } + private static String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } + private boolean tryFillTextField(File key, String location, String fieldName, JTextField fieldToFill) { - File inputFile = new File(System.getProperty("user.dir") + location); + File inputFile = new File(workingDir() + location); FileInputStream keyInput; try { keyInput = new FileInputStream(key); @@ -762,7 +771,7 @@ private void validateUsername() { } private String getCachePath() { - return System.getProperty("user.dir") + "\\Cache"; + return workingDir() + "\\Cache"; } private static final String CHARACTERS @@ -817,7 +826,7 @@ public static String doCrypto(int cipherMode, String key, File inputFile, Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(cipherMode, secretKey); if (cipherMode == Cipher.ENCRYPT_MODE) { - File keyFile = new File(System.getProperty("user.dir") + "\\Key"); + File keyFile = new File(workingDir() + "\\Key"); FileOutputStream keyOutput = new FileOutputStream(keyFile); ObjectOutputStream keyObj = new ObjectOutputStream(keyOutput); keyObj.writeObject(key); diff --git a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbenchdashboard/WorkbenchDashboard.java b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbenchdashboard/WorkbenchDashboard.java index 1ffbb9c..535eb27 100644 --- a/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbenchdashboard/WorkbenchDashboard.java +++ b/WorkbenchProject/src/main/java/edu/uwb/braingrid/workbenchdashboard/WorkbenchDashboard.java @@ -1,17 +1,34 @@ package edu.uwb.braingrid.workbenchdashboard; +import com.fasterxml.jackson.databind.ObjectMapper; +import edu.uwb.braingrid.provenance.ProvMgr; +import edu.uwb.braingrid.workbench.comm.SecureFileTransfer; +import edu.uwb.braingrid.workbench.model.Simulation; +import edu.uwb.braingrid.workbench.ui.LoginCredentialsDialog; +import edu.uwb.braingrid.workbench.ui.SimulationSpecificationDialog; +import java.awt.FlowLayout; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.io.ObjectInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.logging.FileHandler; import java.util.logging.Logger; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; +import org.eclipse.jgit.transport.CredentialItem.Username; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.input.KeyCode; import javafx.application.Application; import javafx.stage.Stage; - import edu.uwb.braingrid.general.LoggerHelper; import edu.uwb.braingrid.workbench.FileManager; import edu.uwb.braingrid.workbench.WorkbenchManager; @@ -122,7 +139,6 @@ public void start(Stage primaryStage) throws Exception { primaryStage.setScene(scene); primaryStage.setMaximized(true); primaryStage.show(); - // Exit application on window close primaryStage.setOnCloseRequest(event -> { Platform.exit(); @@ -131,5 +147,90 @@ public void start(Stage primaryStage) throws Exception { // Initialize Workbench Manager WorkbenchManager.getInstance(); + checkLastSim(); + } + + private String workingDir() { + String dir = System.getProperty("user.dir"); + String target = "\\target"; + if (dir.endsWith(target)) { + dir = dir.substring(0, dir.length() - target.length()); + } + return dir; + } + + private void checkLastSim() { + String workDir = workingDir(); + String substr = "\\target"; + if (workDir.endsWith(substr)) { + workDir = workDir.substring(0, workDir.length() - substr.length()); + } + String simPath = workDir + "\\LastSimulation\\simdir"; + File lastSim = new File(simPath); + if (lastSim.exists() && lastSim.isFile() && lastSim.length() != 0) { + LOG.info("Last Simulation detected"); + JFrame frame = new JFrame(); + String[] options = {"Resume", "No"}; + int option = JOptionPane.showOptionDialog(frame, + "Do you want to resume last simulation?", "Last Simulation Detected", + JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, + null, options, options[0]); + if (option == JOptionPane.YES_NO_OPTION) { + try { + FileInputStream readKey = new FileInputStream( + new File(workingDir() + "\\Key")); + try { + ObjectInputStream readKeyObj = new ObjectInputStream(readKey); + String key; + try { + key = (String) readKeyObj.readObject(); + File hostInfo = new File( + workingDir() + "\\Cache\\hostname.encrypted"); + SimulationSpecificationDialog tempDiaLog = new SimulationSpecificationDialog(); + String realHostInfo = tempDiaLog.decrypt(key, hostInfo, hostInfo, ""); + FileInputStream readSimlationName = new FileInputStream(new File( + workingDir() + "\\LastSimulation\\simName")); + ObjectInputStream readSimNameObj = new ObjectInputStream(readSimlationName); + String simName = (String) readSimNameObj.readObject(); + LoginCredentialsDialog loginToResume = new + LoginCredentialsDialog(realHostInfo, true); + final String username = loginToResume.getUsername(); + final String password = new String(loginToResume.getPassword()); + final SecureFileTransfer fileTransfer = new SecureFileTransfer(); + + File simulationInput = new File(workingDir() + "\\LastSimulation\\simulation"); + FileInputStream simIn = new FileInputStream(simulationInput); + ObjectInputStream simInObj = new ObjectInputStream(simIn); + + BufferedReader readUri = new BufferedReader( + new FileReader(workingDir() + "\\LastSimulation\\uri")); + String provUri = readUri.readLine(); + String localUri = readUri.readLine(); + String remoteUri = readUri.readLine(); + Model model = ModelFactory.createDefaultModel(); + model.read(workingDir() + "\\LastSimulation\\model.ttl", "TURTLE"); + ProvMgr lastMgr = new ProvMgr(provUri, localUri, remoteUri, model); + WorkbenchManager.getInstance().simulationSetter((Simulation) simInObj.readObject()); + WorkbenchManager.getInstance().provMgrSetter(lastMgr); + ObjectInputStream msgReader = new ObjectInputStream( + new FileInputStream(new File(workingDir() + "\\LastSimulation\\message"))); + String message = (String) msgReader.readObject(); + WorkbenchManager.getInstance().setMessages(message); + fileTransfer.checkLastSim(realHostInfo, username, + password, simName, WorkbenchManager.getInstance()); + JOptionPane.getRootFrame().dispose(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } else { + JOptionPane.getRootFrame().dispose(); } + } }