Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart5.png 74% of files have more coverage
153   640   115   1,74
36   423   0,75   88
88     1,31  
1    
 
  KernelContext       Line # 42 153 115 48,7% 0.48736462
 
  (135)
 
1    /* $Id: KernelContext.java,v 1.1 2008/07/26 07:58:30 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17   
18    package org.qedeq.kernel.bo.context;
19   
20    import java.io.File;
21    import java.io.FileOutputStream;
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.net.URL;
25   
26    import org.qedeq.base.io.IoUtility;
27    import org.qedeq.base.trace.Trace;
28    import org.qedeq.kernel.bo.QedeqBo;
29    import org.qedeq.kernel.bo.log.QedeqLog;
30    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
31    import org.qedeq.kernel.common.ModuleAddress;
32    import org.qedeq.kernel.common.SourceFileExceptionList;
33    import org.qedeq.kernel.config.QedeqConfig;
34   
35   
36    /**
37    * This class provides static access methods for basic informations.
38    *
39    * @version $Revision: 1.1 $
40    * @author Michael Meyling
41    */
 
42    public final class KernelContext implements KernelProperties, KernelState, KernelServices {
43   
44    /** Message for non started kernel. */
45    private static final String KERNEL_NOT_STARTED = "Kernel not started";
46   
47    /** Message for non initialized kernel. */
48    private static final String KERNEL_NOT_INITIALIZED = "Kernel not initialized";
49   
50    /** This class. */
51    private static final Class CLASS = KernelContext.class;
52   
53    /** Version of this kernel. */
54    private static final String KERNEL_VERSION = "0.03.11";
55   
56    /** Version dependent directory of this kernel. */
57    private static final String KERNEL_VERSION_DIRECTORY = KERNEL_VERSION.replace('.', '_');
58   
59    /** Descriptive version information of this kernel. */
60    private static final String DESCRIPTIVE_KERNEL_VERSION
61    = "Hilbert II - Version " + KERNEL_VERSION + " (mongaga) ["
62    + getBuildIdFromManifest() + "]";
63   
64    /** Maximal supported rule version of this kernel. */
65    private static final String MAXIMAL_RULE_VERSION = "1.00.00";
66   
67    /** One and only instance of this class. */
68    private static final KernelContext INSTANCE = new KernelContext();
69   
70    /** Lock file. */
71    private File lockFile;
72   
73    /** Lock file stream. */
74    private FileOutputStream lockStream;
75   
76    /** Initial kernel state. */
77    private final KernelState initialState = new KernelState() {
78   
 
79  39 toggle public void init(final KernelServices moduleServices, final QedeqConfig qedeqConfig)
80    throws IOException {
81  39 config = qedeqConfig;
82  39 checkJavaVersion();
83  39 createAllNecessaryDirectories();
84  39 checkIfApplicationIsAlreadyRunning();
85  39 KernelContext.this.services = moduleServices;
86  39 QedeqLog.getInstance().logMessage("This is "
87    + KernelContext.getInstance().getDescriptiveKernelVersion());
88  39 QedeqLog.getInstance().logMessage(" see \"http://www.qedeq.org\" for more "
89    + "information");
90  39 QedeqLog.getInstance().logMessage(" supports rules till version "
91    + KernelContext.getInstance().getMaximalRuleVersion());
92  39 currentState = initializedState;
93    }
94   
 
95  0 toggle public boolean isReady() {
96  0 return false;
97    }
98   
 
99  39 toggle public void shutdown() {
100  39 currentState = initialState;
101  39 if (lockStream == null) {
102  0 return;
103    }
104  39 IoUtility.close(lockStream);
105  39 if (lockFile != null) {
106  39 lockFile.delete();
107    }
108    }
109   
 
110  0 toggle public void startup() {
111  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
112    }
113   
 
114  0 toggle public void removeAllModules() {
115  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
116    }
117   
 
118  0 toggle public void removeModule(final ModuleAddress address) {
119  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
120    }
121   
 
122  0 toggle public void clearLocalBuffer() throws IOException {
123  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
124    }
125   
 
126  0 toggle public QedeqBo loadModule(final ModuleAddress address) {
127  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
128    }
129   
 
130  0 toggle public boolean loadAllModulesFromQedeq() {
131  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
132    }
133   
 
134  0 toggle public void loadRequiredModules(final ModuleAddress address) {
135  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
136    }
137   
 
138  0 toggle public ModuleAddress[] getAllLoadedModules() {
139  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
140    }
141   
 
142  0 toggle public QedeqBo getQedeqBo(final ModuleAddress address) {
143  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
144    }
145   
 
146  0 toggle public ModuleAddress getModuleAddress(final URL url) {
147  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
148    }
149   
 
150  0 toggle public ModuleAddress getModuleAddress(final String url) {
151  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
152    }
153   
 
154  0 toggle public ModuleAddress getModuleAddress(final File file) {
155  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
156    }
157   
 
158  0 toggle public String getSource(final ModuleAddress address) {
159  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
160    }
161   
 
162  0 toggle public boolean checkModule(final ModuleAddress address) {
163  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
164    }
165   
 
166  0 toggle public InputStream createLatex(final ModuleAddress prop, final String language,
167    final String level)
168    throws DefaultSourceFileExceptionList, IOException {
169  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
170    }
171   
 
172  0 toggle public String generateLatex(final ModuleAddress prop, final String language,
173    final String level)
174    throws DefaultSourceFileExceptionList, IOException {
175  0 throw new IllegalStateException(KERNEL_NOT_INITIALIZED);
176    }
177   
178    };
179   
180    /** Initial kernel state. */
181    private final KernelState initializedState = new KernelState() {
182   
 
183  0 toggle public void init(final KernelServices moduleServices, final QedeqConfig qedeqConfig)
184    throws IOException {
185  0 throw new IllegalStateException("Kernel is already initialized");
186    }
187   
 
188  0 toggle public boolean isReady() {
189  0 return false;
190    }
191   
 
192  39 toggle public void shutdown() {
193  39 QedeqLog.getInstance().logMessage("QEDEQ Kernel closed.");
194  39 KernelContext.this.services = null;
195  39 initialState.shutdown();
196    }
197   
 
198  39 toggle public void startup() {
199  39 services.startup();
200  39 currentState = readyState;
201  39 QedeqLog.getInstance().logMessage("QEDEQ kernel opened.");
202    }
203   
 
204  0 toggle public void removeAllModules() {
205  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
206    }
207   
 
208  0 toggle public void removeModule(final ModuleAddress address) {
209  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
210    }
211   
 
212  0 toggle public void clearLocalBuffer() throws IOException {
213  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
214    }
215   
 
216  0 toggle public QedeqBo loadModule(final ModuleAddress address) {
217  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
218    }
219   
 
220  0 toggle public boolean loadAllModulesFromQedeq() {
221  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
222    }
223   
 
224  0 toggle public void loadRequiredModules(final ModuleAddress address) {
225  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
226    }
227   
 
228  0 toggle public ModuleAddress[] getAllLoadedModules() {
229  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
230    }
231   
 
232  0 toggle public QedeqBo getQedeqBo(final ModuleAddress address) {
233  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
234    }
235   
 
236  0 toggle public ModuleAddress getModuleAddress(final URL url) {
237  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
238    }
239   
 
240  0 toggle public ModuleAddress getModuleAddress(final String url) {
241  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
242    }
243   
 
244  0 toggle public ModuleAddress getModuleAddress(final File file) {
245  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
246    }
247   
 
248  0 toggle public String getSource(final ModuleAddress address) {
249  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
250    }
251   
 
252  0 toggle public boolean checkModule(final ModuleAddress address) {
253  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
254    }
255   
 
256  0 toggle public InputStream createLatex(final ModuleAddress prop, final String language,
257    final String level)
258    throws DefaultSourceFileExceptionList, IOException {
259  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
260    }
261   
 
262  0 toggle public String generateLatex(final ModuleAddress prop, final String language,
263    final String level)
264    throws DefaultSourceFileExceptionList, IOException {
265  0 throw new IllegalStateException(KERNEL_NOT_STARTED);
266    }
267   
268    };
269   
270    /** State for ready kernel. */
271    private final KernelState readyState = new KernelState() {
272   
 
273  0 toggle public void init(final KernelServices moduleServices, final QedeqConfig qedeqConfig)
274    throws IOException {
275    // we are already ready
276    }
277   
 
278  0 toggle public boolean isReady() {
279  0 return false;
280    }
281   
 
282  39 toggle public void shutdown() {
283  39 try {
284  39 final ModuleAddress[] addresses = services.getAllLoadedModules();
285  39 final String[] buffer = new String[addresses.length];
286  156 for (int i = 0; i < addresses.length; i++) {
287  117 buffer[i] = addresses[i].toString();
288    }
289  39 config.setLoadedModules(buffer);
290  39 config.store();
291  39 QedeqLog.getInstance().logMessage("Current config file successfully saved.");
292    } catch (IOException e) {
293  0 Trace.trace(CLASS, this, "shutdown()", e);
294  0 QedeqLog.getInstance().logMessage("Saving current config file failed.");
295    }
296  39 initializedState.shutdown();
297    }
298   
 
299  0 toggle public void startup() {
300  0 throw new IllegalStateException("Kernel is already initialized");
301    }
302   
 
303  0 toggle public void removeAllModules() {
304  0 services.removeAllModules();
305    }
306   
 
307  0 toggle public void removeModule(final ModuleAddress address) {
308  0 services.removeModule(address);
309    }
310   
 
311  0 toggle public void clearLocalBuffer() throws IOException {
312  0 services.clearLocalBuffer();
313    }
314   
 
315  37 toggle public QedeqBo loadModule(final ModuleAddress address) {
316  37 return services.loadModule(address);
317    }
318   
 
319  0 toggle public boolean loadAllModulesFromQedeq() {
320  0 return services.loadAllModulesFromQedeq();
321    }
322   
 
323  63 toggle public void loadRequiredModules(final ModuleAddress address)
324    throws SourceFileExceptionList {
325  63 services.loadRequiredModules(address);
326    }
327   
 
328  0 toggle public ModuleAddress[] getAllLoadedModules() {
329  0 return services.getAllLoadedModules();
330    }
331   
 
332  34 toggle public QedeqBo getQedeqBo(final ModuleAddress address) {
333  34 return services.getQedeqBo(address);
334    }
335   
 
336  51 toggle public ModuleAddress getModuleAddress(final URL url) throws IOException {
337  51 return services.getModuleAddress(url);
338    }
339   
 
340  0 toggle public ModuleAddress getModuleAddress(final String url) throws IOException {
341  0 return services.getModuleAddress(url);
342    }
343   
 
344  2 toggle public ModuleAddress getModuleAddress(final File file) throws IOException {
345  2 return services.getModuleAddress(file);
346    }
347   
 
348  2 toggle public String getSource(final ModuleAddress address) throws IOException {
349  2 return services.getSource(address);
350    }
351   
 
352  74 toggle public boolean checkModule(final ModuleAddress address) {
353  74 return services.checkModule(address);
354    }
355   
 
356  0 toggle public InputStream createLatex(final ModuleAddress address, final String language,
357    final String level)
358    throws DefaultSourceFileExceptionList, IOException {
359  0 return services.createLatex(address, language, level);
360    }
361   
 
362  0 toggle public String generateLatex(final ModuleAddress address, final String language,
363    final String level)
364    throws DefaultSourceFileExceptionList, IOException {
365  0 return services.generateLatex(address, language, level);
366    }
367   
368    };
369   
370    /** Initial kernel state. */
371    private KernelState currentState = initialState;
372   
373    /** For config access. */
374    private QedeqConfig config;
375   
376    /** This object can service QEDEQ modules. */
377    private KernelServices services;
378   
379    /**
380    * Constructor.
381    */
 
382  12 toggle private KernelContext() {
383    // nothing to do
384    }
385   
386    /**
387    * Get instance of kernel context.
388    *
389    * @return Singleton, which is responsible for the kernel access.
390    */
 
391  997 toggle public static final KernelContext getInstance() {
392  997 return INSTANCE;
393    }
394   
395    /**
396    * Get build information from JAR manifest file. Is also non empty string if no manifest
397    * information is available.
398    *
399    * @return Implementation-Version.
400    */
 
401  12 toggle public static String getBuildIdFromManifest() {
402  12 String build = KernelContext.class.getPackage().getImplementationVersion();
403  12 if (build == null) {
404  12 build = "no regular build";
405    }
406  12 return build;
407    }
408   
409    /**
410    * Get version of this kernel.
411    *
412    * @return Kernel version.
413    */
 
414  0 toggle public final String getKernelVersion() {
415  0 return KERNEL_VERSION;
416    }
417   
418    /**
419    * Get relative version directory of this kernel.
420    *
421    * @return Version sub directory.
422    */
 
423  663 toggle public final String getKernelVersionDirectory() {
424  663 return KERNEL_VERSION_DIRECTORY;
425    }
426   
427    /**
428    * Get descriptive version information of this kernel.
429    *
430    * @return Version Information.
431    */
 
432  39 toggle public final String getDescriptiveKernelVersion() {
433  39 return DESCRIPTIVE_KERNEL_VERSION;
434    }
435   
436    /**
437    * Get maximal supported rule version of this kernel.
438    *
439    * @return Maximal supported rule version.
440    */
 
441  39 toggle public final String getMaximalRuleVersion() {
442  39 return MAXIMAL_RULE_VERSION;
443    }
444   
445    /**
446    * Is a given rule version supported?
447    *
448    * @param ruleVersion Check this one.
449    * @return Is the given rule version supported?
450    */
 
451  0 toggle public final boolean isRuleVersionSupported(final String ruleVersion) {
452  0 return MAXIMAL_RULE_VERSION.equals(ruleVersion);
453    }
454   
 
455  113 toggle public QedeqConfig getConfig() {
456  113 return config;
457    }
458   
 
459  39 toggle public void init(final KernelServices moduleServices, final QedeqConfig qedeqConfig)
460    throws IOException {
461  39 currentState.init(moduleServices, qedeqConfig);
462    }
463   
 
464  0 toggle public boolean isReady() {
465  0 return currentState.isReady();
466    }
467   
 
468  39 toggle public void shutdown() {
469  39 currentState.shutdown();
470    }
471   
 
472  39 toggle public void startup() {
473  39 currentState.startup();
474    }
475   
 
476  0 toggle public void removeAllModules() {
477  0 currentState.removeAllModules();
478    }
479   
 
480  0 toggle public void removeModule(final ModuleAddress address) {
481  0 currentState.removeModule(address);
482    }
483   
 
484  0 toggle public void clearLocalBuffer() throws IOException {
485  0 currentState.clearLocalBuffer();
486    }
487   
 
488  37 toggle public QedeqBo loadModule(final ModuleAddress address) {
489  37 return currentState.loadModule(address);
490    }
491   
 
492  0 toggle public boolean loadAllModulesFromQedeq() {
493  0 return currentState.loadAllModulesFromQedeq();
494    }
495   
 
496  63 toggle public void loadRequiredModules(final ModuleAddress address) throws SourceFileExceptionList {
497  63 currentState.loadRequiredModules(address);
498    }
499   
 
500  0 toggle public ModuleAddress[] getAllLoadedModules() {
501  0 return currentState.getAllLoadedModules();
502    }
503   
 
504  34 toggle public QedeqBo getQedeqBo(final ModuleAddress address) {
505  34 return currentState.getQedeqBo(address);
506    }
507   
 
508  51 toggle public ModuleAddress getModuleAddress(final URL url) throws IOException {
509  51 return currentState.getModuleAddress(url);
510    }
511   
 
512  0 toggle public ModuleAddress getModuleAddress(final String url) throws IOException {
513  0 return currentState.getModuleAddress(url);
514    }
515   
 
516  2 toggle public ModuleAddress getModuleAddress(final File file) throws IOException {
517  2 return currentState.getModuleAddress(file);
518    }
519   
 
520  2 toggle public String getSource(final ModuleAddress address) throws IOException {
521  2 return currentState.getSource(address);
522    }
523   
 
524  74 toggle public boolean checkModule(final ModuleAddress address) {
525  74 return currentState.checkModule(address);
526    }
527   
 
528  0 toggle public InputStream createLatex(final ModuleAddress address, final String language,
529    final String level) throws DefaultSourceFileExceptionList,
530    IOException {
531  0 return currentState.createLatex(address, language, level);
532    }
533   
 
534  0 toggle public String generateLatex(final ModuleAddress address, final String language,
535    final String level) throws DefaultSourceFileExceptionList,
536    IOException {
537  0 return currentState.generateLatex(address, language, level);
538    }
539    /**
540    * Check java version. We want to be shure that the kernel is run at least with java 1.4.2
541    *
542    * @throws IOException Application is running below java 1.4.2.
543    */
 
544  39 toggle private void checkJavaVersion() throws IOException {
545  39 final int[] versions = IoUtility.getJavaVersion();
546  39 if (versions == null) {
547  0 Trace.fatal(CLASS, this, "checkJavaVersion", "running java version unknown", null);
548    // we try to continue
549  0 return;
550    }
551  39 final StringBuffer version = new StringBuffer();
552  156 for (int i = 0; i < versions.length; i++) {
553  117 if (i > 0) {
554  78 version.append(".");
555    }
556  117 version.append(versions[i]);
557    }
558  39 Trace.paramInfo(CLASS, this, "checkJavaVersion", "version", version);
559    // >= 1
560  39 if (versions.length < 1 || versions[0] < 1) {
561  0 throw new IOException("This application requires at least Java 1.4.2 but we got "
562    + version);
563    }
564  39 if (versions[0] == 1) { // further checking
565    // >= 1.4
566  39 if (versions.length < 2 || versions[1] < 4) {
567  0 throw new IOException("This application requires at least Java 1.4.2 but we got "
568    + version);
569    }
570  39 if (versions[1] == 4) { // further checking
571    // >=1.4.2
572  39 if (versions.length < 3 || versions[2] < 2) {
573  0 throw new IOException(
574    "This application requires at least Java 1.4.2 but we got "
575    + version);
576    }
577    }
578    }
579    }
580   
581    /**
582    * Create all necessary directories for the kernel.
583    *
584    * @throws IOException Creation was not possible.
585    */
 
586  39 toggle void createAllNecessaryDirectories() throws IOException {
587    // log directory
588  39 final File logFile = new File(config.getBasisDirectory(), config.getLogFile());
589  39 final File logDir = logFile.getParentFile();
590  39 if (!logDir.exists() && !logDir.mkdirs()) {
591  0 throw new IOException("can't create directory: " + logDir.getAbsolutePath());
592    }
593    // buffer directory
594  39 final File bufferDir = config.getBufferDirectory();
595  39 if (!bufferDir.exists() && !bufferDir.mkdirs()) {
596  0 throw new IOException("can't create directory: " + bufferDir.getAbsolutePath());
597    }
598    // generation directory
599  39 final File generationDir = config.getGenerationDirectory();
600  39 if (!generationDir.exists() && !generationDir.mkdirs()) {
601  0 throw new IOException("can't create directory: " + generationDir.getAbsolutePath());
602    }
603    }
604   
605   
606    /**
607    * Checks if the application is already running. To check that we create a file in the
608    * buffer directory, open a stream and write something into it. The stream is not closed
609    * until kernel shutdown.
610    *
611    * @throws IOException Application is already running.
612    */
 
613  39 toggle private void checkIfApplicationIsAlreadyRunning()
614    throws IOException {
615  39 lockFile = new File(config.getBufferDirectory(), "qedeq_lock.lck");
616  39 final String osName = System.getProperty("os.name");
617  39 if (osName.startsWith("Windows")) {
618  39 if ((lockFile.exists() && !lockFile.delete())) {
619  0 throw new IOException("It seems the application is already running.\n"
620    + "At least the file \"" + lockFile.getAbsolutePath()
621    + "\" couldn't be deleted.");
622    }
623    } else {
624  0 if ((lockFile.exists())) {
625  0 throw new IOException("It seems the application is already running or crashed."
626    + "\nAt least the file \"" + lockFile.getAbsolutePath()
627    + "\" must be manually deleted!");
628    }
629    }
630  39 try {
631  39 lockStream = new FileOutputStream(lockFile);
632  39 lockStream.write("LOCKED".getBytes());
633  39 lockStream.flush();
634    } catch (IOException e) {
635  0 throw new IOException("It seems the application is already running.\n"
636    + "At least locking the file \"" + lockFile.getAbsolutePath() + "\" failed.");
637    }
638    }
639   
640    }