KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > main > ColumbaClient


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.main;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.net.Socket JavaDoc;
25
26 import org.columba.api.exception.AuthenticationException;
27 import org.columba.core.versioninfo.VersionInfo;
28
29 /**
30  * Client connecting to the {@link ColumbaServer} to check if a session of
31  * Columba is already running.
32  * <p>
33  * If a session is running the client tries to authenticate.
34  *
35  * @author fdietz
36  */

37 public class ColumbaClient {
38     protected static final String JavaDoc NEWLINE = "\r\n";
39
40     protected Socket JavaDoc socket;
41
42     protected Writer JavaDoc writer;
43
44     protected BufferedReader JavaDoc reader;
45
46     public ColumbaClient() {
47     }
48
49     /**
50      * Tries to connect to a running server.
51      * @throws IOException
52      * @throws AuthenticationException
53      */

54     public void connect() throws IOException JavaDoc, AuthenticationException {
55         socket = new Socket JavaDoc("127.0.0.1", SessionController
56                 .deserializePortNumber());
57         writer = new PrintWriter JavaDoc(socket.getOutputStream());
58         writer.write("Columba " + VersionInfo.getVersion());
59         writer.write(NEWLINE);
60         writer.flush();
61
62         writer
63                 .write("User "
64                         + System.getProperty("user.name",
65                                 ColumbaServer.ANONYMOUS_USER));
66         writer.write(NEWLINE);
67         writer.flush();
68         reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(socket
69                 .getInputStream()));
70         String JavaDoc response = reader.readLine();
71         if (response.equals("WRONG USER")) {
72             throw new AuthenticationException();
73         }
74     }
75
76     /**
77      * Submits the given command line options to the server.
78      * @param args
79      * @throws IOException
80      */

81     public void sendCommandLine(String JavaDoc[] args) throws IOException JavaDoc {
82         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
83
84         for (int i = 0; i < args.length; i++) {
85             buf.append(args[i]);
86             buf.append('%');
87         }
88
89         writer.write(buf.toString());
90         writer.write(NEWLINE);
91         writer.flush();
92     }
93
94     /**
95      * Closes this client.
96      */

97     public void close() {
98         try {
99             if (writer != null) {
100                 writer.close();
101             }
102             if (reader != null) {
103                 reader.close();
104             }
105             if (socket != null) {
106                 socket.close();
107             }
108         } catch (IOException JavaDoc ioe) {
109         }
110     }
111 }
112
Popular Tags