first skeleton
[jSite2.git] / src / net / pterodactylus / jsite / core / InsertManager.java
1 /*
2  * jSite2 - InsertManager.java - Copyright © 2008 David Roden
3  * 
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  * 
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 package net.pterodactylus.jsite.core;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import net.pterodactylus.util.logging.Logging;
27
28 /**
29  * Manages all currently running and past inserts.
30  * 
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  */
33 public class InsertManager {
34
35         /** Logger. */
36         private static final Logger logger = Logging.getLogger(InsertManager.class.getName());
37
38         /** The insert listeners. */
39         private final List<InsertListener> insertListeners = new ArrayList<InsertListener>();
40
41         //
42         // EVENT MANAGEMENT
43         //
44
45         /**
46          * Adds an insert listener to the list of insert listeners.
47          * 
48          * @param insertListener
49          *            The insert listener to add
50          */
51         public void addInsertListener(InsertListener insertListener) {
52                 logger.log(Level.FINEST, "addInsertListener(insertListener=" + insertListener + ")");
53                 synchronized (insertListeners) {
54                         insertListeners.add(insertListener);
55                 }
56         }
57
58         /**
59          * Removes an insert listener from the list of insert listeners.
60          * 
61          * @param insertListener
62          *            The insert listener to remove
63          */
64         public void removeInsertListener(InsertListener insertListener) {
65                 logger.log(Level.FINEST, "removeInsertListener(insertListener=" + insertListener + ")");
66                 synchronized (insertListeners) {
67                         insertListeners.remove(insertListener);
68                 }
69         }
70
71         /**
72          * Notifies all listeners that an insert was added.
73          * 
74          * @param insert
75          *            The insert that was added
76          */
77         private void fireInsertAdded(Insert insert) {
78                 synchronized (insertListeners) {
79                         for (InsertListener insertListener : insertListeners) {
80                                 insertListener.insertAdded(insert);
81                         }
82                 }
83         }
84
85         /**
86          * Notifies all listeners that an insert was removed.
87          * 
88          * @param insert
89          *            The insert that was removes
90          */
91         private void fireInsertRemoved(Insert insert) {
92                 synchronized (insertListeners) {
93                         for (InsertListener insertListener : insertListeners) {
94                                 insertListener.insertRemoved(insert);
95                         }
96                 }
97         }
98
99         /**
100          * Notifies all listeners that an insert was started.
101          * 
102          * @param insert
103          *            The insert that was started
104          */
105         private void fireInsertStarted(Insert insert) {
106                 synchronized (insertListeners) {
107                         for (InsertListener insertListener : insertListeners) {
108                                 insertListener.insertStarted(insert);
109                         }
110                 }
111         }
112
113         /**
114          * Notifies all listeners that an insert made some progress
115          * 
116          * @param insert
117          *            The insert that made some progress
118          */
119         private void fireInsertProgressed(Insert insert) {
120                 synchronized (insertListeners) {
121                         for (InsertListener insertListener : insertListeners) {
122                                 insertListener.insertProgressed(insert);
123                         }
124                 }
125         }
126
127         /**
128          * Notifies all listeners that an insert generated a URI.
129          * 
130          * @param insert
131          *            The insert that generated a URI
132          */
133         private void fireInsertGeneratedURI(Insert insert, String uri) {
134                 synchronized (insertListeners) {
135                         for (InsertListener insertListener : insertListeners) {
136                                 insertListener.insertGeneratedURI(insert, uri);
137                         }
138                 }
139         }
140
141         /**
142          * Notifies all listeners that an insert has finished.
143          * 
144          * @param insert
145          *            The insert that has finished
146          */
147         private void fireInsertFinished(Insert insert) {
148                 synchronized (insertListeners) {
149                         for (InsertListener insertListener : insertListeners) {
150                                 insertListener.insertFinished(insert);
151                         }
152                 }
153         }
154
155         //
156         // ACTIONS
157         //
158
159         /**
160          * Starts the insert manager.
161          */
162         public void start() {
163                 logger.log(Level.FINEST, "start()");
164                 loadConfiguration();
165         }
166
167         //
168         // PRIVATE METHODS
169         //
170
171         /**
172          * Loads the configuration.
173          */
174         private void loadConfiguration() {
175                 logger.log(Level.FINEST, "loadConfiguration()");
176         }
177
178 }