KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedWriter JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import javax.swing.JOptionPane JavaDoc;
27
28 import org.columba.api.exception.AuthenticationException;
29 import org.columba.core.config.Config;
30 import org.columba.core.resourceloader.GlobalResourceLoader;
31
32 /**
33  * Contains the logic necessary to search for running Columba sessions and
34  * pass command line arguments to it or to start a new session.
35  */

36 public class SessionController {
37     
38     /**
39  * Tries to connect to a running ColumbaServer using a new ColumbaClient.
40  * If this works, the given command line arguments are passed to the
41  * running server and the startup process is terminated.
42  * If this doesn't work, a new ColumbaServer instance is created and
43  * the startup process isn't terminated.
44  */

45     public static void passToRunningSessionAndExit(String JavaDoc[] args) {
46         //create new client and try to connect to server
47
ColumbaClient client = new ColumbaClient();
48
49         try {
50             client.connect();
51             client.sendCommandLine(args);
52             System.exit(5);
53         } catch (IOException JavaDoc ioe1) {
54             //no server running, start our own
55
try {
56                 ColumbaServer.getColumbaServer().start();
57             } catch (IOException JavaDoc ioe2) {
58                 ioe2.printStackTrace();
59                 //display error message
60
System.exit(1);
61             }
62         } catch (AuthenticationException ae) {
63             JOptionPane.showMessageDialog(null,
64                 GlobalResourceLoader.getString(ColumbaServer.RESOURCE_PATH,
65                     "session", "err_auth_msg"),
66                 GlobalResourceLoader.getString(ColumbaServer.RESOURCE_PATH,
67                     "session", "err_auth_title"),
68                 JOptionPane.ERROR_MESSAGE);
69             System.exit(5);
70         } finally {
71             client.close();
72         }
73     }
74
75     /**
76  * Reads a stored port number from the file ".auth" in the config directory.
77  */

78     protected static int deserializePortNumber() throws IOException JavaDoc {
79         File JavaDoc file = new File JavaDoc(Config.getInstance().getConfigDirectory(), ".auth");
80         BufferedReader JavaDoc reader = null;
81
82         try {
83             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
84
85             String JavaDoc line = reader.readLine();
86
87             return Integer.parseInt(line);
88         } catch (NumberFormatException JavaDoc nfe) {
89             IOException JavaDoc ioe = new IOException JavaDoc();
90             ioe.initCause(nfe);
91             throw ioe;
92         } finally {
93             if (reader != null) {
94                 reader.close();
95             }
96         }
97     }
98
99     /**
100  * Stores the given port number in the file ".auth" in the config directory.
101  * If the passed port number is -1, the existing file is deleted.
102  */

103     protected static void serializePortNumber(int port)
104         throws IOException JavaDoc {
105         File JavaDoc file = new File JavaDoc(Config.getInstance().getConfigDirectory(), ".auth");
106
107         if (port == -1) {
108             file.delete();
109         } else {
110             BufferedWriter JavaDoc writer = null;
111
112             try {
113                 writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
114                 writer.write(Integer.toString(port));
115                 writer.newLine();
116             } finally {
117                 if (writer != null) {
118                     writer.close();
119                 }
120             }
121         }
122     }
123 }
124
Popular Tags