1 5 package org.h2.server; 6 7 import java.io.IOException ; 8 import java.net.InetAddress ; 9 import java.net.ServerSocket ; 10 import java.net.Socket ; 11 import java.sql.SQLException ; 12 import java.util.ArrayList ; 13 import java.util.HashSet ; 14 15 import org.h2.util.MathUtils; 16 import org.h2.util.NetUtils; 17 18 21 public class OdbcServer implements Service { 22 23 public static final int DEFAULT_PORT = 9083; 25 private int port = OdbcServer.DEFAULT_PORT; 26 private boolean stop; 27 private boolean log; 28 private ServerSocket serverSocket; 29 private HashSet running = new HashSet (); 30 private String baseDir; 31 private String url; 32 private boolean allowOthers; 33 private boolean ifExists; 34 35 boolean getLog() { 36 return log; 37 } 38 39 void log(String s) { 40 if (log) { 41 System.out.println(s); 42 } 43 } 44 45 synchronized void remove(OdbcServerThread t) { 46 running.remove(t); 47 } 48 49 void logError(Exception e) { 50 if (log) { 51 e.printStackTrace(); 52 } 53 } 54 55 public void init(String [] args) throws Exception { 56 port = DEFAULT_PORT; 57 for (int i = 0; i < args.length; i++) { 58 String a = args[i]; 59 if (a.equals("-log")) { 60 log = Boolean.valueOf(args[++i]).booleanValue(); 61 } else if (a.equals("-odbcPort")) { 62 port = MathUtils.decodeInt(args[++i]); 63 } else if (a.equals("-baseDir")) { 64 baseDir = args[++i]; 65 } else if (a.equals("-odbcAllowOthers")) { 66 allowOthers = Boolean.valueOf(args[++i]).booleanValue(); 67 } else if (a.equals("-ifExists")) { 68 ifExists = Boolean.valueOf(args[++i]).booleanValue(); 69 } 70 } 71 org.h2.Driver.load(); 72 url = "tcp://localhost:" + port; 73 } 74 75 public String getURL() { 76 return url; 77 } 78 79 boolean allow(Socket socket) { 80 if(allowOthers) { 81 return true; 82 } 83 return NetUtils.isLoopbackAddress(socket); 84 } 85 86 public void start() throws SQLException { 87 serverSocket = NetUtils.createServerSocket(port, false); 88 } 89 90 public void listen() { 91 String threadName = Thread.currentThread().getName(); 92 try { 93 while (!stop) { 94 Socket s = serverSocket.accept(); 95 if(!allow(s)) { 96 log("Connection not allowed"); 97 s.close(); 98 } else { 99 OdbcServerThread c = new OdbcServerThread(s, this); 100 running.add(c); 101 Thread thread = new Thread (c); 102 thread.setName(threadName+" thread"); 103 c.setThread(thread); 104 thread.start(); 105 } 106 } 107 } catch (Exception e) { 108 if(!stop) { 109 e.printStackTrace(); 110 } 111 } 112 } 113 114 public void stop() { 115 if(!stop) { 117 stop = true; 118 if(serverSocket != null) { 119 try { 120 serverSocket.close(); 121 } catch (IOException e) { 122 e.printStackTrace(); 124 } 125 serverSocket = null; 126 } 127 } 128 ArrayList list = new ArrayList (running); 130 for(int i=0; i<list.size(); i++) { 131 OdbcServerThread c = (OdbcServerThread) list.get(i); 132 c.close(); 133 try { 134 c.getThread().join(100); 135 } catch(Exception e) { 136 e.printStackTrace(); 138 } 139 } 140 } 141 142 public boolean isRunning() { 143 if(serverSocket == null) { 144 return false; 145 } 146 try { 147 Socket s = NetUtils.createSocket(InetAddress.getLocalHost(), serverSocket.getLocalPort(), false); 148 s.close(); 149 return true; 150 } catch(Exception e) { 151 return false; 152 } 153 } 154 155 public String getBaseDir() { 156 return baseDir; 157 } 158 159 public boolean getAllowOthers() { 160 return allowOthers; 161 } 162 163 public String getType() { 164 return "ODBC"; 165 } 166 167 public boolean getIfExists() { 168 return ifExists; 169 } 170 171 } 172 | Popular Tags |