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