Using BerkeleyDB for resized images.
This commit is contained in:
parent
65a8226302
commit
167d068c7b
5 changed files with 59 additions and 25 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,3 +1,5 @@
|
||||||
target
|
target
|
||||||
cache.properties
|
cache.properties
|
||||||
build
|
build
|
||||||
|
db
|
||||||
|
|
||||||
|
|
|
||||||
19
pom.xml
19
pom.xml
|
|
@ -108,16 +108,17 @@
|
||||||
<version>2.4</version>
|
<version>2.4</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>net.sf.ehcache</groupId>
|
|
||||||
<artifactId>ehcache</artifactId>
|
|
||||||
<version>1.5.0</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.14</version>
|
<version>1.2.14</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sleepycat</groupId>
|
||||||
|
<artifactId>je</artifactId>
|
||||||
|
<version>4.0.92</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.2</version></dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
|
|
@ -125,8 +126,14 @@
|
||||||
<name>forkalsrud.org maven proxy</name>
|
<name>forkalsrud.org maven proxy</name>
|
||||||
<releases><enabled>true</enabled></releases>
|
<releases><enabled>true</enabled></releases>
|
||||||
<snapshots><enabled>false</enabled></snapshots>
|
<snapshots><enabled>false</enabled></snapshots>
|
||||||
<url>http://www.forkalsrud.org/maven-proxy/repository</url>
|
<url>http://forkalsrud.org:8080/maven-proxy/repository</url>
|
||||||
<layout>legacy</layout>
|
<layout>legacy</layout>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>oracleReleases</id>
|
||||||
|
<name>Oracle Released Java Packages</name>
|
||||||
|
<url>http://download.oracle.com/maven</url>
|
||||||
|
<layout>default</layout>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
@ -11,12 +12,9 @@ import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import net.sf.ehcache.Cache;
|
|
||||||
import net.sf.ehcache.CacheManager;
|
|
||||||
import net.sf.ehcache.Element;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.log4j.PropertyConfigurator;
|
import org.apache.log4j.PropertyConfigurator;
|
||||||
|
import org.forkalsrud.album.db.ThumbnailDatabase;
|
||||||
import org.forkalsrud.album.exif.DirectoryEntry;
|
import org.forkalsrud.album.exif.DirectoryEntry;
|
||||||
import org.forkalsrud.album.exif.Entry;
|
import org.forkalsrud.album.exif.Entry;
|
||||||
import org.forkalsrud.album.exif.FileEntry;
|
import org.forkalsrud.album.exif.FileEntry;
|
||||||
|
|
@ -27,38 +25,56 @@ public class AlbumServlet
|
||||||
{
|
{
|
||||||
File base;
|
File base;
|
||||||
String basePrefix;
|
String basePrefix;
|
||||||
Cache imageCache;
|
// Cache imageCache;
|
||||||
CacheManager cacheManager;
|
// CacheManager cacheManager;
|
||||||
PictureScaler pictureScaler;
|
PictureScaler pictureScaler;
|
||||||
long lastCacheFlushTime;
|
long lastCacheFlushTime;
|
||||||
|
ThumbnailDatabase db = new ThumbnailDatabase();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init()
|
public void init()
|
||||||
throws ServletException
|
throws ServletException
|
||||||
{
|
{
|
||||||
|
log4jInit("/log4j.properties");
|
||||||
System.out.println("in init of Album");
|
System.out.println("in init of Album");
|
||||||
base = new File(getServletConfig().getInitParameter("base")).getAbsoluteFile();
|
base = new File(getServletConfig().getInitParameter("base")).getAbsoluteFile();
|
||||||
basePrefix = "/" + base.getName();
|
basePrefix = "/" + base.getName();
|
||||||
|
|
||||||
PropertyConfigurator.configure("log4j.properties");
|
|
||||||
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
|
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");
|
||||||
cacheManager = CacheManager.create();
|
|
||||||
imageCache = cacheManager.getCache("imageCache");
|
String dbDirName = getServletConfig().getInitParameter("dbdir");
|
||||||
|
File dbDir = dbDirName != null ? new File(dbDirName) : new File(System.getProperty("java.io.tmpdir"), "album");
|
||||||
|
dbDir.mkdir();
|
||||||
|
db.init(dbDir);
|
||||||
|
// cacheManager = CacheManager.create();
|
||||||
|
// imageCache = cacheManager.getCache("imageCache");
|
||||||
pictureScaler = new PictureScaler();
|
pictureScaler = new PictureScaler();
|
||||||
lastCacheFlushTime = System.currentTimeMillis();
|
lastCacheFlushTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void log4jInit(String resource) {
|
||||||
|
try {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.load(getClass().getResourceAsStream(resource));
|
||||||
|
PropertyConfigurator.configure(props);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
imageCache.flush();
|
System.out.println("Shutting down Album");
|
||||||
cacheManager.shutdown();
|
db.destroy();
|
||||||
|
// imageCache.flush();
|
||||||
|
// cacheManager.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest req, HttpServletResponse res)
|
public void doGet(HttpServletRequest req, HttpServletResponse res)
|
||||||
throws ServletException, IOException
|
throws ServletException, IOException
|
||||||
{
|
{
|
||||||
if (req.getParameter("cacheFlush") != null) imageCache.flush();
|
// if (req.getParameter("cacheFlush") != null) imageCache.flush();
|
||||||
|
|
||||||
req.setAttribute("assets", req.getContextPath() + "/assets");
|
req.setAttribute("assets", req.getContextPath() + "/assets");
|
||||||
req.setAttribute("req", req);
|
req.setAttribute("req", req);
|
||||||
|
|
@ -166,28 +182,32 @@ public class AlbumServlet
|
||||||
|
|
||||||
String key = file.getPath() + ":" + size;
|
String key = file.getPath() + ":" + size;
|
||||||
|
|
||||||
CachedImage cimg = null;
|
CachedImage cimg = db.load(key);
|
||||||
Element element = imageCache.get(key);
|
// Element element = imageCache.get(key);
|
||||||
if (element != null) {
|
// if (element != null) {
|
||||||
cimg = (CachedImage) element.getObjectValue();
|
// cimg = (CachedImage) element.getObjectValue();
|
||||||
|
if (cimg != null) {
|
||||||
if (cimg.lastModified == file.lastModified()) {
|
if (cimg.lastModified == file.lastModified()) {
|
||||||
System.out.println("cache hit on " + key);
|
System.out.println("cache hit on " + key);
|
||||||
} else {
|
} else {
|
||||||
System.out.println(" " + key + " has changed so cache entry wil be refreshed");
|
System.out.println(" " + key + " has changed so cache entry wil be refreshed");
|
||||||
imageCache.remove(key);
|
// imageCache.remove(key);
|
||||||
cimg = null;
|
cimg = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cimg == null) {
|
if (cimg == null) {
|
||||||
// try {
|
// try {
|
||||||
cimg = pictureScaler.scalePicture(file, thumbnail, size);
|
cimg = pictureScaler.scalePicture(file, thumbnail, size);
|
||||||
imageCache.put(new Element(key, cimg));
|
db.store(key, cimg);
|
||||||
|
// imageCache.put(new Element(key, cimg));
|
||||||
|
/*
|
||||||
long millisSinceLastFlush = System.currentTimeMillis() - lastCacheFlushTime;
|
long millisSinceLastFlush = System.currentTimeMillis() - lastCacheFlushTime;
|
||||||
if (millisSinceLastFlush > 10 * 60 * 1000L) {
|
if (millisSinceLastFlush > 10 * 60 * 1000L) {
|
||||||
imageCache.flush();
|
imageCache.flush();
|
||||||
lastCacheFlushTime = System.currentTimeMillis();
|
lastCacheFlushTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
System.out.println(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + imageCache.getSize() + " entries");
|
*/
|
||||||
|
System.out.println(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + db.size() + " entries");
|
||||||
// } catch (TimeoutException e) {
|
// } catch (TimeoutException e) {
|
||||||
// res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
// res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
||||||
// return;
|
// return;
|
||||||
|
|
|
||||||
|
|
@ -171,6 +171,7 @@ public class PictureScaler {
|
||||||
reader.setInput(iis, true);
|
reader.setInput(iis, true);
|
||||||
ImageReadParam param = reader.getDefaultReadParam();
|
ImageReadParam param = reader.getDefaultReadParam();
|
||||||
BufferedImage img = reader.read(0, param);
|
BufferedImage img = reader.read(0, param);
|
||||||
|
iis.close();
|
||||||
|
|
||||||
// The orientation is about flipping and rotating. Here is what an 'F' looks like
|
// The orientation is about flipping and rotating. Here is what an 'F' looks like
|
||||||
// on pictures with each orientation.
|
// on pictures with each orientation.
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,10 @@
|
||||||
<param-name>base</param-name>
|
<param-name>base</param-name>
|
||||||
<param-value>photos</param-value>
|
<param-value>photos</param-value>
|
||||||
</init-param>
|
</init-param>
|
||||||
|
<init-param>
|
||||||
|
<param-name>dbdir</param-name>
|
||||||
|
<param-value>db</param-value>
|
||||||
|
</init-param>
|
||||||
</servlet>
|
</servlet>
|
||||||
|
|
||||||
<servlet>
|
<servlet>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue