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    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.util.PropsUtil;
022    
023    import java.io.InputStream;
024    
025    import org.apache.commons.collections.ExtendedProperties;
026    import org.apache.velocity.exception.ResourceNotFoundException;
027    import org.apache.velocity.runtime.resource.Resource;
028    import org.apache.velocity.runtime.resource.loader.ResourceLoader;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class LiferayResourceLoader extends ResourceLoader {
034    
035            public static void setListeners(String[] listeners) {
036                    _listeners = new VelocityResourceListener[listeners.length];
037    
038                    for (int i = 0; i < listeners.length; i++) {
039                            try {
040                                    _listeners[i] = (VelocityResourceListener)Class.forName(
041                                            listeners[i]).newInstance();
042                            }
043                            catch (Exception ex) {
044                                    _log.error(ex);
045    
046                                    _listeners[i] = null;
047                            }
048                    }
049            }
050    
051            public long getLastModified(Resource resource) {
052                    if (_log.isDebugEnabled()) {
053                            _log.debug("Get last modified for " + resource.getName());
054                    }
055    
056                    return 0;
057            }
058    
059            public InputStream getResourceStream(String source)
060                    throws ResourceNotFoundException {
061    
062                    if (_log.isDebugEnabled()) {
063                            _log.debug("Get resource for " + source);
064                    }
065    
066                    InputStream is = null;
067    
068                    for (int i = 0; (is == null) && (i < _listeners.length); i++) {
069                            if (_listeners[i] != null) {
070                                    is = _listeners[i].getResourceStream(source);
071                            }
072                    }
073    
074                    if (is == null) {
075                            if (_log.isDebugEnabled()) {
076                                    _log.debug("Could not find " + source);
077                            }
078    
079                            throw new ResourceNotFoundException(source);
080                    }
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Successfully got " + source);
084                    }
085    
086                    return is;
087            }
088    
089            public void init(ExtendedProperties props) {
090                    boolean cachingOn = GetterUtil.getBoolean(PropsUtil.get(
091                            PropsKeys.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
092                    int modificationCheckInterval = GetterUtil.getInteger(PropsUtil.get(
093                            PropsKeys.
094                                    VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
095    
096                    setCachingOn(cachingOn);
097                    setModificationCheckInterval(modificationCheckInterval);
098            }
099    
100            public boolean isSourceModified(Resource resource) {
101                    if (_log.isDebugEnabled()) {
102                            _log.debug("Check modified status for " + resource.getName());
103                    }
104    
105                    return false;
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(
109                    LiferayResourceLoader.class);
110    
111            private static VelocityResourceListener[] _listeners =
112                    new VelocityResourceListener[0];
113    
114    }