You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.3 KiB
120 lines
3.3 KiB
package org.leolo.rail.nrd; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.FileInputStream; |
|
import java.io.FileReader; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.InputStreamReader; |
|
import java.sql.Connection; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
import java.util.Hashtable; |
|
import java.util.Map; |
|
import java.util.Properties; |
|
import java.util.concurrent.ExecutorService; |
|
import java.util.concurrent.Executors; |
|
import java.util.concurrent.TimeUnit; |
|
import java.util.zip.GZIPInputStream; |
|
|
|
import org.apache.logging.log4j.LogManager; |
|
import org.apache.logging.log4j.Logger; |
|
import org.json.JSONObject; |
|
|
|
public class FileLoader { |
|
|
|
private static Logger log = LogManager.getLogger(FileLoader.class); |
|
|
|
public static final Object SYNC_TOKEN = new Object(); |
|
|
|
public static void main(String [] args) { |
|
new FileLoader().run(); |
|
} |
|
|
|
public void run() { |
|
log.info("Process started"); |
|
ConfigurationManager.getInstance().forEach((k,v)->{ |
|
if(!((String)k).endsWith(".pwd")) { |
|
log.debug("{} -> {}", k, v); |
|
}else { |
|
log.debug("{} -> ****", k); |
|
} |
|
}); |
|
if(DatabaseManager.getInstance().testPool()) { |
|
log.info("Successfully connected to the database"); |
|
} |
|
if(!ConfigurationManager.getInstance().containsKey("file.path")) { |
|
log.fatal("Cannot find file path"); |
|
System.exit(1); |
|
} |
|
InputStream fis = null; |
|
//TODO: get the file from Network Rail |
|
|
|
try { |
|
fis = new FileInputStream(ConfigurationManager.getInstance().getProperty("file.path")); |
|
}catch(IOException e) { |
|
log.fatal(e.getMessage(), e); |
|
System.exit(1); |
|
} |
|
DatabaseManager.getInstance().clear(); |
|
try( |
|
GZIPInputStream gis = new GZIPInputStream(fis); |
|
BufferedReader br = new BufferedReader(new InputStreamReader(gis)) |
|
){ |
|
int count = 0; |
|
AssoicationHandler asso = new AssoicationHandler(); |
|
TiplocHandler tiploc = new TiplocHandler(); |
|
ScheduleHandler schedule = new ScheduleHandler(); |
|
while(true) { |
|
String line = br.readLine(); |
|
if(line==null) { |
|
break; |
|
} |
|
count++; |
|
if(asso.getQueueSize()+tiploc.getQueueSize()+schedule.getQueueSize() > Constants.MAX_QUEUE_SIZE) { |
|
synchronized(SYNC_TOKEN) { |
|
try { |
|
SYNC_TOKEN.wait(); |
|
} catch (InterruptedException e) { |
|
log.error(e.getMessage(), e); |
|
} |
|
} |
|
} |
|
JSONObject obj = new JSONObject(line); |
|
String objectType = obj.keys().next(); |
|
if("JsonTimetableV1".equals(objectType)) { |
|
|
|
}else if("JsonAssociationV1".equals(objectType)){ |
|
asso.add(obj.getJSONObject("JsonAssociationV1")); |
|
}else if("TiplocV1".equals(objectType)){ |
|
tiploc.add(obj.getJSONObject("TiplocV1")); |
|
}else if("JsonScheduleV1".equals(objectType)){ |
|
schedule.add(obj.getJSONObject("JsonScheduleV1")); |
|
}else if("EOF".equals(objectType)){ |
|
//Nothing to do |
|
}else { |
|
log.fatal("Unhandled type {}", objectType); |
|
System.exit(2); |
|
} |
|
|
|
} |
|
log.info("Total count : {}", count); |
|
asso.shutdownAndWait(); |
|
tiploc.shutdownAndWait(); |
|
schedule.shutdownAndWait(); |
|
}catch(IOException e) { |
|
log.error(e.getMessage(), e); |
|
System.exit(1); |
|
} |
|
try { |
|
fis.close(); |
|
}catch(IOException e) { |
|
log.fatal(e.getMessage(), e); |
|
System.exit(1); |
|
} |
|
DatabaseManager.getInstance().shutdown(); |
|
log.info("Job finished!"); |
|
} |
|
|
|
}
|
|
|