upayavira 2003/08/17 06:43:00
Modified: src/java/org/apache/cocoon/bean CocoonBean.java
src/java/org/apache/cocoon/environment/commandline
FileSavingEnvironment.java
LinkSamplingEnvironment.java
src/java/org/apache/cocoon/sitemap LinkGatherer.java
LinkTranslator.java
Log:
Fixing bean to use cache, after Vadim's fixes.
Now doesn't generate pages that are already up-to-date.
Note: only link view works - link gathering is currently broken.
Revision Changes Path
1.15 +24 -11 cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java
Index: CocoonBean.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- CocoonBean.java 18 Jul 2003 05:00:06 -0000 1.14
+++ CocoonBean.java 17 Aug 2003 13:43:00 -0000 1.15
@@ -583,7 +583,7 @@
}
int status =
- getPage(deparameterizedURI, parameters, null, null, outputStream);
+ getPage(deparameterizedURI, 0L, parameters, null, null, outputStream);
if (status >= 400) {
throw new ProcessingException("Resource not found: " + status);
@@ -707,6 +707,9 @@
*/
private Collection processTarget(Target target) throws Exception {
+ long startTimeMillis = System.currentTimeMillis();
+ int status = 0;
+
String uri = target.getSourceURI();
int linkCount = 0;
@@ -811,9 +814,10 @@
// Process URI
DelayedOutputStream output = new DelayedOutputStream();
try {
- int status =
+ status =
getPage(
deparameterizedURI,
+ target.getLastModified(filename),
parameters,
confirmExtension ? translatedLinks : null,
gatheredLinks,
@@ -855,7 +859,7 @@
filename,
DefaultNotifyingBuilder.getRootCause(pe).getMessage());
} finally {
- if (output != null) {
+ if (output != null && status != -1) {
ModifiableSource source = target.getSource(filename);
try {
@@ -863,16 +867,14 @@
output.setFileOutputStream(stream);
output.flush();
+ output.close();
+ } catch (IOException ioex) {
+ log.warn(ioex.toString());
} finally {
target.releaseSource(source);
}
}
- try {
- if (output != null)
- output.close();
- } catch (IOException ioex) {
- log.warn(ioex.toString());
- }
+
}
} catch (Exception rnfe) {
log.warn("Could not process URI: " + deparameterizedURI);
@@ -887,6 +889,9 @@
targets.add(target.getDerivedTarget(link));
}
}
+ double d = (System.currentTimeMillis()- startTimeMillis);
+ String time = " [" + (d/1000) + " seconds]";
+ System.out.println(" "+ time);
return targets;
}
@@ -1069,6 +1074,7 @@
*/
protected int getPage(
String deparameterizedURI,
+ long lastModified,
Map parameters,
Map links,
List gatheredLinks,
@@ -1077,6 +1083,7 @@
FileSavingEnvironment env =
new FileSavingEnvironment(
deparameterizedURI,
+ lastModified,
context,
attributes,
parameters,
@@ -1090,7 +1097,9 @@
cocoon.process(env);
// if we get here, the page was created :-)
- return env.getStatus();
+ int status = env.getStatus();
+ if (!env.isModified()){ status = -1 ; }
+ return status;
}
/** Class <code>NullOutputStream</code> here. */
@@ -1286,6 +1295,10 @@
return (ModifiableSource) src;
}
+ public long getLastModified(String filename) throws IOException,
ProcessingException {
+ return getSource(filename).getLastModified();
+ }
+
public void releaseSource(ModifiableSource source) {
sourceResolver.release(source);
}
1.4 +45 -1
cocoon-2.1/src/java/org/apache/cocoon/environment/commandline/FileSavingEnvironment.java
Index: FileSavingEnvironment.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/commandline/FileSavingEnvironment.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FileSavingEnvironment.java 12 May 2003 13:26:17 -0000 1.3
+++ FileSavingEnvironment.java 17 Aug 2003 13:43:00 -0000 1.4
@@ -70,7 +70,11 @@
*/
public class FileSavingEnvironment extends AbstractCommandLineEnvironment {
+ protected boolean modified = true;
+ protected long sourceLastModified = 0L;
+
public FileSavingEnvironment(String uri,
+ long lastModified,
File context,
Map attributes,
Map parameters,
@@ -84,6 +88,7 @@
this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, new
CommandLineRequest(this, null, uri, null, attributes, parameters));
this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, new
CommandLineResponse());
this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, cliContext);
+ this.sourceLastModified = lastModified;
if (links != null) {
this.objectModel.put(Constants.LINK_OBJECT, links);
}
@@ -91,6 +96,45 @@
this.objectModel.put(Constants.LINK_COLLECTION_OBJECT, gatheredLinks);
}
}
+ public FileSavingEnvironment(String uri,
+ File context,
+ Map attributes,
+ Map parameters,
+ Map links,
+ List gatheredLinks,
+ CommandLineContext cliContext,
+ OutputStream stream,
+ Logger log)
+ throws MalformedURLException {
+ this(uri, 0L, context, attributes, parameters, links, gatheredLinks,
cliContext, stream, log);
+ }
+
+ /**
+ * Check if the response has been modified since the same
+ * "resource" was requested.
+ * The caller has to test if it is really the same "resource"
+ * which is requested.
+ * @return true if the response is modified or if the
+ * environment is not able to test it
+ */
+ public boolean isResponseModified(long cacheLastModified) {
+ if (cacheLastModified != 0) {
+ return cacheLastModified / 1000 > sourceLastModified / 1000;
+ }
+ return true;
+ }
+
+ /**
+ * Mark the response as not modified.
+ */
+ public void setResponseIsNotModified() {
+ this.modified = false;
+ }
+
+ public boolean isModified() {
+ return this.modified;
+ }
+
}
1.4 +12 -15
cocoon-2.1/src/java/org/apache/cocoon/environment/commandline/LinkSamplingEnvironment.java
Index: LinkSamplingEnvironment.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/environment/commandline/LinkSamplingEnvironment.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- LinkSamplingEnvironment.java 10 Aug 2003 19:58:09 -0000 1.3
+++ LinkSamplingEnvironment.java 17 Aug 2003 13:43:00 -0000 1.4
@@ -50,16 +50,17 @@
*/
package org.apache.cocoon.environment.commandline;
-import org.apache.avalon.framework.logger.Logger;
-
-import org.apache.cocoon.Constants;
-import org.apache.cocoon.environment.ObjectModelHelper;
-
import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.environment.ObjectModelHelper;
/**
* This environment is sample the links of the resource.
@@ -101,7 +102,7 @@
* Indicates if other links are present.
*/
public Collection getLinks() throws IOException {
- ArrayList list = new ArrayList();
+ HashSet set = new HashSet();
if (!skip) {
BufferedReader buffer = null;
try {
@@ -110,13 +111,9 @@
new ByteArrayInputStream(
((ByteArrayOutputStream)
super.outputStream).toByteArray())));
- while (true) {
- String line = buffer.readLine();
- if (line == null)
- break;
- if (!list.contains(line)) {
- list.add(line);
- }
+ String line;
+ while ((line = buffer.readLine()) !=null) {
+ set.add(line);
}
} finally {
// explictly close the input
@@ -129,6 +126,6 @@
}
}
}
- return list;
+ return set;
}
}
1.3 +27 -2 cocoon-2.1/src/java/org/apache/cocoon/sitemap/LinkGatherer.java
Index: LinkGatherer.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/sitemap/LinkGatherer.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LinkGatherer.java 28 Mar 2003 12:55:42 -0000 1.2
+++ LinkGatherer.java 17 Aug 2003 13:43:00 -0000 1.3
@@ -53,9 +53,14 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.Constants;
+import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.Transformer;
import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
+
+import org.apache.excalibur.source.SourceValidity;
+import org.apache.excalibur.source.impl.validity.NOPValidity;
+
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@@ -67,7 +72,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a>
* @version CVS $Id$
*/
-public class LinkGatherer extends ExtendedXLinkPipe implements Transformer {
+public class LinkGatherer extends ExtendedXLinkPipe implements Transformer,
CacheableProcessingComponent {
private List links;
@@ -78,6 +83,26 @@
public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters par) throws ProcessingException,
SAXException, IOException {
this.links = (List)objectModel.get(Constants.LINK_COLLECTION_OBJECT);
+ }
+
+ /**
+ * Generate the unique key.
+ * This key must be unique inside the space of this component.
+ *
+ * @return The generated key hashes the src
+ */
+ public java.io.Serializable getKey() {
+ return "1";
+ }
+
+ /**
+ * Generate the validity object.
+ *
+ * @return The generated validity object or <code>null</code> if the
+ * component is currently not cacheable.
+ */
+ public SourceValidity getValidity() {
+ return NOPValidity.SHARED_INSTANCE;
}
public void simpleLink(String href, String role, String arcrole, String title,
String show, String actuate, String uri,
1.2 +26 -2
cocoon-2.1/src/java/org/apache/cocoon/sitemap/LinkTranslator.java
Index: LinkTranslator.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/sitemap/LinkTranslator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LinkTranslator.java 9 Mar 2003 00:09:38 -0000 1.1
+++ LinkTranslator.java 17 Aug 2003 13:43:00 -0000 1.2
@@ -53,9 +53,13 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.Constants;
import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.Transformer;
import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
+import org.apache.excalibur.source.SourceValidity;
+import org.apache.excalibur.source.impl.validity.NOPValidity;
+
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@@ -66,7 +70,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
* @version CVS $Id$
*/
-public class LinkTranslator extends ExtendedXLinkPipe implements Transformer {
+public class LinkTranslator extends ExtendedXLinkPipe implements Transformer,
CacheableProcessingComponent {
private Map links;
/**
@@ -76,6 +80,26 @@
public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters par) throws ProcessingException,
SAXException, IOException {
this.links = (Map)objectModel.get(Constants.LINK_OBJECT);
+ }
+
+ /**
+ * Generate the unique key.
+ * This key must be unique inside the space of this component.
+ *
+ * @return The generated key hashes the src
+ */
+ public java.io.Serializable getKey() {
+ return "1";
+ }
+
+ /**
+ * Generate the validity object.
+ *
+ * @return The generated validity object or <code>null</code> if the
+ * component is currently not cacheable.
+ */
+ public SourceValidity getValidity() {
+ return NOPValidity.SHARED_INSTANCE;
}
public void simpleLink(String href, String role, String arcrole, String title,
String show, String actuate, String uri,