001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void installNativeLibraries(
037                            String name, XugglerInstallStatus xugglerInstallStatus)
038                    throws Exception {
039    
040                    try {
041                            String url = PropsValues.XUGGLER_JAR_URL + name;
042    
043                            JarUtil.downloadAndInstallJar(
044                                    false, url, name, xugglerInstallStatus);
045                    }
046                    catch (Exception e) {
047                            _log.error("Unable to install jar " + name, e);
048    
049                            throw e;
050                    }
051            }
052    
053            public boolean isEnabled() {
054                    return isEnabled(true);
055            }
056    
057            public boolean isEnabled(boolean checkNativeLibraries) {
058                    boolean enabled = false;
059    
060                    try {
061                            enabled = PrefsPropsUtil.getBoolean(
062                                    PropsKeys.XUGGLER_ENABLED, PropsValues.XUGGLER_ENABLED);
063                    }
064                    catch (Exception e) {
065                            if (_log.isWarnEnabled()) {
066                                    _log.warn(e, e);
067                            }
068                    }
069    
070                    if (!checkNativeLibraries) {
071                            return enabled;
072                    }
073    
074                    if (enabled) {
075                            return isNativeLibraryInstalled();
076                    }
077    
078                    return false;
079            }
080    
081            public boolean isNativeLibraryInstalled() {
082                    if (_nativeLibraryInstalled) {
083                            return _nativeLibraryInstalled;
084                    }
085    
086                    String originalLevel = Log4JUtil.getOriginalLevel(
087                            JNILibraryLoader.class.getName());
088    
089                    try {
090                            Log4JUtil.setLevel(JNILibraryLoader.class.getName(), "OFF", false);
091    
092                            IContainer.make();
093    
094                            _nativeLibraryInstalled = true;
095                    }
096                    catch (NoClassDefFoundError ncdfe) {
097                            informAdministrator();
098                    }
099                    catch (UnsatisfiedLinkError ule) {
100                            informAdministrator();
101                    }
102                    finally {
103                            Log4JUtil.setLevel(
104                                    JNILibraryLoader.class.getName(), originalLevel.toString(),
105                                    false);
106                    }
107    
108                    return _nativeLibraryInstalled;
109            }
110    
111            protected void informAdministrator() {
112                    if (!_informAdministrator) {
113                            return;
114                    }
115    
116                    _informAdministrator = false;
117    
118                    StringBundler sb = new StringBundler(6);
119    
120                    sb.append("Liferay does not have the Xuggler native libraries ");
121                    sb.append("installed. In order to generate video and audio previews, ");
122                    sb.append("please follow the instructions for Xuggler in the Server ");
123                    sb.append("Administration control panel at: ");
124                    sb.append("http://<server>/group/control_panel/manage/-/server/");
125                    sb.append("external-services");
126    
127                    _log.error(sb.toString());
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(XugglerImpl.class);
131    
132            private static boolean _informAdministrator = true;
133            private static boolean _nativeLibraryInstalled;
134    
135    }