KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > external > XmlRpcManager


1 package freecs.external;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 import org.apache.xmlrpc.WebServer;
10 import org.apache.xmlrpc.XmlRpcException;
11 import org.apache.xmlrpc.XmlRpcHandler;
12
13 import freecs.Server;
14 import freecs.core.RequestReader;
15 import freecs.core.UserManager;
16
17 public class XmlRpcManager implements XmlRpcHandler {
18
19     public static final XmlRpcManager mgr = new XmlRpcManager ();
20     private WebServer webserver;
21
22     /**
23      * wrap instance's start method.
24      * start the xmlrpc-server using values from server-config.
25      */

26     public static void startManager(){
27         mgr.start();
28     }
29     
30     private XmlRpcManager() {
31     }
32
33     private void start() {
34         // by default, we don't start the xmlrpc-server
35
if (Server.srv.ADMIN_XMLRPC_PORT == 0) {
36             return;
37         }
38         webserver = new WebServer(Server.srv.ADMIN_XMLRPC_PORT);
39         webserver.addHandler("freecs", this);
40         webserver.setParanoid(true);
41         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Started XmlRpc-Server on port " + Server.srv.ADMIN_XMLRPC_PORT + ", open for addresses: ");
42         // parse list of allowed clients:
43
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(Server.srv.ADMIN_XMLRPC_ALLOWED, ",");
44         while (tok.hasMoreTokens()) {
45             String JavaDoc ip = tok.nextToken().trim();
46             webserver.acceptClient(ip);
47             sb.append (ip + ", ");
48         }
49         webserver.start();
50         Server.log(this, sb.toString().substring(0, sb.toString().length()-2), Server.MSG_CONFIG, Server.LVL_MINOR);
51     }
52
53     /**
54      * main method for inbound xmlrpc-calls, distributes the function calls.
55      * exceptions thrown by the underlying methods are passed through to the clients
56      * @see org.apache.xmlrpc.XmlRpcHandler
57      */

58     public Object JavaDoc execute(String JavaDoc name, Vector JavaDoc args) throws Exception JavaDoc {
59         boolean reval = false;
60         if ("freecs.kick".equals(name)) {
61             reval = kick(args);
62         } else if ("freecs.removeUser".equals(name)) {
63             reval = removeUser(args);
64         } else if ("freecs.messageToAll".equals(name)) {
65             reval = messageToAll(args);
66         } else if ("freecs.messageToGroup".equals(name)) {
67             reval = messageToGroup(args);
68         } else if ("freecs.messageToUser".equals(name)) {
69             reval = messageToUser(args);
70         } else if ("freecs.state".equals(name)) {
71             return xmlRpcStateReport(args);
72         }
73         return Boolean.valueOf(reval);
74     }
75
76     private Hashtable JavaDoc xmlRpcStateReport(Vector JavaDoc args) {
77         Hashtable JavaDoc result = new Hashtable JavaDoc();
78         result.put("error", "");
79         result.put("freeThreads", new Integer JavaDoc(Server.srv.MAX_READERS - RequestReader.activeReaders()));
80         result.put("maxThreads", new Integer JavaDoc(Server.srv.MAX_READERS));
81         result.put("usersHighWaterMark", new Integer JavaDoc(UserManager.mgr.getHighWaterMark()));
82         result.put("users", new Integer JavaDoc(UserManager.mgr.getActiveUserCount()));
83         Runtime JavaDoc r = Runtime.getRuntime();
84         result.put("vmFreeMem", new Integer JavaDoc(Math.round(r.freeMemory()/1024)));
85         result.put("vmTotalMem", new Integer JavaDoc(Math.round(r.totalMemory()/1024)));
86         // FIXME: at the moment only for linux
87
Hashtable JavaDoc lomd = getLinuxMemoryDetails(r);
88         if (lomd != null) {
89             result.putAll(lomd);
90         }
91         return result;
92     }
93
94     /**
95      * @return
96      */

97     private Hashtable JavaDoc getLinuxMemoryDetails(Runtime JavaDoc r) {
98         
99         try {
100             // systemcall for checking memory
101
Process JavaDoc p = r.exec("cat /proc/meminfo");
102             try {
103                 int returnCode = p.waitFor();
104                 if (returnCode>0) {
105                     // return ht;
106
return null;
107                 }
108             } catch (InterruptedException JavaDoc ie) {}
109
110             // read in stdout and stderr
111
StringBuffer JavaDoc out = new StringBuffer JavaDoc();
112             StringBuffer JavaDoc err = new StringBuffer JavaDoc();
113             InputStream JavaDoc os = p.getInputStream();
114             int b = os.read();
115             while (b != -1) {
116                 out.append((char) b);
117                 b = os.read();
118             }
119             os = p.getErrorStream();
120             b = os.read();
121             while (b != -1) {
122                 err.append((char) b);
123                 b = os.read();
124             }
125
126             Hashtable JavaDoc ht = new Hashtable JavaDoc();
127             // if stderr isn't empty return the error
128
if (err.length() > 0) {
129                 Server.log("XmlRpc", "error while getting System-Memory-Information: " + err.toString(), Server.MSG_ERROR, Server.LVL_MINOR);
130                 ht.put("error", err.toString());
131                 return ht;
132             }
133             
134             // parse the content of /proc/meminfo
135
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(out.toString(), "\r\n");
136             String JavaDoc props[] = st.nextToken().trim().split(":");
137             StringTokenizer JavaDoc vst = new StringTokenizer JavaDoc(st.nextToken(), " ");
138             String JavaDoc prefix = vst.nextToken().trim();
139             int i = 0;
140             while (vst.hasMoreTokens() && i < props.length) {
141                 props[i]=props[i].trim();
142                 ht.put(prefix + props[i], new Integer JavaDoc(Integer.parseInt(vst.nextToken())));
143                 i++;
144             }
145             vst = new StringTokenizer JavaDoc(st.nextToken(), " ");
146             prefix = vst.nextToken().trim();
147             i = 0;
148             while (vst.hasMoreTokens() && i < props.length) {
149                 ht.put(prefix + props[i], new Integer JavaDoc(Integer.parseInt(vst.nextToken())));
150                 i++;
151             }
152             
153             while (st.hasMoreTokens()) {
154                 String JavaDoc line = st.nextToken();
155                 int idx = line.indexOf(":");
156                 if (idx < 0)
157                     continue;
158                 String JavaDoc key = line.substring(0, idx);
159                 Integer JavaDoc iObj = new Integer JavaDoc (
160                         Integer.parseInt(
161                                 line.substring(idx+1, line.length()-3).trim()));
162                 ht.put(key, iObj);
163             }
164             return ht;
165         } catch (IOException JavaDoc e) {
166             // ignore
167
}
168         return null;
169     }
170
171     /**
172      * kick a user from the chat.
173      * freecs.kick(username, timeout, blockIp)
174      */

175     public boolean kick(Vector JavaDoc args) throws Exception JavaDoc {
176         String JavaDoc username;
177         long timeout = 2000;
178         boolean blockIp = false;
179         try {
180             username = (String JavaDoc)args.elementAt(0);
181             if (args.size()>1) {
182                 Object JavaDoc arg1 = args.elementAt(1);
183                 if (arg1 instanceof String JavaDoc) {
184                     timeout = Long.parseLong((String JavaDoc)arg1);
185                 } else if (arg1 instanceof Number JavaDoc) {
186                     timeout = ((Number JavaDoc)args.elementAt(1)).longValue();
187                 } else {
188                     throw new XmlRpcException (0, "illegal arguments for freecs.kick: couldn't parse timeout");
189                 }
190             }
191             if (args.size()>2) {
192                 Object JavaDoc obj = args.elementAt(2);
193                 if((obj instanceof Boolean JavaDoc && ((Boolean JavaDoc)obj).booleanValue()==true)
194                         || (obj instanceof String JavaDoc && "true".equalsIgnoreCase((String JavaDoc)obj)) ) {
195                     blockIp = true;
196                 }
197             }
198         } catch (Exception JavaDoc any) {
199             throw new XmlRpcException (0, "illegal arguments for freecs.kick: " + any.toString());
200         }
201         return AdminCore.kick(username, timeout, blockIp, "xmlrpc-remote");
202     }
203     
204     /**
205      * removes a user from the chat (the polite version)
206      * freecs.removeUser(username)
207      */

208     public boolean removeUser(Vector JavaDoc args) throws Exception JavaDoc {
209         String JavaDoc username;
210         try {
211             username = (String JavaDoc) args.elementAt(0);
212         } catch (Exception JavaDoc ex) {
213             throw new XmlRpcException (0, "illegal arguments for freecs.removeUser: " + ex.toString());
214         }
215         return AdminCore.removeUser(username);
216     }
217     
218     /**
219      * sends a system message to all users of the chat
220      * freecs.messageToAll(message)
221      */

222     public boolean messageToAll(Vector JavaDoc args) throws Exception JavaDoc {
223         String JavaDoc msg;
224         try {
225             msg = (String JavaDoc) args.elementAt(0);
226         } catch (Exception JavaDoc ex) {
227             throw new XmlRpcException (0, "illegal arguments for freecs.messageToAll: " + ex.toString());
228         }
229         return AdminCore.messageToAll(msg);
230     }
231     
232     /**
233      * sends a system message to all users of a given group
234      * freecs.messageToAll(message, groupname)
235      */

236     public boolean messageToGroup(Vector JavaDoc args) throws Exception JavaDoc {
237         String JavaDoc msg, groupname;
238         try {
239             msg = (String JavaDoc) args.elementAt(0);
240             groupname = (String JavaDoc) args.elementAt(1);
241         } catch (Exception JavaDoc ex) {
242             throw new XmlRpcException (0, "illegal arguments for freecs.messageToGroup: " + ex.toString());
243         }
244         return AdminCore.messageToGroup(msg, groupname);
245     }
246     
247     /**
248      * sends a system message to a single user
249      * freecs.messageToUser(message, username)
250      */

251     public boolean messageToUser(Vector JavaDoc args) throws Exception JavaDoc {
252         String JavaDoc msg, username;
253         try {
254             msg = (String JavaDoc) args.elementAt(0);
255             username = (String JavaDoc) args.elementAt(1);
256         } catch (Exception JavaDoc ex) {
257             throw new XmlRpcException (0, "illegal arguments for freecs.messageToUser: " + ex.toString());
258         }
259         return AdminCore.messageToUser(msg, username);
260     }
261
262 }
263
Popular Tags