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 {
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();
189 * Returns the project manager.
191 * @return The project manager
193 public ProjectManager getProjectManager() {
194 return projectManager;
198 * Sets the project manager to use.
200 * @param projectManager
201 * The project manager to use
203 public void setProjectManager(ProjectManager projectManager) {
204 this.projectManager = projectManager;
208 * Returns the node manager.
210 * @return The node manager
212 public NodeManager getNodeManager() {
217 * Sets the node manager to use.
220 * The node manager to use
222 public void setNodeManager(NodeManager nodeManager) {
223 this.nodeManager = nodeManager;
229 public List<Node> getNodes() {
230 return nodeManager.getNodes();
236 public boolean isNodeConnected(Node node) {
237 return nodeManager.hasNode(node);
247 public void start() {
249 projectManager.load();
250 fireLoadingProjectsDone(projectManager.getDirectory());
251 } catch (IOException ioe1) {
252 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
256 fireLoadingNodesDone(nodeManager.getDirectory());
257 } catch (IOException ioe1) {
258 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
268 projectManager.save();
269 fireSavingProjectsDone(projectManager.getDirectory());
270 } catch (IOException ioe1) {
271 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
275 fireSavingNodesDone(nodeManager.getDirectory());
276 } catch (IOException ioe1) {
277 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
285 public void connectToNode(Node node) {
294 * Loads the configuration.
296 @SuppressWarnings("unused")
297 private void loadConfig() {
302 * Saves the configuration.
304 @SuppressWarnings("unused")
305 private void saveConfig() {