001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.kernel.io.ReaderInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.kernel.template.TemplateResource;
022    import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    
028    import org.apache.commons.collections.ExtendedProperties;
029    import org.apache.velocity.exception.ResourceNotFoundException;
030    import org.apache.velocity.runtime.resource.Resource;
031    import org.apache.velocity.runtime.resource.loader.ResourceLoader;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Shuyang Zhou
036     */
037    public class LiferayResourceLoader extends ResourceLoader {
038    
039            @Override
040            public long getLastModified(Resource resource) {
041                    if (_log.isDebugEnabled()) {
042                            _log.debug("Get last modified for " + resource.getName());
043                    }
044    
045                    return 0;
046            }
047    
048            @Override
049            public InputStream getResourceStream(String source)
050                    throws ResourceNotFoundException {
051    
052                    InputStream is = doGetResourceStream(source);
053    
054                    if (is == null) {
055                            if (_log.isDebugEnabled()) {
056                                    _log.debug("Unable to find " + source);
057                            }
058    
059                            throw new ResourceNotFoundException(source);
060                    }
061    
062                    if (_log.isDebugEnabled()) {
063                            _log.debug("Successfully found " + source);
064                    }
065    
066                    return is;
067            }
068    
069            @Override
070            public void init(ExtendedProperties props) {
071                    setModificationCheckInterval(
072                            PropsValues.VELOCITY_ENGINE_RESOURCE_MODIFICATION_CHECK_INTERVAL);
073            }
074    
075            @Override
076            public boolean isSourceModified(Resource resource) {
077                    if (_log.isDebugEnabled()) {
078                            _log.debug("Check modified status for " + resource.getName());
079                    }
080    
081                    return false;
082            }
083    
084            @Override
085            public boolean resourceExists(String resourceName) {
086                    InputStream is = null;
087    
088                    try {
089                            is = doGetResourceStream(resourceName);
090    
091                            if (is != null) {
092                                    is.close();
093                            }
094                    }
095                    catch (IOException ioe) {
096                    }
097                    catch (ResourceNotFoundException rnfe) {
098                    }
099    
100                    if (is != null) {
101                            return true;
102                    }
103                    else {
104                            return false;
105                    }
106            }
107    
108            protected InputStream doGetResourceStream(String source)
109                    throws ResourceNotFoundException {
110    
111                    if (_log.isDebugEnabled()) {
112                            _log.debug("Get resource for " + source);
113                    }
114    
115                    try {
116                            TemplateResource templateResource =
117                                    TemplateResourceLoaderUtil.getTemplateResource(
118                                            TemplateConstants.LANG_TYPE_VM, source);
119    
120                            return new ReaderInputStream(templateResource.getReader());
121                    }
122                    catch (Exception e) {
123                            return null;
124                    }
125            }
126    
127            private static Log _log = LogFactoryUtil.getLog(
128                    LiferayResourceLoader.class);
129    
130    }