001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.lenya.cms.cocoon.selection;
018:
019: import org.apache.cocoon.selection.Selector;
020: import org.apache.excalibur.source.Source;
021: import org.apache.excalibur.source.SourceNotFoundException;
022: import org.apache.excalibur.source.SourceResolver;
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.parameters.Parameters;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.avalon.framework.service.Serviceable;
029: import org.apache.avalon.framework.thread.ThreadSafe;
030:
031: import java.util.Map;
032:
033: /**
034: * <p>
035: * Last Modified Source Selector.
036: * </p>
037: *
038: * <pre>
039: * <map:selector name="last-mod" src="org.apache.lenya.cms.cocoon.selection.LastModSourceSelector"/>
040: *
041: * <map:select type="last-mod">
042: * <map:parameter name="compare-to" value="{sourceToCompareTo}"/>
043: *
044: * <map:when test="cachedsource">
045: * <!-- executes iff cachedsource last-modified > courceToCompareTo last-modified -->
046: * <map:read src="{cachedsource}" mime-type="text/xml; charset=utf-8"/>
047: * </map:when>
048: * <map:otherwise>
049: * <map:read src="{sourceToCompareTo}" mime-type="text/xml; charset=utf-8"/>
050: * </map:otherwise>
051: * </map:select>
052: * </pre>
053: */
054:
055: public class LastModSourceSelector extends AbstractLogEnabled implements
056: ThreadSafe, Serviceable, Disposable, Selector {
057:
058: private ServiceManager manager;
059: private SourceResolver resolver;
060: private Source source = null;
061: private Source compare = null;
062:
063: public void service(ServiceManager manager) throws ServiceException {
064: this .manager = manager;
065: this .resolver = (SourceResolver) manager
066: .lookup(SourceResolver.ROLE);
067: }
068:
069: public void dispose() {
070: if (null != this .source) {
071: resolver.release(this .source);
072: this .source = null;
073: }
074: if (null != this .compare) {
075: resolver.release(this .compare);
076: this .compare = null;
077: }
078: this .manager.release(this .resolver);
079: this .resolver = null;
080: this .manager = null;
081: }
082:
083: public boolean select(String expression, Map objectModel,
084: Parameters parameters) {
085: String sourceToCompare = parameters.getParameter("compare-to",
086: null);
087: String compareToSource = expression;
088: long sourceModDate = 0;
089: long compareModDate = 0;
090: try {
091: source = resolver.resolveURI(sourceToCompare);
092: sourceModDate = source.getLastModified();
093: compare = resolver.resolveURI(compareToSource);
094: compareModDate = compare.getLastModified();
095: } catch (SourceNotFoundException e) {
096: return false;
097: } catch (Exception e) {
098: getLogger().warn("Exception resolving resource ", e);
099: return false;
100: }
101: boolean isNewer = (compareModDate > sourceModDate);
102:
103: return (sourceToCompare != null && isNewer);
104:
105: }
106: }
|