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.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.xuggler.Xuggler;
022    import com.liferay.portal.kernel.xuggler.XugglerInstallStatus;
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, XugglerInstallStatus xugglerInstallStatus)
039                    throws Exception {
040    
041                    try {
042                            String url = PropsValues.XUGGLER_JAR_URL + name;
043    
044                            JarUtil.downloadAndInstallJar(
045                                    false, url, name, xugglerInstallStatus);
046                    }
047                    catch (Exception e) {
048                            _log.error("Unable to install jar " + name, e);
049    
050                            throw e;
051                    }
052            }
053    
054            @Override
055            public boolean isEnabled() {
056                    return isEnabled(true);
057            }
058    
059            @Override
060            public boolean isEnabled(boolean checkNativeLibraries) {
061                    boolean enabled = false;
062    
063                    try {
064                            enabled = PrefsPropsUtil.getBoolean(
065                                    PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED);
066                    }
067                    catch (Exception e) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn(e, e);
070                            }
071                    }
072    
073                    if (!checkNativeLibraries) {
074                            return enabled;
075                    }
076    
077                    if (enabled) {
078                            return isNativeLibraryInstalled();
079                    }
080    
081                    return false;
082            }
083    
084            @Override
085            public boolean isNativeLibraryInstalled() {
086                    if (_nativeLibraryInstalled) {
087                            return _nativeLibraryInstalled;
088                    }
089    
090                    String originalLevel = Log4JUtil.getOriginalLevel(
091                            JNILibraryLoader.class.getName());
092    
093                    try {
094                            Log4JUtil.setLevel(JNILibraryLoader.class.getName(), "OFF", false);
095    
096                            IContainer.make();
097    
098                            _nativeLibraryInstalled = true;
099                    }
100                    catch (NoClassDefFoundError ncdfe) {
101                            informAdministrator();
102                    }
103                    catch (UnsatisfiedLinkError ule) {
104                            informAdministrator();
105                    }
106                    finally {
107                            Log4JUtil.setLevel(
108                                    JNILibraryLoader.class.getName(), originalLevel.toString(),
109                                    false);
110                    }
111    
112                    return _nativeLibraryInstalled;
113            }
114    
115            protected void informAdministrator() {
116                    if (!_informAdministrator) {
117                            return;
118                    }
119    
120                    _informAdministrator = false;
121    
122                    StringBundler sb = new StringBundler(6);
123    
124                    sb.append("Liferay does not have the Xuggler native libraries ");
125                    sb.append("installed. In order to generate video and audio previews, ");
126                    sb.append("please follow the instructions for Xuggler in the Server ");
127                    sb.append("Administration control panel at: ");
128                    sb.append("http://<server>/group/control_panel/manage/-/server/");
129                    sb.append("external-services");
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    }