add id to projects
[jSite2.git] / src / net / pterodactylus / jsite / project / ProjectManager.java
1 /*
2  * jSite2 - ProjectManager.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.project;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.Random;
33 import java.util.logging.Logger;
34
35 import net.pterodactylus.jsite.core.JSiteException;
36 import net.pterodactylus.jsite.core.NodeManager;
37 import net.pterodactylus.util.io.Closer;
38 import net.pterodactylus.util.logging.Logging;
39 import net.pterodactylus.util.number.Hex;
40
41 /**
42  * Manages projects, taking care of persistence, lifetime statistics, and other
43  * things.
44  * 
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  * @version $Id$
47  */
48 public class ProjectManager {
49
50         /** Logger. */
51         private static final Logger logger = Logging.getLogger(ProjectManager.class.getName());
52
53         /** The RNG used to create project IDs. */
54         private static final Random random = new Random();
55
56         /** The directory the projects are stored in. */
57         private final String directory;
58
59         /** The node manager. */
60         private NodeManager nodeManager;
61
62         /** All projects. */
63         private final List<Project> projects = Collections.synchronizedList(new ArrayList<Project>());
64
65         /**
66          * Creates a new project manager that saves and restores its state to/from
67          * the given directory.
68          * 
69          * @param directory
70          *            The directory to save and restore states to/from
71          */
72         public ProjectManager(String directory) {
73                 this.directory = directory;
74         }
75
76         //
77         // ACCESSORS
78         //
79
80         /**
81          * Returns the directory the projects are loaded from and saved to.
82          * 
83          * @return The directory for storing the projects
84          */
85         public String getDirectory() {
86                 return directory;
87         }
88
89         /**
90          * Returns a list of all projects.
91          * 
92          * @return A list of all projects
93          */
94         public List<Project> getProjects() {
95                 return Collections.unmodifiableList(new ArrayList<Project>(projects));
96         }
97
98         /**
99          * Sets the node manager to use.
100          * 
101          * @param nodeManager
102          *            The node manager to use
103          */
104         public void setNodeManager(NodeManager nodeManager) {
105                 this.nodeManager = nodeManager;
106         }
107
108         //
109         // ACTIONS
110         //
111
112         /**
113          * Loads projects and statistics.
114          * 
115          * @throws IOException
116          *             if an I/O error occurs
117          */
118         public void load() throws IOException {
119                 File directoryFile = new File(directory);
120                 File projectFile = new File(directoryFile, "projects.properties");
121                 if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) {
122                         return;
123                 }
124                 Properties projectProperties = new Properties();
125                 InputStream projectInputStream = null;
126                 try {
127                         projectInputStream = new FileInputStream(projectFile);
128                         projectProperties.load(projectInputStream);
129                 } finally {
130                         Closer.close(projectInputStream);
131                 }
132                 int projectIndex = 0;
133                 while (projectProperties.containsKey("projects." + projectIndex + ".name")) {
134                         String projectPrefix = "projects." + projectIndex;
135                         String projectId = projectProperties.getProperty(projectPrefix + ".id");
136                         String projectName = projectProperties.getProperty(projectPrefix + ".name");
137                         String projectDescription = projectProperties.getProperty(projectPrefix + ".description");
138                         String projectPrivateKey = projectProperties.getProperty(projectPrefix + ".privateKey");
139                         String projectPublicKey = projectProperties.getProperty(projectPrefix + ".publicKey");
140                         Project project = new Project();
141                         project.setId(projectId);
142                         project.setName(projectName);
143                         project.setDescription(projectDescription);
144                         project.setPrivateKey(projectPrivateKey);
145                         project.setPublicKey(projectPublicKey);
146                         projects.add(project);
147                         logger.fine("loaded project “" + project.getName() + "”.");
148                         projectIndex++;
149                 }
150         }
151
152         /**
153          * Saves projects and statistics.
154          * 
155          * @throws IOException
156          *             if an I/O error occurs
157          */
158         public void save() throws IOException {
159                 File directoryFile = new File(directory);
160                 if (!directoryFile.exists()) {
161                         if (!directoryFile.mkdirs()) {
162                                 throw new IOException("could not create directory: " + directory);
163                         }
164                 }
165                 Properties projectProperties = new Properties();
166                 int projectIndex = 0;
167                 for (Project project: projects) {
168                         String projectPrefix = "projects." + projectIndex;
169                         projectProperties.setProperty(projectPrefix + ".id", project.getId());
170                         projectProperties.setProperty(projectPrefix + ".name", project.getName());
171                         projectProperties.setProperty(projectPrefix + ".description", project.getDescription());
172                         projectProperties.setProperty(projectPrefix + ".privateKey", project.getPrivateKey());
173                         projectProperties.setProperty(projectPrefix + ".publicKey", project.getPublicKey());
174                         projectIndex++;
175                 }
176                 File projectFile = new File(directoryFile, "projects.properties");
177                 OutputStream projectOutputStream = null;
178                 try {
179                         projectOutputStream = new FileOutputStream(projectFile);
180                         projectProperties.store(projectOutputStream, "jSite projects");
181                 } finally {
182                         Closer.close(projectOutputStream);
183                 }
184         }
185
186         /**
187          * Creates a new project. The returned {@link Project} will contain a newly
188          * generated key pair.
189          * 
190          * @return A newly created project
191          * @throws IOException
192          *             if an I/O error occured communicating with the node
193          * @throws JSiteException
194          *             if there is a problem with the node
195          */
196         public Project createProject() throws IOException, JSiteException {
197                 Project project = new Project();
198                 String[] keyPair = nodeManager.generateKeyPair();
199                 byte[] idBytes = new byte[16];
200                 random.nextBytes(idBytes);
201                 project.setId(Hex.toHex(idBytes));
202                 project.setPrivateKey(keyPair[0]);
203                 project.setPublicKey(keyPair[1]);
204                 projects.add(project);
205                 return project;
206         }
207 }