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>
35 public class CoreImpl implements Core, NodeListener, RequestListener {
37 /** The core listeners. */
38 private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
40 /** The project manager. */
41 private ProjectManager projectManager;
43 /** The node manager. */
44 private NodeManager nodeManager;
46 /** The request manager. */
48 @SuppressWarnings("unused")
49 private RequestManager requestManager;
52 // LISTENER MANAGEMENT
58 public void addCoreListener(CoreListener coreListener) {
59 coreListeners.add(coreListener);
65 public void removeCoreListener(CoreListener coreListener) {
66 coreListeners.remove(coreListener);
70 * Notifies all listeners that the projects were loaded successfully.
73 * The directory the projects were loaded from
75 private void fireLoadingProjectsDone(String directory) {
76 for (CoreListener coreListener: coreListeners) {
77 coreListener.loadingProjectsDone(directory);
82 * Notifies all core listeners that loading the projects from the given
83 * directory has failed.
86 * The directory the projects were tried to load from
88 * The exception that occured when loading projects
90 private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
91 for (CoreListener coreListener: coreListeners) {
92 coreListener.loadingProjectsFailed(directory, throwable);
97 * Notifies all listeners that the projects were successfully saved.
100 * The directory the projects were saved to
102 private void fireSavingProjectsDone(String directory) {
103 for (CoreListener coreListener: coreListeners) {
104 coreListener.savingProjectsDone(directory);
109 * Notifies all listeners that the projects could not be saved.
112 * The directory the projects were to be saved to
114 * The exception that occured when saving the projects
116 private void fireSavingProjectsFailed(String directory, Throwable throwable) {
117 for (CoreListener coreListener: coreListeners) {
118 coreListener.savingProjectsFailed(directory, throwable);
123 * Notifies all listeners that the nodes were successfully loaded.
126 * The directory the nodes were loaded from
128 private void fireLoadingNodesDone(String directory) {
129 for (CoreListener coreListener: coreListeners) {
130 coreListener.loadingNodesDone(directory);
135 * Notifies all listeners that loading the nodes has failed.
138 * The directory the nodes were loaded from
140 * The exception that occured while loading the nodes
142 private void fireLoadingNodesFailed(String directory, Throwable throwable) {
143 for (CoreListener coreListener: coreListeners) {
144 coreListener.loadingNodesFailed(directory, throwable);
149 * Notifies all listeners that the nodes were saved successfully.
152 * The directory the nodes were saved to
154 private void fireSavingNodesDone(String directory) {
155 for (CoreListener coreListener: coreListeners) {
156 coreListener.savingNodesDone(directory);
161 * Notifies all listeners that saving the nodes has failed.
164 * The directory the nodes were saved to
166 * The exception that occured while saving the nodes
168 private void fireSavingNodesFailed(String directory, Throwable throwable) {
169 for (CoreListener coreListener: coreListeners) {
170 coreListener.savingProjectsFailed(directory, throwable);
175 * Notifies all core listeners that the core has loaded and is ready to run.
177 private void fireCoreLoaded() {
178 for (CoreListener coreListener: coreListeners) {
179 coreListener.coreLoaded();
184 * Notifies all listeners that the core was stopped.
186 private void fireCoreStopped() {
187 for (CoreListener coreListener: coreListeners) {
188 coreListener.coreStopped();
193 * Notifies all listeners that a node was added to the core.
196 * The node that was added
198 private void fireNodeAdded(Node node) {
199 for (CoreListener coreListener: coreListeners) {
200 coreListener.nodeAdded(node);
205 * Notifies all listeners that a node was removed from the core.
208 * The node that was removed
210 private void fireNodeRemoved(Node node) {
211 for (CoreListener coreListener: coreListeners) {
212 coreListener.nodeRemoved(node);
217 * Notifies all listeners that a connection to the given node is now being
221 * The node that is being connected to
223 private void fireNodeConnecting(Node node) {
224 for (CoreListener coreListener: coreListeners) {
225 coreListener.nodeConnecting(node);
230 * Notifies all listeners that the given node is now connected.
233 * The node that is now connected
235 private void fireNodeConnected(Node node) {
236 for (CoreListener coreListener: coreListeners) {
237 coreListener.nodeConnected(node);
242 * Notifies all listeners that a connection to a node has failed.
245 * The node that could not be connected
247 * The cause of the failure
249 private void fireNodeConnectionFailed(Node node, Throwable cause) {
250 for (CoreListener coreListener: coreListeners) {
251 coreListener.nodeConnectionFailed(node, cause);
256 * Notifies all listeners that the given node was disconnected.
259 * The node that is now disconnected
261 * The exception that caused the disconnect, or <code>null</code>
262 * if there was no exception
264 private void fireNodeDisconnected(Node node, Throwable throwable) {
265 for (CoreListener coreListener: coreListeners) {
266 coreListener.nodeDisconnected(node, throwable);
275 * Returns the project manager.
277 * @return The project manager
279 public ProjectManager getProjectManager() {
280 return projectManager;
284 * Sets the project manager to use.
286 * @param projectManager
287 * The project manager to use
289 public void setProjectManager(ProjectManager projectManager) {
290 this.projectManager = projectManager;
294 * Returns the node manager.
296 * @return The node manager
298 public NodeManager getNodeManager() {
303 * Sets the node manager to use.
306 * The node manager to use
308 public void setNodeManager(NodeManager nodeManager) {
309 this.nodeManager = nodeManager;
313 * Sets the request manager to use.
315 * @param requestManager
316 * The request manager to use
318 public void setRequestManager(RequestManager requestManager) {
319 this.requestManager = requestManager;
325 public List<Node> getNodes() {
326 return nodeManager.getNodes();
332 public boolean isNodeConnected(Node node) {
333 return nodeManager.hasNode(node);
339 public List<Project> getProjects() {
340 return projectManager.getProjects();
350 public void start() {
352 projectManager.load();
353 fireLoadingProjectsDone(projectManager.getDirectory());
354 } catch (IOException ioe1) {
355 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
359 fireLoadingNodesDone(nodeManager.getDirectory());
360 } catch (IOException ioe1) {
361 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
371 projectManager.save();
372 fireSavingProjectsDone(projectManager.getDirectory());
373 } catch (IOException ioe1) {
374 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
378 fireSavingNodesDone(nodeManager.getDirectory());
379 } catch (IOException ioe1) {
380 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
388 public boolean addNode(Node node) throws UnknownHostException {
389 return nodeManager.addNode(node);
395 public void removeNode(Node node) {
396 nodeManager.removeNode(node);
402 public void connectToNode(Node node) {
403 fireNodeConnecting(node);
404 nodeManager.connect(node);
410 public void disconnectFromNode(Node node) {
411 nodeManager.disconnect(node);
417 public Project createProject() throws IOException, JSiteException {
418 return projectManager.createProject();
426 * Loads the configuration.
428 @SuppressWarnings("unused")
429 private void loadConfig() {
434 * Saves the configuration.
436 @SuppressWarnings("unused")
437 private void saveConfig() {
442 // INTERFACE NodeListener
448 public void nodeAdded(Node node) {
455 public void nodeRemoved(Node node) {
456 fireNodeRemoved(node);
462 public void nodeConnected(Node node) {
463 fireNodeConnected(node);
469 public void nodeConnectionFailed(Node node, Throwable cause) {
470 fireNodeConnectionFailed(node, cause);
476 public void nodeDisconnected(Node node, Throwable throwable) {
477 fireNodeDisconnected(node, throwable);
481 // INTERFACE RequestListener
487 public void requestAdded(Request request) {
488 /* TODO - find project and process request */
492 * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(Request)
494 public void requestProgressed(Request request) {
495 /* TODO - find project and process request */
499 * @see net.pterodactylus.jsite.core.RequestListener#requestRemoved(net.pterodactylus.jsite.core.Request)
501 public void requestRemoved(Request request) {
502 /* TODO - find project and process request */
506 * @see net.pterodactylus.jsite.core.RequestListener#requestGeneratedURI(net.pterodactylus.jsite.core.Request,
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 */