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