001    /**
002     * Copyright (c) 2000-2012 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.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    }