OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
25 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.modules;
15
 
156 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
25 ilm 17
import org.openconcerto.erp.config.Gestion;
73 ilm 18
import org.openconcerto.erp.modules.ModulePackager.ModuleFiles;
182 ilm 19
import org.openconcerto.utils.BaseDirs;
25 ilm 20
 
21
import java.io.File;
22
import java.io.IOException;
182 ilm 23
import java.nio.file.Files;
24
import java.nio.file.StandardCopyOption;
25 ilm 25
 
26
/**
27
 * Package a module from a project and launch it. The system property {@link #MODULE_DIR_PROP} must
28
 * be defined.
29
 *
30
 * @author Sylvain CUAZ
31
 * @see ModulePackager
32
 */
33
public class ModuleLauncher {
34
    /**
35
     * Required system property, it must point to a directory with module classes in bin/, this
36
     * class will put the packaged module in the dist/ subdirectory.
37
     */
38
    public static final String MODULE_DIR_PROP = "module.dir";
39
    /**
40
     * System property to use if the module properties files isn't "module.properties" (this
41
     * property is evaluated relative to {@link #MODULE_DIR_PROP}).
42
     */
43
    public static final String MODULE_PROPS_FILE_PROP = "module.propsFile";
44
 
73 ilm 45
    static ModuleFiles createModuleFiles(String moduleDirPath) throws IOException {
46
        return new ModuleFiles(new File(moduleDirPath), System.getProperty(MODULE_PROPS_FILE_PROP));
47
    }
48
 
25 ilm 49
    public static void main(String[] args) throws IOException {
73 ilm 50
        final ModuleFiles moduleFiles = createModuleFiles(System.getProperty(MODULE_DIR_PROP));
25 ilm 51
        final boolean launchFromPackage = !Boolean.getBoolean("module.fromProject");
52
 
53
        // always update dist/ to avoid out of date problems
73 ilm 54
        final File jar = ModulePackager.createDist(moduleFiles);
25 ilm 55
 
80 ilm 56
        final PropsModuleFactory factory;
25 ilm 57
        if (launchFromPackage) {
58
            factory = new JarModuleFactory(jar);
59
        } else {
73 ilm 60
            factory = new RuntimeModuleFactory(moduleFiles.getPropertiesFile());
25 ilm 61
            try {
62
                Class.forName(factory.getMainClass());
63
            } catch (ClassNotFoundException e) {
73 ilm 64
                throw new IllegalStateException("Module classes are not in the classpath (they should be in " + moduleFiles.getClassesDir() + ")", e);
25 ilm 65
            }
66
        }
67
 
68
        Gestion.main(args);
182 ilm 69
 
70
        final ModuleManager moduleManager = ComptaPropsConfiguration.getInstanceCompta().getModuleManager();
71
        // to avoid out of date modules from OpenConcerto (e.g. when launching this module, the jars
72
        // of MODULES_DIR are used for dependencies)
73
        final File modulesDir = BaseDirs.getFolderToWrite(moduleManager.getFolders().get(0));
74
        Files.copy(jar.toPath(), modulesDir.toPath().resolve(jar.getName()), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
75
 
25 ilm 76
        // add after main() otherwise we could be overwritten by an older jar
182 ilm 77
        moduleManager.addFactoryAndStart(factory, false);
25 ilm 78
    }
79
}