2 * jSite2 - ProjectManager.java -
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;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.logging.Logger;
34 import net.pterodactylus.util.io.Closer;
35 import net.pterodactylus.util.logging.Logging;
38 * Manages projects, taking care of persistence, lifetime statistics, and other
41 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44 public class ProjectManager {
47 private static final Logger logger = Logging.getLogger(ProjectManager.class.getName());
49 /** The directory the projects are stored in. */
50 private final String directory;
52 /** The node manager. */
53 private NodeManager nodeManager;
56 private final List<Project> projects = Collections.synchronizedList(new ArrayList<Project>());
59 * Creates a new project manager that saves and restores its state to/from
60 * the given directory.
63 * The directory to save and restore states to/from
65 public ProjectManager(String directory) {
66 this.directory = directory;
74 * Returns the directory the projects are loaded from and saved to.
76 * @return The directory for storing the projects
78 public String getDirectory() {
83 * Returns a list of all projects.
85 * @return A list of all projects
87 public List<Project> getProjects() {
88 return Collections.unmodifiableList(new ArrayList<Project>(projects));
92 * Sets the node manager to use.
95 * The node manager to use
97 public void setNodeManager(NodeManager nodeManager) {
98 this.nodeManager = nodeManager;
106 * Loads projects and statistics.
108 * @throws IOException
109 * if an I/O error occurs
111 public void load() throws IOException {
112 File directoryFile = new File(directory);
113 File projectFile = new File(directoryFile, "projects.properties");
114 if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) {
117 Properties projectProperties = new Properties();
118 InputStream projectInputStream = null;
120 projectInputStream = new FileInputStream(projectFile);
121 projectProperties.load(projectInputStream);
123 Closer.close(projectInputStream);
125 int projectIndex = 0;
126 while (projectProperties.containsKey("projects." + projectIndex + ".name")) {
127 String projectPrefix = "projects." + projectIndex;
128 String projectName = projectProperties.getProperty(projectPrefix + ".name");
129 String projectDescription = projectProperties.getProperty(projectPrefix + ".description");
130 String projectPrivateKey = projectProperties.getProperty(projectPrefix + ".privateKey");
131 String projectPublicKey = projectProperties.getProperty(projectPrefix + ".publicKey");
132 Project project = new Project();
133 project.setName(projectName);
134 project.setDescription(projectDescription);
135 project.setPrivateKey(projectPrivateKey);
136 project.setPublicKey(projectPublicKey);
137 projects.add(project);
138 logger.fine("loaded project “" + project.getName() + "”.");
144 * Saves projects and statistics.
146 * @throws IOException
147 * if an I/O error occurs
149 public void save() throws IOException {
150 File directoryFile = new File(directory);
151 if (!directoryFile.exists()) {
152 if (!directoryFile.mkdirs()) {
153 throw new IOException("could not create directory: " + directory);
156 Properties projectProperties = new Properties();
157 int projectIndex = 0;
158 for (Project project: projects) {
159 String projectPrefix = "projects." + projectIndex;
160 projectProperties.setProperty(projectPrefix + ".name", project.getName());
161 projectProperties.setProperty(projectPrefix + ".description", project.getDescription());
162 projectProperties.setProperty(projectPrefix + ".privateKey", project.getPrivateKey());
163 projectProperties.setProperty(projectPrefix + ".publicKey", project.getPublicKey());
166 File projectFile = new File(directoryFile, "projects.properties");
167 OutputStream projectOutputStream = null;
169 projectOutputStream = new FileOutputStream(projectFile);
170 projectProperties.store(projectOutputStream, "jSite projects");
172 Closer.close(projectOutputStream);
177 * Creates a new project. The returned {@link Project} will contain a newly
178 * generated key pair.
180 * @return A newly created project
181 * @throws IOException
182 * if an I/O error occured communicating with the node
183 * @throws NoNodeException
184 * if no node is configured
186 public Project createProject() throws IOException, NoNodeException {
187 Project project = new Project();
188 String[] keyPair = nodeManager.generateKeyPair();
189 project.setPrivateKey(keyPair[0]);
190 project.setPublicKey(keyPair[1]);
191 projects.add(project);