001    /**
002     * Copyright (c) 2000-2011 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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.io.File;
024    
025    import java.net.URI;
026    import java.net.URL;
027    
028    import java.util.jar.Attributes;
029    import java.util.jar.JarFile;
030    import java.util.jar.Manifest;
031    
032    /**
033     * @author Shuyang Zhou
034     * @author Brian Wing Shun Chan
035     */
036    public class JasperVersionDetector {
037    
038            public static String getJasperVersion() {
039                    return _instance._jasperVersion;
040            }
041    
042            private JasperVersionDetector() {
043                    try {
044                            Class<?> clazz = getClass();
045    
046                            URL url = clazz.getResource(
047                                    "/org/apache/jasper/JasperException.class");
048    
049                            if (url == null) {
050                                    return;
051                            }
052    
053                            String path = url.getPath();
054    
055                            int pos = path.indexOf(CharPool.EXCLAMATION);
056    
057                            if (pos == -1) {
058                                    return;
059                            }
060    
061                            URI jarFileURI = new URI(path.substring(0, pos));
062    
063                            JarFile jarFile = new JarFile(new File(jarFileURI));
064    
065                            Manifest manifest = jarFile.getManifest();
066    
067                            Attributes attributes = manifest.getMainAttributes();
068    
069                            _jasperVersion = GetterUtil.getString(
070                                    attributes.getValue("Specification-Version"));
071                    }
072                    catch (Exception e) {
073                            _log.error(e, e);
074                    }
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(
078                    JasperVersionDetector.class);
079    
080            private static JasperVersionDetector _instance =
081                    new JasperVersionDetector();
082    
083            private String _jasperVersion = StringPool.BLANK;
084    
085    }