From 027e159571d58b03bbdcc7398487938d1ace2f1c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 4 Jun 2008 08:55:04 +0200 Subject: [PATCH] add configuration loading --- src/net/pterodactylus/fac/core/InsertManager.java | 70 ++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) 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. */ + } + } + } + } } -- 2.7.4