changeset 8970:da5dc7446652

SLF4J added and configureLogging in main
author mschaefer
date Tue, 03 Apr 2018 10:02:01 +0200
parents fe81eb39080c
children 50416a0df385
files backend/pom.xml backend/src/main/java/org/dive4elements/river/importer/Importer.java
diffstat 2 files changed, 86 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/backend/pom.xml	Tue Apr 03 09:54:26 2018 +0200
+++ b/backend/pom.xml	Tue Apr 03 10:02:01 2018 +0200
@@ -179,6 +179,16 @@
       <version>1.1.1</version>
       <scope>runtime</scope>
     </dependency>
+    <dependency>
+    	<groupId>org.slf4j</groupId>
+    	<artifactId>jul-to-slf4j</artifactId>
+    	<version>1.6.1</version>
+    </dependency>
+    <dependency>
+    	<groupId>org.slf4j</groupId>
+    	<artifactId>slf4j-log4j12</artifactId>
+    	<version>1.6.1</version>
+    </dependency>
   </dependencies>
 
   <repositories>
--- a/backend/src/main/java/org/dive4elements/river/importer/Importer.java	Tue Apr 03 09:54:26 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/Importer.java	Tue Apr 03 10:02:01 2018 +0200
@@ -8,30 +8,26 @@
 
 package org.dive4elements.river.importer;
 
-import org.dive4elements.artifacts.common.utils.XMLUtils;
-
-import org.dive4elements.river.importer.parsers.AnnotationClassifier;
-import org.dive4elements.river.importer.parsers.BundesWasserStrassenParser;
-import org.dive4elements.river.importer.parsers.InfoGewParser;
-
 import java.io.File;
 import java.io.IOException;
-
+import java.net.MalformedURLException;
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import java.sql.SQLException;
-
 import org.apache.log4j.Logger;
-
+import org.apache.log4j.PropertyConfigurator;
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.river.backend.utils.StringUtil;
+import org.dive4elements.river.importer.parsers.AnnotationClassifier;
+import org.dive4elements.river.importer.parsers.BundesWasserStrassenParser;
+import org.dive4elements.river.importer.parsers.InfoGewParser;
+import org.hibernate.HibernateException;
 import org.hibernate.Transaction;
-import org.hibernate.HibernateException;
-
+import org.slf4j.bridge.SLF4JBridgeHandler;
 import org.w3c.dom.Document;
 
-import org.dive4elements.river.backend.utils.StringUtil;
-
 /** Data Importer. Further processing happens per-river. */
 public class Importer
 {
@@ -45,15 +41,15 @@
     public Importer() {
     }
 
-    public Importer(List<ImportRiver> rivers) {
+    public Importer(final List<ImportRiver> rivers) {
         this.rivers = rivers;
     }
 
     public List<ImportRiver> getRivers() {
-        return rivers;
+        return this.rivers;
     }
 
-    public void setRivers(List<ImportRiver> rivers) {
+    public void setRivers(final List<ImportRiver> rivers) {
         this.rivers = rivers;
     }
 
@@ -61,7 +57,7 @@
     public void writeRivers() {
         log.debug("write rivers started");
 
-        for (ImportRiver river: rivers) {
+        for (final ImportRiver river: this.rivers) {
             log.debug("writing river '" + river.getName() + "'");
             river.storeDependencies();
             ImporterSession.getInstance().getDatabaseSession().flush();
@@ -75,16 +71,15 @@
         Transaction tx = null;
 
         try {
-            tx = ImporterSession.getInstance()
-                .getDatabaseSession().beginTransaction();
+            tx = ImporterSession.getInstance().getDatabaseSession().beginTransaction();
 
             try {
                 writeRivers();
             }
-            catch (HibernateException he) {
+            catch (final HibernateException he) {
                 Throwable t = he.getCause();
                 while (t instanceof SQLException) {
-                    SQLException sqle = (SQLException) t;
+                    final SQLException sqle = (SQLException) t;
                     log.error("SQL exeception chain:", sqle);
                     t = sqle.getNextException();
                 }
@@ -93,7 +88,7 @@
 
             tx.commit();
         }
-        catch (RuntimeException re) {
+        catch (final RuntimeException re) {
             if (tx != null) {
                 tx.rollback();
             }
@@ -102,14 +97,14 @@
     }
 
     public static AnnotationClassifier getAnnotationClassifier() {
-        String annotationTypes = Config.INSTANCE.getAnnotationTypes();
+        final String annotationTypes = Config.INSTANCE.getAnnotationTypes();
 
         if (annotationTypes == null) {
             log.info("no annotation types file configured.");
             return null;
         }
 
-        File file = new File(annotationTypes);
+        final File file = new File(annotationTypes);
 
         log.info("use annotation types file '" + file + "'");
 
@@ -118,7 +113,7 @@
             return null;
         }
 
-        Document rules = XMLUtils.parseDocument(file, false, null);
+        final Document rules = XMLUtils.parseDocument(file, false, null);
 
         if (rules == null) {
             log.warn("cannot parse annotation types file.");
@@ -130,77 +125,79 @@
 
 
     /** Starting point for importing river data. */
-    public static void main(String [] args) {
+    public static void main(final String [] args) {
 
-        InfoGewParser infoGewParser = new InfoGewParser(
-            getAnnotationClassifier());
+        configureLogging();
 
-        log.info("Start parsing rivers...");
+        log.info("START parsing rivers...");
+
+        final InfoGewParser infoGewParser = new InfoGewParser(getAnnotationClassifier());
 
         File bwastrFile = null;
 
-        for (String gew: args) {
-            log.info("parsing info gew file: " + gew);
-            File gewFile = new File(gew);
+        // Main parsing loop for all river gew file paths in args
+        // FIXME: Multiple rivers lead to reparsing the already parsed rivers again in InfoGewParser.parse...
+        for (final String gew : args) {
+            log.info("Parsing info gew file: " + gew);
+            final File gewFile = new File(gew);
             if (bwastrFile == null) {
-                bwastrFile = new File(
-                    gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
+                bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
             }
             try {
                 infoGewParser.parse(gewFile);
             }
-            catch (IOException ioe) {
-                log.error("error while parsing gew: " + gew, ioe);
+            catch (final IOException ioe) {
+                log.error("error parsing gew: " + gew, ioe);
                 System.exit(1);
             }
         }
 
-        String gew = Config.INSTANCE.getInfoGewFile();
+        // Parse a single river gew file specified in the flys.backend.importer.infogew.file property
+        // (seems to be an alternative to the args way)
+        final String gew = Config.INSTANCE.getInfoGewFile();
         if (gew != null && gew.length() > 0) {
-            log.info("parsing info gew file: " + gew);
-            File gewFile = new File(gew);
+            log.info("Parsing info gew file: " + gew);
+            final File gewFile = new File(gew);
             if (bwastrFile == null) {
-                bwastrFile = new File(
-                    gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
+                bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
             }
             try {
                 infoGewParser.parse(gewFile);
             }
-            catch (IOException ioe) {
-                log.error("error while parsing gew: " + gew, ioe);
+            catch (final IOException ioe) {
+                log.error("error parsing gew: " + gew, ioe);
                 System.exit(1);
             }
         }
 
         // Look for official numbers.
-        BundesWasserStrassenParser bwastrIdParser =
-            new BundesWasserStrassenParser();
+        final BundesWasserStrassenParser bwastrIdParser = new BundesWasserStrassenParser();
 
         // Read bwastFile (river-dir + BWASTR_ID_CSV_FILE).
         if (!Config.INSTANCE.skipBWASTR()) {
             try{
                 bwastrIdParser.parse(bwastrFile);
-                HashMap<String,Long> map = bwastrIdParser.getMap();
+                final HashMap<String,Long> map = bwastrIdParser.getMap();
 
                 // Now link rivers with official numbers.
-                for(ImportRiver river: infoGewParser.getRivers()) {
-                    for(Map.Entry<String, Long> entry: map.entrySet()) {
-                        if (StringUtil.containsIgnoreCase(
-                                river.getName(), entry.getKey())) {
+                for(final ImportRiver river: infoGewParser.getRivers()) {
+                    for(final Map.Entry<String, Long> entry: map.entrySet()) {
+                        if (StringUtil.containsIgnoreCase(river.getName(), entry.getKey())) {
                             river.setOfficialNumber(entry.getValue());
-                            log.debug(river.getName()
-                                + " is mapped to bwastr " + entry.getValue());
+                            log.debug(river.getName() + " is mapped to bwastr " + entry.getValue());
                         }
                     }
                 }
-            } catch (IOException ioe) {
-                log.warn("BWASTR-file could not be loaded.");
+            }
+            catch (final IOException ioe) {
+                log.warn("BWASTR-file could not be loaded: " + ioe.getMessage());
             }
         }
         else {
-            log.debug("skip reading BWASTR_ID.csv");
+            log.debug("Skip reading BWASTR_ID.csv");
         }
 
+        // Write all parsed objects to the database
         if (!Config.INSTANCE.dryRun()) {
             new Importer(infoGewParser.getRivers()).writeToDatabase();
         }
@@ -208,5 +205,28 @@
             log.info("Dry run, not writing to database.");
         }
     }
+
+    /**
+     * Tries to load the Log4j configuration from the property 'log4j.configuration'.
+     */
+    private static final void configureLogging() {
+        final String configPath = System.getProperty("log4j.configuration");
+        try {
+            final File propFile = new File(configPath);
+            if (propFile.isFile() && propFile.canRead()) {
+                try {
+                    PropertyConfigurator.configure(propFile.toURI().toURL());
+                    SLF4JBridgeHandler.install();
+                }
+                catch (final MalformedURLException mue) {
+                    mue.printStackTrace(System.err);
+                }
+            }
+        }
+        catch (final Exception e) {
+            e.printStackTrace(System.err);
+        }
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org