[Yanel-commits] rev 59441 - public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image
michi at wyona.com
michi at wyona.com
Mon Jul 18 00:00:17 CEST 2011
Author: michi
Date: 2011-07-18 00:00:17 +0200 (Mon, 18 Jul 2011)
New Revision: 59441
Modified:
public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java
Log:
compare last modified
Modified: public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java 2011-07-17 21:25:24 UTC (rev 59440)
+++ public/yanel/trunk/src/contributions/resources/image/src/java/org/wyona/yanel/impl/resources/image/ImageResource.java 2011-07-17 22:00:17 UTC (rev 59441)
@@ -47,8 +47,6 @@
throw new org.wyona.yanel.core.ResourceNotFoundException("No such image: " + getPath());
}
- // TODO: Compare last modified!
-
BufferedImage sourceImg = ImageIO.read(getRealm().getRepository().getNode(getPath()).getInputStream());
int sourceWidth = sourceImg.getWidth();
int sourceHeight = sourceImg.getHeight();
@@ -62,10 +60,16 @@
double scaleFactor = getScaleFactor(sourceWidth, sourceHeight, destWidth, destHeight); //(double) destHeight / (double) sourceHeight;
if (log.isDebugEnabled()) log.debug("Scale factor: " + scaleFactor);
- Node cacheNode = getCacheNode();
+ Node cacheNode;
+ if (existsMoreRecentCacheNode()) {
+ //log.debug("Get image from cache...");
+ cacheNode = getCacheNode();
+ } else {
+ log.debug("Scale image and add scaled version to cache...");
+ cacheNode = createCacheNode();
+ ImageIO.write(scale(sourceImg, scaleFactor), "JPG", cacheNode.getOutputStream());
+ }
- ImageIO.write(scale(sourceImg, scaleFactor), "JPG", cacheNode.getOutputStream());
-
View view = new View();
view.setInputStream(cacheNode.getInputStream());
view.setMimeType("image/jpeg");
@@ -159,9 +163,9 @@
}
/**
- * Get cache node
+ * Create cache node
*/
- private Node getCacheNode() throws Exception {
+ private Node createCacheNode() throws Exception {
String cacheRootPath = getResourceConfigProperty("cache-root-path");
if (cacheRootPath == null) {
cacheRootPath = DEFAULT_CACHE_DIRECTORY;
@@ -174,7 +178,52 @@
log.warn("Cached image did not exist yet, hence has been created: " + cacheNode.getPath());
} else {
cacheNode = getRealm().getRepository().getNode(cacheRootPath + getPath());
+ log.error("The cache already seems to exist: " + cacheNode.getPath());
}
return cacheNode;
}
+
+ /**
+ * Get cache node
+ */
+ private Node getCacheNode() throws Exception {
+ String cacheRootPath = getResourceConfigProperty("cache-root-path");
+ if (cacheRootPath == null) {
+ cacheRootPath = DEFAULT_CACHE_DIRECTORY;
+ log.debug("No cache root path configured within resource configuration. Use default '" + cacheRootPath + "'!");
+ }
+
+ if (getRealm().getRepository().existsNode(cacheRootPath + getPath())) {
+ return getRealm().getRepository().getNode(cacheRootPath + getPath());
+ } else {
+ log.error("No such cache node: " + cacheRootPath + getPath());
+ return null;
+ }
+ }
+
+ /**
+ * Check whether cache node exists and if so, then compare last modified
+ */
+ private boolean existsMoreRecentCacheNode() throws Exception {
+ String cacheRootPath = getResourceConfigProperty("cache-root-path");
+ if (cacheRootPath == null) {
+ cacheRootPath = DEFAULT_CACHE_DIRECTORY;
+ log.debug("No cache root path configured within resource configuration. Use default '" + cacheRootPath + "'!");
+ }
+
+ if (getRealm().getRepository().existsNode(cacheRootPath + getPath())) {
+ long lastModifiedCacheNode = getRealm().getRepository().getNode(cacheRootPath + getPath()).getLastModified();
+ long lastModifiedOriginalNode = getRealm().getRepository().getNode(getPath()).getLastModified();
+ if (lastModifiedCacheNode > lastModifiedOriginalNode) {
+ //log.debug("Compare last modified: " + lastModifiedCacheNode + ", " + lastModifiedOriginalNode);
+ return true;
+ } else {
+ log.warn("Cached image seems to be out of date: " + cacheRootPath + getPath());
+ return false;
+ }
+ } else {
+ log.error("No such cache node: " + cacheRootPath + getPath());
+ return false;
+ }
+ }
}
More information about the Yanel-commits
mailing list