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.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletContextPool;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.template.URLResourceParser;
022    import com.liferay.portal.util.PortalUtil;
023    
024    import java.io.IOException;
025    
026    import java.net.URL;
027    
028    import javax.servlet.ServletContext;
029    
030    /**
031     * @author Alexander Chow
032     * @author Raymond Aug??
033     */
034    public class VelocityServletResourceParser extends URLResourceParser {
035    
036            @Override
037            public URL getURL(String source) throws IOException {
038                    int pos = source.indexOf(TemplateConstants.SERVLET_SEPARATOR);
039    
040                    if (pos == -1) {
041                            return null;
042                    }
043    
044                    String servletContextName = source.substring(0, pos);
045    
046                    if (servletContextName.equals(PortalUtil.getPathContext())) {
047                            servletContextName = PortalUtil.getServletContextName();
048                    }
049    
050                    ServletContext servletContext = ServletContextPool.get(
051                            servletContextName);
052    
053                    if (servletContext == null) {
054                            _log.error(
055                                    source + " is not valid because " + servletContextName +
056                                            " does not map to a servlet context");
057    
058                            return null;
059                    }
060    
061                    String name = source.substring(
062                            pos + TemplateConstants.SERVLET_SEPARATOR.length());
063    
064                    if (_log.isDebugEnabled()) {
065                            _log.debug(
066                                    name + " is associated with the servlet context " +
067                                            servletContextName + " " + servletContext);
068                    }
069    
070                    URL url = servletContext.getResource(name);
071    
072                    if ((url == null) && name.endsWith("/init_custom.vm")) {
073                            if (_log.isWarnEnabled()) {
074                                    _log.warn("The template " + name + " should be created");
075                            }
076    
077                            ServletContext portalServletContext = ServletContextPool.get(
078                                    PortalUtil.getServletContextName());
079    
080                            url = portalServletContext.getResource(
081                                    "/html/themes/_unstyled/templates/init_custom.vm");
082                    }
083    
084                    return url;
085            }
086    
087            private static final Log _log = LogFactoryUtil.getLog(
088                    VelocityServletResourceParser.class);
089    
090    }