Source code: com/virtuosotechnologies/asaph/launch/Launch.java
1 /*
2 ================================================================================
3
4 FILE: Launch.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Launcher for Asaph
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.launch;
43
44
45 import java.util.List;
46 import java.util.ArrayList;
47 import java.util.Set;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.Locale;
51 import java.util.logging.LogManager;
52 import java.util.logging.Handler;
53 import java.util.logging.ConsoleHandler;
54 import java.util.logging.Logger;
55 import java.util.logging.Level;
56 import java.util.prefs.Preferences;
57 import java.io.IOException;
58 import java.io.File;
59 import java.io.InputStream;
60 import java.io.FileFilter;
61 import java.io.FileReader;
62 import javax.swing.JComponent;
63 import javax.swing.UIManager;
64 import javax.swing.UnsupportedLookAndFeelException;
65 import org.xml.sax.InputSource;
66 import org.xml.sax.SAXException;
67
68 import com.virtuosotechnologies.lib.platform.PlatformUtils;
69 import com.virtuosotechnologies.lib.plugin.PluginInfoParser;
70 import com.virtuosotechnologies.lib.plugin.Framework;
71 import com.virtuosotechnologies.lib.plugin.FrameworkLauncher;
72 import com.virtuosotechnologies.lib.plugin.APIVersion;
73 import com.virtuosotechnologies.lib.plugin.PluginException;
74 import com.virtuosotechnologies.lib.plugin.PluginInfo;
75 import com.virtuosotechnologies.lib.util.LocaleUtils;
76
77
78 /**
79 * Launcher
80 */
81 public class Launch
82 {
83 private static final String DEFAULT_PLUGINS_FILE = "pluginList.xml";
84 private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
85 private static final String PLUGIN_INFO_FILE =
86 ResourceAccess.Strings.buildString("Plugins_InfoFileName");
87
88 private static final String LOOKANDFEEL_PREFSKEY = "lookandfeel_class";
89 private static final String PLUGINDIR_PREFSKEY = "plugin_directory";
90 private static final String LOCALE_PREFSKEY = "locale";
91
92
93 private Logger logger_;
94 private Preferences prefs_;
95 private Set builtinPluginNames_;
96
97 private Locale locale_;
98 private String lookAndFeelClass_;
99 private File pluginDir_;
100
101
102 /**
103 * Main
104 */
105 public static void main(
106 String[] args)
107 {
108 new Launch(args).launch();
109 }
110
111
112 /**
113 * Constructor
114 */
115 public Launch(
116 String[] args)
117 {
118 try
119 {
120 LogManager.getLogManager().readConfiguration(
121 getClass().getResourceAsStream(LOGGING_PROPERTIES_FILE));
122 }
123 catch (Exception ex)
124 {
125 System.err.println("Warning: couldn't read logging.properties");
126 }
127
128 Handler defaultHandler = new ConsoleHandler();
129 defaultHandler.setLevel(Level.FINEST);
130 Logger rootLogger = Logger.getLogger("virtuoso");
131 rootLogger.addHandler(defaultHandler);
132 rootLogger.setUseParentHandlers(false);
133
134 logger_ = Logger.getLogger("com.virtuosotechnologies.asaph.launch");
135
136 // Mac OS X specific settings
137 System.setProperty("com.apple.mrj.application.apple.menu.about.name",
138 ResourceAccess.Strings.buildString("AsaphProgramName"));
139 if (PlatformUtils.getNamedPlatformProvider(PlatformUtils.MAC_OS_X_PROVIDERNAME) != null)
140 {
141 System.setProperty("apple.laf.useScreenMenuBar", "true");
142 }
143 System.setProperty("apple.awt.showGrowBox", "true");
144 System.setProperty("apple.awt.fractionalmetrics", "on");
145 }
146
147
148 /**
149 * Launch and set up coordinator
150 */
151 public void launch()
152 {
153 logger_.fine("Starting launch");
154
155 prefs_ = Preferences.userNodeForPackage(getClass());
156
157 // Set the locale
158 locale_ = null;
159 String localeStr = prefs_.get(LOCALE_PREFSKEY, "");
160 if (localeStr.length() > 0)
161 {
162 locale_ = LocaleUtils.getNamedLocale(localeStr);
163 Locale.setDefault(locale_);
164 }
165
166 // Set the look-and-feel
167 lookAndFeelClass_ = prefs_.get(LOOKANDFEEL_PREFSKEY, UIManager.getSystemLookAndFeelClassName());
168 try
169 {
170 UIManager.setLookAndFeel(lookAndFeelClass_);
171 }
172 catch (ClassNotFoundException ex)
173 {
174 logger_.log(Level.WARNING, "Couldn't set look and feel.", ex);
175 }
176 catch (InstantiationException ex)
177 {
178 logger_.log(Level.WARNING, "Couldn't set look and feel.", ex);
179 }
180 catch (IllegalAccessException ex)
181 {
182 logger_.log(Level.WARNING, "Couldn't set look and feel.", ex);
183 }
184 catch (UnsupportedLookAndFeelException ex)
185 {
186 logger_.log(Level.WARNING, "Couldn't set look and feel.", ex);
187 }
188
189 // Show splash screen
190 SplashScreen splash = new SplashScreen();
191 //splash.setStatusString("Looking for plugins...");
192
193 // Build plugin list
194 List pluginInfo = new ArrayList();
195 builtinPluginNames_ = new HashSet();
196
197 // Get parser
198 PluginInfoParser parser = null;
199 try
200 {
201 parser = new PluginInfoParser();
202 }
203 catch (Exception ex)
204 {
205 logger_.log(Level.SEVERE, "Unable to create plugin xml parser.", ex);
206 System.exit(-1);
207 }
208
209 // Read default plugins
210 InputStream stream = getClass().getResourceAsStream(DEFAULT_PLUGINS_FILE);
211 if (stream == null)
212 {
213 logger_.log(Level.SEVERE, "Unable to find default plugin list.");
214 System.exit(-1);
215 }
216 try
217 {
218 pluginInfo.addAll(parser.readPluginList(new InputSource(stream), null));
219 }
220 catch (Exception ex)
221 {
222 logger_.log(Level.SEVERE, "Unable to read default plugin list.", ex);
223 System.exit(-1);
224 }
225 for (Iterator iter = pluginInfo.iterator(); iter.hasNext(); )
226 {
227 builtinPluginNames_.add(((PluginInfo)iter.next()).getPluginName());
228 }
229
230 // Get installed plugins
231 pluginDir_ = new File(prefs_.get(PLUGINDIR_PREFSKEY,
232 ResourceAccess.Strings.buildString("Plugins_DefaultDirectory")));
233 if (pluginDir_.isDirectory())
234 {
235 File[] dirs = pluginDir_.listFiles(
236 new FileFilter()
237 {
238 public boolean accept(
239 File file)
240 {
241 return file.isDirectory();
242 }
243 });
244
245 for (int i=0; i<dirs.length; ++i)
246 {
247 File pluginInfoFile = new File(dirs[i], PLUGIN_INFO_FILE);
248 if (pluginInfoFile.isFile())
249 {
250 try
251 {
252 pluginInfo.addAll(parser.readPluginList(
253 new InputSource(new FileReader(pluginInfoFile)),
254 pluginInfoFile.toURL()));
255 }
256 catch (IOException ex)
257 {
258 logger_.log(Level.WARNING, "Unable to read plugin xml file: "+pluginInfoFile, ex);
259 }
260 catch (SAXException ex)
261 {
262 logger_.log(Level.WARNING, "Unable to read plugin xml file: "+pluginInfoFile, ex);
263 }
264 }
265 }
266 }
267
268 // Create framework and activate plugins
269 Framework framework = FrameworkLauncher.launchFramework();
270 LauncherImpl launcher = new LauncherImpl(framework);
271 try
272 {
273 framework.provideAPI("com.virtuosotechnologies.asaph.launch.Launcher", new APIVersion(0, 0, 0),
274 ResourceAccess.Strings.buildString("LauncherAPIDescription"), launcher);
275 framework.plug(pluginInfo);
276 }
277 catch (PluginException ex)
278 {
279 logger_.log(Level.SEVERE, "Unable to load plugins.", ex);
280 System.exit(-1);
281 }
282 splash.finish();
283 launcher.finishLaunch();
284
285 logger_.fine("Completed launch");
286 }
287
288
289 private class LauncherImpl
290 implements Launcher
291 {
292 private Framework framework_;
293 private List completers_;
294
295
296 /*package*/ LauncherImpl(
297 Framework framework)
298 {
299 framework_ = framework;
300 completers_ = new ArrayList();
301 }
302
303
304 /**
305 * Add a callback to call on completion of the launch.
306 *
307 * @param completer a LaunchCompleter
308 */
309 public void addLaunchCompleter(
310 LaunchCompleter completer)
311 {
312 completers_.add(completer);
313 }
314
315
316 /**
317 * Open the about box.
318 *
319 * @param parent parent component for the dialog
320 */
321 public void openAboutBox(
322 JComponent parent)
323 {
324 AboutBox.show(parent, framework_, builtinPluginNames_);
325 }
326
327
328 /**
329 * Get the current startup look and feel setting
330 *
331 * @return look and feel class name
332 */
333 public String getStartupLookAndFeel()
334 {
335 return lookAndFeelClass_;
336 }
337
338
339 /**
340 * Set the current startup look and feel setting
341 *
342 * @param lafClass look and feel class name
343 */
344 public void setStartupLookAndFeel(
345 String lafClass)
346 {
347 lookAndFeelClass_ = lafClass;
348 prefs_.put(LOOKANDFEEL_PREFSKEY, lafClass);
349 }
350
351
352 /**
353 * Get the current startup locale
354 *
355 * @return locale
356 */
357 public Locale getStartupLocale()
358 {
359 return locale_;
360 }
361
362
363 /**
364 * Set the current startup locale
365 *
366 * @param locale locale
367 */
368 public void setStartupLocale(
369 Locale locale)
370 {
371 locale_ = locale;
372 if (locale_ == null)
373 {
374 prefs_.put(LOCALE_PREFSKEY, "");
375 }
376 else
377 {
378 prefs_.put(LOCALE_PREFSKEY, locale.toString());
379 }
380 }
381
382
383 /**
384 * Get the current plugin directory path
385 *
386 * @return plugin directory
387 */
388 public File getPluginDirectory()
389 {
390 return pluginDir_;
391 }
392
393
394 /**
395 * Set the current plugin directory path
396 *
397 * @param dir plugin directory
398 */
399 public void setPluginDirectory(
400 File dir)
401 {
402 pluginDir_ = dir;
403 prefs_.put(PLUGINDIR_PREFSKEY, dir.getPath());
404 }
405
406
407 /*package*/ void finishLaunch()
408 {
409 for (Iterator iter = completers_.iterator(); iter.hasNext(); )
410 {
411 LaunchCompleter completer = (LaunchCompleter)iter.next();
412 completer.completeLaunch();
413 }
414 completers_ = null;
415 }
416 }
417 }