add project creation
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 May 2008 16:14:06 +0000 (16:14 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 16 May 2008 16:14:06 +0000 (16:14 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@901 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/core/ProjectManager.java

index 8034189..786098b 100644 (file)
@@ -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<String> projectNames = new ArrayList<String>();
+       /** The node manager. */
+       private NodeManager nodeManager;
 
-       /** Mapping from project name to project. */
-       private final Map<String, Project> projects = new HashMap<String, Project>();
-
-       /** Mapping from project to lifetime statistics. */
-       @SuppressWarnings("unused")
-       private final Map<Project, ProjectLifetime> projectLifetimes = new HashMap<Project, ProjectLifetime>();
+       /** All projects. */
+       private final List<Project> projects = Collections.synchronizedList(new ArrayList<Project>());
 
        /**
         * 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<Project> getProjects() {
-               List<Project> projects = new ArrayList<Project>();
-               for (String projectName: projectNames) {
-                       projects.add(this.projects.get(projectName));
-               }
-               return projects;
+               return Collections.unmodifiableList(new ArrayList<Project>(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;
+       }
 }