Strabon

changeset 293:5e7b26452def

minor change to enable resolving files from the /resources folder (for our tests)
author Manos Karpathiotakis <mk@di.uoa.gr>
date Tue Jun 19 16:45:00 2012 +0300 (2012-06-19)
parents daaf7760fc46
children dc26807e3b3f
files runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java
line diff
     1.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 18 21:04:33 2012 +0300
     1.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Tue Jun 19 16:45:00 2012 +0300
     1.3 @@ -90,14 +90,14 @@
     1.4  	SailRepositoryConnection con1 = null;
     1.5  
     1.6  	public Strabon(String databaseName, String user, String password, int port, String serverName, boolean checkForLockTable) 
     1.7 -	throws SQLException, ClassNotFoundException 
     1.8 -	{
     1.9 +			throws SQLException, ClassNotFoundException 
    1.10 +			{
    1.11  		if (checkForLockTable == true) {
    1.12  			checkAndDeleteLock(databaseName, user, password, port, serverName);
    1.13  		}
    1.14  
    1.15  		initiate(databaseName, user, password, port, serverName);
    1.16 -	}
    1.17 +			}
    1.18  
    1.19  
    1.20  	protected abstract void initiate(String databaseName, String user, String password, int port, String serverName) ;
    1.21 @@ -128,7 +128,7 @@
    1.22  
    1.23  
    1.24  	protected abstract void checkAndDeleteLock(String databaseName, String user, String password, int port, String serverName)
    1.25 -	throws SQLException, ClassNotFoundException ;
    1.26 +			throws SQLException, ClassNotFoundException ;
    1.27  
    1.28  	public SailRepositoryConnection getSailRepoConnection() {
    1.29  		return con1;
    1.30 @@ -145,31 +145,31 @@
    1.31  			con1.commit();
    1.32  			con1.close();
    1.33  			repo1.shutDown();
    1.34 -			
    1.35 +
    1.36  		} catch (RepositoryException e) {
    1.37  			logger.error("[Strabon.close]", e);
    1.38  		}
    1.39 -		
    1.40 +
    1.41  		logger.info("[Strabon.close] Connection closed.");
    1.42  	}
    1.43  
    1.44  	public Object query(String queryString)
    1.45 -	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.46 -	{
    1.47 +			throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.48 +			{
    1.49  		return query(queryString, "", this.getSailRepoConnection());	
    1.50 -	}
    1.51 +			}
    1.52  
    1.53  	public Object query(String queryString, String resultsFormat)
    1.54 -	throws  MalformedQueryException , QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.55 -	{
    1.56 +			throws  MalformedQueryException , QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.57 +			{
    1.58  		return query(queryString, resultsFormat, this.getSailRepoConnection());
    1.59 -	}
    1.60 +			}
    1.61  
    1.62  	public Object query(String queryString, SailRepositoryConnection con)
    1.63 -	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.64 -	{
    1.65 +			throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException
    1.66 +			{
    1.67  		return query(queryString, "", con);	
    1.68 -	}
    1.69 +			}
    1.70  
    1.71  	public Object queryBindings(String queryString, SailRepositoryConnection con) throws QueryEvaluationException, MalformedQueryException
    1.72  	{
    1.73 @@ -183,10 +183,10 @@
    1.74  		TupleQueryResult result = tupleQuery.evaluate();
    1.75  		return result;
    1.76  	}
    1.77 -	
    1.78 +
    1.79  	public Object query(String queryString, String resultsFormat, SailRepositoryConnection con)
    1.80 -	throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException 
    1.81 -	{
    1.82 +			throws  MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException 
    1.83 +			{
    1.84  		logger.info("[Strabon.query] Executing query: " + queryString);
    1.85  		TupleQuery tupleQuery = null;
    1.86  		try {
    1.87 @@ -618,11 +618,11 @@
    1.88  			try {
    1.89  				//String cstr = new String("aa", "UTF8");
    1.90  				// ggarbis: For too large strings (e.g., 44MB) it returns empty string.
    1.91 -//				String newString = new String(sb.toString().getBytes(), Charset.availableCharsets().get("UTF-8"));
    1.92 +				//				String newString = new String(sb.toString().getBytes(), Charset.availableCharsets().get("UTF-8"));
    1.93  
    1.94  				if(resultsFormat.equalsIgnoreCase("KML"))
    1.95  				{
    1.96 -//					writeOut.write(newString);
    1.97 +					//					writeOut.write(newString);
    1.98  					writeOut.write(sb.toString());
    1.99  					//					System.out.println(newString);
   1.100  				}
   1.101 @@ -638,7 +638,7 @@
   1.102  
   1.103  					//kmzout.setLevel(6);
   1.104  					kmzout.putNextEntry(entry);
   1.105 -//					kmzout.write(newString.getBytes());
   1.106 +					//					kmzout.write(newString.getBytes());
   1.107  					kmzout.write(sb.toString().getBytes());
   1.108  					kmzout.closeEntry();
   1.109  					kmzout.close();
   1.110 @@ -764,7 +764,7 @@
   1.111  
   1.112  		//return ret;
   1.113  		return retStream.toString();
   1.114 -	}
   1.115 +			}
   1.116  
   1.117  	public void update(String updateString, SailRepositoryConnection con) throws MalformedQueryException 
   1.118  	{
   1.119 @@ -786,9 +786,9 @@
   1.120  			logger.error("[Strabon.update]", e);
   1.121  		}
   1.122  
   1.123 -//		System.out.println("-------------------------------------------");
   1.124 -//		System.out.println("-            UPDATE EXECUTED              -");
   1.125 -//		System.out.println("-------------------------------------------");
   1.126 +		//		System.out.println("-------------------------------------------");
   1.127 +		//		System.out.println("-            UPDATE EXECUTED              -");
   1.128 +		//		System.out.println("-------------------------------------------");
   1.129  	}
   1.130  
   1.131  	public void storeInRepo(String src, String format) throws RDFParseException, RepositoryException, IOException,InvalidDatasetFormatFault, RDFHandlerException
   1.132 @@ -833,21 +833,30 @@
   1.133  		{
   1.134  			throw new InvalidDatasetFormatFault();
   1.135  		}
   1.136 -		
   1.137 -       try{
   1.138 -    	   URL source = new URL(src);
   1.139 -    	   storeURL(source, baseURI, uriContext, realFormat);
   1.140 -    	   
   1.141 -       } catch(MalformedURLException e) {
   1.142 -    	   File file = new File(src);
   1.143 -    	   if (file.exists()) {
   1.144 -    		   storeURL(new URL("file://" + src), baseURI, uriContext, realFormat);
   1.145 -    		   
   1.146 -    	   } else {
   1.147 -    		   logger.info("File \"" + src + "\" does not exist. Trying reading as String.");
   1.148 -    		   storeString((String)src, baseURI, uriContext, realFormat);
   1.149 -    	   }
   1.150 -       }
   1.151 +
   1.152 +		try{
   1.153 +			URL source = new URL(src);
   1.154 +			storeURL(source, baseURI, uriContext, realFormat);
   1.155 +
   1.156 +		} catch(MalformedURLException e) {
   1.157 +
   1.158 +			URL fromClasspath = getClass().getResource(src);
   1.159 +			if(fromClasspath!=null)
   1.160 +			{
   1.161 +				storeURL(fromClasspath, baseURI, uriContext, realFormat);
   1.162 +			}
   1.163 +			else
   1.164 +			{
   1.165 +				File file = new File(src);
   1.166 +				if (file.exists()) {
   1.167 +					storeURL(new URL("file://" + src), baseURI, uriContext, realFormat);
   1.168 +
   1.169 +				} else {
   1.170 +					logger.info("File \"" + src + "\" does not exist. Trying reading as String.");
   1.171 +					storeString((String)src, baseURI, uriContext, realFormat);
   1.172 +				}
   1.173 +			}
   1.174 +		}
   1.175  	}
   1.176  
   1.177  	private void storeURL(URL url, String baseURI, URI context, RDFFormat format) throws RDFParseException, RepositoryException, IOException, RDFHandlerException
   1.178 @@ -860,14 +869,14 @@
   1.179  
   1.180  		InputStream in = (InputStream) url.openStream();
   1.181  		InputStreamReader reader = new InputStreamReader(in);
   1.182 -		
   1.183 +
   1.184  		RDFParser parser = Rio.createParser(format);
   1.185 -		
   1.186 +
   1.187  		GeosparqlRDFHandlerBase handler = new GeosparqlRDFHandlerBase();
   1.188 -		
   1.189 +
   1.190  		parser.setRDFHandler(handler);
   1.191  		parser.parse(reader, "");
   1.192 -		
   1.193 +
   1.194  		logger.info("[Strabon.storeURL] Inferred " + handler.getNumberOfTriples() + " triples.");
   1.195  		if (handler.getNumberOfTriples() > 0) {
   1.196  			logger.info("[Strabon.storeURL] Triples inferred:"+ handler.getTriples().toString());
   1.197 @@ -897,14 +906,14 @@
   1.198  		logger.info("[Strabon.storeString] Format   : " + ((format == null) ? "null" : format.toString()));
   1.199  
   1.200  		StringReader reader = new StringReader(text);
   1.201 -		
   1.202 +
   1.203  		RDFParser parser = Rio.createParser(format);
   1.204 -		
   1.205 +
   1.206  		GeosparqlRDFHandlerBase handler = new GeosparqlRDFHandlerBase();
   1.207 -		
   1.208 +
   1.209  		parser.setRDFHandler(handler);
   1.210  		parser.parse(reader, "");
   1.211 -		
   1.212 +
   1.213  		logger.info("[Strabon.storeString] Inferred " + handler.getNumberOfTriples() + " triples.");
   1.214  		if (handler.getNumberOfTriples() > 0) {
   1.215  			logger.info("[Strabon.storeString] Triples inferred:"+ handler.getTriples().toString());
   1.216 @@ -934,16 +943,16 @@
   1.217  			logger.error("[Strabon.describe]", e);
   1.218  		}
   1.219  
   1.220 -//		System.out.println("Placemark0");
   1.221 -//		System.out.println("\n\n\nGot query: " + describeString + "\n\n\n");
   1.222 +		//		System.out.println("Placemark0");
   1.223 +		//		System.out.println("\n\n\nGot query: " + describeString + "\n\n\n");
   1.224  		logger.info("[Strabon.describe] Executing describe query:" + describeString);
   1.225 -		
   1.226 +
   1.227  		try {
   1.228  			OutputStream out = new FileOutputStream(outFile);
   1.229  			RDFHandler rdfHandler = new NTriplesWriter(out);
   1.230  			graphQuery.evaluate(rdfHandler);
   1.231  			out.close();
   1.232 -			
   1.233 +
   1.234  		} catch (FileNotFoundException e) {
   1.235  			logger.error("[Strabon.describe]", e);
   1.236  		} catch (QueryEvaluationException e) {
   1.237 @@ -955,8 +964,8 @@
   1.238  		}
   1.239  
   1.240  		logger.info("[Strabon.describe] Output: "+outFile);
   1.241 -//		System.out.println("---------------------------------------------");
   1.242 -//		System.out.println("-            DESCRIBE EXECUTED              -");
   1.243 -//		System.out.println("---------------------------------------------");
   1.244 +		//		System.out.println("---------------------------------------------");
   1.245 +		//		System.out.println("-            DESCRIBE EXECUTED              -");
   1.246 +		//		System.out.println("---------------------------------------------");
   1.247  	}
   1.248  }