1 17 package org.columba.core.main; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.PrintWriter ; 23 import java.net.ServerSocket ; 24 import java.net.Socket ; 25 import java.net.SocketException ; 26 import java.net.SocketTimeoutException ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.Random ; 30 import java.util.StringTokenizer ; 31 32 import javax.swing.JOptionPane ; 33 34 import org.apache.commons.cli.CommandLine; 35 import org.apache.commons.cli.ParseException; 36 import org.columba.core.component.ComponentManager; 37 import org.columba.core.resourceloader.GlobalResourceLoader; 38 import org.columba.core.shutdown.ShutdownManager; 39 40 51 public class ColumbaServer { 52 static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 53 54 57 protected static final String ANONYMOUS_USER = "anonymous"; 58 59 62 private static ColumbaServer instance; 63 64 67 private static Random random = new Random (); 68 69 72 private static final int LOWEST_PORT = 30000; 73 74 77 protected Thread thread; 78 79 82 protected ServerSocket serverSocket; 83 84 87 protected ColumbaServer() { 88 thread = new Thread (new Runnable () { 89 public void run() { 90 while (!Thread.currentThread().isInterrupted()) { 91 try { 92 handleClient(serverSocket.accept()); 93 } catch (SocketTimeoutException ste) { 94 } catch (IOException ioe) { 96 ioe.printStackTrace(); 97 98 } 100 } 101 102 try { 103 serverSocket.close(); 104 105 SessionController.serializePortNumber(-1); 107 } catch (IOException ioe) { 108 } 109 110 serverSocket = null; 111 } 112 }, "ColumbaServer"); 113 thread.setDaemon(true); 114 115 ShutdownManager.getInstance().register(new Runnable () { 117 public void run() { 118 stop(); 119 } 120 }); 121 } 122 123 128 public synchronized void start() throws IOException { 129 if (!isRunning()) { 130 int port; 131 int count = 0; 132 133 while (serverSocket == null) { 134 port = random.nextInt(65536 - LOWEST_PORT) + LOWEST_PORT; 136 137 try { 138 serverSocket = new ServerSocket (port); 139 140 SessionController.serializePortNumber(port); 142 } catch (SocketException se) { count++; 144 145 if (count == 10) { JOptionPane.showMessageDialog(null, 147 GlobalResourceLoader.getString(RESOURCE_PATH, 148 "session", "err_10se_msg"), 149 GlobalResourceLoader.getString(RESOURCE_PATH, 150 "session", "err_10se_title"), 151 JOptionPane.ERROR_MESSAGE); 152 153 System.exit(1); 156 } 157 } 158 } 159 160 serverSocket.setSoTimeout(2000); 161 thread.start(); 162 } 163 } 164 165 168 public synchronized void stop() { 169 thread.interrupt(); 170 } 171 172 177 public synchronized boolean isRunning() { 178 return thread.isAlive(); 179 } 180 181 184 protected void handleClient(Socket client) { 185 try { 186 String host = client.getLocalAddress().getHostAddress(); 188 if (!(host.equals("127.0.0.1"))) { 189 return; 191 } 192 193 BufferedReader reader = new BufferedReader (new InputStreamReader ( 194 client.getInputStream())); 195 String line = reader.readLine(); 196 if (!line.startsWith("Columba ")) { 197 return; 198 } 199 200 line = reader.readLine(); 201 if (!line.startsWith("User ")) { 202 return; 203 } 204 205 PrintWriter writer = new PrintWriter (client.getOutputStream()); 206 if (!line.substring(5).equals( 207 System.getProperty("user.name", ANONYMOUS_USER))) { 208 writer.write("WRONG USER\r\n"); 209 writer.close(); 210 return; 211 } 212 writer.write("\r\n"); 213 writer.flush(); 214 215 line = reader.readLine(); 216 List <String > list = new LinkedList <String >(); 218 StringTokenizer st = new StringTokenizer (line, "%"); 219 while (st.hasMoreTokens()) { 220 String tok = (String ) st.nextToken(); 221 list.add(tok); 222 } 223 224 try { 225 CommandLine commandLine = ColumbaCmdLineParser.getInstance() 226 .parse((String []) list.toArray(new String [0])); 227 228 ComponentManager.getInstance().handleCommandLineParameters( 229 commandLine); 230 231 } catch (ParseException e) { 232 e.printStackTrace(); 233 } 234 } catch (IOException ioe) { 235 ioe.printStackTrace(); 236 } finally { 237 try { 238 client.close(); 239 } catch (IOException ioe) { 240 } 241 } 242 } 243 244 249 public static synchronized ColumbaServer getColumbaServer() { 250 if (instance == null) { 251 instance = new ColumbaServer(); 252 } 253 return instance; 254 } 255 } 256 | Popular Tags |