changeset 1237:c3f4f1c02b11

digilib reads configuration from digilib.properties file in classpath (if digilib-config.xml is not found).
author robcast
date Tue, 14 Jan 2014 09:44:22 +0100
parents e8862382a8f2
children f863da95ed6e
files common/src/main/java/digilib/conf/DigilibConfiguration.java servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java webapp/src/main/webapp/jquery/jquery-test-embedded.html
diffstat 3 files changed, 119 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/common/src/main/java/digilib/conf/DigilibConfiguration.java	Mon Jan 13 14:13:05 2014 +0100
+++ b/common/src/main/java/digilib/conf/DigilibConfiguration.java	Tue Jan 14 09:44:22 2014 +0100
@@ -25,7 +25,11 @@
  * Author: Robert Casties (robcast@berlios.de)
  */
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Properties;
 
 import javax.imageio.ImageIO;
 
@@ -34,6 +38,7 @@
 
 import digilib.image.DocuImage;
 import digilib.image.DocuImageFactory;
+import digilib.util.Parameter;
 import digilib.util.ParameterMap;
 
 /**
@@ -47,6 +52,8 @@
     protected static Logger logger = Logger.getLogger(DigilibConfiguration.class);
     
     private static boolean isLoggerConfigured = false;
+    
+    protected static String propertiesFileName = "digilib.properties";
 
     /** digilib version */
     public static String getVersion() {
@@ -85,6 +92,44 @@
     }
 
     /**
+     * read parameters from properties file digilib.properties in class path.
+     */
+    public void readConfig() {
+        Properties props = new Properties();
+        InputStream s = Thread.currentThread().getContextClassLoader()
+                .getResourceAsStream(propertiesFileName);
+        if (s != null) {
+            try {
+                props.load(s);
+                s.close();
+                for (Entry<Object, Object> confEntry : props.entrySet()) {
+                    Parameter param = get((String) confEntry.getKey());
+                    if (param != null) {
+                        if (param.getType() == 's') {
+                            // type 's' Parameters are not overwritten.
+                            continue;
+                        }
+                        if (!param.setValueFromString((String) confEntry.getValue())) {
+                            /*
+                             * automatic conversion failed -- try special casesß
+                             */
+                            logger.warn("Unable to parse config parameter: "+param.getName());
+                        }
+                    } else {
+                        // parameter unknown -- just add
+                        newParameter((String) confEntry.getKey(), null, confEntry.getValue(), 'u');
+                    }
+                }
+                // set config file path parameter
+                newParameter("digilib.config.file", Thread.currentThread().getContextClassLoader()
+                        .getResource("digilib.properties").toString(), null, 's');
+            } catch (IOException e) {
+                logger.error("Error reading digilib properties file.", e);
+            }
+        }
+    }
+    
+    /**
      * Configure digilib.
      * 
      * Sets up Factories and Singletons using the configuration. 
--- a/servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java	Mon Jan 13 14:13:05 2014 +0100
+++ b/servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java	Tue Jan 14 09:44:22 2014 +0100
@@ -28,9 +28,12 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Properties;
 
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
@@ -155,8 +158,9 @@
     }
 
     /**
-     * read parameter list from the XML file in init parameter "config-file" or
-     * file digilib-config.xml
+     * read parameter list from the file WEB-INF/digilib-config.xml 
+     * or digilib.properties in class path.
+     * 
      * @throws IOException 
      * @throws SAXException 
      */
@@ -180,57 +184,85 @@
             // setup config file list reader
             XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value");
             // read config file into HashMap
-            Map<String, String> confTable = lilo.loadUri(f.toURI());
+            Map<String, String> map = lilo.loadUri(f.toURI());
 
             // set config file path parameter
             setValue("servlet.config.file", f.getCanonicalPath());
 
+            readConfigEntries(c, map);
+        } else {
             /*
-             * read parameters
+             * try properties file digilib.properties
              */
-
-            for (Entry<String, String> confEntry : confTable.entrySet()) {
-                Parameter param = get(confEntry.getKey());
-                if (param != null) {
-                    if (param.getType() == 's') {
-                        // type 's' Parameters are not overwritten.
-                        continue;
-                    }
-                    if (!param.setValueFromString(confEntry.getValue())) {
-                        /*
-                         * automatic conversion failed -- try special cases
-                         */
-
-                        // basedir-list
-                        if (confEntry.getKey().equals("basedir-list")) {
-                            // split list into directories
-                            String[] dirs = FileOps.pathToArray(confEntry.getValue());
-                            for (int j = 0; j < dirs.length; j++) {
-                                // make relative directory paths be inside the webapp
-                                dirs[j] = ServletOps.getFile(dirs[j], c);
-                            }
-                            if (dirs != null) {
-                                param.setValue(dirs);
-                            }
-                        }
-                    }
-                } else {
-                    // parameter unknown -- just add
-                    newParameter(confEntry.getKey(), null, confEntry.getValue(), 'u');
+            Properties props = new Properties();
+            InputStream s = Thread.currentThread().getContextClassLoader()
+                    .getResourceAsStream("digilib.properties");
+            if (s != null) {
+                props.load(s);
+                s.close();
+                // re-pack entries
+                HashMap<String,String> map = new HashMap<String,String>();
+                for (Entry<Object, Object> e : props.entrySet()) {
+                    map.put((String)e.getKey(), (String)e.getValue());
                 }
-            }
-        } else {
-            logger.warn("No digilib config file! Using defaults!");
-            // update basedir-list
-            String[] dirs = (String[]) this.getValue("basedir-list");
-            for (int j = 0; j < dirs.length; j++) {
-                // make relative directory paths be inside the webapp
-                dirs[j] = ServletOps.getFile(dirs[j], c);
+                readConfigEntries(c, map);
+                // set config file path parameter
+                setValue("servlet.config.file", Thread.currentThread().getContextClassLoader()
+                        .getResource("digilib.properties").toString());
+            } else {
+                logger.warn("No digilib config file! Using defaults!");
+                // update basedir-list
+                String[] dirs = (String[]) this.getValue("basedir-list");
+                for (int j = 0; j < dirs.length; j++) {
+                    // make relative directory paths be inside the webapp
+                    dirs[j] = ServletOps.getFile(dirs[j], c);
+                }
             }
         }
 
     }
 
+    /**
+     * @param ctx
+     * @param conf
+     */
+    public void readConfigEntries(ServletContext ctx, Map<String, String> conf) {
+        /*
+         * read parameters
+         */
+
+        for (Entry<String, String> confEntry : conf.entrySet()) {
+            Parameter param = get(confEntry.getKey());
+            if (param != null) {
+                if (param.getType() == 's') {
+                    // type 's' Parameters are not overwritten.
+                    continue;
+                }
+                if (!param.setValueFromString(confEntry.getValue())) {
+                    /*
+                     * automatic conversion failed -- try special cases
+                     */
+
+                    // basedir-list
+                    if (confEntry.getKey().equals("basedir-list")) {
+                        // split list into directories
+                        String[] dirs = FileOps.pathToArray(confEntry.getValue());
+                        for (int j = 0; j < dirs.length; j++) {
+                            // make relative directory paths be inside the webapp
+                            dirs[j] = ServletOps.getFile(dirs[j], ctx);
+                        }
+                        if (dirs != null) {
+                            param.setValue(dirs);
+                        }
+                    }
+                }
+            } else {
+                // parameter unknown -- just add
+                newParameter(confEntry.getKey(), null, confEntry.getValue(), 'u');
+            }
+        }
+    }
+
     /*
      * (non-Javadoc)
      * 
--- a/webapp/src/main/webapp/jquery/jquery-test-embedded.html	Mon Jan 13 14:13:05 2014 +0100
+++ b/webapp/src/main/webapp/jquery/jquery-test-embedded.html	Tue Jan 14 09:44:22 2014 +0100
@@ -67,6 +67,7 @@
             $(document).ready(function(){
                 var opts = {
                     interactionMode : 'embedded',
+                    saveStateInCookie : false,
                     scalerBaseUrl : 'http://digilib.biblhertz.it/digilib04/servlet/Scaler',
                     showRegionNumbers : false,
                     autoRegionLinks : true