001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.xuggler;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ProgressTracker;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.xuggler.Xuggler;
023    import com.liferay.portal.util.JarUtil;
024    import com.liferay.portal.util.PrefsPropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.util.log4j.Log4JUtil;
027    
028    import com.xuggle.ferry.JNILibraryLoader;
029    import com.xuggle.xuggler.IContainer;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class XugglerImpl implements Xuggler {
035    
036            public void installNativeLibraries(
037                            String name, ProgressTracker progressTracker)
038                    throws Exception {
039    
040                    try {
041                            String url = PropsValues.XUGGLER_JAR_URL + name;
042    
043                            JarUtil.downloadAndInstallJar(false, url, name, progressTracker);
044                    }
045                    catch (Exception e) {
046                            _log.error("Unable to install jar " + name, e);
047    
048                            throw e;
049                    }
050            }
051    
052            public boolean isEnabled() {
053                    return isEnabled(true);
054            }
055    
056            public boolean isEnabled(boolean checkNativeLibraries) {
057                    boolean enabled = false;
058    
059                    try {
060                            enabled = PrefsPropsUtil.getBoolean(
061                                    PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED);
062                    }
063                    catch (Exception e) {
064                            if (_log.isWarnEnabled()) {
065                                    _log.warn(e, e);
066                            }
067                    }
068    
069                    if (!checkNativeLibraries) {
070                            return enabled;
071                    }
072    
073                    if (enabled) {
074                            return isNativeLibraryInstalled();
075                    }
076    
077                    return false;
078            }
079    
080            public boolean isNativeLibraryInstalled() {
081                    if (_nativeLibraryInstalled) {
082                            return _nativeLibraryInstalled;
083                    }
084    
085                    String originalLevel = Log4JUtil.getOriginalLevel(
086                            JNILibraryLoader.class.getName());
087    
088                    try {
089                            Log4JUtil.setLevel(JNILibraryLoader.class.getName(), "OFF", false);
090    
091                            IContainer.make();
092    
093                            _nativeLibraryInstalled = true;
094                    }
095                    catch (NoClassDefFoundError ncdfe) {
096                            informAdministrator();
097                    }
098                    catch (UnsatisfiedLinkError ule) {
099                            informAdministrator();
100                    }
101                    finally {
102                            Log4JUtil.setLevel(
103                                    JNILibraryLoader.class.getName(), originalLevel.toString(),
104                                    false);
105                    }
106    
107                    return _nativeLibraryInstalled;
108            }
109    
110            protected void informAdministrator() {
111                    if (!_informAdministrator) {
112                            return;
113                    }
114    
115                    _informAdministrator = false;
116    
117                    StringBundler sb = new StringBundler(6);
118    
119                    sb.append("Liferay does not have the Xuggler native libraries ");
120                    sb.append("installed. In order to generate video and audio previews, ");
121                    sb.append("please follow the instructions for Xuggler in the Server ");
122                    sb.append("Administration section of the Control Panel at: ");
123                    sb.append("http://<server>/group/control_panel/manage/-/server/");
124                    sb.append("external-services");
125    
126                    _log.error(sb.toString());
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(XugglerImpl.class);
130    
131            private static boolean _informAdministrator = true;
132            private static boolean _nativeLibraryInstalled;
133    
134    }