001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.targets;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.TreeSet;
025:
026: import org.apache.log4j.NDC;
027:
028: import de.schlund.pfixxml.XMLException;
029: import de.schlund.pfixxml.resources.FileResource;
030: import de.schlund.pfixxml.resources.ResourceUtil;
031:
032: /**
033: * LeafTarget.java
034: *
035: *
036: * Created: Mon Jul 23 19:53:38 2001
037: *
038: * @author <a href="mailto: "Jens Lautenbacher</a>
039: *
040: *
041: */
042:
043: public abstract class LeafTarget extends TargetImpl {
044: // Set this in the Constructor of derived classes!
045: protected SharedLeaf sharedleaf;
046:
047: public void setXMLSource(Target source) {
048: throw new RuntimeException("Can't add a XMLSource to a leaf");
049: }
050:
051: public void setXSLSource(Target source) {
052: throw new RuntimeException("Can't add a XSLSource to a leaf");
053: }
054:
055: public void addParam(String key, Object val) {
056: throw new RuntimeException(
057: "Can't add a stylesheet parameter to a leaf");
058: }
059:
060: public TreeSet<PageInfo> getPageInfos() {
061: return sharedleaf.getPageInfos();
062: }
063:
064: public void addPageInfo(PageInfo info) {
065: sharedleaf.addPageInfo(info);
066: }
067:
068: public long getModTime() {
069: synchronized (sharedleaf) {
070: return sharedleaf.getModTime();
071: }
072: }
073:
074: public boolean needsUpdate() throws Exception {
075: synchronized (sharedleaf) {
076: long mymodtime = sharedleaf.getModTime();
077: FileResource doc = ResourceUtil
078: .getFileResourceFromDocroot(getTargetKey());
079: long maxmodtime = doc.lastModified();
080: boolean depup = true;
081:
082: for (Iterator<AuxDependency> i = this
083: .getAuxDependencyManager().getChildren().iterator(); i
084: .hasNext();) {
085: AuxDependency aux = i.next();
086: if (aux.getType() == DependencyType.TARGET) {
087: Target auxtarget = ((AuxDependencyTarget) aux)
088: .getTarget();
089: maxmodtime = Math.max(auxtarget.getModTime(),
090: maxmodtime);
091: if (auxtarget.needsUpdate()) {
092: depup = true;
093: }
094: }
095: }
096:
097: if (depup || maxmodtime > mymodtime) {
098: return true;
099: }
100: return false;
101: }
102: }
103:
104: public void storeValue(Object obj) {
105: synchronized (sharedleaf) {
106: SPCache<Object, Object> cache = SPCacheFactory
107: .getInstance().getCache();
108: cache.setValue(sharedleaf, obj);
109: }
110: }
111:
112: public String toString() {
113: return "[TARGET: " + getType() + " " + getTargetKey() + "@"
114: + getTargetGenerator().getName() + "]";
115: }
116:
117: protected void setModTime(long mtime) {
118: synchronized (sharedleaf) {
119: sharedleaf.setModTime(mtime);
120: }
121: }
122:
123: protected Object getValueFromSPCache() {
124: synchronized (sharedleaf) {
125: return SPCacheFactory.getInstance().getCache().getValue(
126: sharedleaf);
127: }
128: }
129:
130: protected long getModTimeMaybeUpdate()
131: throws TargetGenerationException, XMLException, IOException {
132: long mymodtime = getModTime();
133: long maxmodtime = ResourceUtil.getFileResourceFromDocroot(
134: getTargetKey()).lastModified();
135: NDC.push(" ");
136: TREE.debug("> " + getTargetKey());
137:
138: for (Iterator<AuxDependency> i = this .getAuxDependencyManager()
139: .getChildren().iterator(); i.hasNext();) {
140: AuxDependency aux = i.next();
141: if (aux.getType() == DependencyType.TARGET) {
142: long tmpmodtime = 0;
143: Target auxtarget = ((AuxDependencyTarget) aux)
144: .getTarget();
145: if (auxtarget instanceof TargetImpl) {
146: tmpmodtime = ((TargetImpl) auxtarget)
147: .getModTimeMaybeUpdate();
148: } else {
149: tmpmodtime = auxtarget.getModTime();
150: }
151: maxmodtime = Math.max(tmpmodtime, maxmodtime);
152: }
153: }
154:
155: if (maxmodtime > mymodtime) {
156: try {
157: // invalidate Memcache:
158: storeValue(null);
159: TREE.debug(" [" + getTargetKey()
160: + ": updated leaf node...]");
161: setModTime(maxmodtime);
162: } catch (Exception e) {
163: LOG.error("Error when updating", e);
164: }
165: } else {
166: TREE.debug(" [" + getTargetKey() + ": leaf node...]");
167: }
168: NDC.pop();
169: return getModTime();
170: }
171:
172: }// LeafTarget
|