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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.InputStream;
021    
022    import org.apache.velocity.exception.ResourceNotFoundException;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class ClassLoaderVelocityResourceListener
028            extends VelocityResourceListener {
029    
030            public InputStream getResourceStream(String source)
031                    throws ResourceNotFoundException {
032    
033                    InputStream is = null;
034    
035                    int pos = source.indexOf(JOURNAL_SEPARATOR);
036    
037                    if (pos == -1) {
038                            pos = source.indexOf(SERVLET_SEPARATOR);
039                    }
040    
041                    if (pos == -1) {
042                            pos = source.indexOf(THEME_LOADER_SEPARATOR);
043                    }
044    
045                    if (pos == -1) {
046                            ClassLoader classLoader = getClass().getClassLoader();
047    
048                            if (_log.isDebugEnabled()) {
049                                    _log.debug("Loading " + source);
050                            }
051    
052                            is = classLoader.getResourceAsStream(source);
053                    }
054    
055                    return is;
056            }
057    
058            private static Log _log = LogFactoryUtil.getLog(
059                    ClassLoaderVelocityResourceListener.class);
060    
061    }