public class SMTPclient implements Runnable {
private boolean isConfigChange = false;
private boolean fatalError = false;
+ private Thread serviceThread = null;
public SMTPclient() {
- Thread thread = new Thread(this);
- thread.start();
+ startServiceThread();
}
+ private void startServiceThread() {
+ if ( serviceThread !=null && serviceThread.isAlive() ) {
+ //Still service thread was working.
+ return;
+ }
+ //Reset flags and start service thread.
+ isConfigChange = false;
+ fatalError = false;
+ serviceThread = new Thread(this);
+ serviceThread.start();
+ }
+
public void configChangeNotify() {
isConfigChange = true;
+ if ( serviceThread !=null && !serviceThread.isAlive() ) {
+ startServiceThread();
+ }
}
-
+
public void run() {
while (!fatalError) {
ServerSocket server = null;
System.err.println("Fatal Error Occurred. Stop service.");
//TODO: Display error dialog.
e.printStackTrace(); //Unexpected!!
+ break;
+ } catch (IllegalArgumentException iaEx) {
+ //Port value out of range
+ fatalError = true;
+ System.err.println("Faital Error Occurted(" + iaEx.getMessage() + "). Stop service." );
+ //TODO: Display error dialog.
+ iaEx.printStackTrace();
+ break;
}
-
+
acceptWait:
while (true) {
// Wait connect from mail client
} catch (IOException ioe) {
//IGNORE close Exception
}
+ } catch (RuntimeException rEx) {
+ fatalError = true;
+ System.err.println("Runtime Exception was occurred.");
+ rEx.printStackTrace();
+ //TODO: Display error dialog.
+ break;
}
}
+ if ( !fatalError ) {
+ //Servce stop by non fatal error. Unexpected.
+ //TODO: Display stop service dialog.
+ System.err.println("Stoped service.");
+ }
}
}