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