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.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.ReflectionUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.File;
026    
027    import java.lang.reflect.Method;
028    
029    import java.net.URI;
030    import java.net.URL;
031    
032    import java.util.Map;
033    import java.util.jar.Attributes;
034    import java.util.jar.JarFile;
035    import java.util.jar.Manifest;
036    
037    /**
038     * @author Shuyang Zhou
039     * @author Brian Wing Shun Chan
040     */
041    public class JasperVersionDetector {
042    
043            public static String getJasperVersion() {
044                    return _instance._jasperVersion;
045            }
046    
047            public static boolean hasJspServletDependantsMap() {
048                    return _instance._jspServletDependantsMap;
049            }
050    
051            private JasperVersionDetector() {
052                    _initializeJasperVersion();
053                    _initializeJspServletDependantsMap();
054            }
055    
056            private void _initializeJasperVersion() {
057                    try {
058                            Class<?> clazz = getClass();
059    
060                            URL url = clazz.getResource(
061                                    "/org/apache/jasper/JasperException.class");
062    
063                            if (url == null) {
064                                    return;
065                            }
066    
067                            String path = url.getPath();
068    
069                            int pos = path.indexOf(CharPool.EXCLAMATION);
070    
071                            if (pos == -1) {
072                                    return;
073                            }
074    
075                            URI jarFileURI = new URI(path.substring(0, pos));
076    
077                            JarFile jarFile = new JarFile(new File(jarFileURI));
078    
079                            Manifest manifest = jarFile.getManifest();
080    
081                            Attributes attributes = manifest.getMainAttributes();
082    
083                            if (attributes.containsKey(Attributes.Name.SPECIFICATION_VERSION)) {
084                                    _jasperVersion = GetterUtil.getString(
085                                            attributes.getValue(Attributes.Name.SPECIFICATION_VERSION));
086    
087                                    if (_isValidJasperVersion(_jasperVersion)) {
088                                            return;
089                                    }
090                            }
091    
092                            if (attributes.containsKey(
093                                            Attributes.Name.IMPLEMENTATION_VERSION)) {
094    
095                                    _jasperVersion = GetterUtil.getString(
096                                            attributes.get(Attributes.Name.IMPLEMENTATION_VERSION));
097    
098                                    if (_isValidJasperVersion(_jasperVersion)) {
099                                            return;
100                                    }
101                            }
102    
103                            Attributes.Name bundleVersionAttributesName = new Attributes.Name(
104                                    "Bundle-Version");
105    
106                            if (attributes.containsKey(bundleVersionAttributesName)) {
107                                    _jasperVersion = GetterUtil.getString(
108                                            attributes.get(bundleVersionAttributesName));
109    
110                                    if (_isValidJasperVersion(_jasperVersion)) {
111                                            return;
112                                    }
113    
114                                    _jasperVersion = StringPool.BLANK;
115                            }
116                    }
117                    catch (Exception e) {
118                            _log.error(e, e);
119                    }
120            }
121    
122            private void _initializeJspServletDependantsMap() {
123                    try {
124                            Class<?> clazz = Class.forName(
125                                    "org.apache.jasper.servlet.JspServletWrapper");
126    
127                            Method method = ReflectionUtil.getDeclaredMethod(
128                                    clazz, "getDependants");
129    
130                            Class<?> returnType = method.getReturnType();
131    
132                            _jspServletDependantsMap = Map.class.isAssignableFrom(returnType);
133                    }
134                    catch (Exception e) {
135                            _log.error(e, e);
136                    }
137            }
138    
139            private boolean _isValidJasperVersion(String jasperVersion) {
140                    if (Validator.isNull(jasperVersion) ||
141                            !Validator.isDigit(jasperVersion.charAt(0))) {
142    
143                            return false;
144                    }
145                    else {
146                            return true;
147                    }
148            }
149    
150            private static Log _log = LogFactoryUtil.getLog(
151                    JasperVersionDetector.class);
152    
153            private static JasperVersionDetector _instance =
154                    new JasperVersionDetector();
155    
156            private String _jasperVersion = StringPool.BLANK;
157            private boolean _jspServletDependantsMap;
158    
159    }