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:
Iain Sproat 2010-06-15 20:55:38 +00:00
parent c94957b6a0
commit b07075bed5
6 changed files with 266 additions and 227 deletions

View File

@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.io.FileProjectManager;
import edu.mit.simile.butterfly.Butterfly; import edu.mit.simile.butterfly.Butterfly;

View File

@ -1,10 +1,5 @@
package com.metaweb.gridworks; 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.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -12,7 +7,6 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,18 +16,18 @@ import com.metaweb.gridworks.util.ParsingUtilities;
public class ProjectMetadata implements Jsonizable { public class ProjectMetadata implements Jsonizable {
private static final int s_expressionHistoryMax = 20; // last n expressions used in this project private static final int s_expressionHistoryMax = 20; // last n expressions used in this project
private final Date _created; private final Date _created;
private Date _modified; private Date _modified;
private String _name; private String _name;
private String _password; private String _password;
private String _encoding; private String _encoding;
private int _encodingConfidence; private int _encodingConfidence;
private List<String> _expressions = new LinkedList<String>(); private List<String> _expressions = new LinkedList<String>();
final Logger logger = LoggerFactory.getLogger("project_metadata"); final Logger logger = LoggerFactory.getLogger("project_metadata");
protected ProjectMetadata(Date date) { protected ProjectMetadata(Date date) {
_created = date; _created = date;
} }
@ -42,109 +36,51 @@ public class ProjectMetadata implements Jsonizable {
_created = new Date(); _created = new Date();
_modified = _created; _modified = _created;
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("name"); writer.value(_name); writer.key("name"); writer.value(_name);
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created)); writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified)); writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
if ("save".equals(options.getProperty("mode"))) { if ("save".equals(options.getProperty("mode"))) {
writer.key("password"); writer.value(_password); writer.key("password"); writer.value(_password);
writer.key("encoding"); writer.value(_encoding); writer.key("encoding"); writer.value(_encoding);
writer.key("encodingConfidence"); writer.value(_encodingConfidence); writer.key("encodingConfidence"); writer.value(_encodingConfidence);
writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions); writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions);
} }
writer.endObject(); writer.endObject();
} }
public void save(File projectDir) throws Exception {
File tempFile = new File(projectDir, "metadata.temp.json");
try { public void write(JSONWriter jsonWriter) throws Exception {
saveToFile(tempFile); Properties options = new Properties();
} catch (Exception e) { options.setProperty("mode", "save");
e.printStackTrace();
write(jsonWriter, options);
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 void saveToFile(File metadataFile) throws Exception {
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
try { static public ProjectMetadata loadFromJSON(JSONObject obj) {
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) {
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date())); ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
pm._modified = JSONUtilities.getDate(obj, "modified", new Date()); pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>"); pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
pm._password = JSONUtilities.getString(obj, "password", ""); pm._password = JSONUtilities.getString(obj, "password", "");
pm._encoding = JSONUtilities.getString(obj, "encoding", ""); pm._encoding = JSONUtilities.getString(obj, "encoding", "");
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0); pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
JSONUtilities.getStringList(obj, "expressions", pm._expressions); JSONUtilities.getStringList(obj, "expressions", pm._expressions);
return pm; return pm;
} }
public Date getCreated() { public Date getCreated() {
return _created; return _created;
} }
@ -168,7 +104,7 @@ public class ProjectMetadata implements Jsonizable {
public void setEncodingConfidence(int confidence) { public void setEncodingConfidence(int confidence) {
this._encodingConfidence = confidence; this._encodingConfidence = confidence;
} }
public void setEncodingConfidence(String confidence) { public void setEncodingConfidence(String confidence) {
if (confidence != null) { if (confidence != null) {
this.setEncodingConfidence(Integer.parseInt(confidence)); this.setEncodingConfidence(Integer.parseInt(confidence));
@ -178,7 +114,7 @@ public class ProjectMetadata implements Jsonizable {
public int getEncodingConfidence() { public int getEncodingConfidence() {
return _encodingConfidence; return _encodingConfidence;
} }
public void setPassword(String password) { public void setPassword(String password) {
this._password = password; this._password = password;
} }
@ -186,25 +122,25 @@ public class ProjectMetadata implements Jsonizable {
public String getPassword() { public String getPassword() {
return _password; return _password;
} }
public Date getModified() { public Date getModified() {
return _modified; return _modified;
} }
public void updateModified() { public void updateModified() {
_modified = new Date(); _modified = new Date();
} }
public void addLatestExpression(String s) { public void addLatestExpression(String s) {
_expressions.remove(s); _expressions.remove(s);
_expressions.add(0, s); _expressions.add(0, s);
while (_expressions.size() > s_expressionHistoryMax) { while (_expressions.size() > s_expressionHistoryMax) {
_expressions.remove(_expressions.size() - 1); _expressions.remove(_expressions.size() - 1);
} }
ProjectManager.singleton.addLatestExpression(s); ProjectManager.singleton.addLatestExpression(s);
} }
public List<String> getExpressions() { public List<String> getExpressions() {
return _expressions; return _expressions;
} }

View File

@ -1,4 +1,4 @@
package com.metaweb.gridworks; package com.metaweb.gridworks.io;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -20,6 +20,8 @@ import org.json.JSONWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.JSONUtilities; import com.metaweb.gridworks.util.JSONUtilities;
@ -63,8 +65,6 @@ public class FileProjectManager extends ProjectManager{
return getProjectDir(_workspaceDir, projectID); return getProjectDir(_workspaceDir, projectID);
} }
/** /**
* Import an external project that has been received as a .tar file, expanded, and * Import an external project that has been received as a .tar file, expanded, and
* copied into our workspace directory. * copied into our workspace directory.
@ -73,7 +73,7 @@ public class FileProjectManager extends ProjectManager{
*/ */
public boolean importProject(long projectID) { public boolean importProject(long projectID) {
synchronized (this) { synchronized (this) {
ProjectMetadata metadata = ProjectMetadata.load(getProjectDir(projectID)); ProjectMetadata metadata = ProjectMetadataUtilities.load(getProjectDir(projectID));
if (metadata != null) { if (metadata != null) {
_projectsMetadata.put(projectID, metadata); _projectsMetadata.put(projectID, metadata);
return true; return true;
@ -96,7 +96,7 @@ public class FileProjectManager extends ProjectManager{
ProjectMetadata metadata = _projectsMetadata.get(id); ProjectMetadata metadata = _projectsMetadata.get(id);
if (metadata != null) { if (metadata != null) {
try { try {
metadata.save(projectDir); ProjectMetadataUtilities.save(metadata, projectDir);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -105,7 +105,7 @@ public class FileProjectManager extends ProjectManager{
Project project = _projects.get(id); Project project = _projects.get(id);
if (project != null && metadata.getModified().after(project.lastSave)) { if (project != null && metadata.getModified().after(project.lastSave)) {
try { try {
project.save(); ProjectUtilities.save(project);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -118,7 +118,7 @@ public class FileProjectManager extends ProjectManager{
if (_projects.containsKey(id)) { if (_projects.containsKey(id)) {
return _projects.get(id); return _projects.get(id);
} else { } else {
Project project = Project.load(getProjectDir(id), id); Project project = ProjectUtilities.load(getProjectDir(id), id);
_projects.put(id, project); _projects.put(id, project);
@ -179,7 +179,7 @@ public class FileProjectManager extends ProjectManager{
jsonWriter.value(id); jsonWriter.value(id);
try { try {
metadata.save(getProjectDir(id)); ProjectMetadataUtilities.save(metadata, getProjectDir(id));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -265,7 +265,7 @@ public class FileProjectManager extends ProjectManager{
i++) { i++) {
try { try {
records.get(i).project.save(); ProjectUtilities.save(records.get(i).project);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -329,7 +329,7 @@ public class FileProjectManager extends ProjectManager{
long id = a.getLong(i); long id = a.getLong(i);
File projectDir = getProjectDir(id); File projectDir = getProjectDir(id);
ProjectMetadata metadata = ProjectMetadata.load(projectDir); ProjectMetadata metadata = ProjectMetadataUtilities.load(projectDir);
_projectsMetadata.put(id, metadata); _projectsMetadata.put(id, metadata);
} }

View File

@ -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();
}
}
}

View 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();
}
}
}

View File

@ -1,9 +1,6 @@
package com.metaweb.gridworks.model; package com.metaweb.gridworks.model;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -12,9 +9,6 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -59,62 +53,7 @@ public class Project {
return ProjectManager.singleton.getProjectMetadata(id); return ProjectManager.singleton.getProjectMetadata(id);
} }
synchronized public void save() { public void saveToOutputStream(OutputStream out, Pool pool) throws IOException {
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 {
Writer writer = new OutputStreamWriter(out); Writer writer = new OutputStreamWriter(out);
try { try {
Properties options = new Properties(); Properties options = new Properties();
@ -142,64 +81,7 @@ public class Project {
} }
} }
static public Project load(File dir, long id) { static public Project loadFromReader(
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(
LineNumberReader reader, LineNumberReader reader,
long id, long id,
Pool pool Pool pool
@ -247,7 +129,7 @@ public class Project {
return project; return project;
} }
public void update() { public void update() {
columnModel.update(); columnModel.update();
recordModel.update(this); recordModel.update(this);