add configuration loading
[fac.git] / src / net / pterodactylus / fac / core / InsertManager.java
1 /*
2  * fac - InsertManager.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fac.core;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Properties;
29
30 /**
31  * TODO
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 public class InsertManager {
36
37         /** The list of inserts. */
38         private final List<Insert> inserts = Collections.synchronizedList(new ArrayList<Insert>());
39
40         //
41         // ACTIONS
42         //
43
44         /**
45          * Starts the insert manager and loads its configuration file.
46          */
47         public void start() {
48                 loadConfiguration();
49         }
50
51         //
52         // PRIVATE METHODS
53         //
54
55         /**
56          * Loads the configuration.
57          */
58         private void loadConfiguration() {
59                 File configurationFile = new File("inserts.properties");
60                 if (!configurationFile.exists()) {
61                         return;
62                 }
63                 FileInputStream fileInputStream = null;
64                 try {
65                         fileInputStream = new FileInputStream(configurationFile);
66                         Properties configurationProperties = new Properties();
67                         configurationProperties.load(fileInputStream);
68                         int insertIndex = 0;
69                         List<Insert> newInserts = new ArrayList<Insert>();
70                         while (configurationProperties.containsKey("insert." + insertIndex + ".insertTime")) {
71                                 long insertTime = Long.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".insertTime"));
72                                 String uri = configurationProperties.getProperty("insert." + insertIndex + ".uri");
73                                 long checkTime = Long.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".checkTime"));
74                                 boolean checked = Boolean.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".checked"));
75                                 boolean successful = Boolean.valueOf(configurationProperties.getProperty("insert." + insertIndex + ".successful"));
76                                 Insert insert = new Insert();
77                                 insert.setInsertTime(insertTime);
78                                 insert.setURI(uri);
79                                 insert.setCheckTime(checkTime);
80                                 insert.setChecked(checked);
81                                 insert.setSuccessful(successful);
82                                 newInserts.add(insert);
83                         }
84                         inserts.clear();
85                         inserts.addAll(newInserts);
86                 } catch (IOException ioe1) {
87                         System.err.println("could not load configuration: " + ioe1.getClass().getSimpleName() + ((ioe1.getMessage() != null) ? (": " + ioe1.getMessage()) : ("")));
88                 } finally {
89                         if (fileInputStream != null) {
90                                 try {
91                                         fileInputStream.close();
92                                 } catch (IOException ioe1) {
93                                         /* ignore. */
94                                 }
95                         }
96                 }
97         }
98
99 }