1 package org.columba.core.main; 19 20 import java.io.File ; 21 import java.io.FilenameFilter ; 22 import java.lang.reflect.Field ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.logging.Logger ; 29 30 import javax.swing.JPopupMenu ; 31 import javax.swing.RepaintManager ; 32 import javax.swing.SwingUtilities ; 33 34 import org.apache.commons.cli.CommandLine; 35 import org.apache.commons.cli.Option; 36 import org.apache.commons.cli.OptionBuilder; 37 import org.apache.commons.cli.ParseException; 38 import org.columba.core.association.AssociationStore; 39 import org.columba.core.backgroundtask.BackgroundTaskManager; 40 import org.columba.core.base.OSInfo; 41 import org.columba.core.component.ComponentManager; 42 import org.columba.core.config.Config; 43 import org.columba.core.config.DefaultConfigDirectory; 44 import org.columba.core.config.SaveConfig; 45 import org.columba.core.desktop.ColumbaDesktop; 46 import org.columba.core.gui.base.DebugRepaintManager; 47 import org.columba.core.gui.frame.FrameManager; 48 import org.columba.core.gui.profiles.Profile; 49 import org.columba.core.gui.profiles.ProfileManager; 50 import org.columba.core.gui.themes.ThemeSwitcher; 51 import org.columba.core.gui.trayicon.ColumbaTrayIcon; 52 import org.columba.core.gui.util.FontProperties; 53 import org.columba.core.gui.util.StartUpFrame; 54 import org.columba.core.logging.Logging; 55 import org.columba.core.plugin.PluginManager; 56 import org.columba.core.resourceloader.GlobalResourceLoader; 57 import org.columba.core.scripting.service.ServiceManager; 58 import org.columba.core.shutdown.ShutdownManager; 59 import org.columba.core.util.StackProfiler; 60 import org.columba.core.versioninfo.VersionInfo; 61 import org.frapuccino.swing.ActiveWindowTracker; 62 63 import sun.misc.URLClassPath; 64 65 public class Bootstrap { 66 67 private static final Logger LOG = Logger.getLogger("org.columba.core.main"); 69 private static final String RESOURCE_PATH = "org.columba.core.i18n.global"; 71 public static boolean ENABLE_TAGS = false; 74 75 private String path; 76 77 private boolean showSplashScreen = true; 78 79 public void run(String args[]) throws Exception { 80 81 addNativeJarsToClasspath(); 82 setLibraryPath(); 83 84 if (OSInfo.isMac()) { 88 System.setProperty("apple.laf.useScreenMenuBar", "true"); 89 System.setProperty( 90 "com.apple.mrj.application.apple.menu.about.name", 91 "Columba"); 92 } 93 94 Logging.createDefaultHandler(); 95 registerCommandLineArguments(); 96 97 StackProfiler profiler = new StackProfiler(); 98 profiler.push("main"); 99 profiler.push("config"); 100 profiler.push("profile"); 101 Profile profile = ProfileManager.getInstance().getProfile(path); 103 profiler.pop("profile"); 104 105 DefaultConfigDirectory.getInstance().setCurrentPath(profile.getLocation()); 107 new Config(profile.getLocation()); 108 profiler.pop("config"); 109 110 Logging.createDefaultFileHandler(DefaultConfigDirectory.getDefaultPath()); 114 115 for (int i = 0; i < args.length; i++) { 116 LOG.info("arg[" + i + "]=" + args[i]); 117 } 118 119 SessionController.passToRunningSessionAndExit(args); 120 121 124 if (Logging.DEBUG) 125 RepaintManager.setCurrentManager(new DebugRepaintManager()); 126 127 JPopupMenu.setDefaultLightWeightPopupEnabled(false); 129 130 ActiveWindowTracker.class.getClass(); 133 134 StartUpFrame frame = null; 136 if (showSplashScreen) { 137 frame = new StartUpFrame(); 138 frame.setVisible(true); 139 } 140 141 System.setProperty("java.protocol.handler.pkgs", 143 "org.columba.core.url|" 144 + System.getProperty("java.protocol.handler.pkgs", "")); 145 146 profiler.push("i18n"); 147 GlobalResourceLoader.loadLanguage(); 149 profiler.pop("i18n"); 150 151 SaveConfig task = new SaveConfig(); 152 BackgroundTaskManager.getInstance().register(task); 153 ShutdownManager.getInstance().register(task); 154 155 profiler.push("plugins core"); 156 initPlugins(); 157 profiler.pop("plugins core"); 158 159 profiler.push("components"); 160 ComponentManager.getInstance().init(); 162 ComponentManager.getInstance().registerCommandLineArguments(); 163 profiler.pop("components"); 164 165 ThemeSwitcher.setTheme(); 167 168 initPlatformServices(); 170 171 new FontProperties(); 173 174 FontProperties.setFont(); 176 177 if (handleCoreCommandLineParameters(args)) { 179 System.exit(0); 180 } 181 182 ComponentManager.getInstance().handleCommandLineParameters( 184 ColumbaCmdLineParser.getInstance().getParsedCommandLine()); 185 186 profiler.push("plugins external"); 187 profiler.pop("plugins external"); 190 191 profiler.push("frames"); 192 193 if (ColumbaCmdLineParser.getInstance().getRestoreLastSession()) { 195 SwingUtilities.invokeLater(new Runnable () { 196 public void run() { 197 FrameManager.getInstance().openStoredViews(); 198 } 199 }); 200 } 201 202 203 ServiceManager.getInstance().initServices(); 204 205 ShutdownManager.getInstance().register(new Runnable () { 207 public void run() { 208 209 ServiceManager.getInstance().stopServices(); 210 ServiceManager.getInstance().disposeServices(); 211 } 212 }); 213 214 profiler.pop("frames"); 215 216 221 profiler.push("tagging"); 222 223 if (ENABLE_TAGS) { 225 AssociationStore.getInstance().init(); 226 ShutdownManager.getInstance().register(AssociationStore.getInstance()); 228 } 229 230 profiler.pop("tagging"); 231 232 if (frame != null) { 234 frame.setVisible(false); 235 } 236 237 ComponentManager.getInstance().postStartup(); 240 241 242 ServiceManager.getInstance().startServices(); 243 244 profiler.pop("main"); 245 246 } 247 248 253 private void initPlugins() { 254 255 259 PluginManager.getInstance().addExtensionHandlers( 261 "org/columba/core/plugin/extensionhandler.xml"); 262 263 PluginManager.getInstance().addExtensionHandlers( 265 "org/columba/addressbook/plugin/extensionhandler.xml"); 266 267 PluginManager.getInstance().addExtensionHandlers( 269 "org/columba/mail/plugin/extensionhandler.xml"); 270 271 String path = "org/columba/core/plugin/plugin.xml"; 273 PluginManager.getInstance().addPlugin(path); 274 275 279 path = "org/columba/addressbook/plugin/plugin.xml"; 281 PluginManager.getInstance().addPlugin(path); 282 283 path = "org/columba/mail/plugin/plugin.xml"; 285 PluginManager.getInstance().addPlugin(path); 286 287 path = "org/columba/calendar/plugin/plugin.xml"; 289 PluginManager.getInstance().addPlugin(path); 290 291 PluginManager.getInstance().initExternalPlugins(); 295 } 296 297 300 private void registerCommandLineArguments() { 301 ColumbaCmdLineParser parser = ColumbaCmdLineParser.getInstance(); 302 303 parser.addOption(new Option("version", GlobalResourceLoader.getString( 304 RESOURCE_PATH, "global", "cmdline_version"))); 305 306 parser.addOption(new Option("help", GlobalResourceLoader.getString( 307 RESOURCE_PATH, "global", "cmdline_help"))); 308 309 parser.addOption(OptionBuilder.withArgName("path").hasArg().create( 310 "profile")); 311 312 parser.addOption(new Option("profile", GlobalResourceLoader.getString( 313 RESOURCE_PATH, "global", "cmdline_profile"))); 314 315 parser.addOption(new Option("debug", GlobalResourceLoader.getString( 316 RESOURCE_PATH, "global", "cmdline_debug"))); 317 318 parser.addOption(new Option("nosplash", GlobalResourceLoader.getString( 319 RESOURCE_PATH, "global", "cmdline_nosplash"))); 320 321 } 330 331 335 private boolean handleCoreCommandLineParameters(String [] args) { 336 ColumbaCmdLineParser parser = ColumbaCmdLineParser.getInstance(); 337 CommandLine commandLine; 338 339 try { 340 commandLine = parser.parse(args); 341 } catch (ParseException e) { 342 System.err.println("Parsing failed. Reason: " + e.getMessage()); 344 parser.printUsage(); 345 346 return true; 347 } 348 349 if (commandLine.hasOption("help")) { 350 parser.printUsage(); 351 352 return true; 353 } 354 355 if (commandLine.hasOption("version")) { 357 LOG.info(MessageFormat.format(GlobalResourceLoader.getString( 358 RESOURCE_PATH, "global", "info_version"), new Object [] { VersionInfo.getVersion(), 360 VersionInfo.getBuildDate() })); 361 System.out.println("Columba (" + VersionInfo.getVersion() 362 + ") built " + VersionInfo.getBuildDate() + "\n"); 363 return true; 364 } 365 366 if (commandLine.hasOption("profile")) { 367 368 path = commandLine.getArgList().toString(); 370 371 path = path.substring(1, path.length() - 1); 374 } 375 376 if (commandLine.hasOption("debug")) { 377 Logging.DEBUG = true; 378 Logging.setDebugging(true); 379 } 380 381 if (commandLine.hasOption("nosplash")) { 382 showSplashScreen = false; 383 } 384 385 return false; 387 } 388 389 397 private void setLibraryPath() throws Exception { 398 String libDir; 399 if (OSInfo.isAMD64Bit()) 400 libDir = "amd64"; 401 else 402 libDir = "lib"; 403 404 406 String propertyPath = System.getProperty("java.library.path"); 407 408 if (OSInfo.isLinux()) 409 propertyPath += ":native/linux/"; 410 else if (OSInfo.isMac()) 411 propertyPath += ":native/mac/"; 412 else if (OSInfo.isWin32Platform()) 413 propertyPath += ";native\\win32\\"; 414 416 propertyPath += libDir; 417 418 System.setProperty("java.library.path", propertyPath); 419 LOG.info("The java.library.path = " + propertyPath); 420 Field fieldSysPath = ClassLoader .class.getDeclaredField("sys_paths"); 421 fieldSysPath.setAccessible(true); 422 if (fieldSysPath != null) { 423 fieldSysPath.set(System .class.getClassLoader(), null); 424 } 425 } 426 427 439 private void addNativeJarsToClasspath() throws Exception { 440 File nativeDir; 441 442 String libDir; 443 if (OSInfo.isAMD64Bit()) 444 libDir = "amd64"; 445 else 446 libDir = "lib"; 447 448 if (OSInfo.isLinux()) 452 nativeDir = new File ("native/linux/" + libDir); 453 else if (OSInfo.isMac()) 454 nativeDir = new File ("native/mac/" + libDir); 455 else if (OSInfo.isWin32Platform()) 456 nativeDir = new File ("native/win32/" + libDir); 457 else { 458 LOG.info("Native support for Platform not available."); 459 return; 460 } 461 462 File [] nativeJars = nativeDir.listFiles(new FilenameFilter () { 464 public boolean accept(File dir, String name) { 465 return name.endsWith("jar") || name.endsWith("jnilib"); 466 } 467 }); 468 if (nativeJars == null) 469 return; 470 471 480 URLClassLoader sysloader = (URLClassLoader ) ClassLoader 483 .getSystemClassLoader(); 484 485 Field ucp = URLClassLoader .class.getDeclaredField("ucp"); 486 ucp.setAccessible(true); 487 URLClassPath currentCP = (URLClassPath) ucp.get(sysloader); 488 URL [] currentURLs = currentCP.getURLs(); 489 490 List <URL > urlList = new ArrayList <URL >(); 492 for (int i = 0; i < nativeJars.length; i++) { 493 urlList.add(nativeJars[i].toURL()); 494 } 495 496 for (int i = 0; i < currentURLs.length; i++) { 498 urlList.add(currentURLs[i]); 499 } 500 501 ucp.set(sysloader, 503 new URLClassPath((URL []) urlList.toArray(new URL [0]))); 504 } 505 506 509 private void initPlatformServices() { 510 511 ColumbaDesktop.getInstance().initActiveDesktop(); 513 ColumbaTrayIcon.getInstance().initActiveIcon(); 514 } 515 } 516 | Popular Tags |