import java.util.Collections;
import java.util.List;
import java.util.Properties;
-import java.util.Random;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.pterodactylus.jsite.core.JSiteException;
import net.pterodactylus.jsite.core.NodeManager;
+import net.pterodactylus.jsite.util.IdGenerator;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.number.Hex;
/**
* Manages projects, taking care of persistence, lifetime statistics, and other
* things.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class ProjectManager implements PropertyChangeListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(ProjectManager.class.getName());
- /** The RNG used to create project IDs. */
- private static final Random random = new Random();
-
/** The directory the projects are stored in. */
private final String directory;
/**
* Creates a new project manager that saves and restores its state to/from
* the given directory.
- *
+ *
* @param directory
* The directory to save and restore states to/from
*/
/**
* Returns the directory the projects are loaded from and saved to.
- *
+ *
* @return The directory for storing the projects
*/
public String getDirectory() {
/**
* Returns a list of all projects.
- *
+ *
* @return A list of all projects
*/
public List<Project> getProjects() {
/**
* Sets the node manager to use.
- *
+ *
* @param nodeManager
* The node manager to use
*/
/**
* Loads projects and statistics.
- *
+ *
* @throws IOException
* if an I/O error occurs
*/
/**
* Saves projects and statistics.
- *
+ *
* @throws IOException
* if an I/O error occurs
*/
/**
* 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
public Project createProject() throws IOException, JSiteException {
Project project = new Project();
String[] keyPair = nodeManager.generateKeyPair();
- project.setId(generateId());
+ project.setId(Hex.toHex(IdGenerator.generateId()));
project.setName("");
project.setDescription("");
project.setPrivateKey(keyPair[0]);
* Clones the given project and returns the clone. The clone will be
* identical in all user-exposed fields, except for the project’s
* {@link Project#getId ID}.
- *
+ *
* @param project
* The project to clone
* @return The cloned project
public Project cloneProject(Project project) {
Project projectClone = new Project(project);
projects.add(projectClone);
- projectClone.setId(generateId());
+ projectClone.setId(Hex.toHex(IdGenerator.generateId()));
projectClone.addPropertyChangeListener(this);
try {
save();
/**
* Removes the given project.
- *
+ *
* @param project
* The project to remove
*/
}
//
- // PRIVATE METHODS
- //
-
- /**
- * Generates a new random ID, consisting of 16 random bytes converted to a
- * hexadecimal number.
- *
- * @return The new ID
- */
- private static String generateId() {
- byte[] idBytes = new byte[16];
- random.nextBytes(idBytes);
- return Hex.toHex(idBytes);
- }
-
- //
// INTERFACE PropertyChangeListener
//