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
033public class ConfigBuilderHelper {
034
035    private static final String JPIPE_LIB_PATH_PROPERTY_KEY = "jod.jpipe.lib.path";
036
037    private static final String[] UNIX_JPIPE_PATHS = { "/usr/lib/ure/lib", "/usr/lib/libreoffice/program" };
038
039    private static final String[] MAC_JPIPE_PATHS = {
040            "/Applications/OpenOffice.org.app/Contents/basis-link/ure-link/lib",
041            "/Applications/LibreOffice.app/Contents/Frameworks"
042    };
043
044    private ConfigBuilderHelper() {
045    }
046
047    protected static void hackClassLoader() throws IOException {
048        try {
049            String ldPath = getLibPath();
050            Field field = ClassLoader.class.getDeclaredField("usr_paths");
051            field.setAccessible(true);
052            String[] paths = (String[]) field.get(null);
053            for (String path : paths) {
054                if (ldPath.equals(path)) {
055                    return;
056                }
057            }
058            String[] tmp = new String[paths.length + 1];
059            System.arraycopy(paths, 0, tmp, 0, paths.length);
060            tmp[paths.length] = ldPath;
061            field.set(null, tmp);
062            System.setProperty("java.library.path", System.getProperty("java.library.path"));
063
064        } catch (IllegalAccessException e) {
065            throw new IOException("Failed to get permissions to set library path");
066        } catch (NoSuchFieldException e) {
067            throw new IOException("Failed to get field handle to set library path");
068        }
069    }
070
071    protected static String getLibPath() throws IOException {
072        String jpipeLibPath = Framework.getProperty(JPIPE_LIB_PATH_PROPERTY_KEY);
073        if (jpipeLibPath != null) {
074            return jpipeLibPath;
075        }
076        jpipeLibPath = findJlibPipe();
077        if (jpipeLibPath != null) {
078            return jpipeLibPath;
079        }
080        throw new IOException("Failed to get jPipe LIbrary path.");
081    }
082
083    protected static String findJlibPipe() {
084        List<String> possiblePaths = new ArrayList<>();
085
086        if (SystemUtils.IS_OS_LINUX) {
087            possiblePaths.addAll(Arrays.asList(UNIX_JPIPE_PATHS));
088        } else if (SystemUtils.IS_OS_MAC_OSX) {
089            possiblePaths.addAll(Arrays.asList(MAC_JPIPE_PATHS));
090        }
091
092        for (String path : possiblePaths) {
093            if (new File(path).exists()) {
094                return path;
095            }
096        }
097        return null;
098    }
099
100    /**
101     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_MAC_OSX}
102     */
103    @Deprecated
104    protected static boolean isMac() {
105        return SystemUtils.IS_OS_MAC_OSX;
106    }
107
108    /**
109     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_LINUX}
110     */
111    @Deprecated
112    protected static boolean isLinux() {
113        return SystemUtils.IS_OS_LINUX;
114    }
115
116}