Strabon

changeset 327:c7dd5add6c9d

changed Strabon.query to accept an output stream instead of returning the answer as an Object
author Babis Nikolaou <charnik@di.uoa.gr>
date Fri Jun 22 18:46:45 2012 +0300 (2012-06-22)
parents b29f0bd444a1
children 5a4a3f40bead
files runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java
line diff
     1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Fri Jun 22 17:44:15 2012 +0300
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Fri Jun 22 18:46:45 2012 +0300
     1.3 @@ -8,17 +8,16 @@
     1.4  import java.io.InputStream;
     1.5  import java.io.InputStreamReader;
     1.6  import java.io.OutputStream;
     1.7 -import java.io.OutputStreamWriter;
     1.8  import java.io.StringReader;
     1.9  import java.net.MalformedURLException;
    1.10  import java.net.URL;
    1.11 +import java.nio.charset.Charset;
    1.12  import java.sql.SQLException;
    1.13  import java.util.ArrayList;
    1.14  import java.util.Set;
    1.15  import java.util.zip.ZipEntry;
    1.16  import java.util.zip.ZipOutputStream;
    1.17  
    1.18 -import org.apache.commons.io.output.ByteArrayOutputStream;
    1.19  import org.geotools.data.simple.SimpleFeatureCollection;
    1.20  import org.geotools.feature.FeatureCollections;
    1.21  import org.geotools.feature.simple.SimpleFeatureBuilder;
    1.22 @@ -71,8 +70,18 @@
    1.23  
    1.24  	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.runtime.generaldb.Strabon.class);
    1.25  
    1.26 +	public static final String FORMAT_DEFAULT	= "";
    1.27 +	public static final String FORMAT_XML		= "XML";
    1.28 +	public static final String FORMAT_KML		= "KML";
    1.29 +	public static final String FORMAT_KMZ		= "KMZ";
    1.30 +	public static final String FORMAT_GEOJSON	= "GeoJSON";
    1.31 +	public static final String FORMAT_EXP		= "EXP";
    1.32 +	public static final String FORMAT_HTML		= "HTML";
    1.33 +	
    1.34 +	public static final String NEWLINE	= "\n";
    1.35 +	
    1.36  	/**
    1.37 -	 * Connection details
    1.38 +	 * Connection details (shared with subclasses)
    1.39  	 */
    1.40  	protected String databaseName;
    1.41  	protected String user;
    1.42 @@ -84,8 +93,7 @@
    1.43  	private SailRepository repo1;
    1.44  	private SailRepositoryConnection con1 = null;
    1.45  
    1.46 -	public Strabon(String databaseName, String user, String password, int port, String serverName, boolean checkForLockTable) 
    1.47 -			throws SQLException, ClassNotFoundException {
    1.48 +	public Strabon(String databaseName, String user, String password, int port, String serverName, boolean checkForLockTable) throws SQLException, ClassNotFoundException {
    1.49  		this.databaseName = databaseName;
    1.50  		this.user = user;
    1.51  		this.password = password;
    1.52 @@ -100,8 +108,21 @@
    1.53  	}
    1.54  
    1.55  
    1.56 +	/**
    1.57 +	 * Called by Strabon.close() to deregister the underlying JDBC driver used.
    1.58 +	 */
    1.59  	protected abstract void deregisterDriver();
    1.60  	
    1.61 +	/**
    1.62 +	 * Called in Strabon constructor to initialize Strabon (establish connection to the
    1.63 +	 * underlying database, etc.).
    1.64 +	 * 
    1.65 +	 * @param databaseName
    1.66 +	 * @param user
    1.67 +	 * @param password
    1.68 +	 * @param port
    1.69 +	 * @param serverName
    1.70 +	 */
    1.71  	protected abstract void initiate(String databaseName, String user, String password, int port, String serverName);
    1.72  
    1.73  	protected void init() {
    1.74 @@ -143,6 +164,9 @@
    1.75  		this.con1 = con1;
    1.76  	}
    1.77  
    1.78 +	/**
    1.79 +	 * Close connection to Strabon.
    1.80 +	 */
    1.81  	public void close() {
    1.82  		logger.info("[Strabon.close] Closing connection...");
    1.83  
    1.84 @@ -161,23 +185,49 @@
    1.85  		logger.info("[Strabon.close] Connection closed.");
    1.86  	}
    1.87  
    1.88 -	public Object query(String queryString)
    1.89 +	public Object query(String queryString, OutputStream out)
    1.90  	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException {
    1.91 -		return query(queryString, "", this.getSailRepoConnection());	
    1.92 +		return query(queryString, "", this.getSailRepoConnection(), out);	
    1.93  	}
    1.94  
    1.95 -	public Object query(String queryString, String resultsFormat)
    1.96 +	public Object query(String queryString, String resultsFormat, OutputStream out)
    1.97  	throws  MalformedQueryException , QueryEvaluationException, IOException, TupleQueryResultHandlerException {
    1.98 -		return query(queryString, resultsFormat, this.getSailRepoConnection());
    1.99 +		return query(queryString, resultsFormat, this.getSailRepoConnection(), out);
   1.100  	}
   1.101  
   1.102 -	public Object query(String queryString, SailRepositoryConnection con)
   1.103 +	public ArrayList<String> query(String queryString, SailRepositoryConnection con)
   1.104  	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException {
   1.105 -		return query(queryString, "", con);	
   1.106 +		TupleQuery tupleQuery = null;
   1.107 +		ArrayList<String> ret = new ArrayList<String>();
   1.108 +		
   1.109 +		try {
   1.110 +
   1.111 +			tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
   1.112 +			TupleQueryResult result = tupleQuery.evaluate();
   1.113 +
   1.114 +			while (result.hasNext()) {
   1.115 +				BindingSet bindingSet = result.next();
   1.116 +				ret.add(bindingSet.toString());
   1.117 +			}
   1.118 +			
   1.119 +		} catch (RepositoryException e) {
   1.120 +			logger.error("[Strabon.query] Error in preparing tuple query.", e);
   1.121 +			
   1.122 +		}
   1.123 +		
   1.124 +		return ret;	
   1.125  	}
   1.126  
   1.127 -	public Object queryBindings(String queryString, SailRepositoryConnection con) throws QueryEvaluationException, MalformedQueryException
   1.128 -	{
   1.129 +	public Object query(String queryString, String resultsFormat, SailRepositoryConnection con, OutputStream out)
   1.130 +	throws MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException {
   1.131 +		boolean status = true;
   1.132 +		
   1.133 +		if (out == null) {
   1.134 +			logger.error("[Strabon.query] Cannot write to null stream.");
   1.135 +			
   1.136 +			return false;
   1.137 +		}
   1.138 +		
   1.139  		logger.info("[Strabon.query] Executing query: {}", queryString);
   1.140  		
   1.141  		TupleQuery tupleQuery = null;
   1.142 @@ -186,96 +236,82 @@
   1.143  			
   1.144  		} catch (RepositoryException e) {
   1.145  			logger.error("[Strabon.query] Error in preparing tuple query.", e);
   1.146 -		}
   1.147 -		
   1.148 -		return tupleQuery.evaluate();
   1.149 -	}
   1.150 -
   1.151 -	public Object query(String queryString, String resultsFormat, SailRepositoryConnection con)
   1.152 -	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException {
   1.153 -		logger.info("[Strabon.query] Executing query: {}", queryString);
   1.154 -		
   1.155 -		TupleQuery tupleQuery = null;
   1.156 -		try {
   1.157 -			tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
   1.158 -			
   1.159 -		} catch (RepositoryException e) {
   1.160 -			logger.error("[Strabon.query] Error in preparing tuple query.", e);
   1.161 +			status = false;
   1.162  		}
   1.163  
   1.164 -		ArrayList<String> ret = new ArrayList<String>();
   1.165 -		ByteArrayOutputStream retStream = new ByteArrayOutputStream();
   1.166 -		OutputStreamWriter writeOut = new OutputStreamWriter(retStream, "UTF-8");
   1.167 -		
   1.168 -		if (resultsFormat.equalsIgnoreCase("EXP")) {
   1.169 +		if (FORMAT_EXP.equalsIgnoreCase(resultsFormat)) {
   1.170  			long results = 0;
   1.171 +			
   1.172  			long t1 = System.nanoTime();
   1.173  			TupleQueryResult result = tupleQuery.evaluate();
   1.174  			long t2 = System.nanoTime();
   1.175 +			
   1.176  			while (result.hasNext()) {
   1.177  				results++;
   1.178  			}
   1.179 +			
   1.180  			long t3 = System.nanoTime();
   1.181  
   1.182 -			return new long[]{t2-t1, t3-t2, t3-t1, results};
   1.183 +			//return new long[]{t2-t1, t3-t2, t3-t1, results};
   1.184  			
   1.185 -		} else if (resultsFormat.equalsIgnoreCase("")) {
   1.186 +		} else if (FORMAT_DEFAULT.equalsIgnoreCase(resultsFormat)) {
   1.187 +			
   1.188  			TupleQueryResult result = null;
   1.189  			result = tupleQuery.evaluate();
   1.190  
   1.191  			while (result.hasNext()) {
   1.192 -
   1.193  				BindingSet bindingSet = result.next();
   1.194 -				System.out.println(bindingSet.toString());
   1.195 -				ret.add(bindingSet.toString());
   1.196 +				
   1.197 +				writeString(out, bindingSet.toString());
   1.198 +				writeString(out, NEWLINE);
   1.199  			}
   1.200 -
   1.201 -			return ret;
   1.202  			
   1.203 -		} else if (resultsFormat.equalsIgnoreCase("XML")) {
   1.204 +		} else if (FORMAT_XML.equalsIgnoreCase(resultsFormat)) {
   1.205  			if (logger.isDebugEnabled()) {
   1.206  				logger.debug("Serializing results (XML)");
   1.207  			}
   1.208  			
   1.209 -			tupleQuery.evaluate(new stSPARQLResultsXMLWriter(System.out));
   1.210 +			tupleQuery.evaluate(new stSPARQLResultsXMLWriter(out));
   1.211  			
   1.212 -		} else if (resultsFormat.equalsIgnoreCase("KML") || resultsFormat.equalsIgnoreCase("KMZ")) {
   1.213 +		} else if (FORMAT_KML.equalsIgnoreCase(resultsFormat)) {
   1.214  			if (logger.isDebugEnabled()) {
   1.215  				logger.debug("Serializing results (KML/KMZ)");
   1.216  			}
   1.217  
   1.218 -			if (resultsFormat.equalsIgnoreCase("KML")) { 
   1.219 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriter(System.out));
   1.220 -				
   1.221 -			} else { // KMZ
   1.222 -				// create a zip entry
   1.223 -				ZipEntry entry = new ZipEntry("sparql_results.kml");
   1.224 -				
   1.225 -				// create a zip stream on standard output
   1.226 -				ZipOutputStream kmzout = new ZipOutputStream(System.out);
   1.227 -				
   1.228 -				// add the zip entry in it
   1.229 -				kmzout.putNextEntry(entry);
   1.230 -				
   1.231 -				// pass the zip stream for evaluation
   1.232 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriter(kmzout));
   1.233 +			tupleQuery.evaluate(new stSPARQLResultsKMLWriter(out));
   1.234 +			
   1.235 +		} else if (FORMAT_KMZ.equalsIgnoreCase(resultsFormat)) {
   1.236 +			// create a zip entry
   1.237 +			ZipEntry entry = new ZipEntry("sparql_results.kml");
   1.238 +			
   1.239 +			// create a zip stream on standard output
   1.240 +			ZipOutputStream kmzout = new ZipOutputStream(out);
   1.241 +			
   1.242 +			// add the zip entry in it
   1.243 +			kmzout.putNextEntry(entry);
   1.244 +			
   1.245 +			// pass the zip stream for evaluation
   1.246 +			tupleQuery.evaluate(new stSPARQLResultsKMLWriter(kmzout));
   1.247  
   1.248 -				// close the zip entry
   1.249 -				kmzout.closeEntry();
   1.250 -				
   1.251 -				// close the zip stream
   1.252 -				kmzout.close();
   1.253 -			}
   1.254 +			// close the zip entry
   1.255 +			kmzout.closeEntry();
   1.256  			
   1.257 -		} else if (resultsFormat.equalsIgnoreCase("GeoJSON")) {
   1.258 +			// close the zip stream
   1.259 +			kmzout.close();
   1.260 +			
   1.261 +		} else if (FORMAT_GEOJSON.equalsIgnoreCase(resultsFormat)) {
   1.262  
   1.263 -			DataOutputStream dos = new DataOutputStream(retStream);
   1.264 +			DataOutputStream dos = new DataOutputStream(out);
   1.265  
   1.266  			TupleQueryResult result = null;
   1.267  			try {
   1.268  				result = tupleQuery.evaluate();
   1.269 -			} catch (QueryEvaluationException e1) {
   1.270 -				e1.printStackTrace();
   1.271 +				
   1.272 +			} catch (QueryEvaluationException e) {
   1.273 +				logger.error("[Strabon.query] Error during query evaluation.", e);
   1.274 +				status = false;
   1.275 +				
   1.276 +				return status;
   1.277  			}
   1.278  
   1.279  			int resultsCounter = 0;
   1.280 @@ -330,7 +366,7 @@
   1.281  				}
   1.282  
   1.283  			} else {
   1.284 -				return retStream.toString(); //empty
   1.285 +				return status;//empty
   1.286  			}
   1.287  
   1.288  			boolean firstLineParsed = false;
   1.289 @@ -389,21 +425,29 @@
   1.290  						} 
   1.291  						catch (ParseException e) {
   1.292  							logger.error("[Strabon.query] Faults detected in spatial literal representation.", e);
   1.293 +							status = false;
   1.294  						}
   1.295  					}
   1.296  
   1.297  					CoordinateReferenceSystem geomCRS = null;
   1.298  					try {
   1.299  						geomCRS = CRS.decode("EPSG:"+SRID);
   1.300 +						
   1.301  					} catch (NoSuchAuthorityCodeException e) {
   1.302  						logger.error("[Strabon.query] Error decoding returned geometry's SRID", e);
   1.303 +						status = false;
   1.304 +						
   1.305 +						return status;
   1.306  					} catch (FactoryException e) {
   1.307  						logger.error("[Strabon.query]", e);
   1.308 +						status = false;
   1.309 +						
   1.310 +						return status;
   1.311  					}
   1.312  
   1.313  					tb[i].setCRS(geomCRS);
   1.314 -					tb[i].setSRS("EPSG:"+SRID);
   1.315 -					tb[i].add("geometry",Geometry.class);
   1.316 +					tb[i].setSRS("EPSG:" + SRID);
   1.317 +					tb[i].add("geometry", Geometry.class);
   1.318  
   1.319  					SimpleFeatureType featureType = tb[i].buildFeatureType();
   1.320  					SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
   1.321 @@ -421,78 +465,66 @@
   1.322  
   1.323  					SimpleFeature feature = featureBuilder.buildFeature(null);
   1.324  					sfCollection.add(feature);
   1.325 -
   1.326 -					//					FeatureJSON fjson22 = new FeatureJSON();
   1.327 -					//					fjson22.setEncodeFeatureCRS(true);
   1.328 -					//					fjson22.writeFeatureCollection(sfCollection, dos);
   1.329 -					//					System.out.println(retStream.toString());
   1.330  				}
   1.331  
   1.332  			}
   1.333 +			
   1.334  			while((result.hasNext()));
   1.335  
   1.336  			FeatureJSON fjson = new FeatureJSON();
   1.337  			fjson.setEncodeFeatureCRS(true);
   1.338  			fjson.writeFeatureCollection(sfCollection, dos);
   1.339 -			System.out.println(retStream.toString());
   1.340 +			writeString(out, NEWLINE);
   1.341  
   1.342 -		} else if(resultsFormat.equalsIgnoreCase("HTML")) {
   1.343 +		} else if(FORMAT_HTML.equalsIgnoreCase(resultsFormat)) {
   1.344  			TupleQueryResult result = tupleQuery.evaluate();
   1.345  			
   1.346 -			if(result.hasNext())
   1.347 -			{
   1.348 +			if (result.hasNext()) {
   1.349  				BindingSet set = result.next();
   1.350  				Set<String> bindingNames = set.getBindingNames();			
   1.351 -				writeOut.write("<tr>");
   1.352 +				writeString(out, "<tr>");
   1.353  				for (String bindingName: bindingNames) {
   1.354 -					writeOut.write("<th>");
   1.355 -					writeOut.write(bindingName);
   1.356 -					writeOut.write("</th>");
   1.357 -				}			
   1.358 -				writeOut.write("</tr>");
   1.359 -
   1.360 -				writeOut.write("<tr>");
   1.361 +					writeString(out, "<th>");
   1.362 +					writeString(out, bindingName);
   1.363 +					writeString(out, "</th>");
   1.364 +				}
   1.365 +				
   1.366 +				writeString(out, "</tr>");
   1.367 +				writeString(out, "<tr>");
   1.368  
   1.369  				for (String bindingName: bindingNames) {
   1.370 -					writeOut.write("<td>");
   1.371 -					writeOut.write(set.getValue(bindingName).stringValue());
   1.372 -					writeOut.write("</td>");
   1.373 +					writeString(out, "<td>");
   1.374 +					writeString(out, set.getValue(bindingName).stringValue());
   1.375 +					writeString(out, "</td>");
   1.376  				}
   1.377 -				writeOut.write("</tr>");
   1.378 +				writeString(out, "</tr>");
   1.379  
   1.380  
   1.381  				while (result.hasNext()) {
   1.382 -					writeOut.write("<tr>");
   1.383 +					writeString(out, "<tr>");
   1.384  					BindingSet bindingSet = result.next();
   1.385  
   1.386  					for (String bindingName: bindingNames) {
   1.387 -						writeOut.write("<td>");
   1.388 +						writeString(out, "<td>");
   1.389  						Binding binding = bindingSet.getBinding(bindingName); 
   1.390  						if (binding != null) {
   1.391  							Value val = binding.getValue();
   1.392 -							writeOut.write(val.stringValue());
   1.393 +							writeString(out, val.stringValue());
   1.394  						}					
   1.395 -						writeOut.write("</td>");
   1.396 +						writeString(out, "</td>");
   1.397  					}
   1.398  
   1.399 -					writeOut.write("</tr>");
   1.400 +					writeString(out, "</tr>");
   1.401  				}
   1.402 +				writeString(out, NEWLINE);
   1.403  			}
   1.404  			
   1.405  		} else {
   1.406 -			//			System.exit(-1);// throw new InvalidDatasetFormatFault(); // TODO
   1.407  			logger.warn("[Strabon.query] No such format available \"" + resultsFormat + "\".");
   1.408 -			return null;
   1.409 +			status = false;
   1.410  		}
   1.411  
   1.412 -		try {
   1.413 -			writeOut.flush();
   1.414 -			retStream.flush();
   1.415 -		} catch (IOException e) {
   1.416 -			logger.error("[Strabon.query]", e);
   1.417 -		}
   1.418 -
   1.419 -		return retStream.toString();
   1.420 +		return status;
   1.421  	}
   1.422  
   1.423  	public void update(String updateString, SailRepositoryConnection con) throws MalformedQueryException 
   1.424 @@ -692,4 +724,10 @@
   1.425  
   1.426  		logger.info("[Strabon.describe] Output: {}", outFile);
   1.427  	}
   1.428 +	
   1.429 +	private static void writeString(OutputStream out, String str) throws IOException {
   1.430 +		if (str != null && str.length() > 0) {
   1.431 +			out.write(str.getBytes(Charset.defaultCharset()));
   1.432 +		}
   1.433 +	}
   1.434  }
     2.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java	Fri Jun 22 17:44:15 2012 +0300
     2.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java	Fri Jun 22 18:46:45 2012 +0300
     2.3 @@ -62,7 +62,7 @@
     2.4  					String filename = children[i];
     2.5  					try {
     2.6  						String queryString = readFile(path + System.getProperty("file.separator") + filename);
     2.7 -						strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
     2.8 +						strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection(), System.out);
     2.9  						
    2.10  					} catch (IOException e) {
    2.11  						logger.error("[Strabon.QueryDir] IOException while reading " + filename, e);
     3.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java	Fri Jun 22 17:44:15 2012 +0300
     3.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java	Fri Jun 22 18:46:45 2012 +0300
     3.3 @@ -39,7 +39,7 @@
     3.4  		Strabon strabon = null;
     3.5  		try {
     3.6  			strabon = new Strabon(db, user, passwd, port, host, true);
     3.7 -			strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
     3.8 +			strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection(), System.out);
     3.9  			
    3.10  		} catch (Exception e) {
    3.11  			logger.error("[Strabon.QueryOp] Error during execution of SPARQL query.", e);
     4.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java	Fri Jun 22 17:44:15 2012 +0300
     4.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java	Fri Jun 22 18:46:45 2012 +0300
     4.3 @@ -64,7 +64,7 @@
     4.4  						logger.info("[Strabon.QueryDir] Evaluating query from '" + path + System.getProperty("file.separator") + filename  +"'.");
     4.5  						String queryString = readFile(path + System.getProperty("file.separator") + filename);
     4.6  						logger.info("[Strabon.QueryDir] Evaluating stSPARQL query: \n"+queryString+"\n");
     4.7 -						strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
     4.8 +						strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection(), System.out);
     4.9  						
    4.10  					} catch (IOException e) {
    4.11  						logger.error("[Strabon.QueryDir] IOException while reading " + filename, e);
     5.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java	Fri Jun 22 17:44:15 2012 +0300
     5.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java	Fri Jun 22 18:46:45 2012 +0300
     5.3 @@ -40,7 +40,7 @@
     5.4  		Strabon strabon = null;
     5.5  		try {
     5.6  			strabon = new Strabon(db, user, passwd, port, host, true);
     5.7 -			strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection());
     5.8 +			strabon.query(queryString, resultsFormat, strabon.getSailRepoConnection(), System.out);
     5.9  			
    5.10  		} catch (Exception e) {
    5.11  			logger.error("[Strabon.QueryOp] Error during execution of SPARQL query.", e);