2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.model.Project;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
|
|
|
public class ProjectManager implements Serializable {
|
|
|
|
private static final long serialVersionUID = -2967415873336723962L;
|
|
|
|
|
|
|
|
protected File _dir;
|
2010-01-26 06:17:14 +01:00
|
|
|
protected Map<Long, ProjectMetadata> _projectsMetadata;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
|
|
|
transient protected Map<Long, Project> _projects;
|
|
|
|
|
|
|
|
static public ProjectManager singleton;
|
|
|
|
|
|
|
|
static public void initialize(File dir) {
|
|
|
|
if (singleton == null) {
|
|
|
|
File file = new File(dir, "projects");
|
|
|
|
if (file.exists()) {
|
|
|
|
singleton = load(file);
|
|
|
|
}
|
|
|
|
if (singleton == null) {
|
|
|
|
singleton = new ProjectManager(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static protected ProjectManager load(File file) {
|
|
|
|
ProjectManager pm = null;
|
|
|
|
FileInputStream fis = null;
|
|
|
|
ObjectInputStream in = null;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(file);
|
|
|
|
in = new ObjectInputStream(fis);
|
|
|
|
|
|
|
|
pm = (ProjectManager) in.readObject();
|
|
|
|
} catch(IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch(ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fis != null) {
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ProjectManager(File dir) {
|
|
|
|
_dir = dir;
|
|
|
|
_dir.mkdirs();
|
2010-01-26 06:17:14 +01:00
|
|
|
|
|
|
|
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
internalInitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
|
|
|
in.defaultReadObject();
|
|
|
|
internalInitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void internalInitialize() {
|
|
|
|
_projects = new HashMap<Long, Project>();
|
|
|
|
}
|
|
|
|
|
2010-01-27 02:48:42 +01:00
|
|
|
public File getDataDir() {
|
|
|
|
return _dir;
|
|
|
|
}
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
public Project createProject(ProjectMetadata projectMetadata) {
|
|
|
|
Project project = new Project();
|
|
|
|
|
|
|
|
_projects.put(project.id, project);
|
|
|
|
_projectsMetadata.put(project.id, projectMetadata);
|
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
2010-01-26 06:17:14 +01:00
|
|
|
public ProjectMetadata getProjectMetadata(long id) {
|
|
|
|
return _projectsMetadata.get(id);
|
|
|
|
}
|
|
|
|
|
2010-02-03 07:23:40 +01:00
|
|
|
public Map<Long, ProjectMetadata> getAllProjectMetadata() {
|
|
|
|
return _projectsMetadata;
|
|
|
|
}
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
public Project getProject(long id) {
|
|
|
|
if (_projects.containsKey(id)) {
|
|
|
|
return _projects.get(id);
|
|
|
|
} else {
|
|
|
|
File file = new File(_dir, id + ".project");
|
|
|
|
|
|
|
|
Project project = null;
|
|
|
|
FileInputStream fis = null;
|
|
|
|
ObjectInputStream in = null;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(file);
|
|
|
|
in = new ObjectInputStream(fis);
|
|
|
|
|
|
|
|
project = (Project) in.readObject();
|
|
|
|
} catch(IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch(ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fis != null) {
|
|
|
|
try {
|
|
|
|
fis.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_projects.put(id, project);
|
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save() {
|
|
|
|
File file = new File(_dir, "projects");
|
|
|
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
ObjectOutputStream out = null;
|
|
|
|
try {
|
|
|
|
fos = new FileOutputStream(file);
|
|
|
|
out = new ObjectOutputStream(fos);
|
|
|
|
out.writeObject(this);
|
|
|
|
out.flush();
|
|
|
|
} catch(IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fos != null) {
|
|
|
|
try {
|
|
|
|
fos.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out != null) {
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveAllProjects() {
|
|
|
|
for (Project project : _projects.values()) {
|
|
|
|
saveProject(project);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void saveProject(Project project) {
|
|
|
|
File file = new File(_dir, project.id + ".project");
|
|
|
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
ObjectOutputStream out = null;
|
|
|
|
try {
|
|
|
|
fos = new FileOutputStream(file);
|
|
|
|
out = new ObjectOutputStream(fos);
|
|
|
|
out.writeObject(project);
|
|
|
|
out.flush();
|
|
|
|
} catch(IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fos != null) {
|
|
|
|
try {
|
|
|
|
fos.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out != null) {
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|