1 29 30 package com.caucho.vfs; 31 32 import com.caucho.util.L10N; 33 import com.caucho.util.Log; 34 35 import java.io.IOException ; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.reflect.Method ; 38 import java.net.BindException ; 39 import java.net.InetAddress ; 40 import java.net.ServerSocket ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class QJniServerSocket { 48 private static final L10N L = new L10N(QJniServerSocket.class); 49 private static final Logger log = Log.open(QJniServerSocket.class); 50 51 private QJniServerSocket() 52 { 53 } 54 55 58 public static QServerSocket create(int port, int listenBacklog) 59 throws IOException 60 { 61 return create(null, port, listenBacklog); 62 } 63 64 67 public static QServerSocket create(InetAddress host, int port, 68 int listenBacklog) 69 throws IOException 70 { 71 try { 72 QServerSocket ss = createJNI(host, port, listenBacklog); 73 74 if (ss != null) 75 return ss; 76 } catch (IOException e) { 77 log.log(Level.FINE, e.toString(), e); 78 } catch (Throwable e) { 79 log.log(Level.FINE, e.toString(), e); 80 } 81 82 for (int i = 0; i < 10; i++) { 83 try { 84 ServerSocket ss = new ServerSocket (port, listenBacklog, host); 85 86 return new QServerSocketWrapper(ss); 87 } catch (BindException e) { 88 } 89 90 try { 91 Thread.currentThread().sleep(1); 92 } catch (Throwable e) { 93 } 94 } 95 96 try { 97 ServerSocket ss = new ServerSocket (port, listenBacklog, host); 98 99 return new QServerSocketWrapper(ss); 100 } catch (BindException e) { 101 if (host != null) 102 throw new BindException (L.l("{0}\nCan't bind to {1}:{2}.\nCheck for another server listening to that port.", e.getMessage(), host, String.valueOf(port))); 103 else 104 throw new BindException (L.l("{0}\nCan't bind to *:{1}.\nCheck for another server listening to that port.", e.getMessage(), String.valueOf(port))); 105 } 106 107 } 108 109 112 public static QServerSocket createJNI(InetAddress host, 113 int port, 114 int listenBacklog) 115 throws IOException 116 { 117 try { 118 Class cl = Class.forName("com.caucho.vfs.JniServerSocketImpl"); 119 120 Method method = cl.getMethod("create", 121 new Class [] { String .class, 122 int.class, 123 int.class }); 124 125 String hostAddress; 126 127 if (host != null) 128 hostAddress = host.getHostAddress(); 129 else 130 hostAddress = "0.0.0.0"; 131 132 try { 133 return (QServerSocket) method.invoke(null, 134 hostAddress, port, listenBacklog); 135 } catch (InvocationTargetException e) { 136 throw e.getTargetException(); 137 } 138 } catch (IOException e) { 139 throw e; 140 } catch (ClassNotFoundException e) { 141 log.fine(e.toString()); 142 143 throw new IOException (L.l("JNI Socket support requires Resin Professional.")); 144 } catch (Throwable e) { 145 log.log(Level.FINE, e.toString(), e); 146 147 throw new IOException (L.l("JNI Socket support requires Resin Professional.")); 148 } 149 } 150 151 154 public static QServerSocket openJNI(int fd, int port) 155 throws IOException 156 { 157 try { 158 Class cl = Class.forName("com.caucho.vfs.JniServerSocketImpl"); 159 160 Method method = cl.getMethod("open", new Class [] { int.class, 161 int.class}); 162 163 try { 164 return (QServerSocket) method.invoke(null, fd, port); 165 } catch (InvocationTargetException e) { 166 throw e.getTargetException(); 167 } 168 } catch (IOException e) { 169 throw e; 170 } catch (ClassNotFoundException e) { 171 log.fine(e.toString()); 172 173 throw new IOException (L.l("JNI Socket support requires Resin Professional.")); 174 } catch (Throwable e) { 175 log.log(Level.FINE, e.toString(), e); 176 177 throw new IOException (L.l("JNI Socket support requires Resin Professional.")); 178 } 179 } 180 } 181 182 | Popular Tags |