OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 61 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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
 
16
import java.io.File;
17
import java.io.IOException;
18
import java.net.MalformedURLException;
19
import java.net.URL;
20
import java.net.URLClassLoader;
21
import java.util.Map;
22
import java.util.Properties;
23
import java.util.jar.JarFile;
24
 
25
/**
26
 * A module factory created from a {@link ModulePackager packaged} module.
27
 *
28
 * @author Sylvain CUAZ
29
 */
30
public final class JarModuleFactory extends ModuleFactory {
31
 
32
    private static Properties getProperties(final File jar) throws IOException {
33
        final JarFile jarFile = new JarFile(jar);
34
        return readAndClose(jarFile.getInputStream(jarFile.getEntry(ModulePackager.MODULE_PROPERTIES_PATH)));
35
    }
36
 
37
    /**
38
     * A class loader that also search dependent modules.
39
     *
40
     * @author Sylvain CUAZ
41
     */
42
    private final class ModuleClassLoader extends URLClassLoader {
43
        private final ClassLoader[] dependencies;
44
 
45
        public ModuleClassLoader(Map<String, AbstractModule> alreadyCreated) {
46
            super(getClassPath(), JarModuleFactory.class.getClassLoader());
47
            this.dependencies = new ClassLoader[getRequiredIDs().size()];
48
            int i = 0;
49
            for (final String requiredID : getRequiredIDs()) {
50
                final AbstractModule m = alreadyCreated.get(requiredID);
51
                if (m == null)
52
                    throw new IllegalStateException("Missing required module : " + requiredID);
53
                this.dependencies[i++] = m.getClass().getClassLoader();
54
            }
55
        }
56
 
57
        @Override
58
        protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
59
            try {
60
                return super.loadClass(name, resolve);
61
            } catch (ClassNotFoundException e) {
62
                // OK, we'll search our dependencies
63
            }
64
            for (final ClassLoader cl : this.dependencies) {
65
                try {
66
                    final Class<?> res = cl.loadClass(name);
67
                    if (resolve)
68
                        resolveClass(res);
69
                    return res;
70
                } catch (ClassNotFoundException e) {
71
                    // OK, we'll search the rest of our dependencies
72
                }
73
            }
74
            throw new ClassNotFoundException(name);
75
        }
76
    }
77
 
78
    private final File jar;
79
 
80
    public JarModuleFactory(final File jar) throws IOException {
81
        super(getProperties(jar));
82
        this.jar = jar;
83
    }
84
 
85
    protected final URL[] getClassPath() {
86
        try {
87
            return new URL[] { this.jar.toURI().toURL() };
88
        } catch (MalformedURLException e) {
89
            // shouldn't happen since we create the URL from an URI
90
            throw new IllegalStateException(e);
91
        }
92
    }
93
 
94
    @Override
95
    protected ClassLoader getRsrcClassLoader() {
96
        return new URLClassLoader(getClassPath()) {
97
            @Override
98
            protected Class<?> findClass(String name) throws ClassNotFoundException {
99
                throw new ClassNotFoundException(name + " is hidden from this loader, it only loads properties");
100
            }
101
        };
102
    }
103
 
104
    @Override
105
    public AbstractModule createModule(Map<String, AbstractModule> alreadyCreated) throws Exception {
106
        return createModule(new ModuleClassLoader(alreadyCreated).loadClass(this.getMainClass()));
107
    }
25 ilm 108
 
109
    @Override
110
    public String toString() {
111
        return super.toString() + " from " + this.jar;
112
    }
18 ilm 113
}