portion of ProjectManager which interacts with File System has been moved to FileProjectManager, which extends ProjectManager. ProjectManager is now abstract.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@970 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
ca5d5ea828
commit
dc7060d390
354
main/src/com/metaweb/gridworks/FileProjectManager.java
Normal file
354
main/src/com/metaweb/gridworks/FileProjectManager.java
Normal file
@ -0,0 +1,354 @@
|
|||||||
|
package com.metaweb.gridworks;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONTokener;
|
||||||
|
import org.json.JSONWriter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
import com.metaweb.gridworks.util.JSONUtilities;
|
||||||
|
|
||||||
|
public class FileProjectManager extends ProjectManager{
|
||||||
|
|
||||||
|
protected File _workspaceDir;
|
||||||
|
|
||||||
|
final static Logger logger = LoggerFactory.getLogger("file_project_manager");
|
||||||
|
|
||||||
|
static public synchronized void initialize(File dir) {
|
||||||
|
if (singleton == null) {
|
||||||
|
logger.info("Using workspace directory: {}", dir.getAbsolutePath());
|
||||||
|
singleton = new FileProjectManager(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileProjectManager(File dir) {
|
||||||
|
_workspaceDir = dir;
|
||||||
|
_workspaceDir.mkdirs();
|
||||||
|
|
||||||
|
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
|
||||||
|
_expressions = new LinkedList<String>();
|
||||||
|
_projects = new HashMap<Long, Project>();
|
||||||
|
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getWorkspaceDir() {
|
||||||
|
return _workspaceDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public File getProjectDir(File workspaceDir, long projectID) {
|
||||||
|
File dir = new File(workspaceDir, projectID + ".project");
|
||||||
|
if (!dir.exists()) {
|
||||||
|
dir.mkdir();
|
||||||
|
}
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getProjectDir(long projectID) {
|
||||||
|
return getProjectDir(_workspaceDir, projectID);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import an external project that has been received as a .tar file, expanded, and
|
||||||
|
* copied into our workspace directory.
|
||||||
|
*
|
||||||
|
* @param projectID
|
||||||
|
*/
|
||||||
|
public boolean importProject(long projectID) {
|
||||||
|
synchronized (this) {
|
||||||
|
ProjectMetadata metadata = ProjectMetadata.load(getProjectDir(projectID));
|
||||||
|
if (metadata != null) {
|
||||||
|
_projectsMetadata.put(projectID, metadata);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure that a project's metadata and data are saved to file. For example,
|
||||||
|
* this method is called before the project is exported to a .tar file.
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
public void ensureProjectSaved(long id) {
|
||||||
|
synchronized (this) {
|
||||||
|
File projectDir = getProjectDir(id);
|
||||||
|
|
||||||
|
ProjectMetadata metadata = _projectsMetadata.get(id);
|
||||||
|
if (metadata != null) {
|
||||||
|
try {
|
||||||
|
metadata.save(projectDir);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Project project = _projects.get(id);
|
||||||
|
if (project != null && metadata.getModified().after(project.lastSave)) {
|
||||||
|
try {
|
||||||
|
project.save();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project getProject(long id) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (_projects.containsKey(id)) {
|
||||||
|
return _projects.get(id);
|
||||||
|
} else {
|
||||||
|
Project project = Project.load(getProjectDir(id), id);
|
||||||
|
|
||||||
|
_projects.put(id, project);
|
||||||
|
|
||||||
|
return project;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(boolean allModified) {
|
||||||
|
if (allModified || _busy == 0) {
|
||||||
|
saveProjects(allModified);
|
||||||
|
saveWorkspace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save the workspace's data out to file in a safe way: save to a temporary file first
|
||||||
|
* and rename it to the real file.
|
||||||
|
*/
|
||||||
|
protected void saveWorkspace() {
|
||||||
|
synchronized (this) {
|
||||||
|
File tempFile = new File(_workspaceDir, "workspace.temp.json");
|
||||||
|
try {
|
||||||
|
saveToFile(tempFile);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
logger.warn("Failed to save workspace");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = new File(_workspaceDir, "workspace.json");
|
||||||
|
File oldFile = new File(_workspaceDir, "workspace.old.json");
|
||||||
|
|
||||||
|
if (file.exists()) {
|
||||||
|
file.renameTo(oldFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFile.renameTo(file);
|
||||||
|
if (oldFile.exists()) {
|
||||||
|
oldFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Saved workspace");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void saveToFile(File file) throws IOException, JSONException {
|
||||||
|
FileWriter writer = new FileWriter(file);
|
||||||
|
try {
|
||||||
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
||||||
|
jsonWriter.object();
|
||||||
|
jsonWriter.key("projectIDs");
|
||||||
|
jsonWriter.array();
|
||||||
|
for (Long id : _projectsMetadata.keySet()) {
|
||||||
|
ProjectMetadata metadata = _projectsMetadata.get(id);
|
||||||
|
if (metadata != null) {
|
||||||
|
jsonWriter.value(id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
metadata.save(getProjectDir(id));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonWriter.endArray();
|
||||||
|
writer.write('\n');
|
||||||
|
|
||||||
|
jsonWriter.key("expressions"); JSONUtilities.writeStringList(jsonWriter, _expressions);
|
||||||
|
jsonWriter.endObject();
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility class to prioritize projects for saving, depending on how long ago
|
||||||
|
* they have been changed but have not been saved.
|
||||||
|
*/
|
||||||
|
static protected class SaveRecord {
|
||||||
|
final Project project;
|
||||||
|
final long overdue;
|
||||||
|
|
||||||
|
SaveRecord(Project project, long overdue) {
|
||||||
|
this.project = project;
|
||||||
|
this.overdue = overdue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static protected final int s_projectFlushDelay = 1000 * 60 * 60; // 1 hour
|
||||||
|
static protected final int s_quickSaveTimeout = 1000 * 30; // 30 secs
|
||||||
|
|
||||||
|
protected void saveProjects(boolean allModified) {
|
||||||
|
List<SaveRecord> records = new ArrayList<SaveRecord>();
|
||||||
|
Date now = new Date();
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
for (long id : _projectsMetadata.keySet()) {
|
||||||
|
ProjectMetadata metadata = _projectsMetadata.get(id);
|
||||||
|
Project project = _projects.get(id);
|
||||||
|
|
||||||
|
if (project != null) {
|
||||||
|
boolean hasUnsavedChanges =
|
||||||
|
metadata.getModified().getTime() > project.lastSave.getTime();
|
||||||
|
|
||||||
|
if (hasUnsavedChanges) {
|
||||||
|
long msecsOverdue = now.getTime() - project.lastSave.getTime();
|
||||||
|
|
||||||
|
records.add(new SaveRecord(project, msecsOverdue));
|
||||||
|
|
||||||
|
} else if (now.getTime() - project.lastSave.getTime() > s_projectFlushDelay) {
|
||||||
|
/*
|
||||||
|
* It's been a while since the project was last saved and it hasn't been
|
||||||
|
* modified. We can safely remove it from the cache to save some memory.
|
||||||
|
*/
|
||||||
|
_projects.remove(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (records.size() > 0) {
|
||||||
|
Collections.sort(records, new Comparator<SaveRecord>() {
|
||||||
|
public int compare(SaveRecord o1, SaveRecord o2) {
|
||||||
|
if (o1.overdue < o2.overdue) {
|
||||||
|
return 1;
|
||||||
|
} else if (o1.overdue > o2.overdue) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info(allModified ?
|
||||||
|
"Saving all modified projects ..." :
|
||||||
|
"Saving some modified projects ..."
|
||||||
|
);
|
||||||
|
|
||||||
|
for (int i = 0;
|
||||||
|
i < records.size() &&
|
||||||
|
(allModified || (new Date().getTime() - now.getTime() < s_quickSaveTimeout));
|
||||||
|
i++) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
records.get(i).project.save();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteProject(long projectID) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (_projectsMetadata.containsKey(projectID)) {
|
||||||
|
_projectsMetadata.remove(projectID);
|
||||||
|
}
|
||||||
|
if (_projects.containsKey(projectID)) {
|
||||||
|
_projects.remove(projectID);
|
||||||
|
}
|
||||||
|
|
||||||
|
File dir = getProjectDir(projectID);
|
||||||
|
if (dir.exists()) {
|
||||||
|
deleteDir(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
saveWorkspace();
|
||||||
|
}
|
||||||
|
|
||||||
|
static protected void deleteDir(File dir) {
|
||||||
|
for (File file : dir.listFiles()) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
deleteDir(file);
|
||||||
|
} else {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dir.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void load() {
|
||||||
|
if (loadFromFile(new File(_workspaceDir, "workspace.json"))) return;
|
||||||
|
if (loadFromFile(new File(_workspaceDir, "workspace.temp.json"))) return;
|
||||||
|
if (loadFromFile(new File(_workspaceDir, "workspace.old.json"))) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean loadFromFile(File file) {
|
||||||
|
logger.info("Loading workspace: {}", file.getAbsolutePath());
|
||||||
|
|
||||||
|
_projectsMetadata.clear();
|
||||||
|
_expressions.clear();
|
||||||
|
|
||||||
|
boolean found = false;
|
||||||
|
|
||||||
|
if (file.exists() || file.canRead()) {
|
||||||
|
FileReader reader = null;
|
||||||
|
try {
|
||||||
|
reader = new FileReader(file);
|
||||||
|
JSONTokener tokener = new JSONTokener(reader);
|
||||||
|
JSONObject obj = (JSONObject) tokener.nextValue();
|
||||||
|
|
||||||
|
JSONArray a = obj.getJSONArray("projectIDs");
|
||||||
|
int count = a.length();
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
long id = a.getLong(i);
|
||||||
|
|
||||||
|
File projectDir = getProjectDir(id);
|
||||||
|
ProjectMetadata metadata = ProjectMetadata.load(projectDir);
|
||||||
|
|
||||||
|
_projectsMetadata.put(id, metadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONUtilities.getStringList(obj, "expressions", _expressions);
|
||||||
|
found = true;
|
||||||
|
} catch (JSONException e) {
|
||||||
|
logger.warn("Error reading file", e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("Error reading file", e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.warn("Exception closing file",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
}
|
@ -20,15 +20,15 @@ import com.metaweb.gridworks.commands.Command;
|
|||||||
import edu.mit.simile.butterfly.Butterfly;
|
import edu.mit.simile.butterfly.Butterfly;
|
||||||
|
|
||||||
public class GridworksServlet extends Butterfly {
|
public class GridworksServlet extends Butterfly {
|
||||||
|
|
||||||
static private final String VERSION = "1.0";
|
static private final String VERSION = "1.0";
|
||||||
|
|
||||||
private static final long serialVersionUID = 2386057901503517403L;
|
private static final long serialVersionUID = 2386057901503517403L;
|
||||||
|
|
||||||
private static final String JAVAX_SERVLET_CONTEXT_TEMPDIR = "javax.servlet.context.tempdir";
|
private static final String JAVAX_SERVLET_CONTEXT_TEMPDIR = "javax.servlet.context.tempdir";
|
||||||
|
|
||||||
static final private Map<String, Command> commands = new HashMap<String, Command>();
|
static final private Map<String, Command> commands = new HashMap<String, Command>();
|
||||||
|
|
||||||
// timer for periodically saving projects
|
// timer for periodically saving projects
|
||||||
static private Timer _timer;
|
static private Timer _timer;
|
||||||
|
|
||||||
@ -40,13 +40,13 @@ public class GridworksServlet extends Butterfly {
|
|||||||
{"import-project", "com.metaweb.gridworks.commands.project.ImportProjectCommand"},
|
{"import-project", "com.metaweb.gridworks.commands.project.ImportProjectCommand"},
|
||||||
{"export-project", "com.metaweb.gridworks.commands.project.ExportProjectCommand"},
|
{"export-project", "com.metaweb.gridworks.commands.project.ExportProjectCommand"},
|
||||||
{"export-rows", "com.metaweb.gridworks.commands.project.ExportRowsCommand"},
|
{"export-rows", "com.metaweb.gridworks.commands.project.ExportRowsCommand"},
|
||||||
|
|
||||||
{"get-project-metadata", "com.metaweb.gridworks.commands.project.GetProjectMetadataCommand"},
|
{"get-project-metadata", "com.metaweb.gridworks.commands.project.GetProjectMetadataCommand"},
|
||||||
{"get-all-project-metadata", "com.metaweb.gridworks.commands.workspace.GetAllProjectMetadataCommand"},
|
{"get-all-project-metadata", "com.metaweb.gridworks.commands.workspace.GetAllProjectMetadataCommand"},
|
||||||
|
|
||||||
{"delete-project", "com.metaweb.gridworks.commands.project.DeleteProjectCommand"},
|
{"delete-project", "com.metaweb.gridworks.commands.project.DeleteProjectCommand"},
|
||||||
{"rename-project", "com.metaweb.gridworks.commands.project.RenameProjectCommand"},
|
{"rename-project", "com.metaweb.gridworks.commands.project.RenameProjectCommand"},
|
||||||
|
|
||||||
{"get-models", "com.metaweb.gridworks.commands.project.GetModelsCommand"},
|
{"get-models", "com.metaweb.gridworks.commands.project.GetModelsCommand"},
|
||||||
{"get-rows", "com.metaweb.gridworks.commands.row.GetRowsCommand"},
|
{"get-rows", "com.metaweb.gridworks.commands.row.GetRowsCommand"},
|
||||||
{"get-processes", "com.metaweb.gridworks.commands.history.GetProcessesCommand"},
|
{"get-processes", "com.metaweb.gridworks.commands.history.GetProcessesCommand"},
|
||||||
@ -54,28 +54,28 @@ public class GridworksServlet extends Butterfly {
|
|||||||
{"get-operations", "com.metaweb.gridworks.commands.history.GetOperationsCommand"},
|
{"get-operations", "com.metaweb.gridworks.commands.history.GetOperationsCommand"},
|
||||||
{"get-columns-info", "com.metaweb.gridworks.commands.column.GetColumnsInfoCommand"},
|
{"get-columns-info", "com.metaweb.gridworks.commands.column.GetColumnsInfoCommand"},
|
||||||
{"get-scatterplot", "com.metaweb.gridworks.commands.browsing.GetScatterplotCommand"},
|
{"get-scatterplot", "com.metaweb.gridworks.commands.browsing.GetScatterplotCommand"},
|
||||||
|
|
||||||
{"undo-redo", "com.metaweb.gridworks.commands.history.UndoRedoCommand"},
|
{"undo-redo", "com.metaweb.gridworks.commands.history.UndoRedoCommand"},
|
||||||
{"apply-operations", "com.metaweb.gridworks.commands.history.ApplyOperationsCommand"},
|
{"apply-operations", "com.metaweb.gridworks.commands.history.ApplyOperationsCommand"},
|
||||||
{"cancel-processes", "com.metaweb.gridworks.commands.history.CancelProcessesCommand"},
|
{"cancel-processes", "com.metaweb.gridworks.commands.history.CancelProcessesCommand"},
|
||||||
|
|
||||||
{"compute-facets", "com.metaweb.gridworks.commands.browsing.ComputeFacetsCommand"},
|
{"compute-facets", "com.metaweb.gridworks.commands.browsing.ComputeFacetsCommand"},
|
||||||
{"compute-clusters", "com.metaweb.gridworks.commands.browsing.ComputeClustersCommand"},
|
{"compute-clusters", "com.metaweb.gridworks.commands.browsing.ComputeClustersCommand"},
|
||||||
|
|
||||||
{"edit-one-cell", "com.metaweb.gridworks.commands.cell.EditOneCellCommand"},
|
{"edit-one-cell", "com.metaweb.gridworks.commands.cell.EditOneCellCommand"},
|
||||||
{"text-transform", "com.metaweb.gridworks.commands.cell.TextTransformCommand"},
|
{"text-transform", "com.metaweb.gridworks.commands.cell.TextTransformCommand"},
|
||||||
{"mass-edit", "com.metaweb.gridworks.commands.cell.MassEditCommand"},
|
{"mass-edit", "com.metaweb.gridworks.commands.cell.MassEditCommand"},
|
||||||
{"join-multi-value-cells", "com.metaweb.gridworks.commands.cell.JoinMultiValueCellsCommand"},
|
{"join-multi-value-cells", "com.metaweb.gridworks.commands.cell.JoinMultiValueCellsCommand"},
|
||||||
{"split-multi-value-cells", "com.metaweb.gridworks.commands.cell.SplitMultiValueCellsCommand"},
|
{"split-multi-value-cells", "com.metaweb.gridworks.commands.cell.SplitMultiValueCellsCommand"},
|
||||||
|
|
||||||
{"add-column", "com.metaweb.gridworks.commands.column.AddColumnCommand"},
|
{"add-column", "com.metaweb.gridworks.commands.column.AddColumnCommand"},
|
||||||
{"remove-column", "com.metaweb.gridworks.commands.column.RemoveColumnCommand"},
|
{"remove-column", "com.metaweb.gridworks.commands.column.RemoveColumnCommand"},
|
||||||
{"rename-column", "com.metaweb.gridworks.commands.column.RenameColumnCommand"},
|
{"rename-column", "com.metaweb.gridworks.commands.column.RenameColumnCommand"},
|
||||||
{"split-column", "com.metaweb.gridworks.commands.column.SplitColumnCommand"},
|
{"split-column", "com.metaweb.gridworks.commands.column.SplitColumnCommand"},
|
||||||
{"extend-data", "com.metaweb.gridworks.commands.column.ExtendDataCommand"},
|
{"extend-data", "com.metaweb.gridworks.commands.column.ExtendDataCommand"},
|
||||||
|
|
||||||
{"denormalize", "com.metaweb.gridworks.commands.row.DenormalizeCommand"},
|
{"denormalize", "com.metaweb.gridworks.commands.row.DenormalizeCommand"},
|
||||||
|
|
||||||
{"reconcile", "com.metaweb.gridworks.commands.recon.ReconcileCommand"},
|
{"reconcile", "com.metaweb.gridworks.commands.recon.ReconcileCommand"},
|
||||||
{"recon-match-best-candidates", "com.metaweb.gridworks.commands.recon.ReconMatchBestCandidatesCommand"},
|
{"recon-match-best-candidates", "com.metaweb.gridworks.commands.recon.ReconMatchBestCandidatesCommand"},
|
||||||
{"recon-mark-new-topics", "com.metaweb.gridworks.commands.recon.ReconMarkNewTopicsCommand"},
|
{"recon-mark-new-topics", "com.metaweb.gridworks.commands.recon.ReconMarkNewTopicsCommand"},
|
||||||
@ -83,24 +83,24 @@ public class GridworksServlet extends Butterfly {
|
|||||||
{"recon-match-specific-topic-to-cells", "com.metaweb.gridworks.commands.recon.ReconMatchSpecificTopicCommand"},
|
{"recon-match-specific-topic-to-cells", "com.metaweb.gridworks.commands.recon.ReconMatchSpecificTopicCommand"},
|
||||||
{"recon-judge-one-cell", "com.metaweb.gridworks.commands.recon.ReconJudgeOneCellCommand"},
|
{"recon-judge-one-cell", "com.metaweb.gridworks.commands.recon.ReconJudgeOneCellCommand"},
|
||||||
{"recon-judge-similar-cells", "com.metaweb.gridworks.commands.recon.ReconJudgeSimilarCellsCommand"},
|
{"recon-judge-similar-cells", "com.metaweb.gridworks.commands.recon.ReconJudgeSimilarCellsCommand"},
|
||||||
|
|
||||||
{"annotate-one-row", "com.metaweb.gridworks.commands.row.AnnotateOneRowCommand"},
|
{"annotate-one-row", "com.metaweb.gridworks.commands.row.AnnotateOneRowCommand"},
|
||||||
{"annotate-rows", "com.metaweb.gridworks.commands.row.AnnotateRowsCommand"},
|
{"annotate-rows", "com.metaweb.gridworks.commands.row.AnnotateRowsCommand"},
|
||||||
{"remove-rows", "com.metaweb.gridworks.commands.row.RemoveRowsCommand"},
|
{"remove-rows", "com.metaweb.gridworks.commands.row.RemoveRowsCommand"},
|
||||||
{"reorder-rows", "com.metaweb.gridworks.commands.row.ReorderRowsCommand"},
|
{"reorder-rows", "com.metaweb.gridworks.commands.row.ReorderRowsCommand"},
|
||||||
|
|
||||||
{"save-protograph", "com.metaweb.gridworks.commands.freebase.SaveProtographCommand"},
|
{"save-protograph", "com.metaweb.gridworks.commands.freebase.SaveProtographCommand"},
|
||||||
|
|
||||||
{"get-expression-language-info", "com.metaweb.gridworks.commands.expr.GetExpressionLanguageInfoCommand"},
|
{"get-expression-language-info", "com.metaweb.gridworks.commands.expr.GetExpressionLanguageInfoCommand"},
|
||||||
{"get-expression-history", "com.metaweb.gridworks.commands.expr.GetExpressionHistoryCommand"},
|
{"get-expression-history", "com.metaweb.gridworks.commands.expr.GetExpressionHistoryCommand"},
|
||||||
{"log-expression", "com.metaweb.gridworks.commands.expr.LogExpressionCommand"},
|
{"log-expression", "com.metaweb.gridworks.commands.expr.LogExpressionCommand"},
|
||||||
|
|
||||||
{"preview-expression", "com.metaweb.gridworks.commands.expr.PreviewExpressionCommand"},
|
{"preview-expression", "com.metaweb.gridworks.commands.expr.PreviewExpressionCommand"},
|
||||||
{"preview-extend-data", "com.metaweb.gridworks.commands.column.PreviewExtendDataCommand"},
|
{"preview-extend-data", "com.metaweb.gridworks.commands.column.PreviewExtendDataCommand"},
|
||||||
{"preview-protograph", "com.metaweb.gridworks.commands.freebase.PreviewProtographCommand"},
|
{"preview-protograph", "com.metaweb.gridworks.commands.freebase.PreviewProtographCommand"},
|
||||||
|
|
||||||
{"guess-types-of-column", "com.metaweb.gridworks.commands.freebase.GuessTypesOfColumnCommand"},
|
{"guess-types-of-column", "com.metaweb.gridworks.commands.freebase.GuessTypesOfColumnCommand"},
|
||||||
|
|
||||||
{"check-authorization", "com.metaweb.gridworks.commands.auth.CheckAuthorizationCommand"},
|
{"check-authorization", "com.metaweb.gridworks.commands.auth.CheckAuthorizationCommand"},
|
||||||
{"authorize", "com.metaweb.gridworks.commands.auth.AuthorizeCommand"},
|
{"authorize", "com.metaweb.gridworks.commands.auth.AuthorizeCommand"},
|
||||||
{"deauthorize", "com.metaweb.gridworks.commands.auth.DeAuthorizeCommand"},
|
{"deauthorize", "com.metaweb.gridworks.commands.auth.DeAuthorizeCommand"},
|
||||||
@ -110,56 +110,56 @@ public class GridworksServlet extends Butterfly {
|
|||||||
{"mqlread", "com.metaweb.gridworks.commands.freebase.MQLReadCommand"},
|
{"mqlread", "com.metaweb.gridworks.commands.freebase.MQLReadCommand"},
|
||||||
{"mqlwrite", "com.metaweb.gridworks.commands.freebase.MQLWriteCommand"},
|
{"mqlwrite", "com.metaweb.gridworks.commands.freebase.MQLWriteCommand"},
|
||||||
};
|
};
|
||||||
|
|
||||||
public static String getVersion() {
|
public static String getVersion() {
|
||||||
return VERSION;
|
return VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
final static protected long s_autoSavePeriod = 1000 * 60 * 5; // 5 minutes
|
final static protected long s_autoSavePeriod = 1000 * 60 * 5; // 5 minutes
|
||||||
|
|
||||||
static protected class AutoSaveTimerTask extends TimerTask {
|
static protected class AutoSaveTimerTask extends TimerTask {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
ProjectManager.singleton.save(false); // quick, potentially incomplete save
|
ProjectManager.singleton.save(false); // quick, potentially incomplete save
|
||||||
} finally {
|
} finally {
|
||||||
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
|
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
|
||||||
// we don't use scheduleAtFixedRate because that might result in
|
// we don't use scheduleAtFixedRate because that might result in
|
||||||
// bunched up events when the computer is put in sleep mode
|
// bunched up events when the computer is put in sleep mode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServletConfig config;
|
protected ServletConfig config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() throws ServletException {
|
public void init() throws ServletException {
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
logger.trace("> initialize");
|
logger.trace("> initialize");
|
||||||
|
|
||||||
String data = getInitParameter("gridworks.data");
|
String data = getInitParameter("gridworks.data");
|
||||||
|
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
throw new ServletException("can't find servlet init config 'gridworks.data', I have to give up initializing");
|
throw new ServletException("can't find servlet init config 'gridworks.data', I have to give up initializing");
|
||||||
}
|
}
|
||||||
|
|
||||||
registerCommands(commandNames);
|
registerCommands(commandNames);
|
||||||
|
|
||||||
ProjectManager.initialize(new File(data));
|
FileProjectManager.initialize(new File(data));
|
||||||
|
|
||||||
if (_timer == null) {
|
if (_timer == null) {
|
||||||
_timer = new Timer("autosave");
|
_timer = new Timer("autosave");
|
||||||
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
|
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trace("< initialize");
|
logger.trace("< initialize");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
logger.trace("> destroy");
|
logger.trace("> destroy");
|
||||||
|
|
||||||
// cancel automatic periodic saving and force a complete save.
|
// cancel automatic periodic saving and force a complete save.
|
||||||
if (_timer != null) {
|
if (_timer != null) {
|
||||||
_timer.cancel();
|
_timer.cancel();
|
||||||
_timer = null;
|
_timer = null;
|
||||||
@ -170,12 +170,12 @@ public class GridworksServlet extends Butterfly {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.config = null;
|
this.config = null;
|
||||||
|
|
||||||
logger.trace("< destroy");
|
logger.trace("< destroy");
|
||||||
|
|
||||||
super.destroy();
|
super.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
if (request.getPathInfo().startsWith("/command")) {
|
if (request.getPathInfo().startsWith("/command")) {
|
||||||
@ -200,19 +200,19 @@ public class GridworksServlet extends Butterfly {
|
|||||||
super.service(request, response);
|
super.service(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getCommandName(HttpServletRequest request) {
|
protected String getCommandName(HttpServletRequest request) {
|
||||||
// Remove extraneous path segments that might be there for other purposes,
|
// Remove extraneous path segments that might be there for other purposes,
|
||||||
// e.g., for /export-rows/filename.ext, export-rows is the command while
|
// e.g., for /export-rows/filename.ext, export-rows is the command while
|
||||||
// filename.ext is only for the browser to prompt a convenient filename.
|
// filename.ext is only for the browser to prompt a convenient filename.
|
||||||
String commandName = request.getPathInfo().substring("/command/".length());
|
String commandName = request.getPathInfo().substring("/command/".length());
|
||||||
int slash = commandName.indexOf('/');
|
int slash = commandName.indexOf('/');
|
||||||
return slash > 0 ? commandName.substring(0, slash) : commandName;
|
return slash > 0 ? commandName.substring(0, slash) : commandName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File tempDir = null;
|
private File tempDir = null;
|
||||||
|
|
||||||
public File getTempDir() {
|
public File getTempDir() {
|
||||||
if (tempDir == null) {
|
if (tempDir == null) {
|
||||||
File tempDir = (File) this.config.getServletContext().getAttribute(JAVAX_SERVLET_CONTEXT_TEMPDIR);
|
File tempDir = (File) this.config.getServletContext().getAttribute(JAVAX_SERVLET_CONTEXT_TEMPDIR);
|
||||||
if (tempDir == null) {
|
if (tempDir == null) {
|
||||||
@ -221,7 +221,7 @@ public class GridworksServlet extends Butterfly {
|
|||||||
}
|
}
|
||||||
return tempDir;
|
return tempDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getTempFile(String name) {
|
public File getTempFile(String name) {
|
||||||
return new File(getTempDir(), name);
|
return new File(getTempDir(), name);
|
||||||
}
|
}
|
||||||
@ -229,10 +229,10 @@ public class GridworksServlet extends Butterfly {
|
|||||||
public String getConfiguration(String name, String def) {
|
public String getConfiguration(String name, String def) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register an array of commands
|
* Register an array of commands
|
||||||
*
|
*
|
||||||
* @param commands
|
* @param commands
|
||||||
* An array of arrays containing pairs of strings with the
|
* An array of arrays containing pairs of strings with the
|
||||||
* command name in the first element of the tuple and the fully
|
* command name in the first element of the tuple and the fully
|
||||||
@ -267,10 +267,10 @@ public class GridworksServlet extends Butterfly {
|
|||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a single command.
|
* Register a single command.
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* command verb for command
|
* command verb for command
|
||||||
* @param commandObject
|
* @param commandObject
|
||||||
|
@ -1,158 +1,67 @@
|
|||||||
package com.metaweb.gridworks;
|
package com.metaweb.gridworks;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.json.JSONTokener;
|
|
||||||
import org.json.JSONWriter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.util.JSONUtilities;
|
|
||||||
|
|
||||||
public class ProjectManager {
|
|
||||||
|
public abstract class ProjectManager {
|
||||||
// last n expressions used across all projects
|
// last n expressions used across all projects
|
||||||
static protected final int s_expressionHistoryMax = 100;
|
static protected final int s_expressionHistoryMax = 100;
|
||||||
|
|
||||||
protected File _workspaceDir;
|
|
||||||
protected Map<Long, ProjectMetadata> _projectsMetadata;
|
protected Map<Long, ProjectMetadata> _projectsMetadata;
|
||||||
protected List<String> _expressions;
|
protected List<String> _expressions;
|
||||||
|
|
||||||
final static Logger logger = LoggerFactory.getLogger("project_manager");
|
final static Logger logger = LoggerFactory.getLogger("project_manager");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* While each project's metadata is loaded completely at start-up, each project's raw data
|
* What caches the joins between projects.
|
||||||
|
*/
|
||||||
|
transient protected InterProjectModel _interProjectModel = new InterProjectModel();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flags
|
||||||
|
*/
|
||||||
|
transient protected int _busy = 0; // heavy operations like creating or importing projects are going on
|
||||||
|
|
||||||
|
/**
|
||||||
|
* While each project's metadata is loaded completely at start-up, each project's raw data
|
||||||
* is loaded only when the project is accessed by the user. This is because project
|
* is loaded only when the project is accessed by the user. This is because project
|
||||||
* metadata is tiny compared to raw project data. This hash map from project ID to project
|
* metadata is tiny compared to raw project data. This hash map from project ID to project
|
||||||
* is more like a last accessed-last out cache.
|
* is more like a last accessed-last out cache.
|
||||||
*/
|
*/
|
||||||
transient protected Map<Long, Project> _projects;
|
transient protected Map<Long, Project> _projects;
|
||||||
|
|
||||||
/**
|
|
||||||
* What caches the joins between projects.
|
|
||||||
*/
|
|
||||||
transient protected InterProjectModel _interProjectModel = new InterProjectModel();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flags
|
|
||||||
*/
|
|
||||||
transient protected int _busy = 0; // heavy operations like creating or importing projects are going on
|
|
||||||
|
|
||||||
static public ProjectManager singleton;
|
static public ProjectManager singleton;
|
||||||
|
|
||||||
static public synchronized void initialize(File dir) {
|
|
||||||
if (singleton == null) {
|
|
||||||
logger.info("Using workspace directory: {}", dir.getAbsolutePath());
|
|
||||||
singleton = new ProjectManager(dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ProjectManager(File dir) {
|
|
||||||
_workspaceDir = dir;
|
|
||||||
_workspaceDir.mkdirs();
|
|
||||||
|
|
||||||
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
|
|
||||||
_expressions = new LinkedList<String>();
|
|
||||||
_projects = new HashMap<Long, Project>();
|
|
||||||
|
|
||||||
load();
|
|
||||||
}
|
|
||||||
|
|
||||||
public InterProjectModel getInterProjectModel() {
|
public InterProjectModel getInterProjectModel() {
|
||||||
return _interProjectModel;
|
return _interProjectModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getWorkspaceDir() {
|
//FIXME this is File System specific, need to remove from this abstract class
|
||||||
return _workspaceDir;
|
public abstract File getProjectDir(long id);
|
||||||
}
|
|
||||||
|
|
||||||
static public File getProjectDir(File workspaceDir, long projectID) {
|
|
||||||
File dir = new File(workspaceDir, projectID + ".project");
|
|
||||||
if (!dir.exists()) {
|
|
||||||
dir.mkdir();
|
|
||||||
}
|
|
||||||
return dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getProjectDir(long projectID) {
|
|
||||||
return getProjectDir(_workspaceDir, projectID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerProject(Project project, ProjectMetadata projectMetadata) {
|
public void registerProject(Project project, ProjectMetadata projectMetadata) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
_projects.put(project.id, project);
|
_projects.put(project.id, project);
|
||||||
_projectsMetadata.put(project.id, projectMetadata);
|
_projectsMetadata.put(project.id, projectMetadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public abstract boolean importProject(long projectID);
|
||||||
* Import an external project that has been received as a .tar file, expanded, and
|
|
||||||
* copied into our workspace directory.
|
public abstract void ensureProjectSaved(long id);
|
||||||
*
|
|
||||||
* @param projectID
|
|
||||||
*/
|
|
||||||
public boolean importProject(long projectID) {
|
|
||||||
synchronized (this) {
|
|
||||||
ProjectMetadata metadata = ProjectMetadata.load(getProjectDir(projectID));
|
|
||||||
if (metadata != null) {
|
|
||||||
_projectsMetadata.put(projectID, metadata);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make sure that a project's metadata and data are saved to file. For example,
|
|
||||||
* this method is called before the project is exported to a .tar file.
|
|
||||||
*
|
|
||||||
* @param id
|
|
||||||
*/
|
|
||||||
public void ensureProjectSaved(long id) {
|
|
||||||
synchronized (this) {
|
|
||||||
File projectDir = getProjectDir(id);
|
|
||||||
|
|
||||||
ProjectMetadata metadata = _projectsMetadata.get(id);
|
|
||||||
if (metadata != null) {
|
|
||||||
try {
|
|
||||||
metadata.save(projectDir);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Project project = _projects.get(id);
|
|
||||||
if (project != null && metadata.getModified().after(project.lastSave)) {
|
|
||||||
try {
|
|
||||||
project.save();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ProjectMetadata getProjectMetadata(long id) {
|
public ProjectMetadata getProjectMetadata(long id) {
|
||||||
return _projectsMetadata.get(id);
|
return _projectsMetadata.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectMetadata getProjectMetadata(String name) {
|
public ProjectMetadata getProjectMetadata(String name) {
|
||||||
for (ProjectMetadata pm : _projectsMetadata.values()) {
|
for (ProjectMetadata pm : _projectsMetadata.values()) {
|
||||||
if (pm.getName().equals(name)) {
|
if (pm.getName().equals(name)) {
|
||||||
@ -161,7 +70,7 @@ public class ProjectManager {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getProjectID(String name) {
|
public long getProjectID(String name) {
|
||||||
for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
|
for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
|
||||||
if (entry.getValue().getName().equals(name)) {
|
if (entry.getValue().getName().equals(name)) {
|
||||||
@ -170,25 +79,14 @@ public class ProjectManager {
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<Long, ProjectMetadata> getAllProjectMetadata() {
|
public Map<Long, ProjectMetadata> getAllProjectMetadata() {
|
||||||
return _projectsMetadata;
|
return _projectsMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Project getProject(long id) {
|
public abstract Project getProject(long parseLong);
|
||||||
synchronized (this) {
|
|
||||||
if (_projects.containsKey(id)) {
|
|
||||||
return _projects.get(id);
|
|
||||||
} else {
|
|
||||||
Project project = Project.load(getProjectDir(id), id);
|
|
||||||
|
|
||||||
_projects.put(id, project);
|
|
||||||
|
|
||||||
return project;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBusy(boolean busy) {
|
public void setBusy(boolean busy) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (busy) {
|
if (busy) {
|
||||||
@ -198,7 +96,7 @@ public class ProjectManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLatestExpression(String s) {
|
public void addLatestExpression(String s) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
_expressions.remove(s);
|
_expressions.remove(s);
|
||||||
@ -208,237 +106,17 @@ public class ProjectManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getExpressions() {
|
public List<String> getExpressions() {
|
||||||
return _expressions;
|
return _expressions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(boolean allModified) {
|
public abstract void save(boolean b);
|
||||||
if (allModified || _busy == 0) {
|
|
||||||
saveProjects(allModified);
|
|
||||||
saveWorkspace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Save the workspace's data out to file in a safe way: save to a temporary file first
|
|
||||||
* and rename it to the real file.
|
|
||||||
*/
|
|
||||||
protected void saveWorkspace() {
|
|
||||||
synchronized (this) {
|
|
||||||
File tempFile = new File(_workspaceDir, "workspace.temp.json");
|
|
||||||
try {
|
|
||||||
saveToFile(tempFile);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
logger.warn("Failed to save workspace");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = new File(_workspaceDir, "workspace.json");
|
|
||||||
File oldFile = new File(_workspaceDir, "workspace.old.json");
|
|
||||||
|
|
||||||
if (file.exists()) {
|
|
||||||
file.renameTo(oldFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
tempFile.renameTo(file);
|
|
||||||
if (oldFile.exists()) {
|
|
||||||
oldFile.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Saved workspace");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void saveToFile(File file) throws IOException, JSONException {
|
|
||||||
FileWriter writer = new FileWriter(file);
|
|
||||||
try {
|
|
||||||
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
||||||
jsonWriter.object();
|
|
||||||
jsonWriter.key("projectIDs");
|
|
||||||
jsonWriter.array();
|
|
||||||
for (Long id : _projectsMetadata.keySet()) {
|
|
||||||
ProjectMetadata metadata = _projectsMetadata.get(id);
|
|
||||||
if (metadata != null) {
|
|
||||||
jsonWriter.value(id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
metadata.save(getProjectDir(id));
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jsonWriter.endArray();
|
|
||||||
writer.write('\n');
|
|
||||||
|
|
||||||
jsonWriter.key("expressions"); JSONUtilities.writeStringList(jsonWriter, _expressions);
|
|
||||||
jsonWriter.endObject();
|
|
||||||
} finally {
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A utility class to prioritize projects for saving, depending on how long ago
|
|
||||||
* they have been changed but have not been saved.
|
|
||||||
*/
|
|
||||||
static protected class SaveRecord {
|
|
||||||
final Project project;
|
|
||||||
final long overdue;
|
|
||||||
|
|
||||||
SaveRecord(Project project, long overdue) {
|
|
||||||
this.project = project;
|
|
||||||
this.overdue = overdue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static protected final int s_projectFlushDelay = 1000 * 60 * 60; // 1 hour
|
|
||||||
static protected final int s_quickSaveTimeout = 1000 * 30; // 30 secs
|
|
||||||
|
|
||||||
protected void saveProjects(boolean allModified) {
|
|
||||||
List<SaveRecord> records = new ArrayList<SaveRecord>();
|
|
||||||
Date now = new Date();
|
|
||||||
|
|
||||||
synchronized (this) {
|
|
||||||
for (long id : _projectsMetadata.keySet()) {
|
|
||||||
ProjectMetadata metadata = _projectsMetadata.get(id);
|
|
||||||
Project project = _projects.get(id);
|
|
||||||
|
|
||||||
if (project != null) {
|
|
||||||
boolean hasUnsavedChanges =
|
|
||||||
metadata.getModified().getTime() > project.lastSave.getTime();
|
|
||||||
|
|
||||||
if (hasUnsavedChanges) {
|
|
||||||
long msecsOverdue = now.getTime() - project.lastSave.getTime();
|
|
||||||
|
|
||||||
records.add(new SaveRecord(project, msecsOverdue));
|
|
||||||
|
|
||||||
} else if (now.getTime() - project.lastSave.getTime() > s_projectFlushDelay) {
|
|
||||||
/*
|
|
||||||
* It's been a while since the project was last saved and it hasn't been
|
|
||||||
* modified. We can safely remove it from the cache to save some memory.
|
|
||||||
*/
|
|
||||||
_projects.remove(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (records.size() > 0) {
|
|
||||||
Collections.sort(records, new Comparator<SaveRecord>() {
|
|
||||||
public int compare(SaveRecord o1, SaveRecord o2) {
|
|
||||||
if (o1.overdue < o2.overdue) {
|
|
||||||
return 1;
|
|
||||||
} else if (o1.overdue > o2.overdue) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info(allModified ?
|
|
||||||
"Saving all modified projects ..." :
|
|
||||||
"Saving some modified projects ..."
|
|
||||||
);
|
|
||||||
|
|
||||||
for (int i = 0;
|
|
||||||
i < records.size() &&
|
|
||||||
(allModified || (new Date().getTime() - now.getTime() < s_quickSaveTimeout));
|
|
||||||
i++) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
records.get(i).project.save();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteProject(Project project) {
|
public void deleteProject(Project project) {
|
||||||
deleteProject(project.id);
|
deleteProject(project.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteProject(long projectID) {
|
public abstract void deleteProject(long projectID) ;
|
||||||
synchronized (this) {
|
|
||||||
if (_projectsMetadata.containsKey(projectID)) {
|
|
||||||
_projectsMetadata.remove(projectID);
|
|
||||||
}
|
|
||||||
if (_projects.containsKey(projectID)) {
|
|
||||||
_projects.remove(projectID);
|
|
||||||
}
|
|
||||||
|
|
||||||
File dir = getProjectDir(projectID);
|
|
||||||
if (dir.exists()) {
|
|
||||||
deleteDir(dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
saveWorkspace();
|
|
||||||
}
|
|
||||||
|
|
||||||
static protected void deleteDir(File dir) {
|
|
||||||
for (File file : dir.listFiles()) {
|
|
||||||
if (file.isDirectory()) {
|
|
||||||
deleteDir(file);
|
|
||||||
} else {
|
|
||||||
file.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dir.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void load() {
|
|
||||||
if (loadFromFile(new File(_workspaceDir, "workspace.json"))) return;
|
|
||||||
if (loadFromFile(new File(_workspaceDir, "workspace.temp.json"))) return;
|
|
||||||
if (loadFromFile(new File(_workspaceDir, "workspace.old.json"))) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean loadFromFile(File file) {
|
|
||||||
logger.info("Loading workspace: {}", file.getAbsolutePath());
|
|
||||||
|
|
||||||
_projectsMetadata.clear();
|
|
||||||
_expressions.clear();
|
|
||||||
|
|
||||||
boolean found = false;
|
|
||||||
|
|
||||||
if (file.exists() || file.canRead()) {
|
|
||||||
FileReader reader = null;
|
|
||||||
try {
|
|
||||||
reader = new FileReader(file);
|
|
||||||
JSONTokener tokener = new JSONTokener(reader);
|
|
||||||
JSONObject obj = (JSONObject) tokener.nextValue();
|
|
||||||
|
|
||||||
JSONArray a = obj.getJSONArray("projectIDs");
|
|
||||||
int count = a.length();
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
long id = a.getLong(i);
|
|
||||||
|
|
||||||
File projectDir = getProjectDir(id);
|
|
||||||
ProjectMetadata metadata = ProjectMetadata.load(projectDir);
|
|
||||||
|
|
||||||
_projectsMetadata.put(id, metadata);
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONUtilities.getStringList(obj, "expressions", _expressions);
|
|
||||||
found = true;
|
|
||||||
} catch (JSONException e) {
|
|
||||||
logger.warn("Error reading file", e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.warn("Error reading file", e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.warn("Exception closing file",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user