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.util.ArrayList;
24 import java.util.List;
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32 public class CoreImpl implements Core, NodeListener, RequestListener {
34 /** The core listeners. */
35 private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
37 /** The project manager. */
38 private ProjectManager projectManager;
40 /** The node manager. */
41 private NodeManager nodeManager;
43 /** The request manager. */
45 @SuppressWarnings("unused")
46 private RequestManager requestManager;
49 // LISTENER MANAGEMENT
55 public void addCoreListener(CoreListener coreListener) {
56 coreListeners.add(coreListener);
62 public void removeCoreListener(CoreListener coreListener) {
63 coreListeners.remove(coreListener);
67 * Notifies all listeners that the projects were loaded successfully.
70 * The directory the projects were loaded from
72 private void fireLoadingProjectsDone(String directory) {
73 for (CoreListener coreListener: coreListeners) {
74 coreListener.loadingProjectsDone(directory);
79 * Notifies all core listeners that loading the projects from the given
80 * directory has failed.
83 * The directory the projects were tried to load from
85 * The exception that occured when loading projects
87 private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
88 for (CoreListener coreListener: coreListeners) {
89 coreListener.loadingProjectsFailed(directory, throwable);
94 * Notifies all listeners that the projects were successfully saved.
97 * The directory the projects were saved to
99 private void fireSavingProjectsDone(String directory) {
100 for (CoreListener coreListener: coreListeners) {
101 coreListener.savingProjectsDone(directory);
106 * Notifies all listeners that the projects could not be saved.
109 * The directory the projects were to be saved to
111 * The exception that occured when saving the projects
113 private void fireSavingProjectsFailed(String directory, Throwable throwable) {
114 for (CoreListener coreListener: coreListeners) {
115 coreListener.savingProjectsFailed(directory, throwable);
120 * Notifies all listeners that the nodes were successfully loaded.
123 * The directory the nodes were loaded from
125 private void fireLoadingNodesDone(String directory) {
126 for (CoreListener coreListener: coreListeners) {
127 coreListener.loadingNodesDone(directory);
132 * Notifies all listeners that loading the nodes has failed.
135 * The directory the nodes were loaded from
137 * The exception that occured while loading the nodes
139 private void fireLoadingNodesFailed(String directory, Throwable throwable) {
140 for (CoreListener coreListener: coreListeners) {
141 coreListener.loadingNodesFailed(directory, throwable);
146 * Notifies all listeners that the nodes were saved successfully.
149 * The directory the nodes were saved to
151 private void fireSavingNodesDone(String directory) {
152 for (CoreListener coreListener: coreListeners) {
153 coreListener.savingNodesDone(directory);
158 * Notifies all listeners that saving the nodes has failed.
161 * The directory the nodes were saved to
163 * The exception that occured while saving the nodes
165 private void fireSavingNodesFailed(String directory, Throwable throwable) {
166 for (CoreListener coreListener: coreListeners) {
167 coreListener.savingProjectsFailed(directory, throwable);
172 * Notifies all core listeners that the core has loaded and is ready to run.
174 private void fireCoreLoaded() {
175 for (CoreListener coreListener: coreListeners) {
176 coreListener.coreLoaded();
181 * Notifies all listeners that the core was stopped.
183 private void fireCoreStopped() {
184 for (CoreListener coreListener: coreListeners) {
185 coreListener.coreStopped();
190 * Notifies all listeners that a connection to the given node is now being
194 * The node that is being connected to
196 private void fireNodeConnecting(Node node) {
197 for (CoreListener coreListener: coreListeners) {
198 coreListener.nodeConnecting(node);
203 * Notifies all listeners that the given node is now connected.
206 * The node that is now connected
208 private void fireNodeConnected(Node node) {
209 for (CoreListener coreListener: coreListeners) {
210 coreListener.nodeConnected(node);
215 * Notifies all listeners that the given node was disconnected.
218 * The node that is now disconnected
220 * The exception that caused the disconnect, or <code>null</code>
221 * if there was no exception
223 private void fireNodeDisconnected(Node node, Throwable throwable) {
224 for (CoreListener coreListener: coreListeners) {
225 coreListener.nodeDisconnected(node, throwable);
230 * Notifies all listeners that a request was added to a node.
233 * The node the request was added to
235 * The request that was added
237 private void fireRequestAdded(Node node, Request request) {
238 for (CoreListener coreListener: coreListeners) {
239 coreListener.requestAdded(node, request);
248 * Returns the project manager.
250 * @return The project manager
252 public ProjectManager getProjectManager() {
253 return projectManager;
257 * Sets the project manager to use.
259 * @param projectManager
260 * The project manager to use
262 public void setProjectManager(ProjectManager projectManager) {
263 this.projectManager = projectManager;
267 * Returns the node manager.
269 * @return The node manager
271 public NodeManager getNodeManager() {
276 * Sets the node manager to use.
279 * The node manager to use
281 public void setNodeManager(NodeManager nodeManager) {
282 this.nodeManager = nodeManager;
286 * Sets the request manager to use.
288 * @param requestManager
289 * The request manager to use
291 public void setRequestManager(RequestManager requestManager) {
292 this.requestManager = requestManager;
298 public List<Node> getNodes() {
299 return nodeManager.getNodes();
305 public boolean isNodeConnected(Node node) {
306 return nodeManager.hasNode(node);
316 public void start() {
318 projectManager.load();
319 fireLoadingProjectsDone(projectManager.getDirectory());
320 } catch (IOException ioe1) {
321 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
325 fireLoadingNodesDone(nodeManager.getDirectory());
326 } catch (IOException ioe1) {
327 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
337 projectManager.save();
338 fireSavingProjectsDone(projectManager.getDirectory());
339 } catch (IOException ioe1) {
340 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
344 fireSavingNodesDone(nodeManager.getDirectory());
345 } catch (IOException ioe1) {
346 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
354 public void connectToNode(Node node) {
355 fireNodeConnecting(node);
356 nodeManager.addNode(node);
357 nodeManager.connect(node);
363 public void disconnectFromNode(Node node) {
364 nodeManager.disconnect(node);
372 * Loads the configuration.
374 @SuppressWarnings("unused")
375 private void loadConfig() {
380 * Saves the configuration.
382 @SuppressWarnings("unused")
383 private void saveConfig() {
388 // INTERFACE NodeListener
394 public void nodeConnected(Node node) {
395 fireNodeConnected(node);
401 public void nodeDisconnected(Node node, Throwable throwable) {
402 fireNodeDisconnected(node, throwable);
406 // INTERFACE RequestListener
412 public void requestAdded(Node node, Request request) {
413 fireRequestAdded(node, request);