Commit c132ddd2 authored by legoc's avatar legoc
Browse files

Review code

parent 32eb3784
...@@ -17,52 +17,28 @@ ...@@ -17,52 +17,28 @@
*/ */
package fr.ill.ics.mail; package fr.ill.ics.mail;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import fr.ill.ics.bridge.LoginManager;
import fr.ill.ics.bridge.command.CommandZoneWrapper;
import fr.ill.ics.cameo.Application; import fr.ill.ics.cameo.Application;
import fr.ill.ics.nscclient.serverconnection.ServerConnection; import fr.ill.ics.cameo.ConnectionTimeout;
import fr.ill.ics.nscclient.serverconnection.ServerInstance; import fr.ill.ics.cameo.Server;
import fr.ill.ics.nscclient.sessionmanagement.SessionManager;
import fr.ill.ics.util.ConfigManager;
import fr.ill.ics.util.exception.ExitException;
public class MailSender { public class MailSender {
private String mailContentFile; private String nomadServerEndpoint;
private static final String VIRTUAL_HOME = "virtualHome"; private Server server;
private String mailContent;
public static String FILE_SEPARATOR = System.getProperty("file.separator");
public static String NOMADENDPOINT_DIRECTORY =System.getProperty("java.io.tmpdir") + FILE_SEPARATOR + "nomad";
public static String NOMADENDPOINT_FILE = "session.properties";
public static String NOMADMAIL = "nomadmail";
public static void main(String[] args) { public static void main(String[] args) {
int exitCode = 0; MailSender sender = new MailSender();
try { sender.init(args);
MailSender sender = new MailSender(); sender.run();
sender.init(args);
sender.run();
} catch (ExitException e) {
System.out.println("Exit exception");
exitCode = e.getExitCode();
} catch (Throwable e) {
System.out.println("Unexpected exception");
e.printStackTrace();
} finally {
ServerConnection.getInstance(CommandZoneWrapper.SERVER_ID).reset();
System.out.println("Resetting Cameo application");
CommandZoneWrapper.getInstance(CommandZoneWrapper.SERVER_ID).release();
LoginManager.getInstance().logout(CommandZoneWrapper.SERVER_ID);
System.out.println("Terminating Cameo application");
Application.This.terminate();
System.out.println("Terminated Cameo application");
}
System.out.println("Exiting with code " + exitCode);
// Get out with exit code.
System.exit(exitCode);
} }
private String getInstrumentName(String endpoint) { private String getInstrumentName(String endpoint) {
...@@ -73,9 +49,44 @@ public class MailSender { ...@@ -73,9 +49,44 @@ public class MailSender {
return endpoint.substring(pos1 + 1, pos2); return endpoint.substring(pos1 + 1, pos2);
} }
private boolean connectToNomadServer() { // private boolean connectToNomadServer() {
//
// String sessionPropertiesFile = ConfigManager.NOMADENDPOINT_DIRECTORY + ConfigManager.FILE_SEPARATOR + ConfigManager.NOMADENDPOINT_FILE;
//
// Properties sessionProperties = new Properties();
//
// try {
// FileInputStream inputStream = new FileInputStream(sessionPropertiesFile);
// sessionProperties.load(inputStream);
// inputStream.close();
//
// } catch (Exception e) {
// System.err.println("Error while reading session properties.");
// }
//
// String nomadServerEndpoint = sessionProperties.getProperty("nomadServerEndpoint");
// String instrumentName = getInstrumentName(nomadServerEndpoint);
//
// System.out.println("nomadServerEndpoint = " + nomadServerEndpoint);
// System.out.println("instrument = " + instrumentName);
//
// String virtualHomeValue = System.getProperty(VIRTUAL_HOME);
// System.out.println(VIRTUAL_HOME + " = " + virtualHomeValue);
//
// ConfigManager.initInstance(virtualHomeValue);
//
// SessionManager.getInstance(CommandZoneWrapper.SERVER_ID).setSessionInformation("", "", instrumentName);
//
// String[] cameoArgs = {ConfigManager.getInstance().getServerEndpoint() + ":nomadmail"};
// Application.This.init(cameoArgs);
// ServerInstance.getInstance().init();
//
// return true;
// }
String sessionPropertiesFile = ConfigManager.NOMADENDPOINT_DIRECTORY + ConfigManager.FILE_SEPARATOR + ConfigManager.NOMADENDPOINT_FILE; private void connectToServer() {
String sessionPropertiesFile = NOMADENDPOINT_DIRECTORY + FILE_SEPARATOR + NOMADENDPOINT_FILE;
Properties sessionProperties = new Properties(); Properties sessionProperties = new Properties();
...@@ -86,28 +97,24 @@ public class MailSender { ...@@ -86,28 +97,24 @@ public class MailSender {
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error while reading session properties."); System.err.println("Error while reading session properties.");
System.exit(1);
} }
String nomadServerEndpoint = sessionProperties.getProperty("nomadServerEndpoint"); nomadServerEndpoint = sessionProperties.getProperty("nomadServerEndpoint");
String instrumentName = getInstrumentName(nomadServerEndpoint); String instrumentName = getInstrumentName(nomadServerEndpoint);
System.out.println("nomadServerEndpoint = " + nomadServerEndpoint); System.out.println("nomadServerEndpoint = " + nomadServerEndpoint);
System.out.println("instrument = " + instrumentName); System.out.println("instrument = " + instrumentName);
String virtualHomeValue = System.getProperty(VIRTUAL_HOME); // Connect to the remote cameo server.
System.out.println(VIRTUAL_HOME + " = " + virtualHomeValue); try {
server = new Server(nomadServerEndpoint);
ConfigManager.initInstance(virtualHomeValue); }
catch (ConnectionTimeout e) {
SessionManager.getInstance(CommandZoneWrapper.SERVER_ID).setSessionInformation("", "", instrumentName); System.exit(1);
}
String[] cameoArgs = {ConfigManager.getInstance().getServerEndpoint() + ":nomadmail"};
Application.This.init(cameoArgs);
ServerInstance.getInstance().init();
return true;
} }
private void init(String[] args) { private void init(String[] args) {
if (args != null && args.length < 1) { if (args != null && args.length < 1) {
...@@ -115,52 +122,23 @@ public class MailSender { ...@@ -115,52 +122,23 @@ public class MailSender {
System.exit(1); System.exit(1);
} }
boolean serverOk = connectToNomadServer(); connectToServer();
if (!serverOk) {
System.exit(1);
}
try {
LoginManager.getInstance().login("mail", true, CommandZoneWrapper.SERVER_ID);
} catch (Exception e) {
System.err.println("Error in login");
e.printStackTrace();
System.exit(1);
}
mailContentFile = args[0]; mailContent = args[0];
}
private static String readFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
reader.close();
return stringBuilder.toString();
} }
private void run() { private void run() {
String mailContent = ""; String[] args = new String[4];
try { args[0] = "nomad crash";
mailContent = readFile(new File(mailContentFile)); args[1] = mailContent;
args[2] = "sci_l@ill.fr";
args[3] = "nomad_l@ill.fr";
System.out.println("Sending " + mailContent); Application.Instance app = server.start(NOMADMAIL, args);
SessionManager.getInstance(CommandZoneWrapper.SERVER_ID).sendCrashMail(mailContent); if (!app.exists()) {
System.err.println("Cannot send mail, the application " + NOMADMAIL + " does not exist in the server " + nomadServerEndpoint);
} catch (IOException e) {
System.err.println("Cannot send mail");
e.printStackTrace();
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment