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.BufferedWriter;
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024: import java.io.IOException;
025:
026: import javax.swing.JOptionPane;
027:
028: import org.columba.api.exception.AuthenticationException;
029: import org.columba.core.config.Config;
030: import org.columba.core.resourceloader.GlobalResourceLoader;
031:
032: /**
033: * Contains the logic necessary to search for running Columba sessions and
034: * pass command line arguments to it or to start a new session.
035: */
036: public class SessionController {
037:
038: /**
039: * Tries to connect to a running ColumbaServer using a new ColumbaClient.
040: * If this works, the given command line arguments are passed to the
041: * running server and the startup process is terminated.
042: * If this doesn't work, a new ColumbaServer instance is created and
043: * the startup process isn't terminated.
044: */
045: public static void passToRunningSessionAndExit(String[] args) {
046: //create new client and try to connect to server
047: ColumbaClient client = new ColumbaClient();
048:
049: try {
050: client.connect();
051: client.sendCommandLine(args);
052: System.exit(5);
053: } catch (IOException ioe1) {
054: //no server running, start our own
055: try {
056: ColumbaServer.getColumbaServer().start();
057: } catch (IOException ioe2) {
058: ioe2.printStackTrace();
059: //display error message
060: System.exit(1);
061: }
062: } catch (AuthenticationException ae) {
063: JOptionPane.showMessageDialog(null, GlobalResourceLoader
064: .getString(ColumbaServer.RESOURCE_PATH, "session",
065: "err_auth_msg"), GlobalResourceLoader
066: .getString(ColumbaServer.RESOURCE_PATH, "session",
067: "err_auth_title"),
068: JOptionPane.ERROR_MESSAGE);
069: System.exit(5);
070: } finally {
071: client.close();
072: }
073: }
074:
075: /**
076: * Reads a stored port number from the file ".auth" in the config directory.
077: */
078: protected static int deserializePortNumber() throws IOException {
079: File file = new File(Config.getInstance().getConfigDirectory(),
080: ".auth");
081: BufferedReader reader = null;
082:
083: try {
084: reader = new BufferedReader(new FileReader(file));
085:
086: String line = reader.readLine();
087:
088: return Integer.parseInt(line);
089: } catch (NumberFormatException nfe) {
090: IOException ioe = new IOException();
091: ioe.initCause(nfe);
092: throw ioe;
093: } finally {
094: if (reader != null) {
095: reader.close();
096: }
097: }
098: }
099:
100: /**
101: * Stores the given port number in the file ".auth" in the config directory.
102: * If the passed port number is -1, the existing file is deleted.
103: */
104: protected static void serializePortNumber(int port)
105: throws IOException {
106: File file = new File(Config.getInstance().getConfigDirectory(),
107: ".auth");
108:
109: if (port == -1) {
110: file.delete();
111: } else {
112: BufferedWriter writer = null;
113:
114: try {
115: writer = new BufferedWriter(new FileWriter(file));
116: writer.write(Integer.toString(port));
117: writer.newLine();
118: } finally {
119: if (writer != null) {
120: writer.close();
121: }
122: }
123: }
124: }
125: }
|