KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > NetworkServer


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.server;
18
19 import java.net.InetAddress JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.alfresco.filesys.server.auth.SrvAuthenticator;
23 import org.alfresco.filesys.server.auth.acl.AccessControlManager;
24 import org.alfresco.filesys.server.config.ServerConfiguration;
25 import org.alfresco.filesys.server.core.ShareMapper;
26 import org.alfresco.filesys.server.core.SharedDevice;
27 import org.alfresco.filesys.server.core.SharedDeviceList;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * Network Server Base Class
33  * <p>
34  * Base class for server implementations for different protocols.
35  */

36 public abstract class NetworkServer
37 {
38     private static final Log logger = LogFactory.getLog("org.alfresco.filesys");
39
40     // Protocol name
41

42     private String JavaDoc m_protoName;
43
44     // Server version
45

46     private String JavaDoc m_version;
47
48     // Server configuration
49
private ServerConfiguration m_config;
50
51     // Debug enabled flag and debug flags
52

53     private boolean m_debug;
54     private int m_debugFlags;
55
56     // List of addresses that the server is bound to
57

58     private InetAddress JavaDoc[] m_ipAddr;
59
60     // Server shutdown flag and server active flag
61

62     private boolean m_shutdown = false;
63     private boolean m_active = false;
64
65     // Server error exception details
66

67     private Exception JavaDoc m_exception;
68
69     // Server events listener
70

71     private ServerListener m_listener;
72
73     // Session listener list
74

75     private Vector JavaDoc<SessionListener> m_sessListeners;
76
77     /**
78      * Class constructor
79      *
80      * @param proto String
81      * @param config ServerConfiguration
82      */

83     public NetworkServer(String JavaDoc proto, ServerConfiguration config)
84     {
85         m_protoName = proto;
86         m_config = config;
87     }
88
89     /**
90      * Returns the server configuration.
91      *
92      * @return ServerConfiguration
93      */

94     public final ServerConfiguration getConfiguration()
95     {
96         return m_config;
97     }
98
99     /**
100      * Return the authenticator for this server
101      *
102      * @return SrvAuthenticator
103      */

104     public final SrvAuthenticator getAuthenticator()
105     {
106         return getConfiguration().getAuthenticator();
107     }
108
109     /**
110      * Determine if an access control manager is configured
111      *
112      * @return boolean
113      */

114     public final boolean hasAccessControlManager()
115     {
116         return getConfiguration().getAccessControlManager() != null ? true : false;
117     }
118
119     /**
120      * Return the access control manager
121      *
122      * @return AccessControlManager
123      */

124     public final AccessControlManager getAccessControlManager()
125     {
126         return getConfiguration().getAccessControlManager();
127     }
128
129     /**
130      * Return the main server name
131      *
132      * @return String
133      */

134     public final String JavaDoc getServerName()
135     {
136         return m_config.getServerName();
137     }
138
139     /**
140      * Return the list of IP addresses that the server is bound to.
141      *
142      * @return java.net.InetAddress[]
143      */

144     public final InetAddress JavaDoc[] getServerAddresses()
145     {
146         return m_ipAddr;
147     }
148
149     /**
150      * Return the share mapper
151      *
152      * @return ShareMapper
153      */

154     public final ShareMapper getShareMapper()
155     {
156         return m_config.getShareMapper();
157     }
158
159     /**
160      * Return the available shared device list.
161      *
162      * @param host String
163      * @param sess SrvSession
164      * @return SharedDeviceList
165      */

166     public final SharedDeviceList getShareList(String JavaDoc host, SrvSession sess)
167     {
168         return getConfiguration().getShareMapper().getShareList(host, sess, false);
169     }
170
171     /**
172      * Return the complete shared device list.
173      *
174      * @param host String
175      * @param sess SrvSession
176      * @return SharedDeviceList
177      */

178     public final SharedDeviceList getFullShareList(String JavaDoc host, SrvSession sess)
179     {
180         return getConfiguration().getShareMapper().getShareList(host, sess, true);
181     }
182
183     /**
184      * Find the shared device with the specified name.
185      *
186      * @param host Host name from the UNC path
187      * @param name Name of the shared device to find.
188      * @param typ Shared device type
189      * @param sess Session details
190      * @param create Create share flag, false indicates lookup only
191      * @return SharedDevice with the specified name and type, else null.
192      * @exception Exception
193      */

194     public final SharedDevice findShare(String JavaDoc host, String JavaDoc name, int typ, SrvSession sess, boolean create)
195             throws Exception JavaDoc
196     {
197
198         // Search for the specified share
199

200         SharedDevice dev = getConfiguration().getShareMapper().findShare(host, name, typ, sess, create);
201
202         // Return the shared device, or null
203

204         return dev;
205     }
206
207     /**
208      * Determine if the SMB server is active.
209      *
210      * @return boolean
211      */

212     public final boolean isActive()
213     {
214         return m_active;
215     }
216
217     /**
218      * Return the server version string, in 'n.n.n' format
219      *
220      * @return String
221      */

222
223     public final String JavaDoc isVersion()
224     {
225         return m_version;
226     }
227
228     /**
229      * Check if there is a stored server exception
230      *
231      * @return boolean
232      */

233     public final boolean hasException()
234     {
235         return m_exception != null ? true : false;
236     }
237
238     /**
239      * Return the stored exception
240      *
241      * @return Exception
242      */

243     public final Exception JavaDoc getException()
244     {
245         return m_exception;
246     }
247
248     /**
249      * Clear the stored server exception
250      */

251     public final void clearException()
252     {
253         m_exception = null;
254     }
255
256     /**
257      * Return the server protocol name
258      *
259      * @return String
260      */

261     public final String JavaDoc getProtocolName()
262     {
263         return m_protoName;
264     }
265
266     /**
267      * Determine if debug output is enabled
268      *
269      * @return boolean
270      */

271     public final boolean hasDebug()
272     {
273         return m_debug;
274     }
275
276     /**
277      * Determine if the specified debug flag is enabled
278      *
279      * @return boolean
280      */

281     public final boolean hasDebugFlag(int flg)
282     {
283         return (m_debugFlags & flg) != 0 ? true : false;
284     }
285
286     /**
287      * Check if the shutdown flag is set
288      *
289      * @return boolean
290      */

291     public final boolean hasShutdown()
292     {
293         return m_shutdown;
294     }
295
296     /**
297      * Set/clear the server active flag
298      *
299      * @param active boolean
300      */

301     protected void setActive(boolean active)
302     {
303         m_active = active;
304     }
305
306     /**
307      * Set the stored server exception
308      *
309      * @param ex Exception
310      */

311     protected final void setException(Exception JavaDoc ex)
312     {
313         m_exception = ex;
314     }
315
316     /**
317      * Set the addresses that the server is bound to
318      *
319      * @param adds InetAddress[]
320      */

321     protected final void setServerAddresses(InetAddress JavaDoc[] addrs)
322     {
323         m_ipAddr = addrs;
324     }
325
326     /**
327      * Set the server version
328      *
329      * @param ver String
330      */

331     protected final void setVersion(String JavaDoc ver)
332     {
333         m_version = ver;
334     }
335
336     /**
337      * Enable/disable debug output for the server
338      *
339      * @param dbg boolean
340      */

341     protected final void setDebug(boolean dbg)
342     {
343         m_debug = dbg;
344     }
345
346     /**
347      * Set the debug flags
348      *
349      * @param flags int
350      */

351     protected final void setDebugFlags(int flags)
352     {
353         m_debugFlags = flags;
354         setDebug(flags == 0 ? false : true);
355     }
356
357     /**
358      * Set/clear the shutdown flag
359      *
360      * @param ena boolean
361      */

362     protected final void setShutdown(boolean ena)
363     {
364         m_shutdown = ena;
365     }
366
367     /**
368      * Add a server listener to this server
369      *
370      * @param l ServerListener
371      */

372     public final void addServerListener(ServerListener l)
373     {
374         m_listener = l;
375     }
376
377     /**
378      * Remove the server listener
379      *
380      * @param l ServerListener
381      */

382     public final void removeServerListener(ServerListener l)
383     {
384         if (m_listener == l)
385             m_listener = null;
386     }
387
388     /**
389      * Add a new session listener to the network server.
390      *
391      * @param l SessionListener
392      */

393     public final void addSessionListener(SessionListener l)
394     {
395
396         // Check if the session listener list is allocated
397

398         if (m_sessListeners == null)
399             m_sessListeners = new Vector JavaDoc<SessionListener>();
400         m_sessListeners.add(l);
401     }
402
403     /**
404      * Remove a session listener from the network server.
405      *
406      * @param l SessionListener
407      */

408     public final void removeSessionListener(SessionListener l)
409     {
410
411         // Check if the listener list is valid
412

413         if (m_sessListeners == null)
414             return;
415         m_sessListeners.removeElement(l);
416     }
417
418     /**
419      * Fire a server event to the registered listener
420      *
421      * @param event int
422      */

423     protected final void fireServerEvent(int event)
424     {
425
426         // Check if there is a listener registered with this server
427

428         if (m_listener != null)
429         {
430             try
431             {
432                 m_listener.serverStatusEvent(this, event);
433             }
434             catch (Exception JavaDoc ex)
435             {
436             }
437         }
438     }
439
440     /**
441      * Start the network server
442      */

443     public abstract void startServer();
444
445     /**
446      * Shutdown the network server
447      *
448      * @param immediate boolean
449      */

450     public abstract void shutdownServer(boolean immediate);
451
452     /**
453      * Trigger a closed session event to all registered session listeners.
454      *
455      * @param sess SrvSession
456      */

457     protected final void fireSessionClosedEvent(SrvSession sess)
458     {
459
460         // Check if there are any listeners
461

462         if (m_sessListeners == null || m_sessListeners.size() == 0)
463             return;
464
465         // Inform all registered listeners
466

467         for (int i = 0; i < m_sessListeners.size(); i++)
468         {
469
470             // Get the current session listener
471

472             try
473             {
474                 SessionListener sessListener = (SessionListener) m_sessListeners.elementAt(i);
475                 sessListener.sessionClosed(sess);
476             }
477             catch (Exception JavaDoc ex)
478             {
479                 logger.error("Session listener error [closed]: ", ex);
480             }
481         }
482     }
483
484     /**
485      * Trigger a new session event to all registered session listeners.
486      *
487      * @param sess SrvSession
488      */

489     protected final void fireSessionLoggedOnEvent(SrvSession sess)
490     {
491
492         // Check if there are any listeners
493

494         if (m_sessListeners == null || m_sessListeners.size() == 0)
495             return;
496
497         // Inform all registered listeners
498

499         for (int i = 0; i < m_sessListeners.size(); i++)
500         {
501
502             // Get the current session listener
503

504             try
505             {
506                 SessionListener sessListener = (SessionListener) m_sessListeners.elementAt(i);
507                 sessListener.sessionLoggedOn(sess);
508             }
509             catch (Exception JavaDoc ex)
510             {
511                 logger.error("Session listener error [logon]: ", ex);
512             }
513         }
514     }
515
516     /**
517      * Trigger a new session event to all registered session listeners.
518      *
519      * @param sess SrvSession
520      */

521     protected final void fireSessionOpenEvent(SrvSession sess)
522     {
523
524         // Check if there are any listeners
525

526         if (m_sessListeners == null || m_sessListeners.size() == 0)
527             return;
528
529         // Inform all registered listeners
530

531         for (int i = 0; i < m_sessListeners.size(); i++)
532         {
533
534             // Get the current session listener
535

536             try
537             {
538                 SessionListener sessListener = (SessionListener) m_sessListeners.elementAt(i);
539                 sessListener.sessionCreated(sess);
540             }
541             catch (Exception JavaDoc ex)
542             {
543                 logger.error("Session listener error [open]: ", ex);
544             }
545         }
546     }
547 }
548
Popular Tags