From: David ‘Bombe’ Roden Date: Fri, 16 May 2008 16:14:06 +0000 (+0000) Subject: add project creation X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8d276e34707fa8827ed31207d63c69632731c1cd;p=jSite2.git add project creation git-svn-id: http://trooper/svn/projects/jSite/trunk@901 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/jsite/core/ProjectManager.java b/src/net/pterodactylus/jsite/core/ProjectManager.java index 8034189..786098b 100644 --- a/src/net/pterodactylus/jsite/core/ProjectManager.java +++ b/src/net/pterodactylus/jsite/core/ProjectManager.java @@ -26,9 +26,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Properties; import net.pterodactylus.util.io.Closer; @@ -45,15 +44,11 @@ public class ProjectManager { /** The directory the projects are stored in. */ private final String directory; - /** The list of project names. */ - private final List projectNames = new ArrayList(); + /** The node manager. */ + private NodeManager nodeManager; - /** Mapping from project name to project. */ - private final Map projects = new HashMap(); - - /** Mapping from project to lifetime statistics. */ - @SuppressWarnings("unused") - private final Map projectLifetimes = new HashMap(); + /** All projects. */ + private final List projects = Collections.synchronizedList(new ArrayList()); /** * Creates a new project manager that saves and restores its state to/from @@ -80,16 +75,22 @@ public class ProjectManager { } /** - * Returns a list of all projects, sorted by their name. + * Returns a list of all projects. * * @return A list of all projects */ public List getProjects() { - List projects = new ArrayList(); - for (String projectName: projectNames) { - projects.add(this.projects.get(projectName)); - } - return projects; + return Collections.unmodifiableList(new ArrayList(projects)); + } + + /** + * Sets the node manager to use. + * + * @param nodeManager + * The node manager to use + */ + public void setNodeManager(NodeManager nodeManager) { + this.nodeManager = nodeManager; } // @@ -120,10 +121,14 @@ public class ProjectManager { while (projectProperties.containsKey("projects." + projectIndex + ".name")) { String projectPrefix = "projects." + projectIndex; String projectName = projectProperties.getProperty(projectPrefix + ".name"); + String projectDescription = projectProperties.getProperty(projectPrefix + ".description"); + String projectPrivateKey = projectProperties.getProperty(projectPrefix + ".privateKey"); + String projectPublicKey = projectProperties.getProperty(projectPrefix + ".publicKey"); Project project = new Project(); project.setName(projectName); - projectNames.add(projectName); - projects.put(projectName, project); + project.setDescription(projectDescription); + project.setPrivateKey(projectPrivateKey); + project.setPublicKey(projectPublicKey); projectIndex++; } } @@ -143,10 +148,12 @@ public class ProjectManager { } Properties projectProperties = new Properties(); int projectIndex = 0; - for (String projectName: projectNames) { + for (Project project: projects) { String projectPrefix = "projects." + projectIndex; - Project project = projects.get(projectName); projectProperties.setProperty("projects." + projectPrefix + ".name", project.getName()); + projectProperties.setProperty("projects." + projectPrefix + ".description", project.getDescription()); + projectProperties.setProperty("projects." + projectPrefix + ".privateKey", project.getPrivateKey()); + projectProperties.setProperty("projects." + projectPrefix + ".publicKey", project.getPublicKey()); projectIndex++; } File projectFile = new File(directoryFile, "projects.properties"); @@ -159,4 +166,27 @@ public class ProjectManager { } } + /** + * Creates a new project. The returned {@link Project} will contain a newly + * generated key pair. + * + * @return A newly created project + * @throws IOException + * if an I/O error occured communicating with the node + * @throws NoNodeException + * if no node is configured + */ + public Project createProject() throws IOException, NoNodeException { + Project project = new Project(); + String[] keyPair = nodeManager.generateKeyPair(); + project.setPrivateKey(keyPair[0]); + project.setPublicKey(keyPair[1]); + projects.add(project); + try { + save(); + } catch (IOException ioe1) { + /* ignore, save() will be called again on quit. */ + } + return project; + } }