001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.platform.convert.ooomanager;
019
020import java.io.File;
021import java.io.IOException;
022import java.lang.reflect.Field;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027import org.apache.commons.lang3.SystemUtils;
028
029import org.nuxeo.runtime.api.Framework;
030
031public class ConfigBuilderHelper {
032
033    private static final String JPIPE_LIB_PATH_PROPERTY_KEY = "jod.jpipe.lib.path";
034
035    private static final String[] UNIX_JPIPE_PATHS = { "/usr/lib/ure/lib" };
036
037    private static final String[] MAC_JPIPE_PATHS = { "/Applications/OpenOffice.org.app/Contents/basis-link/ure-link/lib" };
038
039    private ConfigBuilderHelper() {
040    }
041
042    protected static void hackClassLoader() throws IOException {
043        try {
044            String ldPath = getLibPath();
045            Field field = ClassLoader.class.getDeclaredField("usr_paths");
046            field.setAccessible(true);
047            String[] paths = (String[]) field.get(null);
048            for (String path : paths) {
049                if (ldPath.equals(path)) {
050                    return;
051                }
052            }
053            String[] tmp = new String[paths.length + 1];
054            System.arraycopy(paths, 0, tmp, 0, paths.length);
055            tmp[paths.length] = ldPath;
056            field.set(null, tmp);
057            System.setProperty("java.library.path", System.getProperty("java.library.path"));
058
059        } catch (IllegalAccessException e) {
060            throw new IOException("Failed to get permissions to set library path");
061        } catch (NoSuchFieldException e) {
062            throw new IOException("Failed to get field handle to set library path");
063        }
064    }
065
066    protected static String getLibPath() throws IOException {
067        String jpipeLibPath = Framework.getProperty(JPIPE_LIB_PATH_PROPERTY_KEY);
068        if (jpipeLibPath != null) {
069            return jpipeLibPath;
070        }
071        jpipeLibPath = findJlibPipe();
072        if (jpipeLibPath != null) {
073            return jpipeLibPath;
074        }
075        throw new IOException("Failed to get jPipe LIbrary path.");
076    }
077
078    protected static String findJlibPipe() {
079        List<String> possiblePaths = new ArrayList<>();
080
081        if (SystemUtils.IS_OS_LINUX) {
082            possiblePaths.addAll(Arrays.asList(UNIX_JPIPE_PATHS));
083        } else if (SystemUtils.IS_OS_MAC_OSX) {
084            possiblePaths.addAll(Arrays.asList(MAC_JPIPE_PATHS));
085        }
086
087        for (String path : possiblePaths) {
088            if (new File(path).exists()) {
089                return path;
090            }
091        }
092        return null;
093    }
094
095    /**
096     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_MAC_OSX}
097     */
098    @Deprecated
099    protected static boolean isMac() {
100        return SystemUtils.IS_OS_MAC_OSX;
101    }
102
103    /**
104     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_LINUX}
105     */
106    @Deprecated
107    protected static boolean isLinux() {
108        return SystemUtils.IS_OS_LINUX;
109    }
110
111}