| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.bo.service.dependency; |
| 17 | |
| 18 | import org.qedeq.base.io.Parameters; |
| 19 | import org.qedeq.base.trace.Trace; |
| 20 | import org.qedeq.kernel.bo.module.InternalModuleServiceCall; |
| 21 | import org.qedeq.kernel.bo.module.KernelModuleReferenceList; |
| 22 | import org.qedeq.kernel.bo.module.KernelQedeqBo; |
| 23 | import org.qedeq.kernel.bo.service.basis.ControlVisitor; |
| 24 | import org.qedeq.kernel.bo.service.basis.ModuleServicePluginExecutor; |
| 25 | import org.qedeq.kernel.se.base.module.Import; |
| 26 | import org.qedeq.kernel.se.base.module.ImportList; |
| 27 | import org.qedeq.kernel.se.common.ModuleContext; |
| 28 | import org.qedeq.kernel.se.common.ModuleDataException; |
| 29 | import org.qedeq.kernel.se.common.ModuleService; |
| 30 | import org.qedeq.kernel.se.common.SourceFileExceptionList; |
| 31 | import org.qedeq.kernel.se.state.LoadingImportsState; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Load all directly imported QEDEQ modules. |
| 36 | * |
| 37 | * @author Michael Meyling |
| 38 | */ |
| 39 | public final class LoadDirectlyRequiredModulesExecutor extends ControlVisitor |
| 40 | implements ModuleServicePluginExecutor { |
| 41 | |
| 42 | /** This class. */ |
| 43 | private static final Class CLASS = LoadDirectlyRequiredModulesExecutor.class; |
| 44 | |
| 45 | /** List of required QEDEQ modules. */ |
| 46 | private KernelModuleReferenceList required; |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * @param plugin Plugin we work for. |
| 52 | * @param prop Internal QedeqBo. |
| 53 | * @param parameter Currently ignored. |
| 54 | */ |
| 55 | public LoadDirectlyRequiredModulesExecutor(final ModuleService plugin, final KernelQedeqBo prop, |
| 56 | final Parameters parameter) { |
| 57 | super(plugin, prop); |
| 58 | } |
| 59 | |
| 60 | public Object executePlugin(final InternalModuleServiceCall call, final Object data) { |
| 61 | if (getKernelQedeqBo().hasLoadedImports()) { |
| 62 | return getKernelQedeqBo().getRequiredModules(); |
| 63 | } |
| 64 | this.required = new KernelModuleReferenceList(); |
| 65 | try { |
| 66 | getKernelQedeqBo().setLoadingImportsProgressState(LoadingImportsState.STATE_LOADING_IMPORTS); |
| 67 | super.traverse(call.getInternalServiceProcess()); |
| 68 | getKernelQedeqBo().setLoadedImports(required); |
| 69 | } catch (final SourceFileExceptionList sfl) { |
| 70 | getKernelQedeqBo().setLoadingImportsFailureState( |
| 71 | LoadingImportsState.STATE_LOADING_IMPORTS_FAILED, sfl); |
| 72 | } |
| 73 | return required; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get list of directly referenced modules. |
| 78 | * |
| 79 | * @return List of directly required modules. |
| 80 | */ |
| 81 | public KernelModuleReferenceList getRequired() { |
| 82 | return required; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Visit import. Loads referenced QEDEQ module and saves reference. |
| 87 | * |
| 88 | * @param imp Begin visit of this element. |
| 89 | * @throws ModuleDataException Major problem occurred. |
| 90 | */ |
| 91 | public void visitEnter(final Import imp) throws ModuleDataException { |
| 92 | final ModuleContext context = getCurrentContext(); |
| 93 | context.setLocationWithinModule(context.getLocationWithinModule() + ".getLabel()"); |
| 94 | try { |
| 95 | final KernelQedeqBo propNew = getKernelQedeqBo().getKernelServices().loadKernelModule( |
| 96 | getInternalServiceCall().getInternalServiceProcess(), |
| 97 | getKernelQedeqBo().getModuleAddress(), imp.getSpecification()); |
| 98 | getRequired().addLabelUnique(context, imp.getLabel(), propNew); |
| 99 | 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 | } |