From: David ‘Bombe’ Roden Date: Wed, 4 Jun 2008 06:55:04 +0000 (+0200) Subject: add configuration loading X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=027e159571d58b03bbdcc7398487938d1ace2f1c;p=fac.git add configuration loading --- diff --git a/src/net/pterodactylus/fac/core/InsertManager.java b/src/net/pterodactylus/fac/core/InsertManager.java index 92062a5..6079d88 100644 --- a/src/net/pterodactylus/fac/core/InsertManager.java +++ b/src/net/pterodactylus/fac/core/InsertManager.java @@ -19,13 +19,81 @@ package net.pterodactylus.fac.core; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Properties; /** * TODO + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class InsertManager { - /* TODO */ + /** The list of inserts. */ + private final List inserts = Collections.synchronizedList(new ArrayList()); + + // + // ACTIONS + // + + /** + * Starts the insert manager and loads its configuration file. + */ + public void start() { + loadConfiguration(); + } + + // + // PRIVATE METHODS + // + + /** + * Loads the configuration. + */ + private void loadConfiguration() { + File configurationFile = new File("inserts.properties"); + if (!configurationFile.exists()) { + return; + } + FileInputStream fileInputStream = null; + try { + fileInputStream = new FileInputStream(configurationFile); + Properties configurationProperties = new Properties(); + configurationProperties.load(fileInputStream); + int insertIndex = 0; + List newInserts = new ArrayList(); + while (configurationProperties.containsKey("insert." + insertIndex + ".insertTime")) { + long insertTime = Long.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".insertTime")); + String uri = configurationProperties.getProperty("insert." + insertIndex + ".uri"); + long checkTime = Long.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".checkTime")); + boolean checked = Boolean.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".checked")); + boolean successful = Boolean.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".successful")); + Insert insert = new Insert(); + insert.setInsertTime(insertTime); + insert.setURI(uri); + insert.setCheckTime(checkTime); + insert.setChecked(checked); + insert.setSuccessful(successful); + newInserts.add(insert); + } + inserts.clear(); + inserts.addAll(newInserts); + } catch (IOException ioe1) { + System.err.println("could not load configuration: " + ioe1.getClass().getSimpleName() + ((ioe1.getMessage() != null) ? (": " + ioe1.getMessage()) : (""))); + } finally { + if (fileInputStream != null) { + try { + fileInputStream.close(); + } catch (IOException ioe1) { + /* ignore. */ + } + } + } + } }