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