Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: So Sep 2 2007 02:40:58 CEST
file stats: LOC: 630   Methods: 25
NCLOC: 488   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultModuleFactory.java 43,5% 56,4% 64% 55,2%
coverage coverage
 1    /* $Id: DefaultModuleFactory.java,v 1.4 2007/09/02 00:13:41 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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.load;
 19   
 20    import java.io.File;
 21    import java.io.FileNotFoundException;
 22    import java.io.FileOutputStream;
 23    import java.io.IOException;
 24    import java.io.InputStream;
 25    import java.net.URLConnection;
 26   
 27    import javax.xml.parsers.ParserConfigurationException;
 28   
 29    import org.qedeq.kernel.base.module.Qedeq;
 30    import org.qedeq.kernel.base.module.Specification;
 31    import org.qedeq.kernel.bo.module.Kernel;
 32    import org.qedeq.kernel.bo.module.LoadingState;
 33    import org.qedeq.kernel.bo.module.ModuleAddress;
 34    import org.qedeq.kernel.bo.module.ModuleDataException;
 35    import org.qedeq.kernel.bo.module.ModuleFactory;
 36    import org.qedeq.kernel.bo.module.ModuleProperties;
 37    import org.qedeq.kernel.bo.module.QedeqBo;
 38    import org.qedeq.kernel.common.XmlFileExceptionList;
 39    import org.qedeq.kernel.log.ModuleEventLog;
 40    import org.qedeq.kernel.log.QedeqLog;
 41    import org.qedeq.kernel.trace.Trace;
 42    import org.qedeq.kernel.utility.IoUtility;
 43    import org.qedeq.kernel.xml.handler.module.QedeqHandler;
 44    import org.qedeq.kernel.xml.mapper.ModuleDataException2XmlFileException;
 45    import org.qedeq.kernel.xml.parser.SaxDefaultHandler;
 46    import org.qedeq.kernel.xml.parser.SaxParser;
 47    import org.qedeq.kernel.xml.parser.DefaultXmlFileExceptionList;
 48    import org.xml.sax.SAXException;
 49   
 50   
 51    /**
 52    * This class provides access methods for loading QEDEQ modules.
 53    *
 54    * @version $Revision: 1.4 $
 55    * @author Michael Meyling
 56    */
 57    public class DefaultModuleFactory implements ModuleFactory {
 58   
 59    /** For synchronized waiting. */
 60    private final String monitor = new String();
 61   
 62    /** Token for synchronization. */
 63    private final String syncToken = new String();
 64   
 65    /** Number of method calls. */
 66    private int processCounter = 0;
 67   
 68    /** Collection of modules. */
 69    private final Modules modules;
 70   
 71    /** Kernel access. */
 72    private final Kernel kernel;
 73   
 74    /**
 75    * Constructor.
 76    *
 77    * @param kernel For kernel access.
 78    */
 79  2 public DefaultModuleFactory(final Kernel kernel) {
 80  2 modules = new Modules();
 81  2 this.kernel = kernel;
 82    }
 83   
 84  2 public final void startup() {
 85  2 autoReloadLastSessionChecked();
 86    }
 87   
 88    /**
 89    * If configured load all QEDEQ modules that where successfully loaded the last time.
 90    */
 91  2 public final void autoReloadLastSessionChecked() {
 92  2 if (kernel.getConfig().isAutoReloadLastSessionChecked()) {
 93  2 final Thread thread = new Thread() {
 94  2 public void run() {
 95  2 final String method = "start()";
 96  2 try {
 97  2 Trace.begin(this, method);
 98  2 QedeqLog.getInstance().logMessage(
 99    "Trying to load previously successfully loaded modules.");
 100  2 final int number = kernel.getConfig().getPreviouslyCheckedModules().length;
 101  2 if (loadPreviouslySuccessfullyLoadedModules()) {
 102  2 QedeqLog.getInstance().logMessage(
 103    "Loading of " + number + " previously successfully loaded module"
 104  2 + (number != 1 ? "s" : "") + " successfully done.");
 105    } else {
 106  0 QedeqLog.getInstance().logMessage(
 107    "Loading of all previously successfully checked modules failed. "
 108  0 + number + " module" + (number != 1 ? "s" : "") + " were loaded.");
 109    }
 110    } catch (Exception e) {
 111  0 Trace.trace(this, method, e);
 112    } finally {
 113  2 Trace.begin(this, method);
 114    }
 115    }
 116    };
 117  2 thread.setDaemon(true);
 118  2 thread.start();
 119    }
 120    }
 121   
 122  0 public final void removeAllModules() {
 123  0 do {
 124  0 synchronized (syncToken) {
 125  0 if (processCounter == 0) {
 126  0 getModules().removeAllModules();
 127  0 return;
 128    }
 129    }
 130  0 synchronized (monitor) {
 131  0 try {
 132  0 monitor.wait(10000);
 133    } catch (InterruptedException e) {
 134    }
 135    }
 136    } while (true);
 137   
 138    }
 139   
 140   
 141    /**
 142    * Remove a QEDEQ module from memory.
 143    *
 144    * @param prop Remove module identified by this property.
 145    */
 146  0 public final void removeModuleAndDependents(final ModuleProperties prop) {
 147  0 do {
 148  0 synchronized (syncToken) {
 149  0 if (processCounter == 0) {
 150  0 getModules().removeModuleAndDependents(prop);
 151  0 return;
 152    }
 153    }
 154  0 synchronized (monitor) {
 155  0 try {
 156  0 this.monitor.wait(10000);
 157    } catch (InterruptedException e) {
 158    }
 159    }
 160    } while (true);
 161   
 162    }
 163   
 164    /**
 165    * Clear local file buffer and all loaded QEDEQ modules.
 166    *
 167    * @throws IOException Deletion of all buffered file was not successful.
 168    */
 169  0 public final void clearLocalBuffer()
 170    throws IOException {
 171  0 removeAllModules();
 172  0 final File bufferDir = new File(getBufferDirectory());
 173  0 if (bufferDir.exists() && !IoUtility.deleteDir(bufferDir)) {
 174  0 throw new IOException("buffer could not be deleted");
 175    }
 176    }
 177   
 178    /**
 179    * Get a certain module.
 180    *
 181    * @param address Address of module.
 182    * @return Wanted module.
 183    * @throws XmlFileExceptionList Module could not be successfully loaded.
 184    */
 185  54 public final QedeqBo loadModule(final String address)
 186    throws XmlFileExceptionList {
 187  54 processInc();
 188  54 try {
 189  54 final ModuleAddress moduleAddress;
 190  54 try {
 191  54 moduleAddress = new DefaultModuleAddress(address);
 192    } catch (IOException e) {
 193  0 throw createXmlFileExceptionList(e);
 194    }
 195  53 return loadModule(moduleAddress);
 196    } finally {
 197  54 processDec();
 198    }
 199    }
 200   
 201   
 202  54 public final QedeqBo loadModule(
 203    final ModuleAddress moduleAddress)
 204    throws XmlFileExceptionList {
 205  54 final String method = "loadModule(ModuleAddress)";
 206  54 processInc();
 207  54 try {
 208  54 final ModuleProperties prop = getModules().getModuleProperties(moduleAddress);
 209  54 synchronized (prop) {
 210  54 if (prop.isLoaded()) {
 211  19 return prop.getModule();
 212    }
 213   
 214    // search in local file buffer
 215  35 try {
 216  35 final QedeqBo mod = loadLocalModule(moduleAddress);
 217  21 return mod;
 218    } catch (ModuleFileNotFoundException e) { // file not found
 219    // nothing to do, we will continue by creating a local copy
 220    } catch (XmlFileExceptionList e) {
 221  10 Trace.trace(this, method, e);
 222  10 throw e;
 223    }
 224   
 225    // make local copy
 226  4 try {
 227  4 makeLocalCopy(moduleAddress);
 228    } catch (IOException e) {
 229  0 Trace.trace(this, method, e);
 230  0 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 231    moduleAddress.getURL(), e.getMessage());
 232  0 throw createXmlFileExceptionList(e);
 233    }
 234  4 final QedeqBo mod;
 235  4 try {
 236  4 mod = loadLocalModule(moduleAddress);
 237    } catch (ModuleFileNotFoundException e) {
 238    // TODO mime 20070415: This should not occur because a local copy was
 239    // at least created a few lines above
 240  2 Trace.trace(this, method, e);
 241  2 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 242    moduleAddress.getURL(), e.getMessage());
 243  2 throw e.createXmlFileExceptionList();
 244    } catch (XmlFileExceptionList e) {
 245  0 Trace.trace(this, method, e);
 246  0 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 247    moduleAddress.getURL(), e.getMessage());
 248  0 throw e;
 249    }
 250  2 return mod;
 251    }
 252    } finally {
 253  54 processDec();
 254    }
 255    }
 256   
 257  0 public final QedeqBo loadModule(final QedeqBo module,
 258    final Specification spec) throws XmlFileExceptionList {
 259   
 260  0 final String method = "loadModule(Module, Specification)";
 261  0 Trace.begin(this, method);
 262  0 Trace.trace(this, method, spec);
 263  0 processInc();
 264  0 try {
 265  0 final ModuleAddress[] modulePaths;
 266  0 try {
 267  0 modulePaths = DefaultModuleAddress.getModulePaths(module, spec);
 268    } catch (IOException e) {
 269  0 Trace.trace(this, method, e);
 270  0 throw createXmlFileExceptionList(e);
 271    }
 272    // search in already loaded modules
 273  0 for (int i = 0; i < modulePaths.length; i++) {
 274  0 final ModuleProperties prop
 275    = getModules().getModuleProperties(modulePaths[i]);
 276  0 synchronized (prop) {
 277  0 if (prop.isLoaded()) {
 278  0 return (prop.getModule());
 279    }
 280    }
 281    }
 282   
 283    // search in local file buffer
 284  0 Trace.trace(this, method, "searching file buffer");
 285  0 for (int i = 0; i < modulePaths.length; i++) {
 286  0 try {
 287  0 final ModuleProperties prop
 288    = getModules().getModuleProperties(modulePaths[i]);
 289  0 Trace.trace(this, method, "synchronizing at prop=" + prop);
 290  0 synchronized (prop) {
 291  0 final QedeqBo mod = loadLocalModule(modulePaths[i]);
 292  0 return mod;
 293    }
 294    } catch (ModuleFileNotFoundException e) {
 295    // file not found try loading URL later on
 296    }
 297    }
 298   
 299    // try loading url directly
 300  0 for (int i = 0; i < modulePaths.length; i++) {
 301  0 try {
 302  0 final ModuleProperties prop
 303    = getModules().getModuleProperties(modulePaths[i]);
 304  0 synchronized (prop) {
 305  0 makeLocalCopy(modulePaths[i]);
 306  0 final QedeqBo mod = loadLocalModule(modulePaths[i]);
 307  0 return mod;
 308    }
 309    } catch (IOException e) {
 310  0 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 311    modulePaths[i].getURL(), e.getMessage());
 312  0 Trace.trace(this, method, e);
 313  0 throw createXmlFileExceptionList(e);
 314    } catch (ModuleFileNotFoundException e) {
 315  0 Trace.trace(this, method, e);
 316  0 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 317    modulePaths[i].getURL(), e.getMessage());
 318  0 throw e.createXmlFileExceptionList();
 319    }
 320    }
 321  0 throw (new ModuleFileNotFoundException("no QEDEQ module found"))
 322    .createXmlFileExceptionList();
 323    } finally {
 324  0 processDec();
 325  0 Trace.end(this, method);
 326    }
 327    }
 328   
 329  2 public final String[] getAllLoadedModules() {
 330  2 return getModules().getAllLoadedModules();
 331    }
 332   
 333    /**
 334    * Load all previously checked QEDEQ modules.
 335    *
 336    * @return Successfully reloaded all modules.
 337    */
 338  2 public final boolean loadPreviouslySuccessfullyLoadedModules() {
 339  2 processInc();
 340  2 try {
 341  2 final String[] list = kernel.getConfig().getPreviouslyCheckedModules();
 342  2 boolean errors = false;
 343  2 for (int i = 0; i < list.length; i++) {
 344  8 try {
 345  8 loadModule(list[i]);
 346    } catch (XmlFileExceptionList e) {
 347  0 errors = true;
 348    }
 349    }
 350  2 return !errors;
 351    } finally {
 352  2 processDec();
 353    }
 354    }
 355   
 356    // LATER mime 20070326: dynamic loading from web page directory
 357  0 public final boolean loadAllModulesFromQedeq() {
 358  0 processInc();
 359  0 try {
 360  0 final String prefix = "http://www.qedeq.org/"
 361    + kernel.getKernelVersionDirectory() + "/";
 362  0 final String[] list = new String[] {
 363    prefix + "doc/math/qedeq_logic_v1.xml",
 364    prefix + "doc/math/qedeq_set_theory_v1.xml",
 365    prefix + "doc/math/qedeq_sample1.xml",
 366    prefix + "doc/project/qedeq_basic_concept.xml",
 367    prefix + "doc/project/qedeq_logic_language.xml",
 368    prefix + "sample/qedeq_error_sample_00.xml",
 369    prefix + "sample/qedeq_error_sample_01.xml",
 370    prefix + "sample/qedeq_error_sample_02.xml",
 371    prefix + "sample/qedeq_error_sample_03.xml",
 372    prefix + "sample/qedeq_error_sample_04.xml",
 373    prefix + "sample/qedeq_sample2_error.xml",
 374    prefix + "sample/qedeq_sample3_error.xml",
 375    prefix + "sample/qedeq_sample4_error.xml",
 376    prefix + "sample/qedeq_sample5_error.xml",
 377    prefix + "sample/qedeq_sample6_error.xml",
 378    prefix + "sample/qedeq_sample7_error.xml",
 379    };
 380  0 boolean errors = false;
 381  0 for (int i = 0; i < list.length; i++) {
 382  0 try {
 383  0 loadModule((String) list[i]);
 384    } catch (XmlFileExceptionList e) {
 385  0 errors = true;
 386    }
 387    }
 388  0 return !errors;
 389    } finally {
 390  0 processDec();
 391    }
 392    }
 393   
 394  142 public final String getLocalName(final ModuleAddress moduleAddress) {
 395  142 if (moduleAddress.isFileAddress()) {
 396  100 return moduleAddress.getPath() + moduleAddress.getFileName();
 397    }
 398  42 return kernel.getBufferDirectory() + "/"
 399    + moduleAddress.localizeInFileSystem(moduleAddress.getAddress());
 400    }
 401   
 402   
 403    /**
 404    * Load a local QEDEQ module.
 405    *
 406    * @param moduleAddress Module address.
 407    * @return Loaded module.
 408    * @throws ModuleFileNotFoundException Local file was not found.
 409    * @throws XmlFileExceptionList Module could not be successfully loaded.
 410    */
 411  39 private final QedeqBo loadLocalModule(
 412    final ModuleAddress moduleAddress)
 413    throws ModuleFileNotFoundException, XmlFileExceptionList {
 414  39 final String method = "loadLocalModule";
 415  39 final String localName = getLocalName(moduleAddress);
 416  39 final File file = new File(localName);
 417  39 if (!file.canRead()) {
 418  6 try {
 419  6 Trace.trace(this, method, "file=" + file.getCanonicalPath());
 420    } catch (IOException e) {
 421  0 Trace.trace(this, method, e);
 422    }
 423  6 throw new ModuleFileNotFoundException("file not found");
 424    }
 425  33 ModuleProperties prop = getModules().getModuleProperties(moduleAddress);
 426  33 if (prop.getLoadingState() == LoadingState.STATE_UNDEFINED) {
 427  30 prop.setLoadingProgressState(LoadingState.STATE_LOADING_FROM_BUFFER);
 428  30 ModuleEventLog.getInstance().addModule(prop);
 429    } else {
 430  3 prop.setLoadingProgressState(LoadingState.STATE_LOADING_FROM_BUFFER);
 431  3 ModuleEventLog.getInstance().stateChanged(prop);
 432    }
 433  33 SaxDefaultHandler handler = new SaxDefaultHandler();
 434  33 QedeqHandler simple = new QedeqHandler(handler);
 435  33 handler.setBasisDocumentHandler(simple);
 436  33 Qedeq qedeq = null;
 437  33 SaxParser parser = null;
 438  33 try {
 439  33 parser = new SaxParser(handler);
 440    } catch (SAXException e) {
 441  0 Trace.trace(this, method, e);
 442  0 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED, e);
 443  0 ModuleEventLog.getInstance().stateChanged(prop);
 444  0 throw createXmlFileExceptionList(e);
 445    } catch (ParserConfigurationException e) {
 446  0 Trace.trace(this, method, e);
 447  0 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED, e);
 448  0 ModuleEventLog.getInstance().stateChanged(prop);
 449  0 throw createXmlFileExceptionList(e);
 450    }
 451  33 try {
 452  33 parser.parse(file);
 453    } catch (XmlFileExceptionList e) {
 454  6 Trace.trace(this, method, e);
 455  6 prop.setLoadingFailureState(LoadingSta