01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.portal.velocity;
22:
23: import com.liferay.portal.kernel.util.Validator;
24: import com.liferay.portal.util.PortalUtil;
25:
26: import java.io.ByteArrayInputStream;
27: import java.io.InputStream;
28:
29: import javax.servlet.ServletContext;
30:
31: import org.apache.commons.logging.Log;
32: import org.apache.commons.logging.LogFactory;
33: import org.apache.velocity.exception.ResourceNotFoundException;
34:
35: /**
36: * <a href="ServletVelocityResourceListener.java.html"><b><i>View Source</i></b>
37: * </a>
38: *
39: * @author Alexander Chow
40: *
41: */
42: public class ServletVelocityResourceListener extends
43: VelocityResourceListener {
44:
45: public InputStream getResourceStream(String source)
46: throws ResourceNotFoundException {
47:
48: InputStream is = null;
49:
50: int pos = source.indexOf(SERVLET_SEPARATOR);
51:
52: if (pos != -1) {
53: String ctxName = source.substring(0, pos);
54:
55: if (Validator.isNull(ctxName)) {
56: ctxName = PortalUtil.getPathContext();
57: }
58:
59: ServletContext ctx = VelocityContextPool.get(ctxName);
60:
61: if (ctx != null) {
62: String name = source.substring(pos
63: + SERVLET_SEPARATOR.length());
64:
65: if (_log.isDebugEnabled()) {
66: _log
67: .debug(name
68: + " is associated with the servlet context "
69: + ctxName + " " + ctx);
70: }
71:
72: is = ctx.getResourceAsStream(name);
73:
74: if ((is == null) && (name.endsWith("/init_custom.vm"))) {
75: if (_log.isWarnEnabled()) {
76: _log.warn("The template " + name
77: + " should be created");
78: }
79:
80: is = new ByteArrayInputStream(new byte[0]);
81: }
82: } else {
83: _log.error(source + " is not valid because " + ctxName
84: + " does not map to a servlet context");
85: }
86: }
87:
88: return is;
89: }
90:
91: private static Log _log = LogFactory
92: .getLog(ServletVelocityResourceListener.class);
93:
94: }
|