OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 144 | Go to most recent revision | Details | Compare with Previous | 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;
73 ilm 20
import java.io.FilePermission;
13 ilm 21
import java.io.IOException;
22
import java.net.MalformedURLException;
73 ilm 23
import java.net.SocketPermission;
13 ilm 24
import java.net.URL;
25
import java.net.URLClassLoader;
73 ilm 26
import java.security.CodeSource;
27
import java.security.PermissionCollection;
13 ilm 28
import java.util.List;
29
import java.util.Map;
73 ilm 30
import java.util.PropertyPermission;
13 ilm 31
import java.util.WeakHashMap;
32
 
33
/**
34
 * Connexion avec une instance d'OO
35
 *
36
 * @author Administrateur
37
 */
38
public abstract class OOConnexion {
39
 
40
    // weak to let go OOInstallation instances
41
    private static final Map<OOInstallation, ClassLoader> loaders = new WeakHashMap<OOInstallation, ClassLoader>(2);
42
    // TODO use OOInstallation.getVersion()
43
    // for now only one version (but also works with OO2)
44
    private static final String OO_VERSION = "3";
73 ilm 45
 
46
    protected static final int PORT = 8100;
47
 
13 ilm 48
    static {
49
        // needed to access .class inside a jar inside a jar.
50
        org.openconcerto.utils.protocol.Helper.register();
51
    }
52
 
53
    static private final URL[] getURLs(final OOInstallation ooInstall) {
54
        final List<URL> res = ooInstall.getURLs(CollectionUtils.createSet("ridl.jar", "jurt.jar", "juh.jar", "unoil.jar"));
55
 
56
        final String jarName = "OO" + OO_VERSION + "-link.jar";
57
        final URL resource = OOConnexion.class.getResource(jarName);
58
        if (resource == null)
59
            // Did you run ant in the OO3Link project (or in ours) ?
60
            throw new IllegalStateException("Missing " + jarName);
61
        res.add(org.openconcerto.utils.protocol.Helper.toJarJar(resource));
62
 
63
        return res.toArray(new URL[res.size()]);
64
    }
65
 
66
    static private final ClassLoader getLoader(final OOInstallation ooInstall) {
67
        ClassLoader res = loaders.get(ooInstall);
68
        if (res == null) {
69
            // pass our classloader otherwise the system class loader will be used. This won't work
70
            // in webstart since the classpath is loaded by JNLPClassLoader, thus a class loaded by
71
            // res couldn't refer to the classpath (e.g. this class) but only to the java library.
73 ilm 72
            res = new URLClassLoader(getURLs(ooInstall), OOConnexion.class.getClassLoader()) {
73
                @Override
74
                protected PermissionCollection getPermissions(CodeSource codesource) {
75
                    final PermissionCollection perms = super.getPermissions(codesource);
76
                    perms.add(new FilePermission(ooInstall.getExecutable().getAbsolutePath(), "execute"));
77
                    perms.add(new SocketPermission("localhost:" + PORT + "-" + (PORT + 10), "connect"));
78
                    // needed by OO jars
79
                    perms.add(new PropertyPermission("*", "read"));
80
                    // to be able to open any document
81
                    perms.add(new FilePermission("<<ALL FILES>>", "read"));
82
                    // needed by ThreadPoolExecutor.shutdown()
83
                    perms.add(new RuntimePermission("modifyThread"));
84
                    return perms;
85
                }
86
            };
13 ilm 87
            loaders.put(ooInstall, res);
88
        }
89
        return res;
90
    }
91
 
92
    /**
93
     * Return a connection to the default OpenOffice installation.
94
     *
95
     * @return a connection to the default OpenOffice or <code>null</code> if none is found.
96
     * @throws IllegalStateException if an error occurs while searching.
97
     */
98
    static public OOConnexion create() throws IllegalStateException {
99
        final OOInstallation ooInstall;
100
        try {
101
            ooInstall = OOInstallation.getInstance();
102
        } catch (IOException e) {
103
            throw new IllegalStateException("Couldn't find default OO installation", e);
104
        }
105
        return create(ooInstall);
106
    }
107
 
108
    static public OOConnexion create(final OOInstallation ooInstall) throws IllegalStateException {
109
        if (ooInstall == null)
110
            return null;
111
        try {
112
            final Class<?> c = getLoader(ooInstall).loadClass("org.jopendocument.link" + OO_VERSION + ".OOConnexion");
113
            return (OOConnexion) c.getConstructor(OOInstallation.class).newInstance(ooInstall);
114
        } catch (Exception e) {
115
            throw new IllegalStateException("Couldn't create OOCOnnexion", e);
116
        }
117
    }
118
 
73 ilm 119
    public static void main(String[] args) throws IOException {
142 ilm 120
        if (args.length == 0 || args.length > 2) {
121
            System.out.println("Usage : " + OOConnexion.class.getName() + " officeFile | --type param");
73 ilm 122
            System.out.println("Open officeFile in the default installation of LibreOffice");
142 ilm 123
            System.out.println("--type is either file or url");
73 ilm 124
            System.exit(1);
125
        }
126
        final OOConnexion conn = OOConnexion.create();
127
        if (conn == null)
128
            throw new IllegalStateException("No Office found");
142 ilm 129
        final boolean file;
130
        final String arg;
131
        if (args.length == 1) {
132
            file = true;
133
            arg = args[0];
134
        } else if (args[0].equals("--file")) {
135
            file = true;
136
            arg = args[1];
137
        } else if (args[0].equals("--url")) {
138
            file = false;
139
            arg = args[1];
140
        } else {
141
            throw new IllegalArgumentException("Type not valid : " + args[0]);
142
        }
143
        if (file)
144
            conn.loadDocument(new File(arg), false);
145
        else
146
            conn.loadDocumentFromURLAsync(arg, false);
73 ilm 147
        conn.closeConnexion();
148
    }
149
 
13 ilm 150
    protected abstract Component loadDocumentFromURLAsync(final String url, final boolean hidden);
151
 
152
    /**
153
     * Load a document in OpenOffice.
154
     *
155
     * @param f the file to load.
156
     * @param hidden <code>true</code> if no frame should be visible.
157
     * @return the new component.
158
     * @throws IOException if an error occurs.
159
     */
160
    public final Component loadDocument(final File f, final boolean hidden) throws IOException {
161
        if (!f.exists())
162
            throw new FileNotFoundException(f.getAbsolutePath());
163
 
164
        return loadDocumentFromURLAsync(convertToUrl(f.getAbsolutePath()), hidden);
165
    }
166
 
167
    protected abstract String convertToUrl(String path) throws MalformedURLException;
168
 
169
    public abstract void closeConnexion();
170
 
171
    /**
172
     * Whether the bridge is closed.
173
     *
174
     * @return <code>true</code> if {@link #closeConnexion()} was called or OpenOffice is now longer
175
     *         running.
176
     */
177
    public abstract boolean isClosed();
178
}