1 19 20 package com.sslexplorer.server; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.io.PrintWriter ; 30 import java.net.InetSocketAddress ; 31 import java.net.Socket ; 32 import java.net.SocketAddress ; 33 import java.util.StringTokenizer ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import com.sslexplorer.boot.ContextHolder; 39 import com.sslexplorer.boot.Util; 40 41 52 public class ServerLock { 53 54 final static Log log = LogFactory.getLog(ServerLock.class); 55 56 private File lockFile; 57 private boolean locked; 58 private boolean setup; 59 private int port; 60 private String bindAddress; 61 private boolean started; 62 private long lastLockChange; 63 64 72 public ServerLock(String bindAddress) throws IOException { 73 74 this.bindAddress = bindAddress; 75 lockFile = new File (ContextHolder.getContext().getTempDirectory(), "sslexplorer.run"); 76 77 82 if (lockFile.exists()) { 83 FileInputStream lockIn = null; 84 try { 85 lockIn = new FileInputStream (lockFile); 86 BufferedReader br = new BufferedReader (new InputStreamReader (lockIn)); 87 String r = br.readLine(); 88 if (r != null && !r.equals("")) { 89 try { 90 if(!r.equals("shutdown") || r.equals("restart")) { 91 StringTokenizer t = new StringTokenizer (r, ":"); 92 setup = "true".equals(t.nextToken()); 93 port = Integer.parseInt(t.nextToken()); 94 checkStatus(); 95 } 96 } catch (Exception e) { 97 System.err.println("Could not parse lock file."); 98 e.printStackTrace(); 99 } 100 } 101 } finally { 102 Util.closeStream(lockIn); 103 } 104 } 105 } 106 107 112 public boolean isLocked() { 113 return locked; 114 } 115 116 123 public void start(int port) throws IOException { 124 this.port = port; 125 this.setup = ContextHolder.getContext().isSetupMode(); 126 127 132 checkStatus(); 133 if (locked) { 134 if (port == 443 || port == 8443) { 135 throw new IOException ("Some server other than SSL-Explorer is already running on port " + port + "." 136 + "Most web servers will run on this port by default, so check if you have such " 137 + "a service is installed (IIS, Apache or Tomcat for example). Either shutdown " 138 + "and disable the conflicting server, or if you wish to run both services " 139 + "concurrently, change the port number on which one listens."); 140 } else { 141 throw new IOException ("Some server other than SSL-Explorer is already running on port " + port + "." 142 + "Check which other services you have enabled that may be causing " 143 + "this conflict. Then, either disable the service, change the port on " 144 + "which it is listening or change the port on which SSL-Explorer listens."); 145 146 } 147 } 148 149 PrintWriter pw = new PrintWriter (new FileOutputStream (lockFile)); 151 pw.println(ContextHolder.getContext().isSetupMode() + ":" + port); 152 pw.flush(); 153 pw.close(); 154 started = true; 155 156 lastLockChange = lockFile.lastModified(); 157 158 161 Thread t = new Thread ("ServerLockMonitor") { 162 public void run() { 163 while(true) { 164 try { 165 Thread.sleep(5000); 166 if(lastLockChange != lockFile.lastModified()) { 167 lastLockChange = lockFile.lastModified(); 168 if (log.isDebugEnabled()) 169 log.debug("Lock file changed, examining"); 170 InputStream in = null; 171 try { 172 in = new FileInputStream (lockFile);; 173 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 174 String s = br.readLine(); 175 Util.closeStream(in); if("shutdown".equals(s)) { 177 ContextHolder.getContext().shutdown(false); 178 break; 179 } 180 else if("restart".equals(s)) { 181 ContextHolder.getContext().shutdown(true); 182 break; 183 } 184 } 185 catch(IOException ioe) { 186 Util.closeStream(in); 187 throw ioe; 188 } 189 } 190 } 191 catch(Exception e) { 192 } 193 } 194 } 195 }; 196 t.setDaemon(true); 197 t.setPriority(Thread.MIN_PRIORITY); 198 t.start(); 199 200 201 } 202 203 209 public boolean isStarted() { 210 return started; 211 } 212 213 217 public void stop() { 218 if (log.isInfoEnabled()) 219 log.info("Removing lock"); 220 lockFile.delete(); 221 started = false; 222 } 223 224 229 public boolean isSetup() { 230 return setup; 231 } 232 233 238 public int getPort() { 239 return port; 240 } 241 242 private void checkStatus() { 243 Socket socket = null; 244 try { 245 int timeout = 5000; if (log.isInfoEnabled()) 247 log.info("Connecting to " + bindAddress + ":" + port + " to see if a server is already running."); 248 SocketAddress socketAddress = new InetSocketAddress (bindAddress, port); 249 socket = new Socket (); 250 socket.connect(socketAddress, timeout); 251 locked = true; 252 } catch (Exception e) { 253 locked = false; 254 } finally { 255 if (socket != null) { 256 try { 257 socket.close(); 258 } catch (Exception e) { 259 260 } 261 } 262 } 263 } 264 265 } 266 | Popular Tags |