1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.freemarker;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.util.PortalUtil;
21  import com.liferay.portal.velocity.VelocityContextPool;
22  
23  import java.io.IOException;
24  
25  import java.net.URL;
26  
27  import javax.servlet.ServletContext;
28  
29  /**
30   * <a href="ServletTemplateLoader.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Mika Koivisto
33   */
34  public class ServletTemplateLoader extends URLTemplateLoader {
35  
36      public URL getURL(String name) throws IOException {
37          URL url = null;
38  
39          int pos = name.indexOf(SERVLET_SEPARATOR);
40  
41          if (pos != -1) {
42              String servletContextName = name.substring(0, pos);
43  
44              if (Validator.isNull(servletContextName)) {
45                  servletContextName = PortalUtil.getPathContext();
46              }
47  
48              ServletContext servletContext = VelocityContextPool.get(
49                  servletContextName);
50  
51              if (servletContext != null) {
52                  String templateName =
53                      name.substring(pos + SERVLET_SEPARATOR.length());
54  
55                  if (_log.isDebugEnabled()) {
56                      _log.debug(
57                          name + " is associated with the servlet context " +
58                              servletContextName + " " + servletContext);
59                  }
60  
61                  url = servletContext.getResource(templateName);
62  
63                  if ((url == null) &&
64                      (templateName.endsWith("/init_custom.ftl"))) {
65  
66                      if (_log.isWarnEnabled()) {
67                          _log.warn(
68                              "The template " + name + " should be created");
69                      }
70  
71                      String portalContextName = PortalUtil.getPathContext();
72  
73                      ServletContext portalContext =
74                          VelocityContextPool.get(portalContextName);
75  
76                      url = portalContext.getResource(
77                          "/html/themes/_unstyled/template/init_custom.ftl");
78                  }
79              }
80              else {
81                  _log.error(
82                      name + " is not valid because " + servletContextName +
83                          " does not map to a servlet context");
84              }
85          }
86  
87          return url;
88      }
89  
90      private static Log _log = LogFactoryUtil.getLog(
91          ServletTemplateLoader.class);
92  
93  }