OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13 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.jopendocument.link;
15
 
16
import org.openconcerto.utils.CollectionUtils;
17
 
18
import java.io.File;
19
import java.io.FileNotFoundException;
20
import java.io.IOException;
21
import java.net.MalformedURLException;
22
import java.net.URL;
23
import java.net.URLClassLoader;
24
import java.util.List;
25
import java.util.Map;
26
import java.util.WeakHashMap;
27
 
28
/**
29
 * Connexion avec une instance d'OO
30
 *
31
 * @author Administrateur
32
 */
33
public abstract class OOConnexion {
34
 
35
    // weak to let go OOInstallation instances
36
    private static final Map<OOInstallation, ClassLoader> loaders = new WeakHashMap<OOInstallation, ClassLoader>(2);
37
    // TODO use OOInstallation.getVersion()
38
    // for now only one version (but also works with OO2)
39
    private static final String OO_VERSION = "3";
40
    static {
41
        // needed to access .class inside a jar inside a jar.
42
        org.openconcerto.utils.protocol.Helper.register();
43
    }
44
 
45
    static private final URL[] getURLs(final OOInstallation ooInstall) {
46
        final List<URL> res = ooInstall.getURLs(CollectionUtils.createSet("ridl.jar", "jurt.jar", "juh.jar", "unoil.jar"));
47
 
48
        final String jarName = "OO" + OO_VERSION + "-link.jar";
49
        final URL resource = OOConnexion.class.getResource(jarName);
50
        if (resource == null)
51
            // Did you run ant in the OO3Link project (or in ours) ?
52
            throw new IllegalStateException("Missing " + jarName);
53
        res.add(org.openconcerto.utils.protocol.Helper.toJarJar(resource));
54
 
55
        return res.toArray(new URL[res.size()]);
56
    }
57
 
58
    static private final ClassLoader getLoader(final OOInstallation ooInstall) {
59
        ClassLoader res = loaders.get(ooInstall);
60
        if (res == null) {
61
            // pass our classloader otherwise the system class loader will be used. This won't work
62
            // in webstart since the classpath is loaded by JNLPClassLoader, thus a class loaded by
63
            // res couldn't refer to the classpath (e.g. this class) but only to the java library.
64
            res = new URLClassLoader(getURLs(ooInstall), OOConnexion.class.getClassLoader());
65
            loaders.put(ooInstall, res);
66
        }
67
        return res;
68
    }
69
 
70
    /**
71
     * Return a connection to the default OpenOffice installation.
72
     *
73
     * @return a connection to the default OpenOffice or <code>null</code> if none is found.
74
     * @throws IllegalStateException if an error occurs while searching.
75
     */
76
    static public OOConnexion create() throws IllegalStateException {
77
        final OOInstallation ooInstall;
78
        try {
79
            ooInstall = OOInstallation.getInstance();
80
        } catch (IOException e) {
81
            throw new IllegalStateException("Couldn't find default OO installation", e);
82
        }
83
        return create(ooInstall);
84
    }
85
 
86
    static public OOConnexion create(final OOInstallation ooInstall) throws IllegalStateException {
87
        if (ooInstall == null)
88
            return null;
89
        try {
90
            final Class<?> c = getLoader(ooInstall).loadClass("org.jopendocument.link" + OO_VERSION + ".OOConnexion");
91
            return (OOConnexion) c.getConstructor(OOInstallation.class).newInstance(ooInstall);
92
        } catch (Exception e) {
93
            throw new IllegalStateException("Couldn't create OOCOnnexion", e);
94
        }
95
    }
96
 
97
    protected abstract Component loadDocumentFromURLAsync(final String url, final boolean hidden);
98
 
99
    /**
100
     * Load a document in OpenOffice.
101
     *
102
     * @param f the file to load.
103
     * @param hidden <code>true</code> if no frame should be visible.
104
     * @return the new component.
105
     * @throws IOException if an error occurs.
106
     */
107
    public final Component loadDocument(final File f, final boolean hidden) throws IOException {
108
        if (!f.exists())
109
            throw new FileNotFoundException(f.getAbsolutePath());
110
 
111
        return loadDocumentFromURLAsync(convertToUrl(f.getAbsolutePath()), hidden);
112
    }
113
 
114
    protected abstract String convertToUrl(String path) throws MalformedURLException;
115
 
116
    public abstract void closeConnexion();
117
 
118
    /**
119
     * Whether the bridge is closed.
120
     *
121
     * @return <code>true</code> if {@link #closeConnexion()} was called or OpenOffice is now longer
122
     *         running.
123
     */
124
    public abstract boolean isClosed();
125
}