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 {
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;
44 // LISTENER MANAGEMENT
50 public void addCoreListener(CoreListener coreListener) {
51 coreListeners.add(coreListener);
57 public void removeCoreListener(CoreListener coreListener) {
58 coreListeners.remove(coreListener);
62 * Notifies all listeners that the projects were loaded successfully.
65 * The directory the projects were loaded from
67 private void fireLoadingProjectsDone(String directory) {
68 for (CoreListener coreListener: coreListeners) {
69 coreListener.loadingProjectsDone(directory);
74 * Notifies all core listeners that loading the projects from the given
75 * directory has failed.
78 * The directory the projects were tried to load from
80 * The exception that occured when loading projects
82 private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
83 for (CoreListener coreListener: coreListeners) {
84 coreListener.loadingProjectsFailed(directory, throwable);
89 * Notifies all listeners that the projects were successfully saved.
92 * The directory the projects were saved to
94 private void fireSavingProjectsDone(String directory) {
95 for (CoreListener coreListener: coreListeners) {
96 coreListener.savingProjectsDone(directory);
101 * Notifies all listeners that the projects could not be saved.
104 * The directory the projects were to be saved to
106 * The exception that occured when saving the projects
108 private void fireSavingProjectsFailed(String directory, Throwable throwable) {
109 for (CoreListener coreListener: coreListeners) {
110 coreListener.savingProjectsFailed(directory, throwable);
115 * Notifies all listeners that the nodes were successfully loaded.
118 * The directory the nodes were loaded from
120 private void fireLoadingNodesDone(String directory) {
121 for (CoreListener coreListener: coreListeners) {
122 coreListener.loadingNodesDone(directory);
127 * Notifies all listeners that loading the nodes has failed.
130 * The directory the nodes were loaded from
132 * The exception that occured while loading the nodes
134 private void fireLoadingNodesFailed(String directory, Throwable throwable) {
135 for (CoreListener coreListener: coreListeners) {
136 coreListener.loadingNodesFailed(directory, throwable);
141 * Notifies all listeners that the nodes were saved successfully.
144 * The directory the nodes were saved to
146 private void fireSavingNodesDone(String directory) {
147 for (CoreListener coreListener: coreListeners) {
148 coreListener.savingNodesDone(directory);
153 * Notifies all listeners that saving the nodes has failed.
156 * The directory the nodes were saved to
158 * The exception that occured while saving the nodes
160 private void fireSavingNodesFailed(String directory, Throwable throwable) {
161 for (CoreListener coreListener: coreListeners) {
162 coreListener.savingProjectsFailed(directory, throwable);
167 * Notifies all core listeners that the core has loaded and is ready to run.
169 private void fireCoreLoaded() {
170 for (CoreListener coreListener: coreListeners) {
171 coreListener.coreLoaded();
176 * Notifies all listeners that the core was stopped.
178 private void fireCoreStopped() {
179 for (CoreListener coreListener: coreListeners) {
180 coreListener.coreStopped();
185 * Notifies all listeners that a connection to the given node is now being
189 * The node that is being connected to
191 private void fireNodeConnecting(Node node) {
192 for (CoreListener coreListener: coreListeners) {
193 coreListener.nodeConnecting(node);
198 * Notifies all listeners that the given node is now connected.
201 * The node that is now connected
203 private void fireNodeConnected(Node node) {
204 for (CoreListener coreListener: coreListeners) {
205 coreListener.nodeConnected(node);
210 * Notifies all listeners that the given node was disconnected.
213 * The node that is now disconnected
215 * The exception that caused the disconnect, or <code>null</code>
216 * if there was no exception
218 private void fireNodeDisconnected(Node node, Throwable throwable) {
219 for (CoreListener coreListener: coreListeners) {
220 coreListener.nodeDisconnected(node, throwable);
229 * Returns the project manager.
231 * @return The project manager
233 public ProjectManager getProjectManager() {
234 return projectManager;
238 * Sets the project manager to use.
240 * @param projectManager
241 * The project manager to use
243 public void setProjectManager(ProjectManager projectManager) {
244 this.projectManager = projectManager;
248 * Returns the node manager.
250 * @return The node manager
252 public NodeManager getNodeManager() {
257 * Sets the node manager to use.
260 * The node manager to use
262 public void setNodeManager(NodeManager nodeManager) {
263 this.nodeManager = nodeManager;
269 public List<Node> getNodes() {
270 return nodeManager.getNodes();
276 public boolean isNodeConnected(Node node) {
277 return nodeManager.hasNode(node);
287 public void start() {
289 projectManager.load();
290 fireLoadingProjectsDone(projectManager.getDirectory());
291 } catch (IOException ioe1) {
292 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
296 fireLoadingNodesDone(nodeManager.getDirectory());
297 } catch (IOException ioe1) {
298 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
308 projectManager.save();
309 fireSavingProjectsDone(projectManager.getDirectory());
310 } catch (IOException ioe1) {
311 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
315 fireSavingNodesDone(nodeManager.getDirectory());
316 } catch (IOException ioe1) {
317 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
325 public void connectToNode(Node node) {
326 fireNodeConnecting(node);
327 nodeManager.addNode(node);
328 nodeManager.connect(node);
334 public void disconnectFromNode(Node node) {
335 nodeManager.disconnect(node);
343 * Loads the configuration.
345 @SuppressWarnings("unused")
346 private void loadConfig() {
351 * Saves the configuration.
353 @SuppressWarnings("unused")
354 private void saveConfig() {
359 // INTERFACE NodeListener
365 public void nodeConnected(Node node) {
366 fireNodeConnected(node);
372 public void nodeDisconnected(Node node, Throwable throwable) {
373 fireNodeDisconnected(node, throwable);