Commit 4e295426 authored by rwm's avatar rwm
Browse files

Potential resource leaks closed

parent 9be23954
......@@ -141,9 +141,9 @@ public class PostgresPatientStore extends PostgresExtension<I2b2Patient> impleme
private void loadMaxPatientNum() throws SQLException{
Statement s = db.createStatement();
String sql = "SELECT MAX(patient_num) FROM patient_dimension";
try( ResultSet rs = s.executeQuery(sql) ){
try( Statement s = db.createStatement() ){
String sql = "SELECT MAX(patient_num) FROM patient_dimension";
ResultSet rs = s.executeQuery(sql);
if( rs.next() ){
maxPatientNum = rs.getInt(1);
}else{
......@@ -151,6 +151,7 @@ public class PostgresPatientStore extends PostgresExtension<I2b2Patient> impleme
// start numbering patients with 1
maxPatientNum = 1;
}
rs.close();
}
log.info("MAX(patient_num) = "+maxPatientNum);
}
......
......@@ -113,9 +113,9 @@ public class PostgresVisitStore extends PostgresExtension<I2b2Visit>{
}
private void loadMaxEncounterNum() throws SQLException{
Statement s = db.createStatement();
String sql = "SELECT MAX(encounter_num) FROM visit_dimension";
try( ResultSet rs = s.executeQuery(sql) ){
try( Statement s = db.createStatement() ){
String sql = "SELECT MAX(encounter_num) FROM visit_dimension";
ResultSet rs = s.executeQuery(sql);
if( rs.next() ){
maxEncounterNum = rs.getInt(1);
}else{
......@@ -123,6 +123,7 @@ public class PostgresVisitStore extends PostgresExtension<I2b2Visit>{
// start numbering patients with 1
maxEncounterNum = 1;
}
rs.close();
}
log.info("MAX(encounter_num) = "+maxEncounterNum);
}
......@@ -143,6 +144,7 @@ public class PostgresVisitStore extends PostgresExtension<I2b2Visit>{
count ++;
}
}
stmt.close();
log.info("Loaded MAX(instance_num) for "+count+" encounters");
if( noMatch != 0 ){
log.warning("Encountered "+noMatch+" encounter_num in observation_fact without matching visits");
......
......@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
......@@ -75,14 +76,17 @@ public class Import implements AutoCloseable{
deleteConcepts.setString(1, sourceId);
count = deleteConcepts.executeUpdate();
System.out.println("Deleted "+count+" rows from "+getConceptTable());
deleteConcepts.close();
deleteAccess.setString(1, sourceId+"%");
count = deleteAccess.executeUpdate();
System.out.println("Deleted "+count+" rows from "+getAccessTable());
deleteAccess.close();
deleteOnt.setString(1, sourceId);
count = deleteOnt.executeUpdate();
System.out.println("Deleted "+count+" rows from "+getMetaTable());
deleteOnt.close();
}
......@@ -409,7 +413,9 @@ public class Import implements AutoCloseable{
}
Properties ont_props = new Properties();
ont_props.load(new FileInputStream(ontConfig));
try( InputStream in = new FileInputStream(ontConfig) ){
ont_props.load(in);
}
String ontClass = ont_props.getProperty("ontology.class");
if( ontClass == null ){
......@@ -422,7 +428,10 @@ public class Import implements AutoCloseable{
// open database
props = new Properties();
props.load(new FileInputStream(impConfig));
try( InputStream in = new FileInputStream(impConfig) ){
props.load(in);
}
try {
o.openDatabase((Map)props);
} catch (ClassNotFoundException e) {
......
......@@ -3,6 +3,7 @@ package de.sekmi.histream.i2b2.services;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
......@@ -54,21 +55,14 @@ public class HiveServer implements Provider<Source>{
//scriptEngine = scriptManager.getEngineByName("nashorn");
// load scripts
}
public void loadMainScript(){
public void loadMainScript() throws IOException, ScriptException{
scriptEngine = scriptManager.getEngineByName("nashorn");
try {
scriptEngine.eval(new FileReader(scriptDir.resolve("main.js").toFile()));
try( Reader scriptFile = new FileReader(scriptDir.resolve("main.js").toFile()) ) {
scriptEngine.eval(scriptFile);
Object o = scriptEngine.eval("typeof httpRequest === 'function'");
if( o == null || !(o instanceof Boolean) || ((Boolean)o) != true ){
throw new ScriptException("global function 'httpRequest(?,?,?,?)' needed");
}
} catch (ScriptException e) {
e.printStackTrace();
scriptEngine = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
scriptEngine = null;
}
}
......@@ -83,7 +77,12 @@ public class HiveServer implements Provider<Source>{
break;
}
System.out.println("Reloading scripts..");
loadMainScript();
try {
loadMainScript();
} catch (IOException | ScriptException e1) {
System.err.println("Error loading script");
e1.printStackTrace();
}
// pollEvents() is needed for the key to reset properly
for( WatchEvent<?> e : key.pollEvents() ){
if( e == null )continue;// noop
......@@ -95,7 +94,9 @@ public class HiveServer implements Provider<Source>{
}
}
}
public void loadScipts()throws IOException{
@SuppressWarnings("resource") // cannot close default filesystem
public void loadScipts()throws IOException, ScriptException{
FileSystem fs = FileSystems.getDefault();
scriptDir = fs.getPath("src", "main", "scripts", "i2b2-ws");
if( !Files.isDirectory(scriptDir) )throw new FileNotFoundException("Script dir not found: "+scriptDir);
......@@ -118,7 +119,7 @@ public class HiveServer implements Provider<Source>{
}
}*/
}
public static void main(String args[]) throws IOException{
public static void main(String args[]) throws IOException, ScriptException{
HiveServer hs = new HiveServer();
hs.loadScipts();
Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING, hs);
......
......@@ -150,6 +150,7 @@ public class RunConfiguration implements Closeable{
if( inputStream != null ){
Properties props = new Properties();
props.load(inputStream);
inputStream.close();
version = props.getProperty("version","[unknown]");
}else{
// file is not run from jar
......@@ -200,6 +201,7 @@ public class RunConfiguration implements Closeable{
if( p != null ){
System.out.println("ETL("+p.getMeta("etl.strategy")+"): "+file);
rc.processFile(p);
p.close();
}else{
System.err.println("Unable to find parser for file "+file);
}
......
......@@ -40,6 +40,7 @@ public class FlatProviderFactory implements FileObservationSupplierFactory, Plug
// don't need to close anything
}
@SuppressWarnings("resource")
@Override
public ObservationSupplier forFile(File file, ObservationFactory factory) throws IOException {
return new FlatObservationSupplier(factory, new FileInputStream(file));
......
......@@ -9,18 +9,20 @@ import org.junit.Test;
public class TransformerTest {
@SuppressWarnings("resource")
@Test
public void testPullTransformerIdentity() throws FileNotFoundException, IOException{
FileObservationProviderTest f = new FileObservationProviderTest();
f.initializeObservationFactory();
Transformation t = Transformation.Identity;
FlatObservationSupplier sup = new FlatObservationSupplier(f.getFactory(), new FileInputStream("examples/dwh-flat.txt"));
PullTransformer p = new PullTransformer(sup, t);
// validate content after identity transformation
f.initializeHandler();
f.validateExample(p);
f.closeHandler();
try( FlatObservationSupplier sup = new FlatObservationSupplier(f.getFactory(), new FileInputStream("examples/dwh-flat.txt")) ){
PullTransformer p = new PullTransformer(sup, t);
// validate content after identity transformation
f.initializeHandler();
f.validateExample(p);
f.closeHandler();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment