EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.common]

COVERAGE SUMMARY FOR SOURCE FILE [JomicStartup.java]

nameclass, %method, %block, %line, %
JomicStartup.java100% (1/1)80%  (8/10)61%  (160/262)65%  (43.8/67)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JomicStartup100% (1/1)80%  (8/10)61%  (160/262)65%  (43.8/67)
showError (String, Throwable): void 0%   (0/1)0%   (0/6)0%   (0/2)
showError (String, Throwable, boolean): void 0%   (0/1)0%   (0/29)0%   (0/8)
checkForJAI (): void 100% (1/1)48%  (15/31)50%  (5/10)
checkForJava (int, int): void 100% (1/1)64%  (37/58)83%  (10/12)
setupLogging (): void 100% (1/1)64%  (48/75)65%  (9.8/15)
exit (int): void 100% (1/1)85%  (17/20)75%  (3/4)
JomicStartup (): void 100% (1/1)100% (19/19)100% (6/6)
instance (): JomicStartup 100% (1/1)100% (8/8)100% (3/3)
isStartup (): boolean 100% (1/1)100% (3/3)100% (1/1)
startup (): void 100% (1/1)100% (13/13)100% (6/6)

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/>.
16package net.sf.jomic.common;
17 
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.FileNotFoundException;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.PrintStream;
24import java.util.Properties;
25import java.util.StringTokenizer;
26 
27import javax.swing.JOptionPane;
28 
29import net.sf.jomic.tools.ErrorTools;
30import net.sf.jomic.tools.ItemMustBeDownloadedException;
31import 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 */
39public 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}

[all classes][net.sf.jomic.common]
EMMA 2.0.4217 (C) Vladimir Roubtsov