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>
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
48 * Adds the given listener to the list of registered listeners.
53 public void addCoreListener(CoreListener coreListener) {
54 coreListeners.add(coreListener);
58 * Removes the given listener from the list of registered listeners.
61 * The listener to remove
63 public void removeCoreListener(CoreListener coreListener) {
64 coreListeners.remove(coreListener);
68 * Notifies all listeners that the projects were loaded successfully.
71 * The directory the projects were loaded from
73 private void fireLoadingProjectsDone(String directory) {
74 for (CoreListener coreListener: coreListeners) {
75 coreListener.loadingProjectsDone(directory);
80 * Notifies all core listeners that loading the projects from the given
81 * directory has failed.
84 * The directory the projects were tried to load from
86 * The exception that occured when loading projects
88 private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
89 for (CoreListener coreListener: coreListeners) {
90 coreListener.loadingProjectsFailed(directory, throwable);
95 * Notifies all listeners that the projects were successfully saved.
98 * The directory the projects were saved to
100 private void fireSavingProjectsDone(String directory) {
101 for (CoreListener coreListener: coreListeners) {
102 coreListener.savingProjectsDone(directory);
107 * Notifies all listeners that the projects could not be saved.
110 * The directory the projects were to be saved to
112 * The exception that occured when saving the projects
114 private void fireSavingProjectsFailed(String directory, Throwable throwable) {
115 for (CoreListener coreListener: coreListeners) {
116 coreListener.savingProjectsFailed(directory, throwable);
121 * Notifies all listeners that the nodes were successfully loaded.
124 * The directory the nodes were loaded from
126 private void fireLoadingNodesDone(String directory) {
127 for (CoreListener coreListener: coreListeners) {
128 coreListener.loadingNodesDone(directory);
133 * Notifies all listeners that loading the nodes has failed.
136 * The directory the nodes were loaded from
138 * The exception that occured while loading the nodes
140 private void fireLoadingNodesFailed(String directory, Throwable throwable) {
141 for (CoreListener coreListener: coreListeners) {
142 coreListener.loadingNodesFailed(directory, throwable);
147 * Notifies all listeners that the nodes were saved successfully.
150 * The directory the nodes were saved to
152 private void fireSavingNodesDone(String directory) {
153 for (CoreListener coreListener: coreListeners) {
154 coreListener.savingNodesDone(directory);
159 * Notifies all listeners that saving the nodes has failed.
162 * The directory the nodes were saved to
164 * The exception that occured while saving the nodes
166 private void fireSavingNodesFailed(String directory, Throwable throwable) {
167 for (CoreListener coreListener: coreListeners) {
168 coreListener.savingProjectsFailed(directory, throwable);
173 * Notifies all core listeners that the core has loaded and is ready to run.
175 private void fireCoreLoaded() {
176 for (CoreListener coreListener: coreListeners) {
177 coreListener.coreLoaded();
182 * Notifies all listeners that the core was stopped.
184 private void fireCoreStopped() {
185 for (CoreListener coreListener: coreListeners) {
186 coreListener.coreStopped();
195 * Returns the project manager.
197 * @return The project manager
199 public ProjectManager getProjectManager() {
200 return projectManager;
204 * Sets the project manager to use.
206 * @param projectManager
207 * The project manager to use
209 public void setProjectManager(ProjectManager projectManager) {
210 this.projectManager = projectManager;
214 * Returns the node manager.
216 * @return The node manager
218 public NodeManager getNodeManager() {
223 * Sets the node manager to use.
226 * The node manager to use
228 public void setNodeManager(NodeManager nodeManager) {
229 this.nodeManager = nodeManager;
233 * Returns the list of all configured nodes.
235 * @return All configured nodes
237 public List<Node> getNodes() {
238 return nodeManager.getNodes();
242 * Returns whether the core is currently connected to the given node.
246 * @return <code>true</code> if the core is currently connected to the
247 * node, <code>false</code> otherwise
249 public boolean isNodeConnected(Node node) {
250 return nodeManager.hasNode(node);
260 public void start() {
262 projectManager.load();
263 fireLoadingProjectsDone(projectManager.getDirectory());
264 } catch (IOException ioe1) {
265 fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
269 fireLoadingNodesDone(nodeManager.getDirectory());
270 } catch (IOException ioe1) {
271 fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
281 projectManager.save();
282 fireSavingProjectsDone(projectManager.getDirectory());
283 } catch (IOException ioe1) {
284 fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
288 fireSavingNodesDone(nodeManager.getDirectory());
289 } catch (IOException ioe1) {
290 fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
296 * Connects to the given node.
299 * The node to connect to
301 public void connectToNode(Node node) {
310 * Loads the configuration.
312 @SuppressWarnings("unused")
313 private void loadConfig() {
318 * Saves the configuration.
320 @SuppressWarnings("unused")
321 private void saveConfig() {