d6fc59148874bd1599e06a0f1edee71cb6e6a05f
[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.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Properties;
35 import java.util.Random;
36 import java.util.logging.Logger;
37
38 import net.pterodactylus.jsite.core.JSiteException;
39 import net.pterodactylus.jsite.core.NodeManager;
40 import net.pterodactylus.util.io.Closer;
41 import net.pterodactylus.util.logging.Logging;
42 import net.pterodactylus.util.number.Hex;
43
44 /**
45  * Manages projects, taking care of persistence, lifetime statistics, and other
46  * things.
47  *
48  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49  */
50 public class ProjectManager implements PropertyChangeListener {
51
52         /** Logger. */
53         private static final Logger logger = Logging.getLogger(ProjectManager.class.getName());
54
55         /** The RNG used to create project IDs. */
56         private static final Random random = new Random();
57
58         /** The directory the projects are stored in. */
59         private final String directory;
60
61         /** The node manager. */
62         private NodeManager nodeManager;
63
64         /** All projects. */
65         private final List<Project> projects = Collections.synchronizedList(new ArrayList<Project>());
66
67         /**
68          * Creates a new project manager that saves and restores its state to/from
69          * the given directory.
70          *
71          * @param directory
72          *            The directory to save and restore states to/from
73          */
74         public ProjectManager(String directory) {
75                 this.directory = directory;
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         /**
83          * Returns the directory the projects are loaded from and saved to.
84          *
85          * @return The directory for storing the projects
86          */
87         public String getDirectory() {
88                 return directory;
89         }
90
91         /**
92          * Returns a list of all projects.
93          *
94          * @return A list of all projects
95          */
96         public List<Project> getProjects() {
97                 return Collections.unmodifiableList(new ArrayList<Project>(projects));
98         }
99
100         /**
101          * Sets the node manager to use.
102          *
103          * @param nodeManager
104          *            The node manager to use
105          */
106         public void setNodeManager(NodeManager nodeManager) {
107                 this.nodeManager = nodeManager;
108         }
109
110         //
111         // ACTIONS
112         //
113
114         /**
115          * Loads projects and statistics.
116          *
117          * @throws IOException
118          *             if an I/O error occurs
119          */
120         public void load() throws IOException {
121                 File directoryFile = new File(directory);
122                 File projectFile = new File(directoryFile, "projects.properties");
123                 if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) {
124                         return;
125                 }
126                 Properties projectProperties = new Properties();
127                 InputStream projectInputStream = null;
128                 try {
129                         projectInputStream = new FileInputStream(projectFile);
130                         projectProperties.load(projectInputStream);
131                 } finally {
132                         Closer.close(projectInputStream);
133                 }
134                 int projectIndex = 0;
135                 while (projectProperties.containsKey("projects." + projectIndex + ".name")) {
136                         String projectPrefix = "projects." + projectIndex;
137                         String projectId = projectProperties.getProperty(projectPrefix + ".id");
138                         String projectName = projectProperties.getProperty(projectPrefix + ".name");
139                         String projectDescription = projectProperties.getProperty(projectPrefix + ".description");
140                         String projectPrivateKey = projectProperties.getProperty(projectPrefix + ".privateKey");
141                         String projectPublicKey = projectProperties.getProperty(projectPrefix + ".publicKey");
142                         String projectBasePath = projectProperties.getProperty(projectPrefix + ".basePath");
143                         Project project = new Project();
144                         project.setId(projectId);
145                         project.setName(projectName);
146                         project.setDescription(projectDescription);
147                         project.setPrivateKey(projectPrivateKey);
148                         project.setPublicKey(projectPublicKey);
149                         project.setBasePath(projectBasePath);
150                         int overrideIndex = 0;
151                         while (projectProperties.containsKey(projectPrefix + ".overrides." + overrideIndex)) {
152                                 String filePath = projectProperties.getProperty(projectPrefix + ".overrides." + overrideIndex + ".filePath");
153                                 Override override = Override.valueOf(projectProperties.getProperty(projectPrefix + ".overrides." + overrideIndex + ".override"));
154                                 project.addOverride(filePath, override);
155                                 overrideIndex++;
156                         }
157                         projects.add(project);
158                         logger.fine("loaded project “" + project.getName() + "”.");
159                         projectIndex++;
160                 }
161         }
162
163         /**
164          * Saves projects and statistics.
165          *
166          * @throws IOException
167          *             if an I/O error occurs
168          */
169         public void save() throws IOException {
170                 File directoryFile = new File(directory);
171                 if (!directoryFile.exists()) {
172                         if (!directoryFile.mkdirs()) {
173                                 throw new IOException("could not create directory: " + directory);
174                         }
175                 }
176                 Properties projectProperties = new Properties();
177                 int projectIndex = 0;
178                 for (Project project: projects) {
179                         String projectPrefix = "projects." + projectIndex;
180                         projectProperties.setProperty(projectPrefix + ".id", project.getId());
181                         projectProperties.setProperty(projectPrefix + ".name", project.getName());
182                         projectProperties.setProperty(projectPrefix + ".description", project.getDescription());
183                         projectProperties.setProperty(projectPrefix + ".privateKey", project.getPrivateKey());
184                         projectProperties.setProperty(projectPrefix + ".publicKey", project.getPublicKey());
185                         projectProperties.setProperty(projectPrefix + ".basePath", project.getBasePath());
186                         int overrideIndex = 0;
187                         for (Entry<String, Override> overrideEntry: project.getOverrides().entrySet()) {
188                                 projectProperties.setProperty(projectPrefix + ".overrides." + overrideIndex + ".filePath", overrideEntry.getKey());
189                                 projectProperties.setProperty(projectPrefix + ".overrides." + overrideIndex + ".override", overrideEntry.getValue().toString());
190                                 overrideIndex++;
191                         }
192                         projectIndex++;
193                 }
194                 File projectFile = new File(directoryFile, "projects.properties");
195                 OutputStream projectOutputStream = null;
196                 try {
197                         projectOutputStream = new FileOutputStream(projectFile);
198                         projectProperties.store(projectOutputStream, "jSite projects");
199                 } finally {
200                         Closer.close(projectOutputStream);
201                 }
202         }
203
204         /**
205          * Creates a new project. The returned {@link Project} will contain a newly
206          * generated key pair.
207          *
208          * @return A newly created project
209          * @throws IOException
210          *             if an I/O error occured communicating with the node
211          * @throws JSiteException
212          *             if there is a problem with the node
213          */
214         public Project createProject() throws IOException, JSiteException {
215                 Project project = new Project();
216                 String[] keyPair = nodeManager.generateKeyPair();
217                 project.setId(generateId());
218                 project.setName("");
219                 project.setDescription("");
220                 project.setPrivateKey(keyPair[0]);
221                 project.setPublicKey(keyPair[1]);
222                 project.setBasePath("");
223                 projects.add(project);
224                 project.addPropertyChangeListener(this);
225                 try {
226                         save();
227                 } catch (IOException ioe1) {
228                         /* ignore. */
229                 }
230                 return project;
231         }
232
233         /**
234          * Clones the given project and returns the clone. The clone will be
235          * identical in all user-exposed fields, except for the project’s
236          * {@link Project#getId ID}.
237          *
238          * @param project
239          *            The project to clone
240          * @return The cloned project
241          */
242         public Project cloneProject(Project project) {
243                 Project projectClone = new Project(project);
244                 projects.add(projectClone);
245                 projectClone.setId(generateId());
246                 projectClone.addPropertyChangeListener(this);
247                 try {
248                         save();
249                 } catch (IOException ioe1) {
250                         /* ignore. */
251                 }
252                 return projectClone;
253         }
254
255         /**
256          * Removes the given project.
257          *
258          * @param project
259          *            The project to remove
260          */
261         public void removeProject(Project project) {
262                 projects.remove(project);
263                 try {
264                         save();
265                 } catch (IOException ioe1) {
266                         /* ignore. */
267                 }
268         }
269
270         //
271         // PRIVATE METHODS
272         //
273
274         /**
275          * Generates a new random ID, consisting of 16 random bytes converted to a
276          * hexadecimal number.
277          *
278          * @return The new ID
279          */
280         private static String generateId() {
281                 byte[] idBytes = new byte[16];
282                 random.nextBytes(idBytes);
283                 return Hex.toHex(idBytes);
284         }
285
286         //
287         // INTERFACE PropertyChangeListener
288         //
289
290         /**
291          * {@inheritDoc}
292          */
293         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
294                 try {
295                         save();
296                 } catch (IOException ioe1) {
297                         /* ignore. */
298                 }
299         }
300
301 }