001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.velocity;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.util.PropsUtil;
025:
026: import java.io.InputStream;
027:
028: import org.apache.commons.collections.ExtendedProperties;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.velocity.exception.ResourceNotFoundException;
032: import org.apache.velocity.runtime.resource.Resource;
033: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
034:
035: /**
036: * <a href="LiferayResourceLoader.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class LiferayResourceLoader extends ResourceLoader {
042:
043: private static VelocityResourceListener[] _listeners = new VelocityResourceListener[0];
044:
045: public static void setListeners(String[] listeners) {
046: _listeners = new VelocityResourceListener[listeners.length];
047:
048: for (int i = 0; i < listeners.length; i++) {
049: try {
050: _listeners[i] = (VelocityResourceListener) Class
051: .forName(listeners[i]).newInstance();
052: } catch (Exception ex) {
053: _log.error(ex);
054:
055: _listeners[i] = null;
056: }
057: }
058: }
059:
060: public void init(ExtendedProperties props) {
061: boolean cachingOn = GetterUtil
062: .getBoolean(PropsUtil
063: .get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_CACHE_ENABLED));
064: int modificationCheckInterval = GetterUtil
065: .getInteger(PropsUtil
066: .get(PropsUtil.VELOCITY_ENGINE_RESOURCE_MANAGER_MODIFICATION_CHECK_INTERVAL));
067:
068: setCachingOn(cachingOn);
069: setModificationCheckInterval(modificationCheckInterval);
070: }
071:
072: public InputStream getResourceStream(String source)
073: throws ResourceNotFoundException {
074:
075: if (_log.isDebugEnabled()) {
076: _log.debug("Get resource for " + source);
077: }
078:
079: InputStream is = null;
080:
081: for (int i = 0; (is == null) && (i < _listeners.length); i++) {
082: if (_listeners[i] != null) {
083: if (is == null) {
084: is = _listeners[i].getResourceStream(source);
085: }
086: }
087: }
088:
089: if (is == null) {
090: _log.error("Failed to get " + source);
091:
092: throw new ResourceNotFoundException(source);
093: }
094:
095: if (_log.isDebugEnabled()) {
096: _log.debug("Successfully got " + source);
097: }
098:
099: return is;
100: }
101:
102: public long getLastModified(Resource resource) {
103: if (_log.isDebugEnabled()) {
104: _log.debug("Get last modified for " + resource.getName());
105: }
106:
107: return 0;
108: }
109:
110: public boolean isSourceModified(Resource resource) {
111: if (_log.isDebugEnabled()) {
112: _log.debug("Check modified status for "
113: + resource.getName());
114: }
115:
116: return false;
117: }
118:
119: private static Log _log = LogFactory
120: .getLog(LiferayResourceLoader.class);
121:
122: }
|