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