From e667a83b6e13391ebb699d6b23f5a8eb918b58c6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 3 Jun 2008 08:27:31 +0200 Subject: [PATCH] use id generator --- .../jsite/core/project/ProjectManager.java | 45 +++++++--------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/net/pterodactylus/jsite/core/project/ProjectManager.java b/src/net/pterodactylus/jsite/core/project/ProjectManager.java index 62e8f8e..6e9dd36 100644 --- a/src/net/pterodactylus/jsite/core/project/ProjectManager.java +++ b/src/net/pterodactylus/jsite/core/project/ProjectManager.java @@ -31,13 +31,13 @@ import java.util.ArrayList; 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; @@ -45,7 +45,7 @@ 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 { @@ -53,9 +53,6 @@ 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; @@ -68,7 +65,7 @@ public class ProjectManager implements PropertyChangeListener { /** * 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 */ @@ -82,7 +79,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Returns the directory the projects are loaded from and saved to. - * + * * @return The directory for storing the projects */ public String getDirectory() { @@ -91,7 +88,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Returns a list of all projects. - * + * * @return A list of all projects */ public List getProjects() { @@ -100,7 +97,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Sets the node manager to use. - * + * * @param nodeManager * The node manager to use */ @@ -114,7 +111,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Loads projects and statistics. - * + * * @throws IOException * if an I/O error occurs */ @@ -166,7 +163,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Saves projects and statistics. - * + * * @throws IOException * if an I/O error occurs */ @@ -209,7 +206,7 @@ public class ProjectManager implements PropertyChangeListener { /** * 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 @@ -219,7 +216,7 @@ public class ProjectManager implements PropertyChangeListener { 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]); @@ -240,7 +237,7 @@ public class ProjectManager implements PropertyChangeListener { * 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 @@ -248,7 +245,7 @@ public class ProjectManager implements PropertyChangeListener { 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(); @@ -260,7 +257,7 @@ public class ProjectManager implements PropertyChangeListener { /** * Removes the given project. - * + * * @param project * The project to remove */ @@ -274,22 +271,6 @@ public class ProjectManager implements PropertyChangeListener { } // - // 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 // -- 2.7.4