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.freemarker;
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.util.ContextPathUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PortalUtil;
024    
025    import java.io.IOException;
026    
027    import java.net.URL;
028    
029    import javax.servlet.ServletContext;
030    
031    /**
032     * @author Mika Koivisto
033     */
034    public class ServletTemplateLoader extends URLTemplateLoader {
035    
036            @Override
037            public URL getURL(String name) throws IOException {
038                    URL url = null;
039    
040                    int pos = name.indexOf(SERVLET_SEPARATOR);
041    
042                    if (pos != -1) {
043                            String servletContextName = name.substring(0, pos);
044    
045                            String servletContextPath = ContextPathUtil.getContextPath(
046                                    StringPool.SLASH + servletContextName);
047    
048                            String contextPath = PortalUtil.getPathContext();
049    
050                            if (Validator.isNull(servletContextName) ||
051                                    servletContextPath.equals(contextPath)) {
052    
053                                    servletContextName = contextPath;
054                            }
055    
056                            ServletContext servletContext = ServletContextPool.get(
057                                    servletContextName);
058    
059                            if (servletContext != null) {
060                                    String templateName = name.substring(
061                                            pos + SERVLET_SEPARATOR.length());
062    
063                                    if (_log.isDebugEnabled()) {
064                                            _log.debug(
065                                                    name + " is associated with the servlet context " +
066                                                            servletContextName + " " + servletContext);
067                                    }
068    
069                                    url = servletContext.getResource(templateName);
070    
071                                    if ((url == null) &&
072                                            templateName.endsWith("/init_custom.ftl")) {
073    
074                                            if (_log.isWarnEnabled()) {
075                                                    _log.warn(
076                                                            "The template " + name + " should be created");
077                                            }
078    
079                                            String portalServletContextName =
080                                                    PortalUtil.getPathContext();
081    
082                                            ServletContext portalServletContext =
083                                                    ServletContextPool.get(portalServletContextName);
084    
085                                            url = portalServletContext.getResource(
086                                                    "/html/themes/_unstyled/template/init_custom.ftl");
087                                    }
088                            }
089                            else {
090                                    _log.error(
091                                            name + " is not valid because " + servletContextName +
092                                                    " does not map to a servlet context");
093                            }
094                    }
095    
096                    return url;
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(
100                    ServletTemplateLoader.class);
101    
102    }