001/*
002 * (C) Copyright 2010-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.convert.ooomanager;
021
022import java.io.File;
023import java.io.IOException;
024import java.lang.reflect.Field;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028
029import org.apache.commons.lang3.SystemUtils;
030
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @deprecated Since 8.4. See 'soffice' use with {@link org.nuxeo.ecm.platform.convert.plugins.CommandLineConverter} instead
035 */
036@Deprecated
037public class ConfigBuilderHelper {
038
039    private static final String JPIPE_LIB_PATH_PROPERTY_KEY = "jod.jpipe.lib.path";
040
041    private static final String[] UNIX_JPIPE_PATHS = { "/usr/lib/ure/lib", "/usr/lib/libreoffice/program" };
042
043    private static final String[] MAC_JPIPE_PATHS = {
044            "/Applications/OpenOffice.org.app/Contents/basis-link/ure-link/lib",
045            "/Applications/LibreOffice.app/Contents/Frameworks"
046    };
047
048    private ConfigBuilderHelper() {
049    }
050
051    protected static void hackClassLoader() throws IOException {
052        try {
053            String ldPath = getLibPath();
054            Field field = ClassLoader.class.getDeclaredField("usr_paths");
055            field.setAccessible(true);
056            String[] paths = (String[]) field.get(null);
057            for (String path : paths) {
058                if (ldPath.equals(path)) {
059                    return;
060                }
061            }
062            String[] tmp = new String[paths.length + 1];
063            System.arraycopy(paths, 0, tmp, 0, paths.length);
064            tmp[paths.length] = ldPath;
065            field.set(null, tmp);
066            System.setProperty("java.library.path", System.getProperty("java.library.path"));
067
068        } catch (IllegalAccessException e) {
069            throw new IOException("Failed to get permissions to set library path");
070        } catch (NoSuchFieldException e) {
071            throw new IOException("Failed to get field handle to set library path");
072        }
073    }
074
075    protected static String getLibPath() throws IOException {
076        String jpipeLibPath = Framework.getProperty(JPIPE_LIB_PATH_PROPERTY_KEY);
077        if (jpipeLibPath != null) {
078            return jpipeLibPath;
079        }
080        jpipeLibPath = findJlibPipe();
081        if (jpipeLibPath != null) {
082            return jpipeLibPath;
083        }
084        throw new IOException("Failed to get jPipe LIbrary path.");
085    }
086
087    protected static String findJlibPipe() {
088        List<String> possiblePaths = new ArrayList<>();
089
090        if (SystemUtils.IS_OS_LINUX) {
091            possiblePaths.addAll(Arrays.asList(UNIX_JPIPE_PATHS));
092        } else if (SystemUtils.IS_OS_MAC_OSX) {
093            possiblePaths.addAll(Arrays.asList(MAC_JPIPE_PATHS));
094        }
095
096        for (String path : possiblePaths) {
097            if (new File(path).exists()) {
098                return path;
099            }
100        }
101        return null;
102    }
103
104    /**
105     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_MAC_OSX}
106     */
107    @Deprecated
108    protected static boolean isMac() {
109        return SystemUtils.IS_OS_MAC_OSX;
110    }
111
112    /**
113     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_LINUX}
114     */
115    @Deprecated
116    protected static boolean isLinux() {
117        return SystemUtils.IS_OS_LINUX;
118    }
119
120}