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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import org.apache.commons.collections.ExtendedProperties;
025    import org.apache.velocity.exception.ResourceNotFoundException;
026    import org.apache.velocity.runtime.resource.Resource;
027    import org.apache.velocity.runtime.resource.loader.ResourceLoader;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class LiferayResourceLoader extends ResourceLoader {
034    
035            public static void setVelocityResourceListeners(
036                    String[] velocityResourceListeners) {
037    
038                    _velocityResourceListeners = new VelocityResourceListener[
039                            velocityResourceListeners.length];
040    
041                    for (int i = 0; i < velocityResourceListeners.length; i++) {
042                            try {
043                                    Class<?> clazz = Class.forName(velocityResourceListeners[i]);
044    
045                                    _velocityResourceListeners[i] = (VelocityResourceListener)
046                                            clazz.newInstance();
047                            }
048                            catch (Exception e) {
049                                    _log.error(e);
050    
051                                    _velocityResourceListeners[i] = null;
052                            }
053                    }
054            }
055    
056            @Override
057            public long getLastModified(Resource resource) {
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Get last modified for " + resource.getName());
060                    }
061    
062                    return 0;
063            }
064    
065            @Override
066            public InputStream getResourceStream(String source)
067                    throws ResourceNotFoundException {
068    
069                    InputStream is = doGetResourceStream(source);
070    
071                    if (is == null) {
072                            if (_log.isDebugEnabled()) {
073                                    _log.debug("Could not find " + source);
074                            }
075    
076                            throw new ResourceNotFoundException(source);
077                    }
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug("Successfully got " + source);
081                    }
082    
083                    return is;
084            }
085    
086            @Override
087            public void init(ExtendedProperties props) {
088                    setModificationCheckInterval(
089                            PropsValues.
090                                    VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL);
091            }
092    
093            @Override
094            public boolean isSourceModified(Resource resource) {
095                    if (_log.isDebugEnabled()) {
096                            _log.debug("Check modified status for " + resource.getName());
097                    }
098    
099                    return false;
100            }
101    
102            @Override
103            public boolean resourceExists(String resourceName) {
104                    InputStream is = null;
105    
106                    try {
107                            is = doGetResourceStream(resourceName);
108    
109                            if (is != null) {
110                                    is.close();
111                            }
112                    }
113                    catch (IOException ioe) {
114                    }
115                    catch (ResourceNotFoundException rnfe) {
116                    }
117    
118                    if (is != null) {
119                            return true;
120                    }
121                    else {
122                            return false;
123                    }
124            }
125    
126            protected InputStream doGetResourceStream(String source)
127                    throws ResourceNotFoundException {
128    
129                    if (_log.isDebugEnabled()) {
130                            _log.debug("Get resource for " + source);
131                    }
132    
133                    InputStream is = null;
134    
135                    for (int i = 0; (is == null) && (i < _velocityResourceListeners.length);
136                                    i++) {
137    
138                            VelocityResourceListener velocityResourceListener =
139                                    _velocityResourceListeners[i];
140    
141                            if (velocityResourceListener != null) {
142                                    is = velocityResourceListener.getResourceStream(source);
143                            }
144                    }
145    
146                    return is;
147            }
148    
149            private static Log _log = LogFactoryUtil.getLog(
150                            LiferayResourceLoader.class);
151    
152            private static VelocityResourceListener[] _velocityResourceListeners =
153                    new VelocityResourceListener[0];
154    
155    }