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