Strabon

changeset 233:af3620c85771

added logging in QueryDir and properly closed strabon connection in case of error
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Jun 03 14:36:43 2012 +0300 (2012-06-03)
parents ed6dabab362c
children 073b525b20b9
files runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java
line diff
     1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java	Sun Jun 03 14:32:59 2012 +0300
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java	Sun Jun 03 14:36:43 2012 +0300
     1.3 @@ -6,14 +6,19 @@
     1.4  import java.io.FilenameFilter;
     1.5  import java.io.IOException;
     1.6  
     1.7 +import org.slf4j.Logger;
     1.8 +import org.slf4j.LoggerFactory;
     1.9 +
    1.10  
    1.11  public class QueryDir {
    1.12  
    1.13 +	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.runtime.postgis.QueryDir.class);
    1.14 +	
    1.15  	/**
    1.16  	 * @param args
    1.17  	 * @throws Exception 
    1.18  	 */
    1.19 -	public static void main(String[] args) throws Exception {
    1.20 +	public static void main(String[] args) {
    1.21  
    1.22  		if (args.length < 7) {
    1.23  			System.err.println("Usage: eu.ist.semsorgrid4env.strabon.Strabon <HOST> <PORT> <DATABASE> <USERNAME> <PASSWORD> <PATH> [<FORMAT>]");
    1.24 @@ -40,32 +45,40 @@
    1.25  			resultsFormat = args[7];
    1.26  		}
    1.27  
    1.28 -		Strabon strabon = new Strabon(db, user, passwd, port, host, true);
    1.29 -
    1.30 -		File dir = new File(path);
    1.31 -
    1.32 -		FilenameFilter filter = new FilenameFilter() {
    1.33 -			public boolean accept(File dir, String name) {
    1.34 -				return name.endsWith(extension);
    1.35 -			}
    1.36 -		};
    1.37 -
    1.38 -		String[] children = dir.list(filter);
    1.39 -		if (children != null) {
    1.40 -			for (int i=0; i<children.length; i++) {
    1.41 -				String filename = children[i];
    1.42 -				try {
    1.43 -					System.out.println("Evaluating query from '" + path + System.getProperty("file.separator") + filename  +"'.");
    1.44 -					String queryString = readFile(path + System.getProperty("file.separator") + filename);
    1.45 -					System.out.println("Evaluating stSPARQL query: \n"+queryString+"\n");
    1.46 -					strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
    1.47 -				} catch (IOException e) {
    1.48 -					System.err.println("IOException while reading " + filename);
    1.49 +		Strabon strabon = null;
    1.50 +		try {
    1.51 +			strabon = new Strabon(db, user, passwd, port, host, true);
    1.52 +	
    1.53 +			File dir = new File(path);
    1.54 +			FilenameFilter filter = new FilenameFilter() {
    1.55 +				public boolean accept(File dir, String name) {
    1.56 +					return name.endsWith(extension);
    1.57 +				}
    1.58 +			};
    1.59 +	
    1.60 +			String[] children = dir.list(filter);
    1.61 +			if (children != null) {
    1.62 +				for (int i=0; i<children.length; i++) {
    1.63 +					String filename = children[i];
    1.64 +					try {
    1.65 +						logger.info("[Strabon.QueryDir] Evaluating query from '" + path + System.getProperty("file.separator") + filename  +"'.");
    1.66 +						String queryString = readFile(path + System.getProperty("file.separator") + filename);
    1.67 +						logger.info("[Strabon.QueryDir] Evaluating stSPARQL query: \n"+queryString+"\n");
    1.68 +						strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
    1.69 +						
    1.70 +					} catch (IOException e) {
    1.71 +						logger.error("[Strabon.QueryDir] IOException while reading " + filename, e);
    1.72 +					}
    1.73  				}
    1.74  			}
    1.75 +		} catch (Exception e) {
    1.76 +			logger.error("[Strabon.QueryDir] Error during execution of QueryDir.", e);
    1.77 +			
    1.78 +		} finally {
    1.79 +			if (strabon != null) {
    1.80 +				strabon.close();	
    1.81 +			}
    1.82  		}
    1.83 -		
    1.84 -		strabon.close();
    1.85  	}
    1.86  
    1.87  	private static String readFile(String file) throws IOException {