001    /**
002     * Copyright (c) 2000-present 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.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.xuggler.Xuggler;
022    import com.liferay.portal.kernel.xuggler.XugglerInstallException;
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    import java.net.URL;
032    
033    /**
034     * @author Alexander Chow
035     */
036    public class XugglerImpl implements Xuggler {
037    
038            @Override
039            public void installNativeLibraries(String name) throws Exception {
040                    try {
041                            JarUtil.downloadAndInstallJar(
042                                    new URL(PropsValues.XUGGLER_JAR_URL + name),
043                                    PropsValues.LIFERAY_LIB_PORTAL_DIR, name);
044    
045                            _nativeLibraryCopied = true;
046                    }
047                    catch (Exception e) {
048                            throw new XugglerInstallException.MustInstallJar(name, e);
049                    }
050            }
051    
052            @Override
053            public boolean isEnabled() {
054                    return isEnabled(true);
055            }
056    
057            @Override
058            public boolean isEnabled(boolean checkNativeLibraries) {
059                    boolean enabled = false;
060    
061                    try {
062                            enabled = PrefsPropsUtil.getBoolean(
063                                    PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED);
064                    }
065                    catch (Exception e) {
066                            if (_log.isWarnEnabled()) {
067                                    _log.warn(e, e);
068                            }
069                    }
070    
071                    if (!checkNativeLibraries) {
072                            return enabled;
073                    }
074    
075                    if (enabled) {
076                            return isNativeLibraryInstalled();
077                    }
078    
079                    return false;
080            }
081    
082            @Override
083            public boolean isNativeLibraryCopied() {
084                    return _nativeLibraryCopied;
085            }
086    
087            @Override
088            public boolean isNativeLibraryInstalled() {
089                    if (_nativeLibraryInstalled) {
090                            return _nativeLibraryInstalled;
091                    }
092    
093                    String originalLevel = Log4JUtil.getOriginalLevel(
094                            JNILibraryLoader.class.getName());
095    
096                    try {
097                            Log4JUtil.setLevel(JNILibraryLoader.class.getName(), "OFF", false);
098    
099                            IContainer.make();
100    
101                            _nativeLibraryInstalled = true;
102                    }
103                    catch (NoClassDefFoundError ncdfe) {
104                            informAdministrator(ncdfe.getMessage());
105                    }
106                    catch (UnsatisfiedLinkError ule) {
107                            informAdministrator(ule.getMessage());
108                    }
109                    finally {
110                            Log4JUtil.setLevel(
111                                    JNILibraryLoader.class.getName(), originalLevel.toString(),
112                                    false);
113                    }
114    
115                    return _nativeLibraryInstalled;
116            }
117    
118            protected void informAdministrator(String errorMessage) {
119                    if (!_informAdministrator) {
120                            return;
121                    }
122    
123                    _informAdministrator = false;
124    
125                    StringBundler sb = new StringBundler(7);
126    
127                    sb.append("Liferay does not have the Xuggler native libraries ");
128                    sb.append("installed. In order to generate video and audio previews, ");
129                    sb.append("please follow the instructions for Xuggler in the Server ");
130                    sb.append("Administration section of the Control Panel at: ");
131                    sb.append("http://<server>/group/control_panel/manage/-/server/");
132                    sb.append("external-services. Error message is: ");
133                    sb.append(errorMessage);
134    
135                    _log.error(sb.toString());
136            }
137    
138            private static final Log _log = LogFactoryUtil.getLog(XugglerImpl.class);
139    
140            private static boolean _informAdministrator = true;
141            private static boolean _nativeLibraryCopied;
142            private static boolean _nativeLibraryInstalled;
143    
144    }