| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 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 | import net.sf.wraplog.Logger; |
| 32 | |
| 33 | /** |
| 34 | * Utility class to bootstrap Jomic without refering to package net.sf.jomic.tools (apart from |
| 35 | * StartupTools and ErrorTools) and <code>Settings</code>. |
| 36 | * |
| 37 | * @author Thomas Aglassinger |
| 38 | */ |
| 39 | public final class JomicStartup |
| 40 | { |
| 41 | private static final int MINIMUM_JAVA_RELEASE = 4; |
| 42 | private static final int MINIMUM_JAVA_VERSION = 1; |
| 43 | |
| 44 | private static JomicStartup instance; |
| 45 | |
| 46 | private ErrorTools errorTools; |
| 47 | private Logger logger; |
| 48 | private boolean startupCalled; |
| 49 | private StartupTools startupTools; |
| 50 | |
| 51 | private JomicStartup() { |
| 52 | startupTools = StartupTools.instance(); |
| 53 | errorTools = ErrorTools.instance(); |
| 54 | errorTools.setTitle(JOptionPane.ERROR_MESSAGE, "Jomic error"); |
| 55 | errorTools.setTitle(JOptionPane.WARNING_MESSAGE, "Jomic warning"); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Attempt to read logger settings from file. If the file does not exist, use internal |
| 60 | * defaults. |
| 61 | */ |
| 62 | private void setupLogging() |
| 63 | throws IOException { |
| 64 | startupTools.logDuration("setup logging"); |
| 65 | |
| 66 | File loggerSettingsFile = startupTools.getSettingsFile("jomic-logging"); |
| 67 | Properties loggerProperties = new Properties(); |
| 68 | InputStream loggerSettingsStream = null; |
| 69 | |
| 70 | // Log info as default |
| 71 | org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); |
| 72 | |
| 73 | logger = Logger.getLogger(JomicStartup.class); |
| 74 | try { |
| 75 | loggerSettingsStream = new FileInputStream(loggerSettingsFile); |
| 76 | loggerProperties.load(loggerSettingsStream); |
| 77 | JomicConfigurator.setLevel(loggerProperties); |
| 78 | } catch (FileNotFoundException error) { |
| 79 | if (logger.isInfoEnabled()) { |
| 80 | logger.info("cannot find logger settings \"" + loggerSettingsFile |
| 81 | + "\"; using internal defaults"); |
| 82 | } |
| 83 | } finally { |
| 84 | if (loggerSettingsStream != null) { |
| 85 | loggerSettingsStream.close(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public boolean isStartup() { |
| 91 | return startupCalled; |
| 92 | } |
| 93 | |
| 94 | public static synchronized JomicStartup instance() { |
| 95 | if (instance == null) { |
| 96 | instance = new JomicStartup(); |
| 97 | } |
| 98 | return instance; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Call <code>System.exit(exitCode)</code> unless the system property <code>TEST_IGNORE_EXIT</code> |
| 103 | * is <code>true</code>. |
| 104 | * |
| 105 | * @see System#exit(int) |
| 106 | * @see PropertyConstants#TEST_IGNORE_EXIT |
| 107 | */ |
| 108 | public void exit(int exitCode) { |
| 109 | if (!Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.TEST_IGNORE_EXIT)) { |
| 110 | System.exit(exitCode); |
| 111 | } else { |
| 112 | logger.info("ignoring System.exit(" + exitCode + ")"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Show error message in dialog. Unlike <code>JomicTools.showError()</code> this method does |
| 118 | * not require any initialization. It should only be used in situations where we cannot be sure |
| 119 | * that LocaleTools and other stuff is up and running. |
| 120 | * |
| 121 | * @see net.sf.jomic.common.JomicTools#showError(javax.swing.JFrame, String, Object[], |
| 122 | * Throwable) |
| 123 | */ |
| 124 | public void showError(String message, Throwable error) { |
| 125 | showError(message, error, true); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Show error message in dialog. Unlike <code>JomicTools.showError()</code> this method does |
| 130 | * not require any initialization. It should only be used in situations where we cannot be sure |
| 131 | * that LocaleTools and other stuff is up and running. |
| 132 | * |
| 133 | * @see net.sf.jomic.common.JomicTools#showError(javax.swing.JFrame, String, Object, |
| 134 | * Throwable) |
| 135 | */ |
| 136 | public void showError(String message, Throwable error, boolean modal) { |
| 137 | if (errorTools != null) { |
| 138 | errorTools.showMessage(null, JOptionPane.ERROR_MESSAGE, message, error, modal); |
| 139 | } |
| 140 | if (logger != null) { |
| 141 | logger.error(message, error); |
| 142 | } else { |
| 143 | // In case there isn't anything else to work with, use System.err. |
| 144 | PrintStream errorStream = System.err; |
| 145 | |
| 146 | errorStream.println(message); |
| 147 | error.printStackTrace(errorStream); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public void startup() |
| 152 | throws IOException { |
| 153 | setupLogging(); |
| 154 | checkForJava(MINIMUM_JAVA_VERSION, MINIMUM_JAVA_RELEASE); |
| 155 | checkForJAI(); |
| 156 | JomicConfigurator.configure(); |
| 157 | startupCalled = true; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check that Java Advanced Imaging (JAI) is installed. |
| 162 | * |
| 163 | * @throws ItemMustBeDownloadedException if JAI is missing |
| 164 | */ |
| 165 | private void checkForJAI() { |
| 166 | startupTools.logDuration("setup JAI"); |
| 167 | try { |
| 168 | getClass().getClassLoader().loadClass("javax.media.jai.JAI"); |
| 169 | if (Boolean.getBoolean(PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.TEST_NO_JAI)) { |
| 170 | throw new ClassNotFoundException("javax.media.jai.JAI"); |
| 171 | } |
| 172 | } catch (ClassNotFoundException e) { |
| 173 | String uri = "http://java.sun.com/products/java-media/jai/"; |
| 174 | String message = "Java Advanced Imaging (JAI) must be installed"; |
| 175 | |
| 176 | throw new ItemMustBeDownloadedException(message, uri); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check that the installed Java version conforms to the specified minimum. |
| 182 | * |
| 183 | * @throws ItemMustBeDownloadedException if installed version is to old |
| 184 | */ |
| 185 | private void checkForJava(final int minimumVersion, final int minimumRelease) { |
| 186 | String versionTag = System.getProperty("java.version"); |
| 187 | StringTokenizer tokenizer = new StringTokenizer(versionTag, "._"); |
| 188 | int version = 0; |
| 189 | int release = 0; |
| 190 | |
| 191 | if (tokenizer.hasMoreTokens()) { |
| 192 | version = Integer.parseInt(tokenizer.nextToken()); |
| 193 | } |
| 194 | if (tokenizer.hasMoreTokens()) { |
| 195 | release = Integer.parseInt(tokenizer.nextToken()); |
| 196 | } |
| 197 | if ((version < minimumVersion) || ((version == minimumVersion) && (release < minimumRelease))) { |
| 198 | String message = "At least Java " + minimumVersion + "." + minimumRelease + " must be installed"; |
| 199 | |
| 200 | throw new ItemMustBeDownloadedException(message, "http://java.com/"); |
| 201 | } |
| 202 | } |
| 203 | } |