KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

16 public class AceCommandSession extends Thread JavaDoc
17 {
18     private String JavaDoc commandPrompt;
19     private Socket socket;
20     private BufferedReader reader;
21     private OutputStreamWriter writer;
22     
23     private static int count = 0;
24     
25     /** Creates a new instance of AceCommandSession */
26     public AceCommandSession(ThreadGroup JavaDoc group, Socket sock, String JavaDoc prompt)
27     throws IOException, SocketException
28     {
29         super(group, "CommandSession");
30         setName("CommandSession_" + count++);
31         
32         commandPrompt = prompt;
33         socket = sock;
34         
35         socket.setSoTimeout(5000);
36         
37         reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
38         writer = new OutputStreamWriter(socket.getOutputStream());
39     }
40     
41     public void dispose()
42     {
43         try
44         {
45             socket.close();
46         }
47         catch (IOException ex)
48         {
49             ;
50         }
51         
52         interrupt();
53     }
54     
55     public void run()
56     {
57         sendMessage (AceCommandService.getInstance().getGreetings() + "\n\n");
58         
59         try
60         {
61             boolean no_prompt = false;
62             while (isInterrupted() == false)
63             {
64                 if (no_prompt == false)
65                 {
66                     if (sendMessage(commandPrompt) == false)
67                     {
68                         break;
69                     }
70                 }
71                 no_prompt = false;
72                 
73                 String JavaDoc line = null;
74                 try
75                 {
76                     line = reader.readLine();
77                 }
78                 catch (InterruptedIOException ex)
79                 {
80                     if (isInterrupted() == true)
81                     {
82                         break;
83                     }
84                     else
85                     {
86                         no_prompt = true;
87                         continue;
88                     }
89                 }
90                 
91                 if (isInterrupted() == true)
92                 {
93                     break;
94                 }
95                 
96                 if (line == null)
97                 {
98                     break;
99                 }
100                 
101                 if (AceCommandService.getInstance().executeCommand(line, this)
102                 == false)
103                 {
104                     break;
105                 }
106             } // while
107
}
108         catch (IOException ex)
109         {
110             ;
111         }
112         
113         dispose();
114     }
115        
116     public boolean sendMessage(String JavaDoc message)
117     {
118         try
119         {
120             writer.write(message);
121             writer.flush();
122             return true;
123         }
124         catch (IOException ex)
125         {
126             return false;
127         }
128     }
129     
130     /** Getter for property socket.
131      * @return Value of property socket.
132      */

133     public java.net.Socket JavaDoc getSocket()
134     {
135         return socket;
136     }
137     
138     /** Setter for property socket.
139      * @param socket New value of property socket.
140      */

141     public void setSocket(java.net.Socket JavaDoc socket)
142     {
143         this.socket = socket;
144     }
145     
146 }
147
Popular Tags