implement node addition and removal events
[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 node was added to the core.
191          * 
192          * @param node
193          *            The node that was added
194          */
195         private void fireNodeAdded(Node node) {
196                 for (CoreListener coreListener: coreListeners) {
197                         coreListener.nodeAdded(node);
198                 }
199         }
200
201         /**
202          * Notifies all listeners that a node was removed from the core.
203          * 
204          * @param node
205          *            The node that was removed
206          */
207         private void fireNodeRemoved(Node node) {
208                 for (CoreListener coreListener: coreListeners) {
209                         coreListener.nodeRemoved(node);
210                 }
211         }
212
213         /**
214          * Notifies all listeners that a connection to the given node is now being
215          * established.
216          * 
217          * @param node
218          *            The node that is being connected to
219          */
220         private void fireNodeConnecting(Node node) {
221                 for (CoreListener coreListener: coreListeners) {
222                         coreListener.nodeConnecting(node);
223                 }
224         }
225
226         /**
227          * Notifies all listeners that the given node is now connected.
228          * 
229          * @param node
230          *            The node that is now connected
231          */
232         private void fireNodeConnected(Node node) {
233                 for (CoreListener coreListener: coreListeners) {
234                         coreListener.nodeConnected(node);
235                 }
236         }
237
238         /**
239          * Notifies all listeners that the given node was disconnected.
240          * 
241          * @param node
242          *            The node that is now disconnected
243          * @param throwable
244          *            The exception that caused the disconnect, or <code>null</code>
245          *            if there was no exception
246          */
247         private void fireNodeDisconnected(Node node, Throwable throwable) {
248                 for (CoreListener coreListener: coreListeners) {
249                         coreListener.nodeDisconnected(node, throwable);
250                 }
251         }
252
253         /**
254          * Notifies all listeners that a request was added to a node.
255          * 
256          * @param node
257          *            The node the request was added to
258          * @param request
259          *            The request that was added
260          */
261         private void fireRequestAdded(Node node, Request request) {
262                 for (CoreListener coreListener: coreListeners) {
263                         coreListener.requestAdded(node, request);
264                 }
265         }
266
267         //
268         // ACCESSORS
269         //
270
271         /**
272          * Returns the project manager.
273          * 
274          * @return The project manager
275          */
276         public ProjectManager getProjectManager() {
277                 return projectManager;
278         }
279
280         /**
281          * Sets the project manager to use.
282          * 
283          * @param projectManager
284          *            The project manager to use
285          */
286         public void setProjectManager(ProjectManager projectManager) {
287                 this.projectManager = projectManager;
288         }
289
290         /**
291          * Returns the node manager.
292          * 
293          * @return The node manager
294          */
295         public NodeManager getNodeManager() {
296                 return nodeManager;
297         }
298
299         /**
300          * Sets the node manager to use.
301          * 
302          * @param nodeManager
303          *            The node manager to use
304          */
305         public void setNodeManager(NodeManager nodeManager) {
306                 this.nodeManager = nodeManager;
307         }
308
309         /**
310          * Sets the request manager to use.
311          * 
312          * @param requestManager
313          *            The request manager to use
314          */
315         public void setRequestManager(RequestManager requestManager) {
316                 this.requestManager = requestManager;
317         }
318
319         /**
320          * {@inheritDoc}
321          */
322         public List<Node> getNodes() {
323                 return nodeManager.getNodes();
324         }
325
326         /**
327          * {@inheritDoc}
328          */
329         public boolean isNodeConnected(Node node) {
330                 return nodeManager.hasNode(node);
331         }
332
333         //
334         // ACTIONS
335         //
336
337         /**
338          * {@inheritDoc}
339          */
340         public void start() {
341                 try {
342                         projectManager.load();
343                         fireLoadingProjectsDone(projectManager.getDirectory());
344                 } catch (IOException ioe1) {
345                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
346                 }
347                 try {
348                         nodeManager.load();
349                         fireLoadingNodesDone(nodeManager.getDirectory());
350                 } catch (IOException ioe1) {
351                         fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
352                 }
353                 fireCoreLoaded();
354         }
355
356         /**
357          * {@inheritDoc}
358          */
359         public void stop() {
360                 try {
361                         projectManager.save();
362                         fireSavingProjectsDone(projectManager.getDirectory());
363                 } catch (IOException ioe1) {
364                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
365                 }
366                 try {
367                         nodeManager.save();
368                         fireSavingNodesDone(nodeManager.getDirectory());
369                 } catch (IOException ioe1) {
370                         fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
371                 }
372                 fireCoreStopped();
373         }
374
375         /**
376          * {@inheritDoc}
377          */
378         public void addNode(Node node) {
379                 nodeManager.addNode(node);
380                 fireNodeAdded(node);
381         }
382
383         /**
384          * {@inheritDoc}
385          */
386         public void removeNode(Node node) {
387                 nodeManager.removeNode(node);
388                 fireNodeRemoved(node);
389         }
390
391         /**
392          * {@inheritDoc}
393          */
394         public void connectToNode(Node node) {
395                 fireNodeConnecting(node);
396                 nodeManager.addNode(node);
397                 nodeManager.connect(node);
398         }
399
400         /**
401          * {@inheritDoc}
402          */
403         public void disconnectFromNode(Node node) {
404                 nodeManager.disconnect(node);
405         }
406
407         //
408         // PRIVATE METHODS
409         //
410
411         /**
412          * Loads the configuration.
413          */
414         @SuppressWarnings("unused")
415         private void loadConfig() {
416                 /* TODO */
417         }
418
419         /**
420          * Saves the configuration.
421          */
422         @SuppressWarnings("unused")
423         private void saveConfig() {
424                 /* TODO */
425         }
426
427         //
428         // INTERFACE NodeListener
429         //
430
431         /**
432          * {@inheritDoc}
433          */
434         public void nodeAdded(Node node) {
435                 fireNodeAdded(node);
436         }
437
438         /**
439          * {@inheritDoc}
440          */
441         public void nodeRemoved(Node node) {
442                 fireNodeRemoved(node);
443         }
444
445         /**
446          * {@inheritDoc}
447          */
448         public void nodeConnected(Node node) {
449                 fireNodeConnected(node);
450         }
451
452         /**
453          * {@inheritDoc}
454          */
455         public void nodeDisconnected(Node node, Throwable throwable) {
456                 fireNodeDisconnected(node, throwable);
457         }
458
459         //
460         // INTERFACE RequestListener
461         //
462
463         /**
464          * {@inheritDoc}
465          */
466         public void requestAdded(Node node, Request request) {
467                 fireRequestAdded(node, request);
468         }
469
470 }