3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.core;
22 import java.io.IOException;
23 import java.net.UnknownHostException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import net.pterodactylus.jsite.project.Project;
28 import net.pterodactylus.jsite.project.ProjectManager;
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class CoreImpl implements Core, NodeListener, RequestListener {
38 /** The core listeners. */
39 private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
41 /** The project manager. */
42 private ProjectManager projectManager;
44 /** The node manager. */
45 private NodeManager nodeManager;
47 /** The request manager. */
49 @SuppressWarnings("unused")
50 private RequestManager requestManager;
53 // LISTENER MANAGEMENT
59 public void addCoreListener(CoreListener coreListener) {
60 coreListeners.add(coreListener);
66 public void removeCoreListener(CoreListener coreListener) {
67 coreListeners.remove(coreListener);
71 * Notifies all listeners that the projects were loaded successfully.
74 * The directory the projects were loaded from
76 private void fireLoadingProjectsDone(String directory) {
77 for (CoreListener coreListener: coreListeners) {
78 coreListener.loadingProjectsDone(directory);
83 * Notifies all core listeners that loading the projects from the given
84 * directory has failed.
87 * The directory the projects were tried to load from
89 * The exception that occured when loading projects
91 private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
92 for (CoreListener coreListener: coreListeners) {
93 coreListener.loadingProjectsFailed(directory, throwable);
98 * Notifies all listeners that the projects were successfully saved.
101 * The directory the projects were saved to
103 private void fireSavingProjectsDone(String directory) {
104 for (CoreListener coreListener: coreListeners) {
105 coreListener.savingProjectsDone(directory);
110 * Notifies all listeners that the projects could not be saved.
113 * The directory the projects were to be saved to
115 * The exception that occured when saving the projects
117 private void fireSavingProjectsFailed(String directory, Throwable throwable) {
118 for (CoreListener coreListener: coreListeners) {
119 coreListener.savingProjectsFailed(directory, throwable);
124 * Notifies all listeners that the nodes were successfully loaded.
127 * The directory the nodes were loaded from
129 private void fireLoadingNodesDone(String directory) {
130 for (CoreListener coreListener: coreListeners) {
131 coreListener.loadingNodesDone(directory);
136 * Notifies all listeners that loading the nodes has failed.
139 * The directory the nodes were loaded from
141 * The exception that occured while loading the nodes
143 private void fireLoadingNodesFailed(String directory, Throwable throwable) {
144 for (CoreListener coreListener: coreListeners) {
145 coreListener.loadingNodesFailed(directory, throwable);
150 * Notifies all listeners that the nodes were saved successfully.
153 * The directory the nodes were saved to
155 private void fireSavingNodesDone(String directory) {
156 for (CoreListener coreListener: coreListeners) {
157 coreListener.savingNodesDone(directory);
162 * Notifies all listeners that saving the nodes has failed.
165 * The directory the nodes were saved to
167 * The exception that occured while saving the nodes
169 private void fireSavingNodesFailed(String directory, Throwable throwable) {
170 for (CoreListener coreListener: coreListeners) {
171 coreListener.savingProjectsFailed(directory, throwable);
176 * Notifies all core listeners that the core has loaded and is ready to run.
178 private void fireCoreLoaded() {
179 for (CoreListener coreListener: coreListeners) {
180 coreListener.coreLoaded();
185 * Notifies all listeners that the core was stopped.
187 private void fireCoreStopped() {
188 for (CoreListener coreListener: coreListeners) {
189 coreListener.coreStopped();
194 * Notifies all listeners that a node was added to the core.
197 * The node that was added
199 private void fireNodeAdded(Node node) {
200 for (CoreListener coreListener: coreListeners) {
201 coreListener.nodeAdded(node);
206 * Notifies all listeners that a node was removed from the core.
209 * The node that was removed
211 private void fireNodeRemoved(Node node) {
212 for (CoreListener coreListener: coreListeners) {
213 coreListener.nodeRemoved(node);
218 * Notifies all listeners that a connection to the given node is now being
222 * The node that is being connected to
224 private void fireNodeConnecting(Node node) {
225 for (CoreListener coreListener: coreListeners) {
226 coreListener.nodeConnecting(node);
231 * Notifies all listeners that the given node is now connected.
234 * The node that is now connected
236 private void fireNodeConnected(Node node) {
237 for (CoreListener coreListener: coreListeners) {
238 coreListener.nodeConnected(node);
243 * Notifies all listeners that a connection to a node has failed.
246 * The node that could not be connected
248 * The cause of the failure
250 private void fireNodeConnectionFailed(Node node, Throwable cause) {
251 for (CoreListener coreListener: coreListeners) {
252 coreListener.nodeConnectionFailed(node, cause);
257 * Notifies all listeners that the given node was disconnected.
260 * The node that is now disconnected
262 * The exception that caused the disconnect, or <code>null</code>
263 * if there was no exception
265 private void fireNodeDisconnected(Node node, Throwable throwable) {
266 for (CoreListener coreListener: coreListeners) {
267 coreListener.nodeDisconnected(node, throwable);
276 * Returns the project manager.
278 * @return The project manager
280 public ProjectManager getProjectManager() {
281 return projectManager;
285 * Sets the project manager to use.
287 * @param projectManager
288 * The project manager to use
290 public void setProjectManager(ProjectManager projectManager) {
291 this.projectManager = projectManager;
295 * Returns the node manager.
297 * @return The node manager
299 public NodeManager getNodeManager() {
304 * Sets the node manager to use.
307 * The node manager to use
309 public void setNodeManager(NodeManager nodeManager) {
310 this.nodeManager = nodeManager;
314 * Sets the request manager to use.
316 * @param requestManager
317 * The request manager to use
319 public void setRequestManager(RequestManager requestManager) {
320 this.requestManager = requestManager;
326 public List<Node> getNodes() {
327 return nodeManager.getNodes();
333 public boolean isNodeConnected(Node node) {
334 return nodeManager.hasNode(node);
340 public List<Project> getProjects() {
341 return projectManager.getProjects();
351 public void start() {
353 projectManager.load();
354 fireLoadingProjectsDone(projectManager.getDirectory());
355 } catch (IOException ioe1) {
356 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
360 fireLoadingNodesDone(nodeManager.getDirectory());
361 } catch (IOException ioe1) {
362 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
372 projectManager.save();
373 fireSavingProjectsDone(projectManager.getDirectory());
374 } catch (IOException ioe1) {
375 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
379 fireSavingNodesDone(nodeManager.getDirectory());
380 } catch (IOException ioe1) {
381 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
389 public boolean addNode(Node node) throws UnknownHostException {
390 return nodeManager.addNode(node);
396 public void removeNode(Node node) {
397 nodeManager.removeNode(node);
403 public void connectToNode(Node node) {
404 fireNodeConnecting(node);
405 nodeManager.connect(node);
411 public void disconnectFromNode(Node node) {
412 nodeManager.disconnect(node);
418 public Project createProject() throws IOException, JSiteException {
419 return projectManager.createProject();
427 * Loads the configuration.
429 @SuppressWarnings("unused")
430 private void loadConfig() {
435 * Saves the configuration.
437 @SuppressWarnings("unused")
438 private void saveConfig() {
443 // INTERFACE NodeListener
449 public void nodeAdded(Node node) {
456 public void nodeRemoved(Node node) {
457 fireNodeRemoved(node);
463 public void nodeConnected(Node node) {
464 fireNodeConnected(node);
470 public void nodeConnectionFailed(Node node, Throwable cause) {
471 fireNodeConnectionFailed(node, cause);
477 public void nodeDisconnected(Node node, Throwable throwable) {
478 fireNodeDisconnected(node, throwable);
482 // INTERFACE RequestListener
488 public void requestAdded(Request request) {
489 /* TODO - find project and process request */
493 * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(Request)
495 public void requestProgressed(Request request) {
496 /* TODO - find project and process request */
500 * @see net.pterodactylus.jsite.core.RequestListener#requestRemoved(net.pterodactylus.jsite.core.Request)
502 public void requestRemoved(Request request) {
503 /* TODO - find project and process request */
507 * @see net.pterodactylus.jsite.core.RequestListener#requestGeneratedURI(net.pterodactylus.jsite.core.Request, java.lang.String)
509 public void requestGeneratedURI(Request request, String uri) {
510 /* TODO - find project and process request */
514 * @see net.pterodactylus.jsite.core.RequestListener#requestFinished(net.pterodactylus.jsite.core.Request)
516 public void requestFinished(Request request) {
517 /* TODO - find project and process request */