KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > QJniServerSocket


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38 import java.net.BindException JavaDoc;
39 import java.net.InetAddress JavaDoc;
40 import java.net.ServerSocket JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Abstract socket to handle both normal sockets and bin/resin sockets.
46  */

47 public class QJniServerSocket {
48   private static final L10N L = new L10N(QJniServerSocket.class);
49   private static final Logger JavaDoc log = Log.open(QJniServerSocket.class);
50   
51   private QJniServerSocket()
52   {
53   }
54
55   /**
56    * Creates the SSL ServerSocket.
57    */

58   public static QServerSocket create(int port, int listenBacklog)
59     throws IOException JavaDoc
60   {
61     return create(null, port, listenBacklog);
62   }
63
64   /**
65    * Creates the SSL ServerSocket.
66    */

67   public static QServerSocket create(InetAddress JavaDoc host, int port,
68                      int listenBacklog)
69     throws IOException JavaDoc
70   {
71     try {
72       QServerSocket ss = createJNI(host, port, listenBacklog);
73
74       if (ss != null)
75     return ss;
76     } catch (IOException JavaDoc e) {
77       log.log(Level.FINE, e.toString(), e);
78     } catch (Throwable JavaDoc e) {
79       log.log(Level.FINE, e.toString(), e);
80     }
81
82     for (int i = 0; i < 10; i++) {
83       try {
84     ServerSocket JavaDoc ss = new ServerSocket JavaDoc(port, listenBacklog, host);
85       
86     return new QServerSocketWrapper(ss);
87       } catch (BindException JavaDoc e) {
88       }
89
90       try {
91     Thread.currentThread().sleep(1);
92       } catch (Throwable JavaDoc e) {
93       }
94     }
95     
96     try {
97       ServerSocket JavaDoc ss = new ServerSocket JavaDoc(port, listenBacklog, host);
98       
99       return new QServerSocketWrapper(ss);
100     } catch (BindException JavaDoc e) {
101       if (host != null)
102     throw new BindException JavaDoc(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 JavaDoc(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   /**
110    * Creates the SSL ServerSocket.
111    */

112   public static QServerSocket createJNI(InetAddress JavaDoc host,
113                     int port,
114                     int listenBacklog)
115     throws IOException JavaDoc
116   {
117     try {
118       Class JavaDoc cl = Class.forName("com.caucho.vfs.JniServerSocketImpl");
119
120       Method JavaDoc method = cl.getMethod("create",
121                    new Class JavaDoc[] { String JavaDoc.class,
122                          int.class,
123                          int.class });
124
125       String JavaDoc 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 JavaDoc e) {
136     throw e.getTargetException();
137       }
138     } catch (IOException JavaDoc e) {
139       throw e;
140     } catch (ClassNotFoundException JavaDoc e) {
141       log.fine(e.toString());
142       
143       throw new IOException JavaDoc(L.l("JNI Socket support requires Resin Professional."));
144     } catch (Throwable JavaDoc e) {
145       log.log(Level.FINE, e.toString(), e);
146       
147       throw new IOException JavaDoc(L.l("JNI Socket support requires Resin Professional."));
148     }
149   }
150
151   /**
152    * Creates the SSL ServerSocket.
153    */

154   public static QServerSocket openJNI(int fd, int port)
155     throws IOException JavaDoc
156   {
157     try {
158       Class JavaDoc cl = Class.forName("com.caucho.vfs.JniServerSocketImpl");
159
160       Method JavaDoc method = cl.getMethod("open", new Class JavaDoc[] { int.class,
161                              int.class});
162
163       try {
164     return (QServerSocket) method.invoke(null, fd, port);
165       } catch (InvocationTargetException JavaDoc e) {
166     throw e.getTargetException();
167       }
168     } catch (IOException JavaDoc e) {
169       throw e;
170     } catch (ClassNotFoundException JavaDoc e) {
171       log.fine(e.toString());
172       
173       throw new IOException JavaDoc(L.l("JNI Socket support requires Resin Professional."));
174     } catch (Throwable JavaDoc e) {
175       log.log(Level.FINE, e.toString(), e);
176       
177       throw new IOException JavaDoc(L.l("JNI Socket support requires Resin Professional."));
178     }
179   }
180 }
181
182
Popular Tags