changeset 564:4c51d71aef13 digilibPDF

more reshuffling of classes
author robcast
date Fri, 17 Dec 2010 22:03:14 +0100
parents 686086d6e6d6
children 8beefd1142b2
files servlet/src/digilib/image/DocuImageImpl.java servlet/src/digilib/image/ImageInfoDocuImage.java servlet/src/digilib/image/ImageJobDescription.java servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/image/ImageWorker.java servlet/src/digilib/image/JAIDocuImage.java servlet/src/digilib/servlet/DigilibConfiguration.java servlet/src/digilib/servlet/DigilibRequest.java servlet/src/digilib/servlet/DocumentBean.java servlet/src/digilib/servlet/Initialiser.java
diffstat 10 files changed, 93 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/DocuImageImpl.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/image/DocuImageImpl.java	Fri Dec 17 22:03:14 2010 +0100
@@ -22,16 +22,13 @@
 
 import java.awt.Image;
 import java.awt.Rectangle;
-import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.RandomAccessFile;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.log4j.Logger;
-import org.marcoschmidt.image.ImageInfo;
 
 import digilib.io.FileOpException;
 import digilib.io.ImageFile;
@@ -74,34 +71,7 @@
 		this.quality = quality;
 	}
 
-    /** Check image size and type and store in ImageFile f */
-    public ImageFile identify(ImageFile imgf) throws IOException {
-        // fileset to store the information
-        File f = imgf.getFile();
-        if (f == null) {
-            throw new IOException("File not found!");
-        }
-        RandomAccessFile raf = new RandomAccessFile(f, "r");
-        // set up ImageInfo object
-        ImageInfo iif = new ImageInfo();
-        iif.setInput(raf);
-        iif.setCollectComments(false);
-        iif.setDetermineImageNumber(false);
-        logger.debug("identifying (ImageInfo) " + f);
-        // try with ImageInfo first
-        if (iif.check()) {
-            ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight());
-            imgf.setSize(d);
-            imgf.setMimetype(iif.getMimeType());
-            //logger.debug("  format:"+iif.getFormatName());
-            raf.close();
-            logger.debug("image size: " + imgf.getSize());
-            return imgf;
-        }
-        return null;
-    }
-        
-    /** Crop and scale the current image.
+	/** Crop and scale the current image.
 	 *
 	 * The current image is cropped to a rectangle of width, height at position
 	 * x_off, y_off. The resulting image is scaled by the factor scale using the
@@ -128,6 +98,14 @@
 		return mimeType;
 	}
 
+    /* (non-Javadoc)
+     * @see digilib.image.DocuImage#identify(digilib.io.ImageFile)
+     */
+    public ImageFile identify(ImageFile imgf) throws IOException {
+        // just a do-nothing implementation
+        return null;
+    }
+
 	public void rotate(double angle) throws ImageOpException {
 		// just a do-nothing implementation
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlet/src/digilib/image/ImageInfoDocuImage.java	Fri Dec 17 22:03:14 2010 +0100
@@ -0,0 +1,51 @@
+/**
+ * 
+ */
+package digilib.image;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import org.marcoschmidt.image.ImageInfo;
+
+import digilib.io.ImageFile;
+
+/** Simple abstract implementation of the <code>DocuImage</code> interface.
+ * Implements only the identify method using the ImageInfo class.
+ * @author casties
+ *
+ */
+public abstract class ImageInfoDocuImage extends DocuImageImpl {
+
+    /** Check image size and type and store in ImageFile f */
+    public ImageFile identify(ImageFile imgf) throws IOException {
+        // fileset to store the information
+        File f = imgf.getFile();
+        if (f == null) {
+            throw new IOException("File not found!");
+        }
+        RandomAccessFile raf = new RandomAccessFile(f, "r");
+        // set up ImageInfo object
+        ImageInfo iif = new ImageInfo();
+        iif.setInput(raf);
+        iif.setCollectComments(false);
+        iif.setDetermineImageNumber(false);
+        logger.debug("identifying (ImageInfo) " + f);
+        // try with ImageInfo first
+        if (iif.check()) {
+            ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight());
+            imgf.setSize(d);
+            imgf.setMimetype(iif.getMimeType());
+            //logger.debug("  format:"+iif.getFormatName());
+            raf.close();
+            logger.debug("image size: " + imgf.getSize());
+            return imgf;
+        } else {
+            raf.close();
+        }
+        return null;
+    }
+        
+
+}
--- a/servlet/src/digilib/image/ImageJobDescription.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/image/ImageJobDescription.java	Fri Dec 17 22:03:14 2010 +0100
@@ -2,9 +2,7 @@
 
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
-import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.Method;
 
 import org.apache.log4j.Logger;
 
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java	Fri Dec 17 22:03:14 2010 +0100
@@ -52,7 +52,7 @@
 import digilib.io.ImageFileset;
 
 /** Implementation of DocuImage using the ImageLoader API of Java 1.4 and Java2D. */
-public class ImageLoaderDocuImage extends DocuImageImpl {
+public class ImageLoaderDocuImage extends ImageInfoDocuImage {
 
 	/** image object */
 	protected BufferedImage img;
@@ -139,38 +139,20 @@
         /*
          * try ImageReader
          */
-        RandomAccessFile raf = new RandomAccessFile(f, "r");
-        ImageInputStream istream = ImageIO.createImageInputStream(raf);
-        Iterator<ImageReader> readers = ImageIO.getImageReaders(istream);
-        if (readers.hasNext()) {
-            ImageReader reader = readers.next();
-            /* are there more readers? */
-            logger.debug("ImageIO: this reader: " + reader.getClass());
-            while (readers.hasNext()) {
-                logger.debug("ImageIO: next reader: "
-                        + readers.next().getClass());
-            }
-            try {
-                reader.setInput(istream);
-                ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0));
-                imageFile.setSize(d);
-                //String t = reader.getFormatName();
-                String t = FileOps.mimeForFile(f);
-                imageFile.setMimetype(t);
-                //logger.debug("  format:"+t);
-                if (imgfs != null) {
-                    imgfs.setAspect(d);
-                }
-                return imageFile;
-            } finally {
-                // dispose the reader to free resources
-                reader.dispose();
-                raf.close();
-            }
+        if ((reader == null) || (imgFile != imageFile.getFile())) {
+            getReader(imageFile);
         }
-        throw new FileOpException("ERROR: unknown image file format!");
+        ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0));
+        imageFile.setSize(d);
+        // String t = reader.getFormatName();
+        String t = FileOps.mimeForFile(f);
+        imageFile.setMimetype(t);
+        // logger.debug("  format:"+t);
+        if (imgfs != null) {
+            imgfs.setAspect(d);
+        }
+        return imageFile;
     }
-
     
     /* load image file */
 	public void loadImage(ImageFile f) throws FileOpException {
@@ -195,7 +177,6 @@
 			// clean up old reader
 			dispose();
 		}
-		// System.gc();
 		RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r");
 		ImageInputStream istream = new FileImageInputStream(rf);
 		// Iterator readers = ImageIO.getImageReaders(istream);
@@ -203,15 +184,15 @@
 		logger.debug("File type:" + mt);
 		Iterator<ImageReader> readers = ImageIO.getImageReadersByMIMEType(mt);
 		if (!readers.hasNext()) {
+		    rf.close();
 			throw new FileOpException("Unable to load File!");
 		}
 		reader = readers.next();
 		/* are there more readers? */
 		logger.debug("ImageIO: this reader: " + reader.getClass());
-		while (readers.hasNext()) {
+		/* while (readers.hasNext()) {
 			logger.debug("ImageIO: next reader: " + readers.next().getClass());
-		}
-		// */
+		} */
 		reader.setInput(istream);
 		imgFile = f.getFile();
 		return reader;
--- a/servlet/src/digilib/image/ImageWorker.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/image/ImageWorker.java	Fri Dec 17 22:03:14 2010 +0100
@@ -42,7 +42,7 @@
         /* crop and scale image */
 
         // new DocuImage instance
-        DocuImage docuImage = dlConfig.getDocuImageInstance();
+        DocuImage docuImage = DigilibConfiguration.getDocuImageInstance();
         if (docuImage == null) {
             throw new ImageOpException("Unable to load DocuImage class!");
         }
--- a/servlet/src/digilib/image/JAIDocuImage.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/image/JAIDocuImage.java	Fri Dec 17 22:03:14 2010 +0100
@@ -49,7 +49,7 @@
 import digilib.io.ImageFileset;
 
 /** A DocuImage implementation using Java Advanced Imaging Library. */
-public class JAIDocuImage extends DocuImageImpl {
+public class JAIDocuImage extends ImageInfoDocuImage {
 
 	protected RenderedImage img;
 
--- a/servlet/src/digilib/servlet/DigilibConfiguration.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/servlet/DigilibConfiguration.java	Fri Dec 17 22:03:14 2010 +0100
@@ -55,10 +55,7 @@
 public class DigilibConfiguration extends ParameterMap {
 
 	/** DocuImage class instance */
-	private Class<DocuImageImpl> docuImageClass = null;
-
-	/** DocuImage instance */
-	private static DocuImage docuImage = null;
+	private static Class<DocuImageImpl> docuImageClass = null;
 
 	/** Log4J logger */
 	private Logger logger = Logger.getLogger("digilib.config");
@@ -194,7 +191,8 @@
 	 * read parameter list from the XML file in init parameter "config-file"
 	 * or file digilib-config.xml
 	 */
-	public void readConfig(ServletConfig c) throws Exception {
+	@SuppressWarnings("unchecked")
+    public void readConfig(ServletConfig c) throws Exception {
 
 		/*
 		 * Get config file name. The file name is first looked for as an init
@@ -252,24 +250,20 @@
 				newParameter(confEntry.getKey(), null, confEntry.getValue(), 'f');
 			}
 		}
-		// initialise static DocuImage instance
-		DigilibConfiguration.docuImage = getDocuImageInstance();
+		// initialise static DocuImage class instance
+		DigilibConfiguration.docuImageClass = (Class<DocuImageImpl>) Class.forName(getAsString("docuimage-class"));
 	}
 
 	/**
 	 * Creates a new DocuImage instance.
 	 * 
-	 * The type of DocuImage is specified by docuImageType.
+	 * The type of DocuImage is specified by docuimage-class.
 	 * 
 	 * @return DocuImage
 	 */
-	@SuppressWarnings("unchecked")
-    public DocuImage getDocuImageInstance() {
+    public static DocuImage getDocuImageInstance() {
 		DocuImageImpl di = null;
 		try {
-			if (docuImageClass == null) {
-				docuImageClass = (Class<DocuImageImpl>) Class.forName(getAsString("docuimage-class"));
-			}
 			di = docuImageClass.newInstance();
 		} catch (Exception e) {
 		}
@@ -284,20 +278,22 @@
 	 * @throws IOException
 	 */
 	public static ImageFile docuImageIdentify(ImageFile imgf) throws IOException {
-		return docuImage.identify(imgf);
+	    // use fresh DocuImage instance
+	    DocuImage di = getDocuImageInstance();
+		return di.identify(imgf);
 	}
 	
 	/**
 	 * @return Returns the docuImageClass.
 	 */
-	public Class<DocuImageImpl> getDocuImageClass() {
+	public static Class<DocuImageImpl> getDocuImageClass() {
 		return docuImageClass;
 	}
 
 	/**
 	 * @param docuImageClass The docuImageClass to set.
 	 */
-	public void setDocuImageClass(Class<DocuImageImpl> docuImageClass) {
-		this.docuImageClass = docuImageClass;
+	public static void setDocuImageClass(Class<DocuImageImpl> dic) {
+		docuImageClass = dic;
 	}
 }
--- a/servlet/src/digilib/servlet/DigilibRequest.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/servlet/DigilibRequest.java	Fri Dec 17 22:03:14 2010 +0100
@@ -392,7 +392,7 @@
 	 * @param request
 	 *            ServletRequest to get parameters from.
 	 */
-	@SuppressWarnings("unchecked") // ServletRequest.getParameterNames() returns naked Enumeration
+	//@SuppressWarnings("unchecked") // ServletRequest.getParameterNames() returns naked Enumeration
     public void setWithParamRequest(ServletRequest request) {
 		setValue("servlet.request", request);
 		// go through all request parameters
--- a/servlet/src/digilib/servlet/DocumentBean.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/servlet/DocumentBean.java	Fri Dec 17 22:03:14 2010 +0100
@@ -36,7 +36,6 @@
 import digilib.image.ImageSize;
 import digilib.io.DocuDirCache;
 import digilib.io.DocuDirectory;
-import digilib.io.FileOps;
 import digilib.io.FileOps.FileClass;
 import digilib.io.ImageFile;
 import digilib.io.ImageFileset;
--- a/servlet/src/digilib/servlet/Initialiser.java	Fri Dec 17 20:42:25 2010 +0100
+++ b/servlet/src/digilib/servlet/Initialiser.java	Fri Dec 17 22:03:14 2010 +0100
@@ -37,7 +37,6 @@
 import digilib.image.DocuImage;
 import digilib.io.AliasingDocuDirCache;
 import digilib.io.DocuDirCache;
-import digilib.io.FileOps;
 import digilib.io.FileOps.FileClass;
 import digilib.util.DigilibJobCenter;
 
@@ -132,7 +131,7 @@
 					dlConfig.setValue("auth-file", authConf);
 				}
 				// DocuImage class
-				DocuImage di = dlConfig.getDocuImageInstance();
+				DocuImage di = DigilibConfiguration.getDocuImageInstance();
 				dlConfig.setValue("servlet.docuimage.class", di.getClass().getName());
 				// digilib worker threads
 				int nt = dlConfig.getAsInt("worker-threads");