001    /**
002     * Copyright (c) 2000-2011 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     * @author Raymond Augé
033     */
034    public class ServletVelocityResourceListener extends VelocityResourceListener {
035    
036            @Override
037            public InputStream getResourceStream(String source)
038                    throws ResourceNotFoundException {
039    
040                    try {
041                            return doGetResourceStream(source);
042                    }
043                    catch (Exception e) {
044                            throw new ResourceNotFoundException(source);
045                    }
046            }
047    
048            protected InputStream doGetResourceStream(String source) throws Exception {
049                    int pos = source.indexOf(SERVLET_SEPARATOR);
050    
051                    if (pos == -1) {
052                            return null;
053                    }
054    
055                    String servletContextName = source.substring(0, pos);
056    
057                    if (Validator.isNull(servletContextName)) {
058                            servletContextName = PortalUtil.getPathContext();
059                    }
060    
061                    ServletContext servletContext = ServletContextPool.get(
062                            servletContextName);
063    
064                    if (servletContext == null) {
065                            _log.error(
066                                    source + " is not valid because " + servletContextName +
067                                            " does not map to a servlet context");
068    
069                            return null;
070                    }
071    
072                    String name = source.substring(pos + SERVLET_SEPARATOR.length());
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug(
076                                    name + " is associated with the servlet context " +
077                                            servletContextName + " " + servletContext);
078                    }
079    
080                    InputStream inputStream = servletContext.getResourceAsStream(name);
081    
082                    if ((inputStream == null) && name.endsWith("/init_custom.vm")) {
083                            if (_log.isWarnEnabled()) {
084                                    _log.warn(
085                                            "The template " + name + " should be created");
086                            }
087    
088                            return new UnsyncByteArrayInputStream(new byte[0]);
089                    }
090                    else {
091                            return inputStream;
092                    }
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(
096                    ServletVelocityResourceListener.class);
097    
098    }