2 * jSite2 - ProjectManager.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.core;
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.HashMap;
30 import java.util.List;
32 import java.util.Properties;
34 import net.pterodactylus.util.io.Closer;
37 * Manages projects, taking care of persistence, lifetime statistics, and other
40 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
43 public class ProjectManager {
45 /** The directory the projects are stored in. */
46 private final String directory;
48 /** The list of project names. */
49 private final List<String> projectNames = new ArrayList<String>();
51 /** Mapping from project name to project. */
52 private final Map<String, Project> projects = new HashMap<String, Project>();
54 /** Mapping from project to lifetime statistics. */
55 @SuppressWarnings("unused")
56 private final Map<Project, ProjectLifetime> projectLifetimes = new HashMap<Project, ProjectLifetime>();
59 * Creates a new project manager that saves and restores its state to/from
60 * the given directory.
63 * The directory to save and restore states to/from
65 public ProjectManager(String directory) {
66 this.directory = directory;
74 * Returns the directory the projects are loaded from and saved to.
76 * @return The directory for storing the projects
78 public String getDirectory() {
83 * Returns a list of all projects, sorted by their name.
85 * @return A list of all projects
87 public List<Project> getProjects() {
88 List<Project> projects = new ArrayList<Project>();
89 for (String projectName: projectNames) {
90 projects.add(this.projects.get(projectName));
100 * Loads projects and statistics.
102 * @throws IOException
103 * if an I/O error occurs
105 public void load() throws IOException {
106 File directoryFile = new File(directory);
107 File projectFile = new File(directoryFile, "projects.properties");
108 if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) {
111 Properties projectProperties = new Properties();
112 InputStream projectInputStream = null;
114 projectInputStream = new FileInputStream(projectFile);
115 projectProperties.load(projectInputStream);
117 Closer.close(projectInputStream);
119 int projectIndex = 0;
120 while (projectProperties.containsKey("projects." + projectIndex + ".name")) {
121 String projectPrefix = "projects." + projectIndex;
122 String projectName = projectProperties.getProperty(projectPrefix + ".name");
123 Project project = new Project();
124 project.setName(projectName);
125 projectNames.add(projectName);
126 projects.put(projectName, project);
132 * Saves projects and statistics.
134 * @throws IOException
135 * if an I/O error occurs
137 public void save() throws IOException {
138 File directoryFile = new File(directory);
139 if (!directoryFile.exists()) {
140 if (!directoryFile.mkdirs()) {
141 throw new IOException("could not create directory: " + directory);
144 Properties projectProperties = new Properties();
145 int projectIndex = 0;
146 for (String projectName: projectNames) {
147 String projectPrefix = "projects." + projectIndex;
148 Project project = projects.get(projectName);
149 projectProperties.setProperty("projects." + projectPrefix + ".name", project.getName());
152 File projectFile = new File(directoryFile, "projects.properties");
153 OutputStream projectOutputStream = null;
155 projectOutputStream = new FileOutputStream(projectFile);
156 projectProperties.store(projectOutputStream, "jSite projects");
158 Closer.close(projectOutputStream);