01: /**
02: * Start.java
03: * Created on 19.08.2003, 20:40:08 Alex
04: * Package: net.sf.memoranda
05: *
06: * @author Alex V. Alishevskikh, alex@openmechanics.net
07: * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net
08: */package net.sf.memoranda;
09:
10: import java.net.ServerSocket;
11: import java.net.Socket;
12:
13: import net.sf.memoranda.ui.*;
14: import net.sf.memoranda.util.Configuration;
15:
16: /**
17: *
18: */
19: /*$Id: Start.java,v 1.7 2004/11/22 10:02:37 alexeya Exp $*/
20: public class Start {
21:
22: static App app = null;
23:
24: static int DEFAULT_PORT = 19432;
25: static boolean checkIfAlreadyStartet = true;
26:
27: static {
28: String port = Configuration.get("PORT_NUMBER").toString()
29: .trim();
30: if (port.length() > 0) {
31: // The Portnumber must be between 1024 (in *nix all Port's < 1024
32: // are privileged) and 65535 (the highest Portnumber everywhere)
33: int i = Integer.parseInt(port);
34: if ((i >= 1024) && (i <= 65535)) {
35: DEFAULT_PORT = i;
36: }
37: /*DEBUG*///System.out.println("Port " + DEFAULT_PORT + " used.");
38: }
39:
40: String check = Configuration.get("CHECK_IF_ALREADY_STARTED")
41: .toString().trim();
42: if (check.length() > 0 && check.equalsIgnoreCase("no")) {
43: checkIfAlreadyStartet = false;
44: }
45: }
46:
47: public static void main(String[] args) {
48: if (checkIfAlreadyStartet) {
49: try {
50: // Try to open a socket. If socket opened successfully (app is already started), take no action and exit.
51: Socket socket = new Socket("127.0.0.1", DEFAULT_PORT);
52: socket.close();
53: System.exit(0);
54:
55: } catch (Exception e) {
56: // If socket is not opened (app is not started), continue
57: // e.printStackTrace();
58: }
59: new SLThread().start();
60: }
61:
62: //System.out.println(EventsScheduler.isEventScheduled());
63: if ((args.length == 0) || (!args[0].equals("-m"))) {
64: app = new App(true);
65: } else
66: app = new App(false);
67: }
68: }
69:
70: class SLThread extends Thread {
71:
72: public void run() {
73: ServerSocket serverSocket = null;
74: try {
75: serverSocket = new ServerSocket(Start.DEFAULT_PORT);
76: serverSocket.accept();
77: Start.app.show();
78: serverSocket.close();
79: new SLThread().start();
80:
81: } catch (Exception e) {
82: System.err.println("Port:" + Start.DEFAULT_PORT);
83: e.printStackTrace();
84: new ExceptionDialog(e,
85: "Cannot create a socket connection on localhost:"
86: + Start.DEFAULT_PORT,
87: "Make sure that other software does not use the port "
88: + Start.DEFAULT_PORT
89: + " and examine your security settings.");
90: }
91: }
92: }
|