XNap.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap;
import gnu.getopt.Getopt;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.log4j.Level;
import org.xnap.cmdl.CommandLine;
import org.xnap.gui.ErrorHandler;
import org.xnap.gui.SplashWindow;
import org.xnap.gui.XNapFrame;
import org.xnap.gui.theme.ThemeManager;
import org.xnap.loader.XNapClassLoader;
import org.xnap.loader.XNapLoader;
import org.xnap.util.Debug;
import org.xnap.util.FileHelper;
import org.xnap.util.Preferences;
import org.xnap.util.SystemHelper;
import org.xnap.util.UncaughtExceptionListener;
import org.xnap.util.UncaughtExceptionManager;
import org.xnap.util.Updater;
import org.xnap.util.VersionParser;
import org.xnap.util.XNapThreadGroup;
/**
* Starts the main application.
*/
public class XNap {
//--- Constant(s) ---
public static final String FILE_REVISION = "$Revision: 1.20 $";
/**
* The minimal JDK version required to run the commandline version
*/
public static final String REQUIRED_CMDL_JDK = "1.2.0";
/**
* The minimal JDK version required to run the gui version
*/
public static final String REQUIRED_GUI_JDK = "1.3.0";
/**
* This list contains all languages the XNap interface has been
* translated into. Add new languages here (see TRANSLATION file).
*/
public static final Locale[] LANGUAGES = {
Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN,
Locale.ITALIAN, Locale.JAPANESE, new Locale("nl", "", ""),
new Locale("es", "", ""), new Locale("ru", "", ""),
};
/**
* This list contains all public keys automatically accepted by xnap.
*/
public static final String[] TRUSTED_KEYS = {
// first xnap key (xnap-3.0-beta4)
"MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4"
+"C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7u"
+"p1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUA"
+"l2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMC"
+"z0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpn"
+"WRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYU"
+"AAoGBAO2rM0tzQs52zUd0yXq7JbPd9lclsE00b9H5s4dDRtkGzzqViywV8qxCbkjJc4Kq"
+"O249NBY5Je0nlLnMRxqTepHgeXF2WEDQmIsz5r7ZoVfUhZ90XTIHnE4IwdktX02Pbj/H6"
+"a1aOugUBaNnkBIClv239djzy1FekT+N0gGsN5w4"
};
public static final long START_TIME = System.currentTimeMillis();
//--- Data field(s) ---
public static boolean clearPackageInfos = false;
/** Reference to the current localization bundle. */
private static ResourceBundle resources;
/** The global thread group */
private static XNapThreadGroup tg;
private static UncaughtExceptionListener exceptionHandler;
/** The version that was last launched. */
public static String lastLaunchVersion;
/**
* True if xnap is run from cvs. Enables a few development quirks.
*/
private static boolean isRunFromCvs = false;
private static boolean firstTime = true;
//--- Method(s) ---
/**
* Cleanly exits the applicaton.
*/
public static void exit()
{
if (XNapFrame.getInstance() != null) {
XNapFrame.getInstance().stop();
}
if (CommandLine.getInstance() != null) {
CommandLine.getInstance().stop();
}
Updater.stop();
System.exit(0);
}
/**
* Brings up the Command Line (Cmdl) or Graphic User Interface (GUI).
*
* @param args parameters
*/
public static void main(final String[] argv)
{
tg = new XNapThreadGroup("XNapThreadGroup");
System.setProperty("sun.awt.exception.handler",
"xnap.util.XNapAWTExceptionHandler");
Thread mainRunner = new Thread(tg, "XNapMain") {
public void run() {
setContextClassLoader(XNapClassLoader.getInstance());
runMain(argv);
}
};
mainRunner.start();
}
private static void runMain(String[] argv)
{
Level level = Level.OFF;
boolean debug = false;
boolean startCmdl = false;
boolean showHelp = false;
boolean jdkCheck = true;
Getopt g = new Getopt("xnap", argv, "cd::hnpvx");
int c;
while ((c = g.getopt()) != -1) {
//System.out.println((char)c + " [" + g.getOptarg() + "]");
switch (c) {
case 'c':
startCmdl = true;
break;
case 'd':
debug = true;
String arg = g.getOptarg();
level = (arg != null) ? Level.toLevel(arg) : Level.ALL;
break;
case 'h':
showHelp = true;
break;
case 'n':
jdkCheck = false;
break;
case 'p':
clearPackageInfos = true;
break;
case 'v':
System.out.println(XNapLoader.VERSION);
System.exit(0);
break;
case 'x':
isRunFromCvs = true;
break;
case '?':
System.exit(1);
break;
}
}
File debugPrefs = new File(FileHelper.getHomeDir() + "debug.prefs");
Debug.init(debugPrefs, level);
if (!debug) {
File f = new File(FileHelper.getHomeDir() + "error.log");
Debug.setErrorFile(f);
}
// here we go...
SystemHelper.init();
Preferences prefs = Preferences.getInstance();
prefs.read();
lastLaunchVersion = prefs.getLastLaunchVersion();
if (!XNapLoader.VERSION.equals(lastLaunchVersion)) {
// clear package information when launching a different version
clearPackageInfos = true;
}
prefs.setLastLaunchVersion(XNapLoader.VERSION);
initLocale(prefs.getLanguage());
if (debug) {
try {
InputStreamReader in = new InputStreamReader(System.in);
System.err.println("Default encoding: " + in.getEncoding());
in.close();
}
catch (IOException e) {
}
}
if (showHelp) {
showHelp();
System.exit(0);
}
else {
if (jdkCheck) {
checkJDK(REQUIRED_CMDL_JDK);
}
if (!startCmdl) {
exceptionHandler = new ErrorHandler();
UncaughtExceptionManager.getInstance().addExceptionListener
(exceptionHandler);
if (prefs.getShowSplash()) {
SplashWindow.showSplashWindow(0);
SplashWindow.setText(XNap.tr("Starting up"));
}
}
Updater.start();
if (startCmdl) {
CommandLine cmdl = new CommandLine();
cmdl.start();
}
else {
startGUI();
}
}
}
/**
* Sets default locale and loads resources.
*/
public static void initLocale(String language)
{
for (int i = 0; i < LANGUAGES.length; i++) {
if (language.equals(LANGUAGES[i].getLanguage())) {
Locale.setDefault(LANGUAGES[i]);
break;
}
}
try {
resources = ResourceBundle.getBundle("org.xnap.resources.XNap",
Locale.getDefault());
}
catch (MissingResourceException e) {
System.err.println("org/xnap/resources/XNap.properties not found");
System.exit(1);
}
}
public static final XNapThreadGroup getThreadGroup()
{
return tg;
}
public static boolean isRunFromCvs()
{
return isRunFromCvs;
}
/**
* Prints a warning message if <code>reqVer</code> is smaller than the
* current jdk version.
*/
public static final void checkJDK(String reqVer)
{
String jdkVer = System.getProperty("java.version");
if (VersionParser.compare(jdkVer, reqVer) < 0) {
System.out.println(XNap.tr("Your java version") + "\t: " + jdkVer);
System.out.println(XNap.tr("Required version") + "\t: " + reqVer);
System.out.println(XNap.tr("If XNap runs fine, you can safely ignore this message.\n")
+ XNap.tr("Otherwise you probably need to update your jdk from\nhttp://java.sun.com/products/jdk/1.3/jre/index.html."));
System.out.println(tr("See http://xnap.sourceforge.net for more information.\n")
+ "(" + XNap.tr("use -n to suppress this message") + ")\n");
}
}
/**
* Print a help message to stdout.
*/
public static final void showHelp()
{
System.out.println(XNap.tr("Usage: xnap [options]"));
System.out.println(" -c\t " + XNap.tr("start in commandline mode"));
System.out.println(" -d[level]\t " + XNap.tr("show debug messages"));
System.out.println(" -h\t " + XNap.tr("show this help"));
System.out.println(" -n\t" + XNap.tr("do not check jdk version"));
}
public static final void startGUI()
{
if (exceptionHandler == null) {
exceptionHandler = new ErrorHandler();
UncaughtExceptionManager.getInstance().addExceptionListener
(exceptionHandler);
}
if (firstTime) {
ThemeManager.initialize();
XNapFrame.updateLookAndFeel();
firstTime = false;
}
XNapFrame f = new XNapFrame();
f.setVisible(true);
f.guiVisible();
SplashWindow.closeSplashWindow();
}
public static final void stopGUI()
{
XNapFrame.getInstance().stop();
UncaughtExceptionManager.getInstance().removeExceptionListener
(exceptionHandler);
exceptionHandler = null;
}
/**
* Returns <code>text</code> translated into the currently selected
* language. Every user-visible string in the program must be wrapped
* into this function.
*/
public static final String tr(String text)
{
try {
return resources.getString(text);
}
catch (MissingResourceException e) {
//System.err.println("missing translation key: \"" + text + "\"");
//e.printStackTrace(System.err);
return text;
}
catch (NullPointerException e) {
// the message bundle has not been loaded yet
return text;
}
}
/**
* Returns <code>text</code> translated into the currently selected
* language.
*
* <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
*/
public static final String tr(String text, Object o1)
{
return MessageFormat.format(XNap.tr(text), new Object[] { o1 });
}
/**
* Returns <code>text</code> translated into the currently selected
* language.
*
* <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
* The first occurence of {1} is replaced by <code>o2.toString()</code>.
*/
public static final String tr(String text, Object o1, Object o2)
{
return MessageFormat.format(XNap.tr(text), new Object[] { o1, o2 });
}
/**
* Returns <code>text</code> translated into the currently selected
* language.
*
* <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
* The first occurence of {1} is replaced by <code>o2.toString()</code>.
* The first occurence of {2} is replaced by <code>o3.toString()</code>.
*/
public static final String tr(String text, Object o1, Object o2,
Object o3)
{
return MessageFormat.format(XNap.tr(text),
new Object[] { o1, o2, o3 });
}
/**
* Returns <code>text</code> translated into the currently selected
* language.
*
* <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
* The first occurence of {1} is replaced by <code>o2.toString()</code>.
* The first occurence of {2} is replaced by <code>o3.toString()</code>.
* The first occurence of {3} is replaced by <code>o4.toString()</code>.
*/
public static final String tr(String text, Object o1, Object o2,
Object o3, Object o4)
{
return MessageFormat.format(XNap.tr(text),
new Object[] { o1, o2, o3, o4 });
}
/**
* Returns <code>text</code> translated into the currently selected
* language. Prepends and appends <code>padding</code> whitespaces.
*/
public static final String tr(String text, int padding)
{
String s = tr(text);
if (padding <= 0) {
return s;
}
StringBuffer sb = new StringBuffer(s.length() + padding * 2);
append(sb, " ", padding);
sb.append(s);
append(sb, " ", padding);
return sb.toString();
}
/**
* Returns <code>text</code> translated into the currently selected
* language. Prepends <code>lpadding</code> whitespaces. Appends
* <code>rpadding</code> whitespaces.
*/
public static final String tr(String text, int lpadding, int rpadding)
{
String s = tr(text);
StringBuffer sb = new StringBuffer(s.length() + lpadding + rpadding);
append(sb, " ", lpadding);
sb.append(s);
append(sb, " ", rpadding);
return sb.toString();
}
private static final void append(StringBuffer sb, String s, int count)
{
for (int i = 0; i < count; i++) {
sb.append(s);
}
}
}
The table below shows all metrics for XNap.java.




