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;
/** 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
}
/**
- * 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;
}
//
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++;
}
}
}
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");
}
}
+ /**
+ * 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;
+ }
}