001    /**
002     * Copyright (c) 2000-2013 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.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/template/init_custom.vm");
082                    }
083    
084                    return url;
085            }
086    
087            private static Log _log = LogFactoryUtil.getLog(
088                    VelocityServletResourceParser.class);
089    
090    }