Commit 4fec65e8 authored by legoc's avatar legoc

Renamed VerifyApplicationThread into LifecycleApplicationThread

parent 2ed25b95
......@@ -38,7 +38,7 @@ import fr.ill.ics.cameo.exception.UnknownPublisherException;
import fr.ill.ics.cameo.exception.UnmanagedApplicationException;
import fr.ill.ics.cameo.messages.Message;
import fr.ill.ics.cameo.threads.StreamApplicationThread;
import fr.ill.ics.cameo.threads.VerifyApplicationThread;
import fr.ill.ics.cameo.threads.LifecycleApplicationThread;
public class Manager extends ConfigLoader {
......@@ -240,7 +240,7 @@ public class Manager extends ConfigLoader {
// Threads
// Verifiy application thread
VerifyApplicationThread verifyThread = new VerifyApplicationThread(application, this, LogInfo.getInstance().getLogger());
LifecycleApplicationThread verifyThread = new LifecycleApplicationThread(application, this, LogInfo.getInstance().getLogger());
verifyThread.start();
LogInfo.getInstance().getLogger().fine("Application " + application.getNameId() + " write stream = " + application.isWriteStream() + ", show stream = " + application.hasStream());
......@@ -824,7 +824,7 @@ public class Manager extends ConfigLoader {
// Threads
// Verifiy application thread
VerifyApplicationThread verifyThread = new VerifyApplicationThread(application, this, LogInfo.getInstance().getLogger());
LifecycleApplicationThread verifyThread = new LifecycleApplicationThread(application, this, LogInfo.getInstance().getLogger());
verifyThread.start();
LogInfo.getInstance().getLogger().fine("Unmanaged application " + application.getNameId() + " is started");
......
......@@ -3,6 +3,10 @@ package fr.ill.ics.cameo.threads;
import fr.ill.ics.cameo.manager.Application;
import fr.ill.ics.cameo.manager.ConfigManager;
/**
* Base class for the application thread: LifecycleApplicationThread and StreamApplicationThread.
*
*/
public class ApplicationThread extends Thread {
protected Application application;
......
......@@ -29,7 +29,7 @@ import fr.ill.ics.cameo.manager.ProcessState;
* It is implemented as a thread that is starting before the execution of the application and stopping after it.
*
*/
public class VerifyApplicationThread extends ApplicationThread {
public class LifecycleApplicationThread extends ApplicationThread {
private Manager manager;
private Logger logger;
......@@ -40,7 +40,7 @@ public class VerifyApplicationThread extends ApplicationThread {
* @param manager
* @param logger
*/
public VerifyApplicationThread(Application application, Manager manager, Logger logger) {
public LifecycleApplicationThread(Application application, Manager manager, Logger logger) {
super(application);
this.manager = manager;
this.logger = logger;
......@@ -225,7 +225,7 @@ public class VerifyApplicationThread extends ApplicationThread {
terminateStreamThread();
// Launch a new verification thread here.
VerifyApplicationThread applicationThread = new VerifyApplicationThread(application, manager, logger);
LifecycleApplicationThread applicationThread = new LifecycleApplicationThread(application, manager, logger);
applicationThread.start();
}
else {
......
......@@ -30,6 +30,11 @@ import fr.ill.ics.cameo.manager.LogInfo;
import fr.ill.ics.cameo.manager.Manager;
import fr.ill.ics.cameo.messages.Message;
/**
* Class getting the stream from the process input stream.
* It is implemented as a thread.
*
*/
public class StreamApplicationThread extends ApplicationThread {
private BufferedReader reader;
......@@ -41,8 +46,7 @@ public class StreamApplicationThread extends ApplicationThread {
private FileOutputStream fileOutputStream;
/**
* used to listen stream
*
* Constructor.
* @param application
* @param logger
* @param streamName
......@@ -56,19 +60,24 @@ public class StreamApplicationThread extends ApplicationThread {
private void sendMessage(String line, boolean endOfLine) {
// prepare our context and publisher
// Prepare the file if the log is written.
if (application.isWriteStream()) {
if (fileOutputStream == null) { // if we have to write log but file is not yet created
if (fileOutputStream == null) {
// Create the log file if it has not been created.
createFile(application.getLogPath());
}
try {
if (fileOutputStream != null) {
fileOutputStream.write(line.getBytes());
fileOutputStream.write("\n".getBytes());
// Finish the line.
if (endOfLine) {
fileOutputStream.write("\n".getBytes());
}
}
} catch (IOException e) {
System.err.println("error writing stream");
}
catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Error while writing stream to file for " + application.getNameId() + " : " + e.getMessage());
}
}
......@@ -130,7 +139,9 @@ public class StreamApplicationThread extends ApplicationThread {
send = true;
}
}
} catch (IOException e) {
}
catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Error while reading stream to file for " + application.getNameId() + " : " + e.getMessage());
}
}
......@@ -172,20 +183,16 @@ public class StreamApplicationThread extends ApplicationThread {
sendMessage(characters.toString(), eol);
}
}
} catch (IOException e) { // if a file is not created
}
catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Reader error for application " + application.getNameId());
}
} catch (NullPointerException e) {
e.printStackTrace();
LogInfo.getInstance().getLogger().severe("Problem while streaming " + application.getNameId());
} finally {
}
finally {
sendEndOfStream();
// close the file properly
// Close the file properly.
try {
if (fileOutputStream != null) {
fileOutputStream.flush();
......@@ -194,19 +201,17 @@ public class StreamApplicationThread extends ApplicationThread {
}
} catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Problem while closing log file of " + application.getNameId());
LogInfo.getInstance().getLogger().severe("Problem while closing log file of " + application.getNameId() + " : " + e.getMessage());
}
}
LogInfo.getInstance().getLogger().info("Finished listening stream for application " + application.getNameId());
}
public void sendEndOfStream() {
// send the end of stream
// the message was originally done in manager when the application was terminated
// but not the stream thread because they are not synchronized
// Send the end of stream.
// The message was originally done in manager when the application was terminated but not the stream thread because they are not synchronized.
if (application.hasStream()) {
// Send the stream.
JSONObject event = new JSONObject();
......@@ -220,26 +225,26 @@ public class StreamApplicationThread extends ApplicationThread {
}
/**
* create file to log
*
* Create the log file.
* @param path
* @return
*/
private void createFile(String path) {
// no creation if path is empty
// Do not create if the path is empty.
if (path.isEmpty()) {
return;
}
File file = null;
// create file
// Create the file.
try {
file = new java.io.File(path + "/" + application.getNameId() + ".log");
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
} catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Unable to create file " + file.getAbsolutePath() + " for application " + application.getNameId());
}
catch (IOException e) {
LogInfo.getInstance().getLogger().severe("Unable to create file " + file.getAbsolutePath() + " for application " + application.getNameId() + " : " + e.getMessage());
}
}
}
\ No newline at end of file
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