Source code: org/altara/mars/Main.java
1 /* MARS Network Monitor
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars;
23
24 import java.util.*;
25 import java.net.*;
26 import java.io.*;
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import javax.swing.event.*;
32
33 import org.jdom.*;
34 import org.jdom.input.*;
35 import org.jdom.output.*;
36
37 import gnu.getopt.*;
38
39 import org.altara.util.*;
40 import org.altara.mars.engine.*;
41 import org.altara.mars.swingui.*;
42 import org.altara.mars.plugin.*;
43 import org.altara.mars.plugin.mailnotify.*;
44 import org.altara.mars.plugin.swingnotify.*;
45
46 public class Main {
47
48 /* Application-wide constaints */
49 public static final String VERSION = "2.2.2";
50 public static final String RELEASE_DATE = "March 11, 2003";
51
52 private static Main current;
53
54 public static final void main (String[] args) throws Exception {
55 // default options
56 String homeDirName = ".";
57 String configName = null;
58 boolean startUI = true;
59 // parse command line options
60 int c;
61 LongOpt[] longopts = {
62 new LongOpt("nogui", LongOpt.NO_ARGUMENT, null, 'n'),
63 new LongOpt("home", LongOpt.REQUIRED_ARGUMENT, null, 'h'),
64 };
65 Getopt g = new Getopt("mars",args,"h:n",longopts);
66 while ((c = g.getopt()) != -1) {
67 switch (c) {
68 case 'h':
69 homeDirName = g.getOptarg();
70 break;
71 case 'n':
72 startUI = false;
73 break;
74 default:
75 break;
76 }
77 }
78 if (g.getOptind() < args.length) {
79 configName = args[g.getOptind()];
80 }
81
82 // check for silly argument combinations
83 if (startUI == false && configName == null) {
84 System.err.println(
85 "It would be useless to start Mars with neither "+
86 "a configuration file\nnor a user interface. Exiting.");
87 System.exit(1);
88 }
89
90 // check for invalid home directory
91 File homeDir = new File(homeDirName);
92 if (!homeDir.isDirectory()) {
93 System.err.println(
94 "Invalid Mars home directory "+homeDirName+
95 "\nExiting.");
96 System.exit(1);
97 }
98
99 // create a new Main and make it current
100 new Main(homeDir,configName,startUI);
101 }
102
103 // singleton children
104 private Controller controller;
105 private PluginRegistry pluginRegistry;
106
107 // current status view (status bar, etc.)
108 private StatusView statusView;
109
110 private Main(File homeDir, String configName, final boolean startUI)
111 throws Exception {
112 // Make myself current immediately (initialization will need to
113 // access singletons through this instance)
114 current = this;
115
116 // Display splash screen if UI desired
117 if (startUI) {
118 // initialize icon service - we'll need it later, too
119 IconService.initialize(MarsView.class,"icons");
120 displaySplashScreen();
121 } else {
122 displaySplashText();
123 }
124
125 // Initialize the Controller
126 controller = new Controller();
127
128 // Hook up the status view to the controller
129 controller.addStatusChangeListener(new StatusChangeListener() {
130 public void statusChanged(StatusChangeEvent ev) {
131 showStatus(ev.getService()+": "+ev.getNewStatus());
132 }
133 });
134
135 // Attach XML-defined probes
136 XmlProbeFactory.registerAll(homeDir);
137
138 // Dynamically load probes in the home directory
139 ProbeFactory.loadDynamic(homeDir);
140
141 // Initialize the PluginRegistry
142 pluginRegistry = new PluginRegistry();
143
144 // Dynamically load plugins in the home directory
145 pluginRegistry.loadDynamic(homeDir);
146
147 // Set up a model - either from a file, or from scratch
148 if (configName == null) {
149 newModel();
150 } else {
151 loadConfig(new File(configName));
152 }
153
154 // Initialize the Swing UI, if desired
155 if (startUI) {
156 MarsView mv = new MarsView();
157 if (configName == null) {
158 configName = "./mars-config.xml";
159 }
160 mv.setDefaultSaveFile(new File(configName));
161 setStatusView(mv);
162 showStatus("Welcome to Mars "+VERSION+" ("+RELEASE_DATE+")");
163 } else {
164 // Otherwise, start the controller
165 showStatus("Welcome to Mars "+VERSION+" ("+RELEASE_DATE+")");
166 controller.start();
167 }
168 }
169
170 /* Status management */
171
172 public void showStatus(String status) {
173 if (statusView != null) statusView.showStatus(status);
174 }
175
176 private void setStatusView(StatusView statusView) {
177 this.statusView = statusView;
178 }
179
180 /* Splash screens */
181
182 private void displaySplashScreen() {
183 // show the splash screen
184 final MarsView.SplashScreen splashScreen =
185 new MarsView.SplashScreen();
186 setStatusView(splashScreen);
187 // set up a timer to kill it
188 javax.swing.Timer splashTimer =
189 new javax.swing.Timer(5000,new ActionListener() {
190 public void actionPerformed(ActionEvent ae) {
191 splashScreen.dispose();
192 }
193 });
194 splashTimer.setCoalesce(false);
195 splashTimer.setRepeats(false);
196 splashTimer.start();
197 }
198
199 private void displaySplashText() {
200 System.out.print(
201 "Mars "+VERSION+" starting without GUI...\n"+
202 "Mars is (c) 2002-2003 Leapfrog R&D, LLC (http://www.lfrd.com).\n"+
203 " (c) 1999 Brian H. Trammell.\n"+
204 "Mars is released with NO WARRANTY, and is distributed under\n"+
205 "the terms of the GNU General Public License, \n"+
206 "available at http://www.gnu.org/copyleft/gpl.html\n\n");
207 setStatusView(new StatusView() {
208 public void showStatus(String status) {
209 System.out.println(status);
210 }
211 });
212 }
213
214 /* Configuration management */
215
216 public void newModel() {
217 controller.setModel(new MarsModel());
218 }
219
220 public void loadConfig(File file) throws Exception {
221 // load the file into an XML document
222 Element config = new SAXBuilder().build(file).getRootElement();
223 // create a new model
224 MarsModel newModel = MarsModel.fromJDOMElem(config);
225 // attach it to the controller
226 controller.setModel(newModel);
227 // configure plugins
228 pluginRegistry.configAll(config);
229 }
230
231 public void saveConfig(File file) throws IOException {
232 // get the model configuration
233 Element config = controller.getModel().toJDOMElem(false);
234 // merge plugin configs into the element
235 pluginRegistry.mergeConfig(config);
236 // build a document around it
237 Document out = new Document(config);
238 // write it out
239 new XMLOutputter(" ",true).output(config,new FileWriter(file));
240 }
241
242 /* Singleton access */
243 public static Main getMain() {
244 return current;
245 }
246
247 public Controller getController() {
248 return controller;
249 }
250
251 public PluginRegistry getPluginRegistry() {
252 return pluginRegistry;
253 }
254 }