KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > SessionSocketHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server;
18
19 import java.net.InetAddress JavaDoc;
20 import java.net.ServerSocket JavaDoc;
21 import java.net.SocketException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * Session Socket Handler Abstract Class
28  *
29  * @author GKSpencer
30  */

31 public abstract class SessionSocketHandler implements Runnable JavaDoc
32 {
33     // Debug logging
34

35     protected static Log logger = LogFactory.getLog("org.alfresco.smb.protocol");
36
37     // Define the listen backlog for the server socket
38

39     protected static final int LISTEN_BACKLOG = 10;
40
41     // Server that the socket handler is associated with
42

43     private SMBServer m_server;
44
45     // Address/post to use
46

47     private int m_port;
48     private InetAddress JavaDoc m_bindAddr;
49
50     // Server socket
51

52     private ServerSocket JavaDoc m_srvSock;
53
54     // Debug output enable
55

56     private boolean m_debug;
57
58     // Socket handler thread shutdown flag
59

60     private boolean m_shutdown;
61
62     // Session socket handler name
63

64     private String JavaDoc m_name;
65
66     // Session id
67

68     private static int m_sessId;
69
70     /**
71      * Class constructor
72      *
73      * @param name String
74      * @param srv SMBServer
75      * @param port int
76      * @param bindAddr InetAddress
77      * @param debug boolean
78      */

79     public SessionSocketHandler(String JavaDoc name, SMBServer srv, int port, InetAddress JavaDoc bindAddr, boolean debug)
80     {
81         m_name = name;
82         m_server = srv;
83         m_port = port;
84         m_bindAddr = bindAddr;
85         m_debug = debug;
86     }
87
88     /**
89      * Class constructor
90      *
91      * @param name String
92      * @param srv SMBServer
93      * @param debug boolean
94      */

95     public SessionSocketHandler(String JavaDoc name, SMBServer srv, boolean debug)
96     {
97         m_name = name;
98         m_server = srv;
99         m_debug = debug;
100     }
101
102     /**
103      * Return the handler name
104      *
105      * @return String
106      */

107     public final String JavaDoc getName()
108     {
109         return m_name;
110     }
111
112     /**
113      * Return the server
114      *
115      * @return SMBServer
116      */

117     protected final SMBServer getServer()
118     {
119         return m_server;
120     }
121
122     /**
123      * Return the port
124      *
125      * @return int
126      */

127     protected final int getPort()
128     {
129         return m_port;
130     }
131
132     /**
133      * Determine if the socket handler should bind to a particular address
134      *
135      * @return boolean
136      */

137     protected final boolean hasBindAddress()
138     {
139         return m_bindAddr != null ? true : false;
140     }
141
142     /**
143      * Return the bind address return InetAddress
144      */

145     protected final InetAddress JavaDoc getBindAddress()
146     {
147         return m_bindAddr;
148     }
149
150     /**
151      * Return the next session id
152      *
153      * @return int
154      */

155     protected final synchronized int getNextSessionId()
156     {
157         return m_sessId++;
158     }
159
160     /**
161      * Determine if debug output is enabled
162      *
163      * @return boolean
164      */

165     protected final boolean hasDebug()
166     {
167         return m_debug;
168     }
169
170     /**
171      * Return the server socket
172      *
173      * @return ServerSocket
174      */

175     protected final ServerSocket JavaDoc getSocket()
176     {
177         return m_srvSock;
178     }
179
180     /**
181      * Set the server socket
182      *
183      * @param sock ServerSocket
184      */

185     protected final void setSocket(ServerSocket JavaDoc sock)
186     {
187         m_srvSock = sock;
188     }
189
190     /**
191      * Determine if the shutdown flag is set
192      *
193      * @return boolean
194      */

195     protected final boolean hasShutdown()
196     {
197         return m_shutdown;
198     }
199
200     /**
201      * Clear the shutdown request flag
202      */

203     protected final void clearShutdown()
204     {
205         m_shutdown = false;
206     }
207
208     /**
209      * Request the socket handler to shutdown
210      */

211     public void shutdownRequest()
212     {
213
214         // Indicate that the server is closing
215

216         m_shutdown = true;
217
218         try
219         {
220
221             // Close the server socket so that any pending receive is cancelled
222

223             if (m_srvSock != null)
224                 m_srvSock.close();
225         }
226         catch (SocketException JavaDoc ex)
227         {
228         }
229         catch (Exception JavaDoc ex)
230         {
231         }
232     }
233
234     /**
235      * Initialize the session socket handler
236      *
237      * @exception Exception
238      */

239     public void initialize() throws Exception JavaDoc
240     {
241
242         // Check if the server should bind to a particular local address, or all local addresses
243

244         ServerSocket JavaDoc srvSock = null;
245
246         if (hasBindAddress())
247             srvSock = new ServerSocket JavaDoc(getPort(), LISTEN_BACKLOG, getBindAddress());
248         else
249             srvSock = new ServerSocket JavaDoc(getPort(), LISTEN_BACKLOG);
250         setSocket(srvSock);
251
252         // DEBUG
253

254         if (logger.isDebugEnabled() && hasDebug())
255             logger.debug("[SMB] Binding " + getName() + " session handler to local address : "
256                     + (hasBindAddress() ? getBindAddress().getHostAddress() : "ALL"));
257     }
258
259     /**
260      * @see Runnable#run()
261      */

262     public abstract void run();
263
264     /**
265      * Return the session socket handler as a string
266      *
267      * @return String
268      */

269     public String JavaDoc toString()
270     {
271         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
272
273         str.append("[");
274         str.append(getName());
275         str.append(",");
276         str.append(getServer().getServerName());
277         str.append(",");
278         str.append(getBindAddress() != null ? getBindAddress().getHostAddress() : "<All>");
279         str.append(":");
280         str.append(getPort());
281         str.append("]");
282
283         return str.toString();
284     }
285 }
286
Popular Tags