KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > QSAdminAPI


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.qsadmin;
16
17 import java.io.*;
18 import java.net.*;
19 import java.util.logging.*;
20
21 /**
22  * QSAdminAPI class to communicate to QsAdmin from java applications.
23  * <p>
24  * Eg:
25  * <code><BLOCKQUOTE><pre>
26     QSAdminAPI qsAdminApi = new QSAdminAPI("127.0.0.1", 9080);
27     if(qsAdminApi.logon()) {
28         System.out.println("Logged in");
29         String info = qsAdminApi.sendCommand("info server");
30         System.out.println("Info on Server :\n"+info);
31         qsAdminApi.logoff();
32     } else {
33         System.out.println("Bad Login");
34         qsAdminApi.close();
35     }
36 </pre></BLOCKQUOTE></code></p>
37  * @see QSAdminServer
38  * @since 1.4
39  * @author Akshathkumar Shetty
40  */

41 public class QSAdminAPI {
42     private static Logger logger = Logger.getLogger(QSAdminAPI.class.getName());
43
44     private String JavaDoc username = "Admin";
45     private String JavaDoc password = "QsAdm1n";
46
47     private String JavaDoc host = "localhost";
48     private int port = 9877;
49
50     private Socket socket;
51     private InputStream in;
52     private OutputStream out;
53     private BufferedReader br;
54     private BufferedWriter bw;
55  
56     /**
57      * Creates QSAdminAPI object that will communicate with the
58      * passed host and port.
59      */

60     public QSAdminAPI(String JavaDoc host, int port) {
61         this.host = host;
62         this.port = port;
63     }
64  
65     /**
66      * Will attempt to connect and logon to the remote QsAdminServer.
67      */

68     public boolean logon() throws IOException {
69         return logon(username, password);
70     }
71
72     /**
73      * Will attempt to connect and logon to the remote QsAdminServer.
74      */

75     public boolean logon(String JavaDoc username, String JavaDoc password)
76             throws IOException {
77         this.username = username;
78         this.password = password;
79
80         logger.fine("Connecting to "+host+":"+port);
81         socket = new Socket(host, port);
82         in = socket.getInputStream();
83         out = socket.getOutputStream();
84         br = new BufferedReader(new InputStreamReader(in));
85         bw = new BufferedWriter(new OutputStreamWriter(out));
86         logger.fine("Got : "+br.readLine());
87         logger.fine("Got : "+br.readLine());
88         logger.fine("Got : "+br.readLine());
89
90         logger.fine("Got : "+br.readLine());
91         logger.fine("Sending username");
92         bw.write(username+"\r\n");
93         bw.flush();
94         logger.fine("Got : "+br.readLine());
95
96         logger.fine("Sending password");
97         bw.write(password+"\r\n");
98         bw.flush();
99
100         String JavaDoc temp = br.readLine();
101         logger.fine("Got : "+temp);
102         return temp.startsWith("+OK ");
103     }
104  
105     /**
106      * Sends the given command to QSAdmin and gives the response back.
107      */

108     public String JavaDoc sendCommand(String JavaDoc data) throws IOException {
109         logger.fine("Sending command : "+data);
110         bw.write(data+"\r\n");
111         bw.flush();
112         String JavaDoc temp = readResponse();
113         logger.fine("Got : "+temp);
114         return temp;
115     }
116
117     private String JavaDoc readResponse() throws IOException {
118         StringBuffer JavaDoc command = new StringBuffer JavaDoc();
119         String JavaDoc res = br.readLine();
120         if(res!=null && res.equals("+OK info follows")==false)
121             return res;
122         while(res!=null && res.equals(".")==false) {
123             command.append(res + "\r\n");
124             res = br.readLine();
125         }
126         return command.toString();
127     }
128  
129     /**
130      * Logoff the QSAdminServer and closed the socket associated.
131      */

132     public void logoff() throws IOException {
133         logger.fine("Logging off");
134         logger.fine("Sending command : quit");
135         bw.write("quit"+"\r\n");
136         bw.flush();
137         logger.fine("Got : "+br.readLine());
138         close();
139     }
140
141     /**
142      * Closes the socket associated.
143      */

144     public void close() throws IOException {
145         logger.fine("Closing");
146         socket.close();
147         socket = null;
148     }
149  
150     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
151         QSAdminAPI qsAdminApi = new QSAdminAPI("127.0.0.1", 9080);
152         if(qsAdminApi.logon()) {
153             logger.info("Logged in");
154             String JavaDoc info = qsAdminApi.sendCommand("info server");
155             logger.info("Info on Server :\n"+info);
156             qsAdminApi.logoff();
157         } else {
158             logger.warning("Bad Login!");
159             qsAdminApi.close();
160         }
161     }
162 }
163
Popular Tags