extract interface from core
[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 {
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         // ACCESSORS
186         //
187
188         /**
189          * Returns the project manager.
190          *
191          * @return The project manager
192          */
193         public ProjectManager getProjectManager() {
194                 return projectManager;
195         }
196
197         /**
198          * Sets the project manager to use.
199          *
200          * @param projectManager
201          *            The project manager to use
202          */
203         public void setProjectManager(ProjectManager projectManager) {
204                 this.projectManager = projectManager;
205         }
206
207         /**
208          * Returns the node manager.
209          *
210          * @return The node manager
211          */
212         public NodeManager getNodeManager() {
213                 return nodeManager;
214         }
215
216         /**
217          * Sets the node manager to use.
218          *
219          * @param nodeManager
220          *            The node manager to use
221          */
222         public void setNodeManager(NodeManager nodeManager) {
223                 this.nodeManager = nodeManager;
224         }
225
226         /**
227          * {@inheritDoc}
228          */
229         public List<Node> getNodes() {
230                 return nodeManager.getNodes();
231         }
232
233         /**
234          * {@inheritDoc}
235          */
236         public boolean isNodeConnected(Node node) {
237                 return nodeManager.hasNode(node);
238         }
239
240         //
241         // ACTIONS
242         //
243
244         /**
245          * {@inheritDoc}
246          */
247         public void start() {
248                 try {
249                         projectManager.load();
250                         fireLoadingProjectsDone(projectManager.getDirectory());
251                 } catch (IOException ioe1) {
252                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
253                 }
254                 try {
255                         nodeManager.load();
256                         fireLoadingNodesDone(nodeManager.getDirectory());
257                 } catch (IOException ioe1) {
258                         fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
259                 }
260                 fireCoreLoaded();
261         }
262
263         /**
264          * {@inheritDoc}
265          */
266         public void stop() {
267                 try {
268                         projectManager.save();
269                         fireSavingProjectsDone(projectManager.getDirectory());
270                 } catch (IOException ioe1) {
271                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
272                 }
273                 try {
274                         nodeManager.save();
275                         fireSavingNodesDone(nodeManager.getDirectory());
276                 } catch (IOException ioe1) {
277                         fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
278                 }
279                 fireCoreStopped();
280         }
281
282         /**
283          * {@inheritDoc}
284          */
285         public void connectToNode(Node node) {
286                 /* TODO */
287         }
288
289         //
290         // PRIVATE METHODS
291         //
292
293         /**
294          * Loads the configuration.
295          */
296         @SuppressWarnings("unused")
297         private void loadConfig() {
298                 /* TODO */
299         }
300
301         /**
302          * Saves the configuration.
303          */
304         @SuppressWarnings("unused")
305         private void saveConfig() {
306                 /* TODO */
307         }
308
309 }