001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.qsadmin;
016:
017: import java.io.*;
018: import java.net.*;
019: import java.util.logging.*;
020:
021: /**
022: * QSAdminAPI class to communicate to QsAdmin from java applications.
023: * <p>
024: * Eg:
025: * <code><BLOCKQUOTE><pre>
026: QSAdminAPI qsAdminApi = new QSAdminAPI("127.0.0.1", 9080);
027: if(qsAdminApi.logon()) {
028: System.out.println("Logged in");
029: String info = qsAdminApi.sendCommand("info server");
030: System.out.println("Info on Server :\n"+info);
031: qsAdminApi.logoff();
032: } else {
033: System.out.println("Bad Login");
034: qsAdminApi.close();
035: }
036: </pre></BLOCKQUOTE></code></p>
037: * @see QSAdminServer
038: * @since 1.4
039: * @author Akshathkumar Shetty
040: */
041: public class QSAdminAPI {
042: private static Logger logger = Logger.getLogger(QSAdminAPI.class
043: .getName());
044:
045: private String username = "Admin";
046: private String password = "QsAdm1n";
047:
048: private String host = "localhost";
049: private int port = 9877;
050:
051: private Socket socket;
052: private InputStream in;
053: private OutputStream out;
054: private BufferedReader br;
055: private BufferedWriter bw;
056:
057: /**
058: * Creates QSAdminAPI object that will communicate with the
059: * passed host and port.
060: */
061: public QSAdminAPI(String host, int port) {
062: this .host = host;
063: this .port = port;
064: }
065:
066: /**
067: * Will attempt to connect and logon to the remote QsAdminServer.
068: */
069: public boolean logon() throws IOException {
070: return logon(username, password);
071: }
072:
073: /**
074: * Will attempt to connect and logon to the remote QsAdminServer.
075: */
076: public boolean logon(String username, String password)
077: throws IOException {
078: this .username = username;
079: this .password = password;
080:
081: logger.fine("Connecting to " + host + ":" + port);
082: socket = new Socket(host, port);
083: in = socket.getInputStream();
084: out = socket.getOutputStream();
085: br = new BufferedReader(new InputStreamReader(in));
086: bw = new BufferedWriter(new OutputStreamWriter(out));
087: logger.fine("Got : " + br.readLine());
088: logger.fine("Got : " + br.readLine());
089: logger.fine("Got : " + br.readLine());
090:
091: logger.fine("Got : " + br.readLine());
092: logger.fine("Sending username");
093: bw.write(username + "\r\n");
094: bw.flush();
095: logger.fine("Got : " + br.readLine());
096:
097: logger.fine("Sending password");
098: bw.write(password + "\r\n");
099: bw.flush();
100:
101: String temp = br.readLine();
102: logger.fine("Got : " + temp);
103: return temp.startsWith("+OK ");
104: }
105:
106: /**
107: * Sends the given command to QSAdmin and gives the response back.
108: */
109: public String sendCommand(String data) throws IOException {
110: logger.fine("Sending command : " + data);
111: bw.write(data + "\r\n");
112: bw.flush();
113: String temp = readResponse();
114: logger.fine("Got : " + temp);
115: return temp;
116: }
117:
118: private String readResponse() throws IOException {
119: StringBuffer command = new StringBuffer();
120: String res = br.readLine();
121: if (res != null && res.equals("+OK info follows") == false)
122: return res;
123: while (res != null && res.equals(".") == false) {
124: command.append(res + "\r\n");
125: res = br.readLine();
126: }
127: return command.toString();
128: }
129:
130: /**
131: * Logoff the QSAdminServer and closed the socket associated.
132: */
133: public void logoff() throws IOException {
134: logger.fine("Logging off");
135: logger.fine("Sending command : quit");
136: bw.write("quit" + "\r\n");
137: bw.flush();
138: logger.fine("Got : " + br.readLine());
139: close();
140: }
141:
142: /**
143: * Closes the socket associated.
144: */
145: public void close() throws IOException {
146: logger.fine("Closing");
147: socket.close();
148: socket = null;
149: }
150:
151: public static void main(String[] args) throws Exception {
152: QSAdminAPI qsAdminApi = new QSAdminAPI("127.0.0.1", 9080);
153: if (qsAdminApi.logon()) {
154: logger.info("Logged in");
155: String info = qsAdminApi.sendCommand("info server");
156: logger.info("Info on Server :\n" + info);
157: qsAdminApi.logoff();
158: } else {
159: logger.warning("Bad Login!");
160: qsAdminApi.close();
161: }
162: }
163: }
|