Coverage Report - org.jtheque.core.managers.lifecycle.Instances
 
Classes in this File Line Coverage Branch Coverage Complexity
Instances
0 %
0/42
0 %
0/6
2.222
Instances$1
N/A
N/A
2.222
Instances$ServerThread
0 %
0/17
0 %
0/6
2.222
 
 1  
 package org.jtheque.core.managers.lifecycle;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.log.ILoggingManager;
 21  
 import org.jtheque.utils.io.FileUtils;
 22  
 import org.jtheque.utils.io.SocketUtils;
 23  
 import org.slf4j.LoggerFactory;
 24  
 
 25  
 import java.io.IOException;
 26  
 import java.io.PrintStream;
 27  
 import java.net.ServerSocket;
 28  
 import java.net.Socket;
 29  
 import java.net.SocketException;
 30  
 import java.net.UnknownHostException;
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 
 34  
 /**
 35  
  * This class permit to have only one launched instance of the application.
 36  
  *
 37  
  * @author Baptiste Wicht
 38  
  */
 39  0
 final class Instances {
 40  
     private ServerSocket socket;
 41  0
     private final Collection<Client> clients = new ArrayList<Client>(5);
 42  
     private Thread thread;
 43  
 
 44  
     private static final String LOCALHOST = "127.0.0.1";
 45  
     private static final int PORT = 12345;
 46  
 
 47  
     /**
 48  
      * Launch the application. If an other instance of the application is soon launched,
 49  
      * call it and exit the current instance.
 50  
      */
 51  
     @SuppressWarnings({"SocketOpenedButNotSafelyClosed"})
 52  
     public void launchApplication() {
 53  0
         boolean canLaunch = true;
 54  
 
 55  
         try {
 56  0
             socket = new ServerSocket(PORT);
 57  
 
 58  0
             registerNewClient();
 59  0
         } catch (IOException e) {
 60  0
             LoggerFactory.getLogger(getClass()).error("Unable to register socket", e);
 61  
 
 62  0
             canLaunch = false;
 63  0
         }
 64  
 
 65  0
         if (!canLaunch) {
 66  0
             exit();
 67  
         }
 68  0
     }
 69  
 
 70  
     /**
 71  
      * Register a new client.
 72  
      */
 73  
     private void registerNewClient() {
 74  0
         thread = new ServerThread();
 75  
 
 76  0
         thread.start();
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Exit.
 81  
      */
 82  
     private void exit() {
 83  0
         callInstance();
 84  
 
 85  0
         System.exit(1); //At this moment, nothing need to be released
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Call the another instance of JTheque.
 90  
      */
 91  
     @SuppressWarnings({"SocketOpenedButNotSafelyClosed", "IOResourceOpenedButNotSafelyClosed"})
 92  
     private void callInstance() {
 93  0
         Socket clientSocket = null;
 94  0
         PrintStream stream = null;
 95  
 
 96  
         try {
 97  0
             clientSocket = new Socket(LOCALHOST, PORT);
 98  0
             stream = new PrintStream(clientSocket.getOutputStream());
 99  
 
 100  0
             stream.println("open");
 101  0
         } catch (UnknownHostException e) {
 102  0
             LoggerFactory.getLogger(getClass()).error("The host is unknown", e);
 103  0
         } catch (IOException e) {
 104  0
             LoggerFactory.getLogger(getClass()).error("I/O error", e);
 105  
         } finally {
 106  0
             FileUtils.close(stream);
 107  0
             SocketUtils.close(clientSocket);
 108  0
         }
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Close definitively all the open flows.
 113  
      */
 114  
     public void closeInstance() {
 115  0
         stopClients();
 116  0
         stopServer();
 117  0
         SocketUtils.close(socket);
 118  0
     }
 119  
 
 120  
     /**
 121  
      * Stop all the clients.
 122  
      */
 123  
     private void stopClients() {
 124  0
         for (Client client : clients) {
 125  0
             client.interrupt();
 126  
         }
 127  0
     }
 128  
 
 129  
     /**
 130  
      * Stop the server.
 131  
      */
 132  
     private void stopServer() {
 133  0
         if (thread != null) {
 134  0
             thread.interrupt();
 135  
         }
 136  0
     }
 137  
 
 138  
     /**
 139  
      * A thread for a client.
 140  
      *
 141  
      * @author Baptiste Wicht
 142  
      */
 143  0
     @SuppressWarnings({"SocketOpenedButNotSafelyClosed"})
 144  0
     private final class ServerThread extends Thread {
 145  
         private boolean closing;
 146  
 
 147  
         @Override
 148  
         public void run() {
 149  0
             while (!socket.isClosed() && socket != null) {
 150  
                 try {
 151  0
                     Client client = new Client(socket.accept());
 152  0
                     clients.add(client);
 153  
 
 154  0
                     client.start();
 155  0
                 } catch (SocketException e1) {
 156  0
                     if (!closing) {
 157  0
                         Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e1, "Socket closed");
 158  
                     }
 159  0
                 } catch (IOException e2) {
 160  0
                     Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e2);
 161  0
                     interrupt();
 162  0
                 }
 163  
             }
 164  0
         }
 165  
 
 166  
         @Override
 167  
         public void interrupt() {
 168  0
             closing = true;
 169  
 
 170  0
             SocketUtils.close(socket);
 171  
 
 172  0
             super.interrupt();
 173  0
         }
 174  
     }
 175  
 }