Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 819   Methods: 32
NCLOC: 624   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultInternalKernelServices.java 45,1% 74,6% 87,5% 70,1%
coverage coverage
 1    /* $Id: DefaultInternalKernelServices.java,v 1.1 2008/03/27 05:16:24 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.control;
 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.io.UnsupportedEncodingException;
 26    import java.net.MalformedURLException;
 27    import java.net.URL;
 28    import java.net.URLConnection;
 29    import java.net.URLEncoder;
 30    import java.util.ArrayList;
 31    import java.util.List;
 32   
 33    import org.qedeq.kernel.base.module.Qedeq;
 34    import org.qedeq.kernel.base.module.Specification;
 35    import org.qedeq.kernel.bo.load.QedeqVoBuilder;
 36    import org.qedeq.kernel.bo.module.KernelProperties;
 37    import org.qedeq.kernel.bo.module.KernelServices;
 38    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
 39    import org.qedeq.kernel.common.DependencyState;
 40    import org.qedeq.kernel.common.LoadingState;
 41    import org.qedeq.kernel.common.LogicalState;
 42    import org.qedeq.kernel.common.ModuleAddress;
 43    import org.qedeq.kernel.common.ModuleDataException;
 44    import org.qedeq.kernel.common.QedeqBo;
 45    import org.qedeq.kernel.common.SourceArea;
 46    import org.qedeq.kernel.common.SourceFileException;
 47    import org.qedeq.kernel.common.SourceFileExceptionList;
 48    import org.qedeq.kernel.dto.module.QedeqVo;
 49    import org.qedeq.kernel.log.QedeqLog;
 50    import org.qedeq.kernel.trace.Trace;
 51    import org.qedeq.kernel.utility.IoUtility;
 52    import org.qedeq.kernel.utility.StringUtility;
 53    import org.qedeq.kernel.utility.TextInput;
 54   
 55   
 56    /**
 57    * This class provides access methods for loading QEDEQ modules.
 58    *
 59    * @version $Revision: 1.1 $
 60    * @author Michael Meyling
 61    */
 62    public class DefaultInternalKernelServices implements KernelServices, InternalKernelServices {
 63   
 64    /** This class. */
 65    private static final Class CLASS = DefaultInternalKernelServices.class;
 66   
 67    /** For synchronized waiting. */
 68    private final String monitor = "";
 69   
 70    /** Token for synchronization. */
 71    private final String syncToken = "";
 72   
 73    /** Number of method calls. */
 74    private volatile int processCounter = 0;
 75   
 76    /** Collection of already known QEDEQ modules. */
 77    private final KernelQedeqBoPool modules;
 78   
 79    /** Kernel properties access. */
 80    private final KernelProperties kernel;
 81   
 82    /** This instance nows how to load a module from the file system. */
 83    private final ModuleLoader loader;
 84   
 85    /** Validate module dependencies and status. */
 86    private boolean validate = true;
 87   
 88    /**
 89    * Constructor.
 90    *
 91    * @param kernel For kernel access.
 92    * @param loader For loading QEDEQ modules.
 93    */
 94  31 public DefaultInternalKernelServices(final KernelProperties kernel, final ModuleLoader loader) {
 95  31 modules = new KernelQedeqBoPool();
 96  31 this.kernel = kernel;
 97  31 this.loader = loader;
 98  31 loader.setServices(this);
 99    }
 100   
 101  31 public void startup() {
 102  31 if (kernel.getConfig().isAutoReloadLastSessionChecked()) {
 103  0 autoReloadLastSessionChecked();
 104    }
 105    }
 106   
 107    /**
 108    * If configured load all QEDEQ modules that where successfully loaded the last time.
 109    */
 110  0 public void autoReloadLastSessionChecked() {
 111  0 if (kernel.getConfig().isAutoReloadLastSessionChecked()) {
 112  0 final Thread thread = new Thread() {
 113  0 public void run() {
 114  0 final String method = "start()";
 115  0 try {
 116  0 Trace.begin(CLASS, this, method);
 117  0 QedeqLog.getInstance().logMessage(
 118    "Trying to load previously successfully loaded modules.");
 119  0 final int number = kernel.getConfig().getPreviouslyCheckedModules().length;
 120  0 if (loadPreviouslySuccessfullyLoadedModules()) {
 121  0 QedeqLog.getInstance().logMessage(
 122    "Loading of " + number + " previously successfully loaded module"
 123  0 + (number != 1 ? "s" : "") + " successfully done.");
 124    } else {
 125  0 QedeqLog.getInstance().logMessage(
 126    "Loading of all previously successfully checked modules failed. "
 127  0 + number + " module" + (number != 1 ? "s" : "") + " were tried.");
 128    }
 129    } catch (Exception e) {
 130  0 Trace.trace(CLASS, this, method, e);
 131    } finally {
 132  0 Trace.begin(CLASS, this, method);
 133    }
 134    }
 135    };
 136  0 thread.setDaemon(true);
 137  0 thread.start();
 138    }
 139    }
 140   
 141  1 public void removeAllModules() {
 142  1 do {
 143  1 synchronized (syncToken) {
 144  1 if (processCounter == 0) {
 145  1 getModules().removeAllModules();
 146  1 return;
 147    }
 148    }
 149  0 synchronized (monitor) {
 150  0 try {
 151  0 monitor.wait(10000);
 152    } catch (InterruptedException e) {
 153    }
 154    }
 155    } while (true);
 156   
 157    }
 158   
 159    /**
 160    * Remove a QEDEQ module from memory.
 161    *
 162    * @param address Remove module identified by this address.
 163    */
 164  6 public void removeModule(final ModuleAddress address) {
 165  6 final QedeqBo prop = getQedeqBo(address);
 166  6 if (prop != null) {
 167  6 removeModule(getKernelQedeqBo(address));
 168  6 if (validate) {
 169  6 modules.validateDependencies();
 170    }
 171    }
 172    }
 173   
 174    /**
 175    * Remove a QEDEQ module from memory.
 176    *
 177    * This method must block all other methods and if this method runs no other
 178    * is allowed to run
 179    *
 180    * @param prop Remove module identified by this property.
 181    */
 182  6 public void removeModule(final KernelQedeqBo prop) {
 183  6 do {
 184  6 synchronized (syncToken) {
 185  6 if (processCounter == 0) { // no other method is allowed to run
 186    // TODO mime 20080319: one could call prop.setLoadingProgressState(
 187    // LoadingState.STATE_DELETED) alone but that would
 188    // miss to inform the KernelQedeqBoPool. How do we inform the pool?
 189    // must the StateManager have a reference to it?
 190  6 prop.delete();
 191  6 getModules().removeModule(prop);
 192  6 return;
 193    }
 194    }
 195  0 synchronized (monitor) {
 196  0 try {
 197  0 this.monitor.wait(10000);
 198    } catch (InterruptedException e) {
 199    }
 200    }
 201    } while (true);
 202   
 203    }
 204   
 205    /**
 206    * Clear local file buffer and all loaded QEDEQ modules.
 207    *
 208    * @throws IOException Deletion of all buffered file was not successful.
 209    */
 210  1 public void clearLocalBuffer()
 211    throws IOException {
 212  1 removeAllModules();
 213  1 final File bufferDir = getBufferDirectory().getCanonicalFile();
 214  1 if (bufferDir.exists() && !IoUtility.deleteDir(bufferDir, false)) {
 215  0 throw new IOException("buffer could not be deleted: " + bufferDir);
 216    }
 217    }
 218   
 219    /**
 220    * Get a certain module.
 221    *
 222    * @param address Address of module.
 223    * @return Wanted module.
 224    * @throws SourceFileExceptionList Module could not be successfully loaded.
 225    */
 226  206 public QedeqBo loadModule(final ModuleAddress address) throws SourceFileExceptionList {
 227  206 final String method = "loadModule(ModuleAddress)";
 228  206 processInc();
 229  206 try {
 230  206 final KernelQedeqBo prop = getModules().getKernelQedeqBo(address);
 231  206 synchronized (prop) {
 232  206 if (prop.isLoaded()) {
 233  117 return prop;
 234    }
 235   
 236    // search in local file buffer
 237  89 try {
 238  89 loadLocalModule(prop);
 239  49 return prop;
 240    } catch (ModuleFileNotFoundException e) { // file not found
 241    // nothing to do, we will continue by creating a local copy
 242    } catch (SourceFileExceptionList e) {
 243  19 Trace.trace(CLASS, this, method, e);
 244  19 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 245    address.getURL(), e.toString());
 246  19 throw e;
 247    }
 248   
 249    // make local copy
 250  21 try {
 251  21 makeLocalCopy(prop);
 252    } catch (IOException e) {
 253  1 Trace.trace(CLASS, this, method, e);
 254  1 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 255    address.getURL(), e.toString());
 256  1 throw createSourceFileExceptionList(e);
 257    }
 258  20 try {
 259  20 loadLocalModule(prop);
 260    } catch (ModuleFileNotFoundException e) {
 261    // TODO mime 20070415: This should not occur because a local copy was
 262    // at least created a few lines above
 263  3 Trace.trace(CLASS, this, method, e);
 264  3 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 265    address.getURL(), e.getMessage());
 266    // TODO mime 20071125: refac codes
 267  3 final SourceFileException sf = new SourceFileException(1021,
 268    "Loading of module \"" + address.getURL() + "\"failed",
 269    e, (SourceArea) null, (SourceArea) null);
 270  3 final DefaultSourceFileExceptionList sfl = new DefaultSourceFileExceptionList(
 271    sf);
 272  3 throw sfl;
 273    } catch (SourceFileExceptionList e) {
 274  6 Trace.trace(CLASS, this, method, e);
 275  6 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 276    address.getURL(), e.getMessage());
 277  6 throw e;
 278    }
 279  11 return prop;
 280    }
 281    } finally {
 282  206 processDec();
 283    }
 284    }
 285   
 286    /**
 287    * Load local QEDEQ module file with loader.
 288    *
 289    * @param prop Load this.
 290    * @throws ModuleFileNotFoundException Local file not found.
 291    * @throws SourceFileExceptionList Loading failed.
 292    */
 293  187 private void loadLocalModule(final KernelQedeqBo prop)
 294    throws ModuleFileNotFoundException, SourceFileExceptionList {
 295  187 final String method = "loadLocalModule(KernelQedeqBo)";
 296  187 final File localFile = getLocalFilePath(prop.getModuleAddress());
 297  187 prop.setLoader(loader); // remember loader for this module
 298  187 final Qedeq qedeq = loader.loadLocalModule(prop, localFile);
 299  135 prop.setLoadingProgressState(LoadingState.STATE_LOADING_INTO_MEMORY);
 300  135 QedeqVo vo = null;
 301  135 try {
 302  135 vo = QedeqVoBuilder.createQedeq(prop.getModuleAddress(), qedeq);
 303    } catch (ModuleDataException e) {
 304  0 Trace.trace(CLASS, this, method, e);
 305  0 final SourceFileExceptionList xl
 306    = prop.createSourceFileExceptionList(e, qedeq);
 307  0 prop.setLoadingFailureState(LoadingState.STATE_LOADING_INTO_MEMORY_FAILED, xl);
 308  0 throw xl;
 309    }
 310  135 prop.setQedeqVo(vo);
 311  135 ModuleLabelsCreator moduleNodesCreator = new ModuleLabelsCreator(prop);
 312  135 try {
 313  135 prop.setLoaded(vo, moduleNodesCreator.createLabels());
 314    } catch (SourceFileExceptionList sfl) {
 315  5 prop.setLoadingFailureState(LoadingState.STATE_LOADING_INTO_MEMORY_FAILED, sfl);
 316  5 throw sfl;
 317    }
 318    }
 319   
 320    /**
 321    * Load specified QEDEQ module from QEDEQ parent module.
 322    *
 323    * @param parent Parent module address.
 324    * @param spec Specification for another QEDEQ module.
 325    * @return Loaded module.
 326    * @throws SourceFileExceptionList Loading failed.
 327    */
 328  162 public KernelQedeqBo loadModule(final ModuleAddress parent,
 329    final Specification spec) throws SourceFileExceptionList {
 330   
 331  162 final String method = "loadModule(Module, Specification)";
 332  162 Trace.begin(CLASS, this, method);
 333  162 Trace.trace(CLASS, this, method, spec);
 334  162 processInc();
 335  162 try {
 336  162 final ModuleAddress[] modulePaths;
 337  162 try {
 338  162 modulePaths = DefaultModuleAddress.getModulePaths(parent, spec);
 339    } catch (IOException e) {
 340  0 Trace.trace(CLASS, this, method, e);
 341  0 throw createSourceFileExceptionList(e);
 342    }
 343    // search in already loaded modules
 344  162 for (int i = 0; i < modulePaths.length; i++) {
 345  185 final KernelQedeqBo prop
 346    = getModules().getKernelQedeqBo(modulePaths[i]);
 347  185 synchronized (prop) {
 348  185 if (prop.isLoaded()) {
 349  91 return (prop);
 350    }
 351    }
 352    }
 353   
 354    // search in local file buffer
 355  71 Trace.trace(CLASS, this, method, "searching file buffer");
 356  71 for (int i = 0; i < modulePaths.length; i++) {
 357  75 try {
 358  75 final KernelQedeqBo prop
 359    = getModules().getKernelQedeqBo(modulePaths[i]);
 360  75 Trace.trace(CLASS, this, method, "synchronizing at prop=" + prop);
 361  75 synchronized (prop) {
 362  75 loadLocalModule(prop);
 363  68 return prop;
 364    }
 365    } catch (Exception e) {
 366    // file not found try loading URL later on
 367    }
 368    }
 369   
 370    // try loading url directly
 371  3 SourceFileExceptionList sfl = null;
 372   
 373    // FIXME mime 20080324: add warning if loading failed (but not for the last one)
 374  3 for (int i = 0; i < modulePaths.length; i++) {
 375  3 KernelQedeqBo prop = null;
 376  3 try {
 377  3 prop = getModules().getKernelQedeqBo(modulePaths[i]);
 378  3 synchronized (prop) {
 379  3 makeLocalCopy(prop);
 380  3 loadLocalModule(prop);
 381  2 return prop;
 382    }
 383    } catch (IOException e) {
 384  0 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 385    modulePaths[i].getURL(), e.toString());
 386  0 Trace.trace(CLASS, this, method, e);
 387  0 sfl = createSourceFileExceptionList(e);
 388    // delete unsuccessful load
 389  0 if (i + 1 < modulePaths.length) {
 390  0 prop.delete();
 391  0 getModules().removeModule(prop);
 392    }
 393    } catch (ModuleFileNotFoundException e) {
 394  1 Trace.trace(CLASS, this, method, e);
 395  1 QedeqLog.getInstance().logFailureState("Loading of module failed!",
 396    modulePaths[i].getURL(), e.getMessage());
 397  1 sfl = createSourcelFileExceptionList(e);
 398    // delete unsuccessful load
 399  1 if (i + 1 < modulePaths.length) {
 400  0 prop.delete();
 401  0 getModules().removeModule(prop);
 402    }
 403    }
 404    }
 405    // throw last loading error
 406  1 throw sfl;
 407    } finally {
 408  162 processDec();
 409  162 Trace.end(CLASS, this, method);
 410    }
 411    }
 412   
 413  31 public ModuleAddress[] getAllLoadedModules() {
 414  31 return getModules().getAllLoadedModules();
 415    }
 416   
 417   
 418  60 public void loadRequiredModules(final ModuleAddress address) throws SourceFileExceptionList {
 419  60 final KernelQedeqBo prop = (KernelQedeqBo) loadModule(address);
 420  60 LoadRequiredModules.loadRequired(prop, this);
 421    }
 422   
 423    /**
 424    * Load all previously checked QEDEQ modules.
 425    *
 426    * @return Successfully reloaded all modules.
 427    */
 428  0 public boolean loadPreviouslySuccessfullyLoadedModules() {
 429  0 processInc();
 430  0 try {
 431  0 final String[] list = kernel.getConfig().getPreviouslyCheckedModules();
 432  0 boolean errors = false;
 433  0 for (int i = 0; i < list.length; i++) {
 434  0 try {
 435  0 final ModuleAddress address = getModuleAddress(list[i]);
 436  0 loadModule(address);
 437    } catch (SourceFileExceptionList e) {
 438  0 errors = true;
 439    } catch (IOException e) {
 440  0 Trace.fatal(CLASS, this, "loadPreviouslySuccessfullyLoadedModules",
 441    "internal error: "
 442    + "saved URLs are malformed", e);
 443  0 errors = true;
 444    }
 445    }
 446  0 return !errors;
 447    } finally {
 448  0 processDec();
 449    }
 450    }
 451   
 452    // LATER mime 20070326: dynamic loading from web page directory
 453  3 public boolean loadAllModulesFromQedeq() {
 454  3 processInc();
 455  3 try {
 456  3 final String prefix = "http://qedeq.org/"
 457    + kernel.getKernelVersionDirectory() + "/";
 458  3 final String[] list = new String[] {
 459    prefix + "doc/math/qedeq_logic_v1.xml",
 460    prefix + "doc/math/qedeq_set_theory_v1.xml",
 461    prefix + "doc/project/qedeq_basic_concept.xml",
 462    prefix + "doc/project/qedeq_logic_language.xml",
 463    prefix + "sample/qedeq_sample1.xml",
 464    prefix + "sample/qedeq_error_sample_00.xml",
 465    prefix + "sample/qedeq_error_sample_01.xml",
 466    prefix + "sample/qedeq_error_sample_02.xml",
 467    prefix + "sample/qedeq_error_sample_03.xml",
 468    prefix + "sample/qedeq_error_sample_04.xml",
 469    prefix + "sample/qedeq_sample2_error.xml",
 470    prefix + "sample/qedeq_sample3_error.xml",
 471    prefix + "sample/qedeq_sample4_error.xml",
 472    prefix + "sample/qedeq_sample5_error.xml",
 473    prefix + "sample/qedeq_sample6_error.xml",
 474    prefix + "sample/qedeq_sample7_error.xml",
 475    };
 476  3 boolean errors = false;
 477  3 for (int i = 0; i < list.length; i++) {
 478  48 try {
 479  48 final ModuleAddress address = getModuleAddress(list[i]);
 480  48 loadModule(address);
 481    } catch (SourceFileExceptionList e) {
 482  15 errors = true;
 483    } catch (IOException e) {
 484  0 Trace.fatal(CLASS, this, "loadPreviouslySuccessfullyLoadedModules",
 485    "internal error: "
 486    + "saved URLs are malformed", e);
 487  0 errors = true;
 488    }
 489    }
 490  3 return !errors;
 491    } finally {
 492  3 processDec();
 493    }
 494    }
 495   
 496    /**
 497    * Make local copy of a module if it is no file address.
 498    *
 499    * @param prop Module properties.
 500    * @throws IOException if address was malformed or the file can not be found
 501    */
 502  24 private synchronized void makeLocalCopy(
 503    final KernelQedeqBo prop)
 504    throws IOException {
 505  24 final String method = "makeLocalCopy";
 506  24 Trace.begin(CLASS, this, method);
 507   
 508  24 prop.setLoadingProgressState(LoadingState.STATE_LOADING_FROM_WEB);
 509   
 510  24 if (prop.getModuleAddress().isFileAddress()) { // this is already a local file
 511  4 return;
 512    }
 513  20 FileOutputStream out = null;
 514  20 InputStream in = null;
 515  20 final File f = getLocalFilePath(prop.getModuleAddress());
 516  20 try {
 517  20 final URLConnection connection = prop.getUrl().openConnection();
 518  20 in = connection.getInputStream();
 519  19 final int maximum = connection.getContentLength();
 520  19 if (!prop.getUrl().equals(connection.getURL())) {
 521  0 throw new FileNotFoundException("\"" + prop.getUrl()
 522    + "\" was substituted by " + "\"" + connection.getURL()
 523    + "\" from server");
 524    }
 525  19 IoUtility.createNecessaryDirectories(f);
 526  19 out = new FileOutputStream(f);
 527  19 final byte[] buffer = new byte[4096];
 528  19 int bytesRead; // bytes read during one buffer read
 529  19 int position = 0; // current reading position within the whole document
 530    // continue writing
 531  ? while ((bytesRead = in.read(buffer)) != -1) {
 532  264 position += bytesRead;
 533  264 out.write(buffer, 0, bytesRead);
 534  264 int completeness = (int) (position * 100 / maximum);
 535  264 if (completeness < 0) {
 536  0 completeness = 0;
 537    }
 538  264 if (completeness > 100) {
 539  0 completeness = 100;
 540    }
 541  264 prop.setLoadingCompleteness(completeness);
 542    }
 543  19 prop.setLoadingCompleteness(100);
 544    } catch (IOException e) {
 545  1 Trace.trace(CLASS, this, method, e);
 546  1 try {
 547  1 out.close();
 548    } catch (Exception ex) {
 549    }
 550  1 try {
 551  1 f.delete();
 552    } catch (Exception ex) {
 553  0 Trace.trace(CLASS, this, method, ex);
 554    }
 555  1 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_WEB_FAILED,
 556    new DefaultSourceFileExceptionList(e));
 557  1 Trace.trace(CLASS, this, method, "Couldn't access " + prop.getUrl());
 558  1 throw e;
 559    } finally {
 560  20 try {
 561  20 out.close();
 562    } catch (Exception e) {
 563    };
 564  20 try {
 565  20 in.close();
 566    } catch (Exception e) {
 567    };
 568  20 Trace.end(CLASS, this, method);
 569    }
 570    }
 571   
 572    /**
 573    * Transform an URL address into a relative local file path. This can also be another file name.
 574    *
 575    * @param address Transform this URL.
 576    * @return Result of transformation.
 577    */
 578  25410 public final File getLocalFilePath(final ModuleAddress address) {
 579  25410 final String method = "localizeInFileSystem(URL)";
 580  25410 if (address.isFileAddress()) {
 581  25047 return new File(address.getURL().getFile());
 582    }
 583  363 final URL url = address.getURL();
 584  363 Trace.param(CLASS, this, method, "protocol", url.getProtocol());
 585  363 Trace.param(CLASS, this, method, "host", url.getHost());
 586  363 Trace.param(CLASS, this, method, "port", url.getPort());
 587  363 Trace.param(CLASS, this, method, "path", url.getPath());
 588  363 Trace.param(CLASS, this, method, "file", url.getFile());
 589  363 StringBuffer file = new StringBuffer(url.getFile());
 590  363 StringUtility.replace(file, "_", "__"); // remember all '_'
 591  363 StringUtility.replace(file, "/", "_1"); // preserve all '/'
 592  363 String encoded = file.toString(); // fallback file name
 593  363 try {
 594  363 encoded = URLEncoder.encode(file.toString(), "UTF-8");
 595    } catch (UnsupportedEncodingException e) {
 596    // should not occur
 597  0 Trace.trace(DefaultModuleAddress.class, "localizeInFileSystem(String)", e);
 598    }
 599  363 file.setLength(0);
 600  363 file.append(encoded);
 601  363 StringUtility.replace(file, "#", "##"); // escape all '#'
 602  363 StringUtility.replace(file, "_1", "#"); // from '/' into '#'
 603  363 StringUtility.replace(file, "__", "_"); // from '_' into '_'
 604  363 StringBuffer adr = new StringBuffer(url.toExternalForm());
 605  363 try {
 606  363 adr = new StringBuffer(new URL(url.getProtocol(), url.getHost(),
 607    url.getPort(), file.toString()).toExternalForm());
 608    } catch (MalformedURLException e) {
 609  0 Trace.fatal(CLASS, this, "localizeInFileSystem(URL)", "unexpected", e);
 610  0 e.printStackTrace();
 611    }
 612    // escape characters:
 613  363 StringUtility.replace(adr, "://", "_"); // before host
 614  363 StringUtility.replace(adr, ":", "_"); // before protocol
 615  363 return new File(getBufferDirectory(), adr.toString());
 616    }
 617   
 618    /**
 619    * Increment intern process counter.
 620    */
 621  371 private void processInc() {
 622  371 synchronized (syncToken) {
 623  371 this.processCounter++;
 624    }
 625    }
 626   
 627   
 628    /**
 629    * Decrement intern process counter.
 630    */
 631  371 private void processDec() {
 632  371 synchronized (syncToken) {
 633  371 this.processCounter--;
 634    }
 635    }
 636   
 637  364 public File getBufferDirectory() {
 638  364 return kernel.getConfig().getBufferDirectory();
 639    }
 640   
 641  0 public File getGenerationDirectory() {
 642  0 return kernel.getConfig().getGenerationDirectory();
 643    }
 644   
 645  531 public KernelQedeqBo getKernelQedeqBo(final ModuleAddress address) {
 646  531 return getModules().getKernelQedeqBo(address);
 647    }
 648   
 649  29 public QedeqBo getQedeqBo(final ModuleAddress address) {
 650  29 return getModules().getKernelQedeqBo(address);
 651    }
 652   
 653  35 public ModuleAddress getModuleAddress(final URL url) throws IOException {
 654  35 return new DefaultModuleAddress(url);
 655    }
 656   
 657  53 public ModuleAddress getModuleAddress(final String url) throws IOException {
 658  53 return new DefaultModuleAddress(url);
 659    }
 660   
 661  2 public ModuleAddress getModuleAddress(final File file) throws IOException {
 662  2 return new DefaultModuleAddress(file);
 663    }
 664   
 665  439 public String getSource(final ModuleAddress address) throws IOException {
 666  439 final KernelQedeqBo bo = getKernelQedeqBo(address);
 667  439 if (bo.getLoadingState().equals(LoadingState.STATE_UNDEFINED)
 668    || bo.getLoadingState().equals(LoadingState.STATE_LOADING_FROM_WEB)
 669    || bo.getLoadingState().equals(LoadingState.STATE_LOADING_FROM_WEB_FAILED)) {
 670  63 return null;
 671    // TODO mime 20080313: remove me
 672    // throw new IllegalStateException("module is not yet buffered " + address);
 673    }
 674  376 final StringBuffer buffer = new StringBuffer();
 675  376 IoUtility.loadReader(loader.getModuleReader(bo), buffer);
 676  376 return buffer.toString();
 677    }
 678   
 679  73 public boolean checkModule(final ModuleAddress address) {
 680   
 681  73 final String method = "checkModule(ModuleAddress)";
 682  73 final KernelQedeqBo prop = getKernelQedeqBo(address);
 683  73 try {
 684  73 QedeqLog.getInstance().logRequest("Check logical correctness for \""
 685    + prop.getUrl() + "\"");
 686   
 687  73 loadModule(address);
 688  66 LoadRequiredModules.loadRequired(prop, this);
 689   
 690  66 QedeqBoFormalLogicChecker.check(prop);
 691  48 QedeqLog.getInstance().logSuccessfulReply(
 692    "Check of logical correctness successful for \""
 693    + prop.getUrl() + "\"");
 694    } catch (final SourceFileExceptionList e) {
 695  25 final String msg = "Check of logical correctness failed for \""
 696    + address.getURL() + "\"";
 697  25 QedeqLog.getInstance().logFailureReply(msg, e.getMessage());
 698    } catch (final RuntimeException e) {
 699  0 final String msg = "Check of logical correctness failed for \""
 700    + address.getURL() + "\"";
 701  0 Trace.fatal(CLASS, this, method, msg, e);
 702  0 final SourceFileExceptionList xl =
 703    new DefaultSourceFileExceptionList(e);
 704    // TODO mime 20080124: every state must be able to change into
 705    // a failure state, here we only assume three cases
 706  0 if (!prop.isLoaded()) {
 707  0 if (!prop.getLoadingState().isFailure()) {
 708  0 prop.setLoadingFailureState(
 709    LoadingState.STATE_LOADING_INTO_MEMORY_FAILED, xl);
 710    }
 711  0 } else if (!prop.hasLoadedRequiredModules()) {
 712  0 if (!prop.getDependencyState().isFailure()) {
 713  0 prop.setDependencyFailureState(
 714    DependencyState.STATE_LOADING_REQUIRED_MODULES_FAILED, xl);
 715    }
 716    } else {
 717  0 if (!prop.getLogicalState().isFailure()) {
 718  0 prop.setLogicalFailureState(
 719    LogicalState.STATE_EXTERNAL_CHECKING_FAILED, xl);
 720    }
 721    }
 722  0 QedeqLog.getInstance().logFailureReply(msg, e.toString());
 723    } catch (final Throwable e) {
 724  0 final String msg = "Check of logical correctness failed for \""
 725    + prop.getUrl() + "\"";
 726  0 Trace.fatal(CLASS, this, method, msg, e);
 727  0 final SourceFileExceptionList xl =
 728    new DefaultSourceFileExceptionList(e);
 729    // TODO mime 20080124: every state must be able to change into
 730    // a failure state, here we only assume three cases
 731  0 if (!prop.isLoaded()) {
 732  0 if (!prop.getLoadingState().isFailure()) {
 733  0 prop.setLoadingFailureState(
 734    LoadingState.STATE_LOADING_INTO_MEMORY_FAILED, xl);
 735    }
 736  0 } else if (!prop.hasLoadedRequiredModules()) {
 737  0 if (!prop.getDependencyState().isFailure()) {
 738  0 prop.setDependencyFailureState(
 739    DependencyState.STATE_LOADING_REQUIRED_MODULES_FAILED, xl);
 740    }
 741    } else {
 742  0 if (!prop.getLogicalState().isFailure()) {
 743  0 prop.setLogicalFailureState(
 744    LogicalState.STATE_EXTERNAL_CHECKING_FAILED, xl);
 745    }
 746    }
 747  0 QedeqLog.getInstance().logFailureReply(msg, e.toString());
 748    }
 749  73 if (validate) {
 750  64 modules.validateDependencies();
 751    }
 752  73 return prop.isChecked();
 753    }
 754   
 755    /**
 756    * Get all loaded QEDEQ modules.
 757    *
 758    * @return All QEDEQ modules.
 759    */
 760  1067 private KernelQedeqBoPool getModules() {
 761  1067 return modules;
 762    }
 763   
 764  1 private SourceFileExceptionList createSourceFileExceptionList(final IOException e) {
 765  1 return new DefaultSourceFileExceptionList(e);
 766    }
 767   
 768  1 private SourceFileExceptionList createSourcelFileExceptionList(
 769    final ModuleFileNotFoundException e) {
 770  1 return new DefaultSourceFileExceptionList(new IOException(e.getMessage()));
 771    }
 772   
 773  13 public String[] getSourceFileExceptionList(final ModuleAddress address) {
 774  13 final List list = new ArrayList();
 775  13 final KernelQedeqBo bo = getKernelQedeqBo(address);
 776  13 final SourceFileExceptionList sfl = bo.getException();
 777  13 if (sfl != null) {
 778  13 final StringBuffer buffer = new StringBuffer();
 779  13 do {
 780  13 try {
 781  13 IoUtility.loadReader(loader.getModuleReader(bo), buffer);
 782    } catch (IOException e) {
 783  3 for (int i = 0; i < sfl.size(); i++) {
 784  3 list.add(sfl.get(i).getDescription());
 785    }
 786  3 break; // out of do while
 787    }
 788  10 final TextInput input = new TextInput(buffer);
 789  10 input.setPosition(0);
 790  10 final StringBuffer buf = new StringBuffer();
 791  10 for (int i = 0; i < sfl.size(); i++) {
 792  10 buf.setLength(0);
 793  10 final SourceFileException sf = sfl.get(i);
 794  10 buf.append(sf.getDescription());
 795  10 try {
 796  10 if (sf.getSourceArea() != null && sf.getSourceArea().getStartPosition()
 797    != null) {
 798  8 buf.append("\n");
 799  8 input.setRow(sf.getSourceArea().getStartPosition().getLine());
 800  8 buf.append(StringUtility.replace(input.getLine(), "\t", " "));
 801  8 buf.append("\n");
 802  8 final StringBuffer whitespace = StringUtility.getSpaces(
 803    sf.getSourceArea().getStartPosition().getColumn() - 1);
 804  8 buffer.append(whitespace);
 805  8 buffer.append("^");
 806    }
 807    } catch (Exception e) {
 808  0 Trace.trace(CLASS, this, "getSourceFileExceptionList(ModuleAddress)",
 809    e);
 810    }
 811  10 list.add(buf.toString());
 812    }
 813  10 break; // out of do while
 814    } while (true);
 815    }
 816  13 return (String[]) list.toArray(new String[]{});
 817    }
 818   
 819    }