add more status notifications to core listener
[jSite2.git] / src / net / pterodactylus / jsite / core / Core.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 Core {
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          * Adds the given listener to the list of registered listeners.
49          *
50          * @param coreListener
51          *            The listener to add
52          */
53         public void addCoreListener(CoreListener coreListener) {
54                 coreListeners.add(coreListener);
55         }
56
57         /**
58          * Removes the given listener from the list of registered listeners.
59          *
60          * @param coreListener
61          *            The listener to remove
62          */
63         public void removeCoreListener(CoreListener coreListener) {
64                 coreListeners.remove(coreListener);
65         }
66
67         /**
68          * Notifies all listeners that the projects were loaded successfully.
69          *
70          * @param directory
71          *            The directory the projects were loaded from
72          */
73         private void fireLoadingProjectsDone(String directory) {
74                 for (CoreListener coreListener: coreListeners) {
75                         coreListener.loadingProjectsDone(directory);
76                 }
77         }
78
79         /**
80          * Notifies all core listeners that loading the projects from the given
81          * directory has failed.
82          *
83          * @param directory
84          *            The directory the projects were tried to load from
85          * @param throwable
86          *            The exception that occured when loading projects
87          */
88         private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
89                 for (CoreListener coreListener: coreListeners) {
90                         coreListener.loadingProjectsFailed(directory, throwable);
91                 }
92         }
93
94         /**
95          * Notifies all listeners that the projects were successfully saved.
96          *
97          * @param directory
98          *            The directory the projects were saved to
99          */
100         private void fireSavingProjectsDone(String directory) {
101                 for (CoreListener coreListener: coreListeners) {
102                         coreListener.savingProjectsDone(directory);
103                 }
104         }
105
106         /**
107          * Notifies all listeners that the projects could not be saved.
108          *
109          * @param directory
110          *            The directory the projects were to be saved to
111          * @param throwable
112          *            The exception that occured when saving the projects
113          */
114         private void fireSavingProjectsFailed(String directory, Throwable throwable) {
115                 for (CoreListener coreListener: coreListeners) {
116                         coreListener.savingProjectsFailed(directory, throwable);
117                 }
118         }
119
120         /**
121          * Notifies all listeners that the nodes were successfully loaded.
122          *
123          * @param directory
124          *            The directory the nodes were loaded from
125          */
126         private void fireLoadingNodesDone(String directory) {
127                 for (CoreListener coreListener: coreListeners) {
128                         coreListener.loadingNodesDone(directory);
129                 }
130         }
131
132         /**
133          * Notifies all listeners that loading the nodes has failed.
134          *
135          * @param directory
136          *            The directory the nodes were loaded from
137          * @param throwable
138          *            The exception that occured while loading the nodes
139          */
140         private void fireLoadingNodesFailed(String directory, Throwable throwable) {
141                 for (CoreListener coreListener: coreListeners) {
142                         coreListener.loadingNodesFailed(directory, throwable);
143                 }
144         }
145
146         /**
147          * Notifies all listeners that the nodes were saved successfully.
148          *
149          * @param directory
150          *            The directory the nodes were saved to
151          */
152         private void fireSavingNodesDone(String directory) {
153                 for (CoreListener coreListener: coreListeners) {
154                         coreListener.savingNodesDone(directory);
155                 }
156         }
157
158         /**
159          * Notifies all listeners that saving the nodes has failed.
160          *
161          * @param directory
162          *            The directory the nodes were saved to
163          * @param throwable
164          *            The exception that occured while saving the nodes
165          */
166         private void fireSavingNodesFailed(String directory, Throwable throwable) {
167                 for (CoreListener coreListener: coreListeners) {
168                         coreListener.savingProjectsFailed(directory, throwable);
169                 }
170         }
171
172         /**
173          * Notifies all core listeners that the core has loaded and is ready to run.
174          */
175         private void fireCoreLoaded() {
176                 for (CoreListener coreListener: coreListeners) {
177                         coreListener.coreLoaded();
178                 }
179         }
180
181         /**
182          * Notifies all listeners that the core was stopped.
183          */
184         private void fireCoreStopped() {
185                 for (CoreListener coreListener: coreListeners) {
186                         coreListener.coreStopped();
187                 }
188         }
189
190         //
191         // ACCESSORS
192         //
193
194         /**
195          * Returns the project manager.
196          *
197          * @return The project manager
198          */
199         public ProjectManager getProjectManager() {
200                 return projectManager;
201         }
202
203         /**
204          * Sets the project manager to use.
205          *
206          * @param projectManager
207          *            The project manager to use
208          */
209         public void setProjectManager(ProjectManager projectManager) {
210                 this.projectManager = projectManager;
211         }
212
213         /**
214          * Returns the node manager.
215          *
216          * @return The node manager
217          */
218         public NodeManager getNodeManager() {
219                 return nodeManager;
220         }
221
222         /**
223          * Sets the node manager to use.
224          *
225          * @param nodeManager
226          *            The node manager to use
227          */
228         public void setNodeManager(NodeManager nodeManager) {
229                 this.nodeManager = nodeManager;
230         }
231
232         /**
233          * Returns the list of all configured nodes.
234          *
235          * @return All configured nodes
236          */
237         public List<Node> getNodes() {
238                 return nodeManager.getNodes();
239         }
240
241         /**
242          * Returns whether the core is currently connected to the given node.
243          *
244          * @param node
245          *            The node to check
246          * @return <code>true</code> if the core is currently connected to the
247          *         node, <code>false</code> otherwise
248          */
249         public boolean isNodeConnected(Node node) {
250                 return nodeManager.hasNode(node);
251         }
252
253         //
254         // ACTIONS
255         //
256
257         /**
258          * Starts the core.
259          */
260         public void start() {
261                 try {
262                         projectManager.load();
263                         fireLoadingProjectsDone(projectManager.getDirectory());
264                 } catch (IOException ioe1) {
265                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
266                 }
267                 try {
268                         nodeManager.load();
269                         fireLoadingNodesDone(nodeManager.getDirectory());
270                 } catch (IOException ioe1) {
271                         fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
272                 }
273                 fireCoreLoaded();
274         }
275
276         /**
277          * Stops the core.
278          */
279         public void stop() {
280                 try {
281                         projectManager.save();
282                         fireSavingProjectsDone(projectManager.getDirectory());
283                 } catch (IOException ioe1) {
284                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
285                 }
286                 try {
287                         nodeManager.save();
288                         fireSavingNodesDone(nodeManager.getDirectory());
289                 } catch (IOException ioe1) {
290                         fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
291                 }
292                 fireCoreStopped();
293         }
294
295         /**
296          * Connects to the given node.
297          *
298          * @param node
299          *            The node to connect to
300          */
301         public void connectToNode(Node node) {
302                 /* TODO */
303         }
304
305         //
306         // PRIVATE METHODS
307         //
308
309         /**
310          * Loads the configuration.
311          */
312         @SuppressWarnings("unused")
313         private void loadConfig() {
314                 /* TODO */
315         }
316
317         /**
318          * Saves the configuration.
319          */
320         @SuppressWarnings("unused")
321         private void saveConfig() {
322                 /* TODO */
323         }
324
325 }