*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Timer;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author chm
*/
public class StopServer {
- private static final Logger logger = Logger.getLogger(StopServer.class);
- private int port = 0;
- private ArrayList<MyTimer> timers;
- private ServerSocket ss;
- private String[] allowedStoppers;
- protected boolean listening = true;
-
- /**
- *
- * @param port
- * @param timers
- * @param allowedStoppers
- */
- public StopServer(int port, final ArrayList<MyTimer> timers, String allowedStoppers) {
- super();
- this.port = port;
- this.timers=timers;
- StringTokenizer st = new StringTokenizer(allowedStoppers, ",");
- this.allowedStoppers = new String[st.countTokens()];
- for(int i=0;i<st.countTokens();i++) {
- this.allowedStoppers[i] = st.nextToken();
- }
- }
-
- public void start() throws Exception {
- Thread starter = new Thread() {
- @Override
- public void run() {
- try {
- ss = new ServerSocket(port);
- int count = 0;
- while(listening) {
- if(listening) {
- ++count;
- try {
- new StopServerThread(ss.accept(),StopServer.this,allowedStoppers).start();
- } catch(Exception ex) {
- // throwed when ss has been closed
- }
- }
- }
- if(!ss.isClosed()) ss.close();
- } catch(IOException ioEx) {
- System.err.println("Another instance is already running.");
- System.exit(2);
- }
- }
- };
- starter.start();
- }
- protected void stop() {
- listening=false;
- BatchInformationListener.getInstance().stop();
- for(Timer timer:timers) {
- timer.cancel();
- timer.purge();
- }
- PoolManager.getInstance().shutdown();
- System.exit(1);
- }
-
- private class StopServerThread extends Thread {
- private Socket so;
- private StopServer instance;
- private String[] allowedStoppers;
- public StopServerThread(Socket so, StopServer instance, String[] allowedStoppers) {
- super();
- this.so=so;
- this.instance=instance;
- this.allowedStoppers=allowedStoppers;
- }
- @Override
- public void run() {
- try {
- PrintWriter out = new PrintWriter(so.getOutputStream(),true);
-
- BufferedReader in = new BufferedReader(new InputStreamReader(so.getInputStream()));
-
- String inputLine, outputLine;
- if(isAcceptedStopper(so.getInetAddress().getHostAddress())) {
- logger.info("received stop order from "+so.getInetAddress().getHostAddress());
- Protocol kkp = new Protocol();
- outputLine = kkp.processInput(null);
- out.println(outputLine);
-
- while ((inputLine = in.readLine()) != null) {
- outputLine = kkp.processInput(inputLine);
- out.println(outputLine);
- if (outputLine.startsWith("+OK Bye.")) {
- instance.stop();
- break;
- }
- }
- } else {
- out.println("-ERR Unauthorized stopper, allowed are: "+allowedStoppers+", found "+so.getInetAddress().getHostAddress());
- }
- out.close();
- in.close();
- so.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
-
- protected boolean isAcceptedStopper(String address) {
- boolean ret = false;
- for(String s:allowedStoppers) {
- if("*".equals(s)) {
- ret = true;
- break;
- } else if(address.equals(s)) {
- ret = true;
- break;
- }
- }
- return ret;
- }
- }
-
- private class Protocol {
- private static final String WELCOME_STRING = "+OK Welcome on XeMeLios batch stopper";
- public String processInput(String input) {
- if(input==null) {
- return WELCOME_STRING;
- } else {
- if("stop".equals(input)) {
- return "+OK Bye.";
- } else {
- return "-ERR Unknown command.";
- }
- }
- }
- }
+ private static final Logger logger = Logger.getLogger(StopServer.class);
+ private int port = 0;
+ private final ArrayList<MyTimer> timers;
+ private ServerSocket ss;
+ private final String[] allowedStoppers;
+ protected boolean listening = true;
+
+ /**
+ *
+ * @param port
+ * @param timers
+ * @param allowedStoppers
+ */
+ public StopServer(final int port, final ArrayList<MyTimer> timers, final String allowedStoppers) {
+ super();
+ this.port = port;
+ this.timers = timers;
+ final StringTokenizer st = new StringTokenizer(allowedStoppers, ",");
+ this.allowedStoppers = new String[st.countTokens()];
+ for (int i = 0; i < st.countTokens(); i++) {
+ this.allowedStoppers[i] = st.nextToken();
+ }
+ }
+
+ public void start() throws Exception {
+ final Thread starter = new Thread() {
+ @Override
+ public void run() {
+ try {
+ StopServer.this.ss = new ServerSocket(StopServer.this.port);
+ int count = 0;
+ while (StopServer.this.listening) {
+ if (StopServer.this.listening) {
+ ++count;
+ try {
+ new StopServerThread(StopServer.this.ss.accept(), StopServer.this, StopServer.this.allowedStoppers).start();
+ } catch (final Exception ex) {
+ // throwed when ss has been closed
+ }
+ }
+ }
+ if (!StopServer.this.ss.isClosed()) {
+ StopServer.this.ss.close();
+ }
+ } catch (final IOException ioEx) {
+ System.err.println("Another instance is already running.");
+ System.exit(2);
+ }
+ }
+ };
+ starter.start();
+ }
+
+ protected void stop() {
+ this.listening = false;
+ BatchInformationListener.getInstance().stop();
+ for (final Timer timer : this.timers) {
+ timer.cancel();
+ timer.purge();
+ }
+ PoolManager.getInstance().shutdown();
+ System.exit(1);
+ }
+
+ private class StopServerThread extends Thread {
+ private final Socket so;
+ private final StopServer instance;
+ private final String[] allowedStoppers;
+
+ public StopServerThread(final Socket so, final StopServer instance, final String[] allowedStoppers) {
+ super();
+ this.so = so;
+ this.instance = instance;
+ this.allowedStoppers = allowedStoppers;
+ }
+
+ @Override
+ public void run() {
+ try {
+ final PrintWriter out = new PrintWriter(this.so.getOutputStream(), true);
+
+ final BufferedReader in = new BufferedReader(new InputStreamReader(this.so.getInputStream()));
+
+ String inputLine, outputLine;
+ if (this.isAcceptedStopper(this.so.getInetAddress().getHostAddress())) {
+ logger.info("received stop order from " + this.so.getInetAddress().getHostAddress());
+ final Protocol kkp = new Protocol();
+ outputLine = kkp.processInput(null);
+ out.println(outputLine);
+
+ while ((inputLine = in.readLine()) != null) {
+ outputLine = kkp.processInput(inputLine);
+ out.println(outputLine);
+ if (outputLine.startsWith("+OK Bye.")) {
+ this.instance.stop();
+ break;
+ }
+ }
+ } else {
+ out.println("-ERR Unauthorized stopper, allowed are: " + this.allowedStoppers + ", found " + this.so.getInetAddress().getHostAddress());
+ }
+ out.close();
+ in.close();
+ this.so.close();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ protected boolean isAcceptedStopper(final String address) {
+ boolean ret = false;
+ for (final String s : this.allowedStoppers) {
+ if ("*".equals(s)) {
+ ret = true;
+ break;
+ } else if (address.equals(s)) {
+ ret = true;
+ break;
+ }
+ }
+ return ret;
+ }
+ }
+
+ private class Protocol {
+ private static final String WELCOME_STRING = "+OK Welcome on XeMeLios batch stopper";
+
+ public String processInput(final String input) {
+ if (input == null) {
+ return WELCOME_STRING;
+ } else {
+ if ("stop".equals(input)) {
+ return "+OK Bye.";
+ } else {
+ return "-ERR Unknown command.";
+ }
+ }
+ }
+ }
}