| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.common; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.FileNotFoundException; |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.io.PrintStream; |
| 24 | import java.util.Properties; |
| 25 | import java.util.StringTokenizer; |
| 26 | |
| 27 | import javax.swing.JOptionPane; |
| 28 | |
| 29 | import net.sf.jomic.tools.ErrorTools; |
| 30 | import net.sf.jomic.tools.ItemMustBeDownloadedException; |
| 31 | |
| 32 | import org.apache.commons.logging.Log; |
| 33 | import org.apache.commons.logging.LogFactory; |
| 34 | |
| 35 | /** |
| 36 | * Utility class to bootstrap Jomic without refering to package net.sf.jomic.tools (apart from |
| 37 | * StartupTools and ErrorTools) and <code>Settings</code>. |
| 38 | * |
| 39 | * @author Thomas Aglassinger |
| 40 | */ |
| 41 | public final class JomicStartup |
| 42 | { |
| 43 | private static final int MINIMUM_JAVA_RELEASE = 4; |
| 44 | private static final int MINIMUM_JAVA_VERSION = 1; |
| 45 | |
| 46 | private static JomicStartup instance; |
| 47 | |
| 48 | private ErrorTools errorTools; |
| 49 | private Log logger; |
| 50 | private boolean startupCalled; |
| 51 | private StartupTools startupTools; |
| 52 | |
| 53 | private JomicStartup() { |
| 54 | startupTools = StartupTools.instance(); |
| 55 | errorTools = ErrorTools.instance(); |
| 56 | errorTools.setTitle(JOptionPane.ERROR_MESSAGE, "Jomic error"); |
| 57 | errorTools.setTitle(JOptionPane.WARNING_MESSAGE, "Jomic warning"); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Attempt to read logger settings from file. If the file does not exist, use internal |
| 62 | * defaults. |
| 63 | */ |
| 64 | private void setupLogging() |
| 65 | throws IOException { |
| 66 | startupTools.logDuration("setup logging"); |
| 67 | |
| 68 | File loggerSettingsFile = startupTools.getSettingsFile("jomic-logging"); |
| 69 | Properties loggerProperties = new Properties(); |
| 70 | InputStream loggerSettingsStream = null; |
| 71 | |
| 72 | // Log info as default |
| 73 | org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); |
| 74 | |
| 75 | logger = LogFactory.getLog(JomicStartup.class); |
| 76 | try { |
| 77 | loggerSettingsStream = new FileInputStream(loggerSettingsFile); |
| 78 | loggerProperties.load(loggerSettingsStream); |
| 79 | JomicConfigurator.setLevel(loggerProperties); |
| 80 | } catch (FileNotFoundException error) { |
| 81 | if (logger.isInfoEnabled()) { |
| 82 | logger.info("cannot find logger settings \"" + loggerSettingsFile |
| 83 | + "\"; using internal defaults"); |
| 84 | } |
| 85 | } finally { |
| 86 | if (loggerSettingsStream != null) { |
| 87 | loggerSettingsStream.close(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public boolean isStartup() { |
| 93 | return startupCalled; |
| 94 | } |
| 95 | |
| 96 | public static synchronized JomicStartup instance() { |
| 97 | if (instance == null) { |
| 98 | instance = new JomicStartup(); |
| 99 | } |
| 100 | return instance; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Call <code>System.exit(exitCode)</code> unless the system property <code>TEST_IGNORE_EXIT</code> |
| 105 | * is <code>true</code>. |
| 106 | * |
| 107 | * @see System#exit(int) |
| 108 | * @see PropertyConstants#TEST_IGNORE_EXIT |
| 109 | */ |
| 110 | public void exit(int exitCode) { |
| 111 | if (!Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.TEST_IGNORE_EXIT)) { |
| 112 | System.exit(exitCode); |
| 113 | } else { |
| 114 | logger.info("ignoring System.exit(" + exitCode + ")"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Show error message in dialog. Unlike <code>JomicTools.showError()</code> this method does |
| 120 | * not require any initialization. It should only be used in situations where we cannot be sure |
| 121 | * that LocaleTools and other stuff is up and running. |
| 122 | * |
| 123 | * @see net.sf.jomic.common.JomicTools#showError(javax.swing.JFrame, String, Object[], |
| 124 | * Throwable) |
| 125 | */ |
| 126 | public void showError(String message, Throwable error) { |
| 127 | showError(message, error, true); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Show error message in dialog. Unlike <code>JomicTools.showError()</code> this method does |
| 132 | * not require any initialization. It should only be used in situations where we cannot be sure |
| 133 | * that LocaleTools and other stuff is up and running. |
| 134 | * |
| 135 | * @see net.sf.jomic.common.JomicTools#showError(javax.swing.JFrame, String, Object, |
| 136 | * Throwable) |
| 137 | */ |
| 138 | public void showError(String message, Throwable error, boolean modal) { |
| 139 | if (errorTools != null) { |
| 140 | errorTools.showMessage(null, JOptionPane.ERROR_MESSAGE, message, error, modal); |
| 141 | } |
| 142 | if (logger != null) { |
| 143 | logger.error(message, error); |
| 144 | } else { |
| 145 | // In case there isn't anything else to work with, use System.err. |
| 146 | PrintStream errorStream = System.err; |
| 147 | |
| 148 | errorStream.println(message); |
| 149 | error.printStackTrace(errorStream); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | public void startup() |
| 154 | throws IOException { |
| 155 | setupLogging(); |
| 156 | checkForJava(MINIMUM_JAVA_VERSION, MINIMUM_JAVA_RELEASE); |
| 157 | JomicConfigurator.configure(); |
| 158 | startupCalled = true; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Check that the installed Java version conforms to the specified minimum. |
| 163 | * |
| 164 | * @throws ItemMustBeDownloadedException if installed version is to old |
| 165 | */ |
| 166 | private void checkForJava(final int minimumVersion, final int minimumRelease) { |
| 167 | String versionTag = System.getProperty("java.version"); |
| 168 | StringTokenizer tokenizer = new StringTokenizer(versionTag, "._"); |
| 169 | int version = 0; |
| 170 | int release = 0; |
| 171 | |
| 172 | if (tokenizer.hasMoreTokens()) { |
| 173 | version = Integer.parseInt(tokenizer.nextToken()); |
| 174 | } |
| 175 | if (tokenizer.hasMoreTokens()) { |
| 176 | release = Integer.parseInt(tokenizer.nextToken()); |
| 177 | } |
| 178 | if ((version < minimumVersion) || ((version == minimumVersion) && (release < minimumRelease))) { |
| 179 | String message = "At least Java " + minimumVersion + "." + minimumRelease + " must be installed"; |
| 180 | |
| 181 | throw new ItemMustBeDownloadedException(message, "http://java.com/"); |
| 182 | } |
| 183 | } |
| 184 | } |