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