LoadDirectlyRequiredModulesExecutor.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.service.dependency;
017 
018 import org.qedeq.base.io.Parameters;
019 import org.qedeq.base.trace.Trace;
020 import org.qedeq.kernel.bo.module.InternalModuleServiceCall;
021 import org.qedeq.kernel.bo.module.KernelModuleReferenceList;
022 import org.qedeq.kernel.bo.module.KernelQedeqBo;
023 import org.qedeq.kernel.bo.service.basis.ControlVisitor;
024 import org.qedeq.kernel.bo.service.basis.ModuleServicePluginExecutor;
025 import org.qedeq.kernel.se.base.module.Import;
026 import org.qedeq.kernel.se.base.module.ImportList;
027 import org.qedeq.kernel.se.common.ModuleContext;
028 import org.qedeq.kernel.se.common.ModuleDataException;
029 import org.qedeq.kernel.se.common.ModuleService;
030 import org.qedeq.kernel.se.common.SourceFileExceptionList;
031 import org.qedeq.kernel.se.state.LoadingImportsState;
032 
033 
034 /**
035  * Load all directly imported QEDEQ modules.
036  *
037  @author  Michael Meyling
038  */
039 public final class LoadDirectlyRequiredModulesExecutor extends ControlVisitor
040         implements ModuleServicePluginExecutor {
041 
042     /** This class. */
043     private static final Class CLASS = LoadDirectlyRequiredModulesExecutor.class;
044 
045     /** List of required QEDEQ modules. */
046     private KernelModuleReferenceList required;
047 
048     /**
049      * Constructor.
050      *
051      @param   plugin      Plugin we work for.
052      @param   prop        Internal QedeqBo.
053      @param   parameter   Currently ignored.
054      */
055     public LoadDirectlyRequiredModulesExecutor(final ModuleService plugin, final KernelQedeqBo prop,
056             final Parameters parameter) {
057         super(plugin, prop);
058     }
059 
060     public Object executePlugin(final InternalModuleServiceCall call, final Object data) {
061         if (getKernelQedeqBo().hasLoadedImports()) {
062             return getKernelQedeqBo().getRequiredModules();
063         }
064         this.required = new KernelModuleReferenceList();
065         try {
066             getKernelQedeqBo().setLoadingImportsProgressState(LoadingImportsState.STATE_LOADING_IMPORTS);
067             super.traverse(call.getInternalServiceProcess());
068             getKernelQedeqBo().setLoadedImports(required);
069         catch (final SourceFileExceptionList sfl) {
070             getKernelQedeqBo().setLoadingImportsFailureState(
071                 LoadingImportsState.STATE_LOADING_IMPORTS_FAILED, sfl);
072         }
073         return required;
074     }
075 
076     /**
077      * Get list of directly referenced modules.
078      *
079      @return  List of directly required modules.
080      */
081     public KernelModuleReferenceList getRequired() {
082         return required;
083     }
084 
085     /**
086      * Visit import. Loads referenced QEDEQ module and saves reference.
087      *
088      @param   imp                 Begin visit of this element.
089      @throws  ModuleDataException Major problem occurred.
090      */
091     public void visitEnter(final Import impthrows ModuleDataException {
092         final ModuleContext context = getCurrentContext();
093         context.setLocationWithinModule(context.getLocationWithinModule() ".getLabel()");
094         try {
095             final KernelQedeqBo propNew = getKernelQedeqBo().getKernelServices().loadKernelModule(
096                 getInternalServiceCall().getInternalServiceProcess(),
097                 getKernelQedeqBo().getModuleAddress(), imp.getSpecification());
098             getRequired().addLabelUnique(context, imp.getLabel(), propNew);
099             Trace.param(CLASS, "visitEnter(Import)""adding context", getCurrentContext());
100         catch (SourceFileExceptionList e) {
101             final ModuleDataException me = new LoadRequiredModuleException(e.get(0).getErrorCode(),
102                 "import of module with label \"" + imp.getLabel() "\" failed: "
103                 + e.get(0).getMessage(), context);
104             // TODO mime 20080227: also include reference area in sf creation?
105             addError(me);
106             Trace.trace(CLASS, this, "visitEnter(Import)", e);
107         }
108     }
109 
110     /**
111      * End of visit of import list. Blocks further visits.
112      *
113      @param   imports             This visit has just ended.
114      */
115     public void visitLeave(final ImportList imports) {
116         setBlocked(true)// block further traverse
117     }
118 
119 }