001    /**
002     * Copyright (c) 2000-2010 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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.ServletContextPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PortalUtil;
023    
024    import java.io.InputStream;
025    
026    import javax.servlet.ServletContext;
027    
028    import org.apache.velocity.exception.ResourceNotFoundException;
029    
030    /**
031     * @author Alexander Chow
032     */
033    public class ServletVelocityResourceListener extends VelocityResourceListener {
034    
035            public InputStream getResourceStream(String source)
036                    throws ResourceNotFoundException {
037    
038                    InputStream is = null;
039    
040                    int pos = source.indexOf(SERVLET_SEPARATOR);
041    
042                    if (pos != -1) {
043                            String servletContextName = source.substring(0, pos);
044    
045                            if (Validator.isNull(servletContextName)) {
046                                    servletContextName = PortalUtil.getPathContext();
047                            }
048    
049                            ServletContext servletContext = ServletContextPool.get(
050                                    servletContextName);
051    
052                            if (servletContext != null) {
053                                    String name =
054                                            source.substring(pos + SERVLET_SEPARATOR.length());
055    
056                                    if (_log.isDebugEnabled()) {
057                                            _log.debug(
058                                                    name + " is associated with the servlet context " +
059                                                            servletContextName + " " + servletContext);
060                                    }
061    
062                                    is = servletContext.getResourceAsStream(name);
063    
064                                    if ((is == null) && (name.endsWith("/init_custom.vm"))) {
065                                            if (_log.isWarnEnabled()) {
066                                                    _log.warn(
067                                                            "The template " + name + " should be created");
068                                            }
069    
070                                            is = new UnsyncByteArrayInputStream(new byte[0]);
071                                    }
072                            }
073                            else {
074                                    _log.error(
075                                            source + " is not valid because " + servletContextName +
076                                                    " does not map to a servlet context");
077                            }
078                    }
079    
080                    return is;
081            }
082    
083            private static Log _log = LogFactoryUtil.getLog(
084                    ServletVelocityResourceListener.class);
085    
086    }