From b07075bed507471bb047cc26bd16c734ffc33622 Mon Sep 17 00:00:00 2001 From: Iain Sproat Date: Tue, 15 Jun 2010 20:55:38 +0000 Subject: [PATCH] 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 --- .../metaweb/gridworks/GridworksServlet.java | 1 + .../metaweb/gridworks/ProjectMetadata.java | 128 +++++------------ .../{ => io}/FileProjectManager.java | 20 +-- .../io/ProjectMetadataUtilities.java | 85 +++++++++++ .../gridworks/io/ProjectUtilities.java | 135 ++++++++++++++++++ .../com/metaweb/gridworks/model/Project.java | 124 +--------------- 6 files changed, 266 insertions(+), 227 deletions(-) rename main/src/com/metaweb/gridworks/{ => io}/FileProjectManager.java (91%) create mode 100644 main/src/com/metaweb/gridworks/io/ProjectMetadataUtilities.java create mode 100644 main/src/com/metaweb/gridworks/io/ProjectUtilities.java diff --git a/main/src/com/metaweb/gridworks/GridworksServlet.java b/main/src/com/metaweb/gridworks/GridworksServlet.java index cb2e2b748..28975664f 100644 --- a/main/src/com/metaweb/gridworks/GridworksServlet.java +++ b/main/src/com/metaweb/gridworks/GridworksServlet.java @@ -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; diff --git a/main/src/com/metaweb/gridworks/ProjectMetadata.java b/main/src/com/metaweb/gridworks/ProjectMetadata.java index 2008d0690..acaf97ddf 100644 --- a/main/src/com/metaweb/gridworks/ProjectMetadata.java +++ b/main/src/com/metaweb/gridworks/ProjectMetadata.java @@ -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; @@ -22,18 +16,18 @@ import com.metaweb.gridworks.util.ParsingUtilities; public class ProjectMetadata implements Jsonizable { private static final int s_expressionHistoryMax = 20; // last n expressions used in this project - + private final Date _created; private Date _modified; private String _name; private String _password; - + private String _encoding; private int _encodingConfidence; private List _expressions = new LinkedList(); - + final Logger logger = LoggerFactory.getLogger("project_metadata"); - + protected ProjectMetadata(Date date) { _created = date; } @@ -42,109 +36,51 @@ public class ProjectMetadata implements Jsonizable { _created = new Date(); _modified = _created; } - + public void write(JSONWriter writer, Properties options) throws JSONException { - + writer.object(); writer.key("name"); writer.value(_name); writer.key("created"); writer.value(ParsingUtilities.dateToString(_created)); writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified)); - + if ("save".equals(options.getProperty("mode"))) { writer.key("password"); writer.value(_password); - + writer.key("encoding"); writer.value(_encoding); writer.key("encodingConfidence"); writer.value(_encodingConfidence); writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions); } 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"); - - if (file.exists()) { - file.renameTo(oldFile); - } - - tempFile.renameTo(file); - if (oldFile.exists()) { - oldFile.delete(); - } + + + + public void write(JSONWriter jsonWriter) throws Exception { + Properties options = new Properties(); + options.setProperty("mode", "save"); + + 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()); pm._name = JSONUtilities.getString(obj, "name", ""); pm._password = JSONUtilities.getString(obj, "password", ""); - + pm._encoding = JSONUtilities.getString(obj, "encoding", ""); pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0); - + JSONUtilities.getStringList(obj, "expressions", pm._expressions); - + return pm; } - + public Date getCreated() { return _created; } @@ -168,7 +104,7 @@ public class ProjectMetadata implements Jsonizable { public void setEncodingConfidence(int confidence) { this._encodingConfidence = confidence; } - + public void setEncodingConfidence(String confidence) { if (confidence != null) { this.setEncodingConfidence(Integer.parseInt(confidence)); @@ -178,7 +114,7 @@ public class ProjectMetadata implements Jsonizable { public int getEncodingConfidence() { return _encodingConfidence; } - + public void setPassword(String password) { this._password = password; } @@ -186,25 +122,25 @@ public class ProjectMetadata implements Jsonizable { public String getPassword() { return _password; } - + public Date getModified() { return _modified; } - + public void updateModified() { _modified = new Date(); } - + public void addLatestExpression(String s) { _expressions.remove(s); _expressions.add(0, s); while (_expressions.size() > s_expressionHistoryMax) { _expressions.remove(_expressions.size() - 1); } - + ProjectManager.singleton.addLatestExpression(s); } - + public List getExpressions() { return _expressions; } diff --git a/main/src/com/metaweb/gridworks/FileProjectManager.java b/main/src/com/metaweb/gridworks/io/FileProjectManager.java similarity index 91% rename from main/src/com/metaweb/gridworks/FileProjectManager.java rename to main/src/com/metaweb/gridworks/io/FileProjectManager.java index e51e34ff9..6f78dae23 100644 --- a/main/src/com/metaweb/gridworks/FileProjectManager.java +++ b/main/src/com/metaweb/gridworks/io/FileProjectManager.java @@ -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); } diff --git a/main/src/com/metaweb/gridworks/io/ProjectMetadataUtilities.java b/main/src/com/metaweb/gridworks/io/ProjectMetadataUtilities.java new file mode 100644 index 000000000..2eb5e7089 --- /dev/null +++ b/main/src/com/metaweb/gridworks/io/ProjectMetadataUtilities.java @@ -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(); + } + } +} diff --git a/main/src/com/metaweb/gridworks/io/ProjectUtilities.java b/main/src/com/metaweb/gridworks/io/ProjectUtilities.java new file mode 100644 index 000000000..a048a051b --- /dev/null +++ b/main/src/com/metaweb/gridworks/io/ProjectUtilities.java @@ -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(); + } + } +} diff --git a/main/src/com/metaweb/gridworks/model/Project.java b/main/src/com/metaweb/gridworks/model/Project.java index d73bee522..ff5d6d42b 100644 --- a/main/src/com/metaweb/gridworks/model/Project.java +++ b/main/src/com/metaweb/gridworks/model/Project.java @@ -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 @@ -247,7 +129,7 @@ public class Project { return project; } - + public void update() { columnModel.update(); recordModel.update(this);