FileProjectManager and portions of Project and ProjectMetadata classes which deal with io are moved to an io directory.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@972 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
c94957b6a0
commit
b07075bed5
@ -16,6 +16,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.commands.Command;
|
||||
import com.metaweb.gridworks.io.FileProjectManager;
|
||||
|
||||
import edu.mit.simile.butterfly.Butterfly;
|
||||
|
||||
|
@ -1,10 +1,5 @@
|
||||
package com.metaweb.gridworks;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -12,7 +7,6 @@ import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -61,76 +55,18 @@ public class ProjectMetadata implements Jsonizable {
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
public void save(File projectDir) throws Exception {
|
||||
File tempFile = new File(projectDir, "metadata.temp.json");
|
||||
try {
|
||||
saveToFile(tempFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
logger.warn("Failed to save project metadata");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(projectDir, "metadata.json");
|
||||
File oldFile = new File(projectDir, "metadata.old.json");
|
||||
public void write(JSONWriter jsonWriter) throws Exception {
|
||||
Properties options = new Properties();
|
||||
options.setProperty("mode", "save");
|
||||
|
||||
if (file.exists()) {
|
||||
file.renameTo(oldFile);
|
||||
}
|
||||
|
||||
tempFile.renameTo(file);
|
||||
if (oldFile.exists()) {
|
||||
oldFile.delete();
|
||||
}
|
||||
write(jsonWriter, options);
|
||||
}
|
||||
|
||||
protected void saveToFile(File metadataFile) throws Exception {
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
|
||||
try {
|
||||
Properties options = new Properties();
|
||||
options.setProperty("mode", "save");
|
||||
|
||||
JSONWriter jsonWriter = new JSONWriter(writer);
|
||||
|
||||
write(jsonWriter, options);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
static public ProjectMetadata load(File projectDir) {
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.temp.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.old.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static protected ProjectMetadata loadFromFile(File metadataFile) throws Exception {
|
||||
FileReader reader = new FileReader(metadataFile);
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(reader);
|
||||
JSONObject obj = (JSONObject) tokener.nextValue();
|
||||
|
||||
return loadFromJSON(obj);
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
static protected ProjectMetadata loadFromJSON(JSONObject obj) {
|
||||
static public ProjectMetadata loadFromJSON(JSONObject obj) {
|
||||
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
|
||||
|
||||
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.metaweb.gridworks;
|
||||
package com.metaweb.gridworks.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
@ -20,6 +20,8 @@ import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.ProjectManager;
|
||||
import com.metaweb.gridworks.ProjectMetadata;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.util.JSONUtilities;
|
||||
|
||||
@ -63,8 +65,6 @@ public class FileProjectManager extends ProjectManager{
|
||||
return getProjectDir(_workspaceDir, projectID);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Import an external project that has been received as a .tar file, expanded, and
|
||||
* copied into our workspace directory.
|
||||
@ -73,7 +73,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
*/
|
||||
public boolean importProject(long projectID) {
|
||||
synchronized (this) {
|
||||
ProjectMetadata metadata = ProjectMetadata.load(getProjectDir(projectID));
|
||||
ProjectMetadata metadata = ProjectMetadataUtilities.load(getProjectDir(projectID));
|
||||
if (metadata != null) {
|
||||
_projectsMetadata.put(projectID, metadata);
|
||||
return true;
|
||||
@ -96,7 +96,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
ProjectMetadata metadata = _projectsMetadata.get(id);
|
||||
if (metadata != null) {
|
||||
try {
|
||||
metadata.save(projectDir);
|
||||
ProjectMetadataUtilities.save(metadata, projectDir);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -105,7 +105,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
Project project = _projects.get(id);
|
||||
if (project != null && metadata.getModified().after(project.lastSave)) {
|
||||
try {
|
||||
project.save();
|
||||
ProjectUtilities.save(project);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -118,7 +118,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
if (_projects.containsKey(id)) {
|
||||
return _projects.get(id);
|
||||
} else {
|
||||
Project project = Project.load(getProjectDir(id), id);
|
||||
Project project = ProjectUtilities.load(getProjectDir(id), id);
|
||||
|
||||
_projects.put(id, project);
|
||||
|
||||
@ -179,7 +179,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
jsonWriter.value(id);
|
||||
|
||||
try {
|
||||
metadata.save(getProjectDir(id));
|
||||
ProjectMetadataUtilities.save(metadata, getProjectDir(id));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -265,7 +265,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
i++) {
|
||||
|
||||
try {
|
||||
records.get(i).project.save();
|
||||
ProjectUtilities.save(records.get(i).project);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -329,7 +329,7 @@ public class FileProjectManager extends ProjectManager{
|
||||
long id = a.getLong(i);
|
||||
|
||||
File projectDir = getProjectDir(id);
|
||||
ProjectMetadata metadata = ProjectMetadata.load(projectDir);
|
||||
ProjectMetadata metadata = ProjectMetadataUtilities.load(projectDir);
|
||||
|
||||
_projectsMetadata.put(id, metadata);
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.metaweb.gridworks.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.ProjectMetadata;
|
||||
|
||||
|
||||
public class ProjectMetadataUtilities {
|
||||
final static Logger logger = LoggerFactory.getLogger("project_metadata_utilities");
|
||||
|
||||
public static void save(ProjectMetadata projectMeta, File projectDir) throws Exception {
|
||||
File tempFile = new File(projectDir, "metadata.temp.json");
|
||||
try {
|
||||
saveToFile(projectMeta, tempFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
logger.warn("Failed to save project metadata");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(projectDir, "metadata.json");
|
||||
File oldFile = new File(projectDir, "metadata.old.json");
|
||||
|
||||
if (file.exists()) {
|
||||
file.renameTo(oldFile);
|
||||
}
|
||||
|
||||
tempFile.renameTo(file);
|
||||
if (oldFile.exists()) {
|
||||
oldFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
protected static void saveToFile(ProjectMetadata projectMeta, File metadataFile) throws Exception {
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
|
||||
try {
|
||||
JSONWriter jsonWriter = new JSONWriter(writer);
|
||||
projectMeta.write(jsonWriter);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
static public ProjectMetadata load(File projectDir) {
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.temp.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return loadFromFile(new File(projectDir, "metadata.old.json"));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static protected ProjectMetadata loadFromFile(File metadataFile) throws Exception {
|
||||
FileReader reader = new FileReader(metadataFile);
|
||||
try {
|
||||
JSONTokener tokener = new JSONTokener(reader);
|
||||
JSONObject obj = (JSONObject) tokener.nextValue();
|
||||
|
||||
return ProjectMetadata.loadFromJSON(obj);
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
135
main/src/com/metaweb/gridworks/io/ProjectUtilities.java
Normal file
135
main/src/com/metaweb/gridworks/io/ProjectUtilities.java
Normal file
@ -0,0 +1,135 @@
|
||||
package com.metaweb.gridworks.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.util.Date;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.ProjectManager;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.util.Pool;
|
||||
|
||||
|
||||
public class ProjectUtilities {
|
||||
final static Logger logger = LoggerFactory.getLogger("project_utilities");
|
||||
|
||||
synchronized public static void save(Project project) {
|
||||
synchronized (project) {
|
||||
long id = project.id;
|
||||
File dir = ProjectManager.singleton.getProjectDir(id);
|
||||
|
||||
File tempFile = new File(dir, "data.temp.zip");
|
||||
try {
|
||||
saveToFile(project, tempFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
logger.warn("Failed to save project {}", id);
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(dir, "data.zip");
|
||||
File oldFile = new File(dir, "data.old.zip");
|
||||
|
||||
if (file.exists()) {
|
||||
file.renameTo(oldFile);
|
||||
}
|
||||
|
||||
tempFile.renameTo(file);
|
||||
if (oldFile.exists()) {
|
||||
oldFile.delete();
|
||||
}
|
||||
|
||||
project.lastSave = new Date();
|
||||
|
||||
logger.info("Saved project '{}'",id);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void saveToFile(Project project, File file) throws Exception {
|
||||
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
|
||||
try {
|
||||
Pool pool = new Pool();
|
||||
|
||||
out.putNextEntry(new ZipEntry("data.txt"));
|
||||
try {
|
||||
project.saveToOutputStream(out, pool);
|
||||
} finally {
|
||||
out.closeEntry();
|
||||
}
|
||||
|
||||
out.putNextEntry(new ZipEntry("pool.txt"));
|
||||
try {
|
||||
pool.save(out);
|
||||
} finally {
|
||||
out.closeEntry();
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
static public Project load(File dir, long id) {
|
||||
try {
|
||||
File file = new File(dir, "data.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(dir, "data.temp.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(dir, "data.old.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static protected Project loadFromFile(
|
||||
File file,
|
||||
long id
|
||||
) throws Exception {
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
try {
|
||||
Pool pool = new Pool();
|
||||
ZipEntry poolEntry = zipFile.getEntry("pool.txt");
|
||||
if (poolEntry != null) {
|
||||
pool.load(new InputStreamReader(
|
||||
zipFile.getInputStream(poolEntry)));
|
||||
} // else, it's a legacy project file
|
||||
|
||||
return Project.loadFromReader(
|
||||
new LineNumberReader(
|
||||
new InputStreamReader(
|
||||
zipFile.getInputStream(
|
||||
zipFile.getEntry("data.txt")))),
|
||||
id,
|
||||
pool
|
||||
);
|
||||
} finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
package com.metaweb.gridworks.model;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
@ -12,9 +9,6 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -59,62 +53,7 @@ public class Project {
|
||||
return ProjectManager.singleton.getProjectMetadata(id);
|
||||
}
|
||||
|
||||
synchronized public void save() {
|
||||
synchronized (this) {
|
||||
File dir = ProjectManager.singleton.getProjectDir(id);
|
||||
|
||||
File tempFile = new File(dir, "data.temp.zip");
|
||||
try {
|
||||
saveToFile(tempFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
logger.warn("Failed to save project {}", id);
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(dir, "data.zip");
|
||||
File oldFile = new File(dir, "data.old.zip");
|
||||
|
||||
if (file.exists()) {
|
||||
file.renameTo(oldFile);
|
||||
}
|
||||
|
||||
tempFile.renameTo(file);
|
||||
if (oldFile.exists()) {
|
||||
oldFile.delete();
|
||||
}
|
||||
|
||||
lastSave = new Date();
|
||||
|
||||
logger.info("Saved project '{}'",id);
|
||||
}
|
||||
}
|
||||
|
||||
protected void saveToFile(File file) throws Exception {
|
||||
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
|
||||
try {
|
||||
Pool pool = new Pool();
|
||||
|
||||
out.putNextEntry(new ZipEntry("data.txt"));
|
||||
try {
|
||||
saveToOutputStream(out, pool);
|
||||
} finally {
|
||||
out.closeEntry();
|
||||
}
|
||||
|
||||
out.putNextEntry(new ZipEntry("pool.txt"));
|
||||
try {
|
||||
pool.save(out);
|
||||
} finally {
|
||||
out.closeEntry();
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void saveToOutputStream(OutputStream out, Pool pool) throws IOException {
|
||||
public void saveToOutputStream(OutputStream out, Pool pool) throws IOException {
|
||||
Writer writer = new OutputStreamWriter(out);
|
||||
try {
|
||||
Properties options = new Properties();
|
||||
@ -142,64 +81,7 @@ public class Project {
|
||||
}
|
||||
}
|
||||
|
||||
static public Project load(File dir, long id) {
|
||||
try {
|
||||
File file = new File(dir, "data.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(dir, "data.temp.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(dir, "data.old.zip");
|
||||
if (file.exists()) {
|
||||
return loadFromFile(file, id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static protected Project loadFromFile(
|
||||
File file,
|
||||
long id
|
||||
) throws Exception {
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
try {
|
||||
Pool pool = new Pool();
|
||||
ZipEntry poolEntry = zipFile.getEntry("pool.txt");
|
||||
if (poolEntry != null) {
|
||||
pool.load(new InputStreamReader(
|
||||
zipFile.getInputStream(poolEntry)));
|
||||
} // else, it's a legacy project file
|
||||
|
||||
return loadFromReader(
|
||||
new LineNumberReader(
|
||||
new InputStreamReader(
|
||||
zipFile.getInputStream(
|
||||
zipFile.getEntry("data.txt")))),
|
||||
id,
|
||||
pool
|
||||
);
|
||||
} finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
static protected Project loadFromReader(
|
||||
static public Project loadFromReader(
|
||||
LineNumberReader reader,
|
||||
long id,
|
||||
Pool pool
|
||||
|
Loading…
Reference in New Issue
Block a user