KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceCommandService


1 /*
2  * AceCommandService.java
3  *
4  * Created on August 22, 2002, 5:06 PM
5  */

6
7 package com.quikj.server.framework;
8
9 import java.util.*;
10 import java.io.*;
11
12 /**
13  *
14  * @author amit
15  */

16 public class AceCommandService
17 {
18     private static AceCommandService instance = null;
19     private int port = -1;
20     private String JavaDoc commandPrompt = null;
21     private int maxUsers = 0;
22     private ThreadGroup JavaDoc group = new ThreadGroup JavaDoc("AceCommandGroup");
23     private AceCommandConnectionListener connectionListener = null;
24     private String JavaDoc greetings = null;
25     private Hashtable commandList = new Hashtable();
26     
27     /** Creates a new instance of AceCommandService */
28     public AceCommandService(String JavaDoc greetings,
29     String JavaDoc prompt,
30     int port,
31     int max_users)
32     throws IOException
33     {
34         commandPrompt = prompt;
35         maxUsers = max_users;
36         this.greetings = greetings;
37         
38         // start the AceCommandConnectionListener
39
connectionListener = new AceCommandConnectionListener(port);
40         connectionListener.start();
41         
42         instance = this;
43     }
44     
45     public static AceCommandService getInstance()
46     {
47         return instance;
48     }
49     
50     public void dispose()
51     {
52         // close the connection listener
53
if (connectionListener != null)
54         {
55             connectionListener.dispose();
56             connectionListener = null;
57         }
58         
59         // next, close all the command session threads
60
AceCommandSession[] sessions = new AceCommandSession[group.activeCount()];
61         if (sessions != null)
62         {
63             int count = group.enumerate(sessions);
64             for (int i = 0; i < count; i++)
65             {
66                 if (sessions[i] != null)
67                 {
68                     sessions[i].dispose();
69                 }
70             }
71         }
72         
73         instance = null;
74     }
75     
76     public void broadcastMessage(String JavaDoc message)
77     {
78         AceCommandSession[] sessions = new AceCommandSession[group.activeCount()];
79         if (sessions != null)
80         {
81             int count = group.enumerate(sessions);
82             for (int i = 0; i < count; i++)
83             {
84                 if (sessions[i] != null)
85                 {
86                     sessions[i].sendMessage(message);
87                 }
88             }
89         }
90     }
91     
92     public boolean registerCommandHandler(String JavaDoc command,
93     AceCommandHandlerInterface handler)
94     {
95         commandList.put(command, handler);
96         return true;
97     }
98     
99     public boolean unregisterCommandHandler(String JavaDoc command)
100     {
101         if (commandList.remove(command) == null)
102         {
103             return false;
104         }
105         return true;
106     }
107     
108     protected synchronized boolean executeCommand(String JavaDoc command_line,
109     AceCommandSession session)
110     {
111         String JavaDoc line = command_line.trim();
112         if (line.length() == 0)
113         {
114             return true;
115         }
116         
117         StringTokenizer tokens = new StringTokenizer(line);
118         int num_tokens = tokens.countTokens();
119         String JavaDoc command = tokens.nextToken();
120         
121         AceCommandHandlerInterface handler = null;
122         
123         handler = (AceCommandHandlerInterface)commandList.get(command);
124         
125         if (handler != null)
126         {
127             handler.actionPerformed(command_line, session);
128         }
129         else // handler not found
130
{
131             if ((command.equals("quit") == true) ||
132             (command.equals("exit") == true))
133             {
134                 session.sendMessage("Goodbye!\n");
135                 return false;
136             }
137             else if (command.equals("help") == true)
138             {
139                 if (num_tokens >= 2)
140                 {
141                     // help on a specific topic
142
String JavaDoc topic = tokens.nextToken();
143                     
144                     handler = (AceCommandHandlerInterface)commandList.get(topic);
145                     if (handler != null)
146                     {
147                         session.sendMessage(handler.usage() + "\n\n");
148                         return true;
149                     }
150                     else
151                     {
152                         session.sendMessage("Help on " + topic + " not found\n");
153                         return true;
154                     }
155                     
156                 }
157                 else // generalized help
158
{
159                     Enumeration iter = commandList.keys();
160                     session.sendMessage("List of commands:\n");
161                     while (iter.hasMoreElements() == true)
162                     {
163                         session.sendMessage((String JavaDoc)iter.nextElement() + "\n");
164                     }
165                     
166                     return true;
167                 }
168             }
169             else
170             {
171                 session.sendMessage("Bad command!\n");
172                 return true;
173             }
174         }
175         
176         return true;
177     }
178     
179     protected ThreadGroup JavaDoc getCommandSessionThreadGrup()
180     {
181         return group;
182     }
183     
184     /** Getter for property port.
185      * @return Value of property port.
186      */

187     public int getPort()
188     {
189         return port;
190     }
191     
192     /** Setter for property port.
193      * @param port New value of property port.
194      */

195     private void setPort(int port)
196     {
197         this.port = port;
198     }
199     
200     /** Getter for property commandPrompt.
201      * @return Value of property commandPrompt.
202      */

203     public java.lang.String JavaDoc getCommandPrompt()
204     {
205         return commandPrompt;
206     }
207     
208     /** Setter for property commandPrompt.
209      * @param commandPrompt New value of property commandPrompt.
210      */

211     public void setCommandPrompt(java.lang.String JavaDoc commandPrompt)
212     {
213         this.commandPrompt = commandPrompt;
214     }
215     
216     /** Getter for property maxUsers.
217      * @return Value of property maxUsers.
218      */

219     public int getMaxUsers()
220     {
221         return maxUsers;
222     }
223     
224     /** Setter for property maxUsers.
225      * @param maxUsers New value of property maxUsers.
226      */

227     public void setMaxUsers(int maxUsers)
228     {
229         this.maxUsers = maxUsers;
230     }
231     
232     /** Getter for property greetings.
233      * @return Value of property greetings.
234      */

235     public java.lang.String JavaDoc getGreetings()
236     {
237         return greetings;
238     }
239     
240     /** Setter for property greetings.
241      * @param greetings New value of property greetings.
242      */

243     public void setGreetings(java.lang.String JavaDoc greetings)
244     {
245         this.greetings = greetings;
246     }
247     
248 }
249
Popular Tags