001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.main;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStreamReader;
022: import java.io.PrintWriter;
023: import java.io.Writer;
024: import java.net.Socket;
025:
026: import org.columba.api.exception.AuthenticationException;
027: import org.columba.core.versioninfo.VersionInfo;
028:
029: /**
030: * Client connecting to the {@link ColumbaServer} to check if a session of
031: * Columba is already running.
032: * <p>
033: * If a session is running the client tries to authenticate.
034: *
035: * @author fdietz
036: */
037: public class ColumbaClient {
038: protected static final String NEWLINE = "\r\n";
039:
040: protected Socket socket;
041:
042: protected Writer writer;
043:
044: protected BufferedReader reader;
045:
046: public ColumbaClient() {
047: }
048:
049: /**
050: * Tries to connect to a running server.
051: * @throws IOException
052: * @throws AuthenticationException
053: */
054: public void connect() throws IOException, AuthenticationException {
055: socket = new Socket("127.0.0.1", SessionController
056: .deserializePortNumber());
057: writer = new PrintWriter(socket.getOutputStream());
058: writer.write("Columba " + VersionInfo.getVersion());
059: writer.write(NEWLINE);
060: writer.flush();
061:
062: writer.write("User "
063: + System.getProperty("user.name",
064: ColumbaServer.ANONYMOUS_USER));
065: writer.write(NEWLINE);
066: writer.flush();
067: reader = new BufferedReader(new InputStreamReader(socket
068: .getInputStream()));
069: String response = reader.readLine();
070: if (response.equals("WRONG USER")) {
071: throw new AuthenticationException();
072: }
073: }
074:
075: /**
076: * Submits the given command line options to the server.
077: * @param args
078: * @throws IOException
079: */
080: public void sendCommandLine(String[] args) throws IOException {
081: StringBuffer buf = new StringBuffer();
082:
083: for (int i = 0; i < args.length; i++) {
084: buf.append(args[i]);
085: buf.append('%');
086: }
087:
088: writer.write(buf.toString());
089: writer.write(NEWLINE);
090: writer.flush();
091: }
092:
093: /**
094: * Closes this client.
095: */
096: public void close() {
097: try {
098: if (writer != null) {
099: writer.close();
100: }
101: if (reader != null) {
102: reader.close();
103: }
104: if (socket != null) {
105: socket.close();
106: }
107: } catch (IOException ioe) {
108: }
109: }
110: }
|