Refactored ImportProjectCommand and ExportProjectCommand. These are no longer dependent on the File System, and all file system related work is done in FileProjectManager.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@981 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-06-16 07:44:46 +00:00
parent f47cb75525
commit 280daad2f6
4 changed files with 105 additions and 94 deletions

View File

@ -1,10 +1,13 @@
package com.metaweb.gridworks;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.tools.tar.TarOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,6 +59,10 @@ public abstract class ProjectManager {
public abstract boolean importProject(long projectID);
public abstract void importProject(long projectID, InputStream inputStream, boolean gziped) throws IOException;
public abstract void exportProject(long projectId, TarOutputStream tos) throws IOException;
public abstract void ensureProjectSaved(long id);
public ProjectMetadata getProjectMetadata(long id) {

View File

@ -1,7 +1,5 @@
package com.metaweb.gridworks.commands.project;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
@ -10,12 +8,10 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.io.FileProjectManager;
import com.metaweb.gridworks.model.Project;
public class ExportProjectCommand extends Command {
@ -32,10 +28,7 @@ public class ExportProjectCommand extends Command {
OutputStream os = response.getOutputStream();
try {
gzipTarToOutputStream(//FIXME relies on FileProjectManager
((FileProjectManager)ProjectManager.singleton).getProjectDir(project.id),
os
);
gzipTarToOutputStream(project, os);
} finally {
os.close();
}
@ -44,62 +37,23 @@ public class ExportProjectCommand extends Command {
}
}
protected void gzipTarToOutputStream(File dir, OutputStream os) throws IOException {
protected void gzipTarToOutputStream(Project project, OutputStream os) throws IOException {
GZIPOutputStream gos = new GZIPOutputStream(os);
try {
tarToOutputStream(dir, gos);
tarToOutputStream(project, gos);
} finally {
gos.close();
}
}
protected void tarToOutputStream(File dir, OutputStream os) throws IOException {
protected void tarToOutputStream(Project project, OutputStream os) throws IOException {
TarOutputStream tos = new TarOutputStream(os);
try {
tarDir("", dir, tos);
ProjectManager.singleton.exportProject(project.id, tos);
} finally {
tos.close();
}
}
protected void tarDir(String relative, File dir, TarOutputStream tos) throws IOException {
File[] files = dir.listFiles();
for (File file : files) {
if (!file.isHidden()) {
String path = relative + file.getName();
if (file.isDirectory()) {
tarDir(path + File.separator, file, tos);
} else {
TarEntry entry = new TarEntry(path);
entry.setMode(TarEntry.DEFAULT_FILE_MODE);
entry.setSize(file.length());
entry.setModTime(file.lastModified());
tos.putNextEntry(entry);
copyFile(file, tos);
tos.closeEntry();
}
}
}
}
protected void copyFile(File file, OutputStream os) throws IOException {
final int buffersize = 4096;
FileInputStream fis = new FileInputStream(file);
try {
byte[] buf = new byte[buffersize];
int count;
while((count = fis.read(buf, 0, buffersize)) != -1) {
os.write(buf, 0, count);
}
} finally {
fis.close();
}
}
}

View File

@ -1,13 +1,10 @@
package com.metaweb.gridworks.commands.project;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -17,15 +14,12 @@ import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.ProjectMetadata;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.io.FileProjectManager;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.ParsingUtilities;
@ -94,7 +88,7 @@ public class ImportProjectCommand extends Command {
} else {
String fileName = item.getName().toLowerCase();
try {
internalImportInputStream(projectID, stream, !fileName.endsWith(".tar"));
ProjectManager.singleton.importProject(projectID, stream, !fileName.endsWith(".tar"));
} finally {
stream.close();
}
@ -131,46 +125,11 @@ public class ImportProjectCommand extends Command {
}
try {
internalImportInputStream(projectID, inputStream, !urlString.endsWith(".tar"));
ProjectManager.singleton.importProject(projectID, inputStream, !urlString.endsWith(".tar"));
} finally {
inputStream.close();
}
}
protected void internalImportInputStream(long projectID, InputStream inputStream, boolean gziped) throws IOException {
File destDir = ((FileProjectManager)ProjectManager.singleton).getProjectDir(projectID);//FIXME relies on FileProjectManager
destDir.mkdirs();
if (gziped) {
GZIPInputStream gis = new GZIPInputStream(inputStream);
untar(destDir, gis);
} else {
untar(destDir, inputStream);
}
}
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarInputStream tin = new TarInputStream(inputStream);
TarEntry tarEntry = null;
while ((tarEntry = tin.getNextEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
tin.copyEntryContents(fout);
} finally {
fout.close();
}
}
}
}
}

View File

@ -1,9 +1,13 @@
package com.metaweb.gridworks.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -11,7 +15,11 @@ import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -86,6 +94,89 @@ public class FileProjectManager extends ProjectManager{
}
}
public void importProject(long projectID, InputStream inputStream, boolean gziped) throws IOException {
File destDir = this.getProjectDir(projectID);
destDir.mkdirs();
if (gziped) {
GZIPInputStream gis = new GZIPInputStream(inputStream);
untar(destDir, gis);
} else {
untar(destDir, inputStream);
}
}
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarInputStream tin = new TarInputStream(inputStream);
TarEntry tarEntry = null;
while ((tarEntry = tin.getNextEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
tin.copyEntryContents(fout);
} finally {
fout.close();
}
}
}
}
public void exportProject(long projectId, TarOutputStream tos) throws IOException {
File dir = this.getProjectDir(projectId);
this.tarDir("", dir, tos);
}
protected void tarDir(String relative, File dir, TarOutputStream tos) throws IOException{
File[] files = dir.listFiles();
for (File file : files) {
if (!file.isHidden()) {
String path = relative + file.getName();
if (file.isDirectory()) {
tarDir(path + File.separator, file, tos);
} else {
TarEntry entry = new TarEntry(path);
entry.setMode(TarEntry.DEFAULT_FILE_MODE);
entry.setSize(file.length());
entry.setModTime(file.lastModified());
tos.putNextEntry(entry);
copyFile(file, tos);
tos.closeEntry();
}
}
}
}
protected void copyFile(File file, OutputStream os) throws IOException {
final int buffersize = 4096;
FileInputStream fis = new FileInputStream(file);
try {
byte[] buf = new byte[buffersize];
int count;
while((count = fis.read(buf, 0, buffersize)) != -1) {
os.write(buf, 0, count);
}
} finally {
fis.close();
}
}
/**
* 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.