add request management
[jSite2.git] / src / net / pterodactylus / jsite / core / CoreImpl.java
1 /*
2  * jSite2 - Core.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.jsite.core;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27  * The core of jSite.
28  * 
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  * @version $Id$
31  */
32 public class CoreImpl implements Core, NodeListener, RequestListener {
33
34         /** The core listeners. */
35         private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
36
37         /** The project manager. */
38         private ProjectManager projectManager;
39
40         /** The node manager. */
41         private NodeManager nodeManager;
42
43         /** The request manager. */
44         /* TODO - remove */
45         @SuppressWarnings("unused")
46         private RequestManager requestManager;
47
48         //
49         // LISTENER MANAGEMENT
50         //
51
52         /**
53          * {@inheritDoc}
54          */
55         public void addCoreListener(CoreListener coreListener) {
56                 coreListeners.add(coreListener);
57         }
58
59         /**
60          * {@inheritDoc}
61          */
62         public void removeCoreListener(CoreListener coreListener) {
63                 coreListeners.remove(coreListener);
64         }
65
66         /**
67          * Notifies all listeners that the projects were loaded successfully.
68          * 
69          * @param directory
70          *            The directory the projects were loaded from
71          */
72         private void fireLoadingProjectsDone(String directory) {
73                 for (CoreListener coreListener: coreListeners) {
74                         coreListener.loadingProjectsDone(directory);
75                 }
76         }
77
78         /**
79          * Notifies all core listeners that loading the projects from the given
80          * directory has failed.
81          * 
82          * @param directory
83          *            The directory the projects were tried to load from
84          * @param throwable
85          *            The exception that occured when loading projects
86          */
87         private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
88                 for (CoreListener coreListener: coreListeners) {
89                         coreListener.loadingProjectsFailed(directory, throwable);
90                 }
91         }
92
93         /**
94          * Notifies all listeners that the projects were successfully saved.
95          * 
96          * @param directory
97          *            The directory the projects were saved to
98          */
99         private void fireSavingProjectsDone(String directory) {
100                 for (CoreListener coreListener: coreListeners) {
101                         coreListener.savingProjectsDone(directory);
102                 }
103         }
104
105         /**
106          * Notifies all listeners that the projects could not be saved.
107          * 
108          * @param directory
109          *            The directory the projects were to be saved to
110          * @param throwable
111          *            The exception that occured when saving the projects
112          */
113         private void fireSavingProjectsFailed(String directory, Throwable throwable) {
114                 for (CoreListener coreListener: coreListeners) {
115                         coreListener.savingProjectsFailed(directory, throwable);
116                 }
117         }
118
119         /**
120          * Notifies all listeners that the nodes were successfully loaded.
121          * 
122          * @param directory
123          *            The directory the nodes were loaded from
124          */
125         private void fireLoadingNodesDone(String directory) {
126                 for (CoreListener coreListener: coreListeners) {
127                         coreListener.loadingNodesDone(directory);
128                 }
129         }
130
131         /**
132          * Notifies all listeners that loading the nodes has failed.
133          * 
134          * @param directory
135          *            The directory the nodes were loaded from
136          * @param throwable
137          *            The exception that occured while loading the nodes
138          */
139         private void fireLoadingNodesFailed(String directory, Throwable throwable) {
140                 for (CoreListener coreListener: coreListeners) {
141                         coreListener.loadingNodesFailed(directory, throwable);
142                 }
143         }
144
145         /**
146          * Notifies all listeners that the nodes were saved successfully.
147          * 
148          * @param directory
149          *            The directory the nodes were saved to
150          */
151         private void fireSavingNodesDone(String directory) {
152                 for (CoreListener coreListener: coreListeners) {
153                         coreListener.savingNodesDone(directory);
154                 }
155         }
156
157         /**
158          * Notifies all listeners that saving the nodes has failed.
159          * 
160          * @param directory
161          *            The directory the nodes were saved to
162          * @param throwable
163          *            The exception that occured while saving the nodes
164          */
165         private void fireSavingNodesFailed(String directory, Throwable throwable) {
166                 for (CoreListener coreListener: coreListeners) {
167                         coreListener.savingProjectsFailed(directory, throwable);
168                 }
169         }
170
171         /**
172          * Notifies all core listeners that the core has loaded and is ready to run.
173          */
174         private void fireCoreLoaded() {
175                 for (CoreListener coreListener: coreListeners) {
176                         coreListener.coreLoaded();
177                 }
178         }
179
180         /**
181          * Notifies all listeners that the core was stopped.
182          */
183         private void fireCoreStopped() {
184                 for (CoreListener coreListener: coreListeners) {
185                         coreListener.coreStopped();
186                 }
187         }
188
189         /**
190          * Notifies all listeners that a connection to the given node is now being
191          * established.
192          * 
193          * @param node
194          *            The node that is being connected to
195          */
196         private void fireNodeConnecting(Node node) {
197                 for (CoreListener coreListener: coreListeners) {
198                         coreListener.nodeConnecting(node);
199                 }
200         }
201
202         /**
203          * Notifies all listeners that the given node is now connected.
204          * 
205          * @param node
206          *            The node that is now connected
207          */
208         private void fireNodeConnected(Node node) {
209                 for (CoreListener coreListener: coreListeners) {
210                         coreListener.nodeConnected(node);
211                 }
212         }
213
214         /**
215          * Notifies all listeners that the given node was disconnected.
216          * 
217          * @param node
218          *            The node that is now disconnected
219          * @param throwable
220          *            The exception that caused the disconnect, or <code>null</code>
221          *            if there was no exception
222          */
223         private void fireNodeDisconnected(Node node, Throwable throwable) {
224                 for (CoreListener coreListener: coreListeners) {
225                         coreListener.nodeDisconnected(node, throwable);
226                 }
227         }
228
229         /**
230          * Notifies all listeners that a request was added to a node.
231          * 
232          * @param node
233          *            The node the request was added to
234          * @param request
235          *            The request that was added
236          */
237         private void fireRequestAdded(Node node, Request request) {
238                 for (CoreListener coreListener: coreListeners) {
239                         coreListener.requestAdded(node, request);
240                 }
241         }
242
243         //
244         // ACCESSORS
245         //
246
247         /**
248          * Returns the project manager.
249          * 
250          * @return The project manager
251          */
252         public ProjectManager getProjectManager() {
253                 return projectManager;
254         }
255
256         /**
257          * Sets the project manager to use.
258          * 
259          * @param projectManager
260          *            The project manager to use
261          */
262         public void setProjectManager(ProjectManager projectManager) {
263                 this.projectManager = projectManager;
264         }
265
266         /**
267          * Returns the node manager.
268          * 
269          * @return The node manager
270          */
271         public NodeManager getNodeManager() {
272                 return nodeManager;
273         }
274
275         /**
276          * Sets the node manager to use.
277          * 
278          * @param nodeManager
279          *            The node manager to use
280          */
281         public void setNodeManager(NodeManager nodeManager) {
282                 this.nodeManager = nodeManager;
283         }
284
285         /**
286          * Sets the request manager to use.
287          * 
288          * @param requestManager
289          *            The request manager to use
290          */
291         public void setRequestManager(RequestManager requestManager) {
292                 this.requestManager = requestManager;
293         }
294
295         /**
296          * {@inheritDoc}
297          */
298         public List<Node> getNodes() {
299                 return nodeManager.getNodes();
300         }
301
302         /**
303          * {@inheritDoc}
304          */
305         public boolean isNodeConnected(Node node) {
306                 return nodeManager.hasNode(node);
307         }
308
309         //
310         // ACTIONS
311         //
312
313         /**
314          * {@inheritDoc}
315          */
316         public void start() {
317                 try {
318                         projectManager.load();
319                         fireLoadingProjectsDone(projectManager.getDirectory());
320                 } catch (IOException ioe1) {
321                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
322                 }
323                 try {
324                         nodeManager.load();
325                         fireLoadingNodesDone(nodeManager.getDirectory());
326                 } catch (IOException ioe1) {
327                         fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
328                 }
329                 fireCoreLoaded();
330         }
331
332         /**
333          * {@inheritDoc}
334          */
335         public void stop() {
336                 try {
337                         projectManager.save();
338                         fireSavingProjectsDone(projectManager.getDirectory());
339                 } catch (IOException ioe1) {
340                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
341                 }
342                 try {
343                         nodeManager.save();
344                         fireSavingNodesDone(nodeManager.getDirectory());
345                 } catch (IOException ioe1) {
346                         fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
347                 }
348                 fireCoreStopped();
349         }
350
351         /**
352          * {@inheritDoc}
353          */
354         public void connectToNode(Node node) {
355                 fireNodeConnecting(node);
356                 nodeManager.addNode(node);
357                 nodeManager.connect(node);
358         }
359
360         /**
361          * {@inheritDoc}
362          */
363         public void disconnectFromNode(Node node) {
364                 nodeManager.disconnect(node);
365         }
366
367         //
368         // PRIVATE METHODS
369         //
370
371         /**
372          * Loads the configuration.
373          */
374         @SuppressWarnings("unused")
375         private void loadConfig() {
376                 /* TODO */
377         }
378
379         /**
380          * Saves the configuration.
381          */
382         @SuppressWarnings("unused")
383         private void saveConfig() {
384                 /* TODO */
385         }
386
387         //
388         // INTERFACE NodeListener
389         //
390
391         /**
392          * {@inheritDoc}
393          */
394         public void nodeConnected(Node node) {
395                 fireNodeConnected(node);
396         }
397
398         /**
399          * {@inheritDoc}
400          */
401         public void nodeDisconnected(Node node, Throwable throwable) {
402                 fireNodeDisconnected(node, throwable);
403         }
404
405         //
406         // INTERFACE RequestListener
407         //
408
409         /**
410          * {@inheritDoc}
411          */
412         public void requestAdded(Node node, Request request) {
413                 fireRequestAdded(node, request);
414         }
415
416 }