Commit c132ddd2 authored by legoc's avatar legoc

Review code

parent 32eb3784
......@@ -17,52 +17,28 @@
*/
package fr.ill.ics.mail;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
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.nscclient.serverconnection.ServerConnection;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
import fr.ill.ics.nscclient.sessionmanagement.SessionManager;
import fr.ill.ics.util.ConfigManager;
import fr.ill.ics.util.exception.ExitException;
import fr.ill.ics.cameo.ConnectionTimeout;
import fr.ill.ics.cameo.Server;
public class MailSender {
private String mailContentFile;
private static final String VIRTUAL_HOME = "virtualHome";
private String nomadServerEndpoint;
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) {
int exitCode = 0;
try {
MailSender sender = new MailSender();
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);
MailSender sender = new MailSender();
sender.init(args);
sender.run();
}
private String getInstrumentName(String endpoint) {
......@@ -73,9 +49,44 @@ public class MailSender {
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();
......@@ -86,28 +97,24 @@ public class MailSender {
} catch (Exception e) {
System.err.println("Error while reading session properties.");
System.exit(1);
}
String nomadServerEndpoint = sessionProperties.getProperty("nomadServerEndpoint");
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;
// Connect to the remote cameo server.
try {
server = new Server(nomadServerEndpoint);
}
catch (ConnectionTimeout e) {
System.exit(1);
}
}
private void init(String[] args) {
if (args != null && args.length < 1) {
......@@ -115,52 +122,23 @@ public class MailSender {
System.exit(1);
}
boolean serverOk = connectToNomadServer();
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);
}
connectToServer();
mailContentFile = 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();
mailContent = args[0];
}
private void run() {
String mailContent = "";
try {
mailContent = readFile(new File(mailContentFile));
String[] args = new String[4];
args[0] = "nomad crash";
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);
} catch (IOException e) {
System.err.println("Cannot send mail");
e.printStackTrace();
if (!app.exists()) {
System.err.println("Cannot send mail, the application " + NOMADMAIL + " does not exist in the server " + nomadServerEndpoint);
}
}
......
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