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:
parent
f47cb75525
commit
280daad2f6
@ -1,10 +1,13 @@
|
|||||||
package com.metaweb.gridworks;
|
package com.metaweb.gridworks;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
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.apache.tools.tar.TarOutputStream;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -56,6 +59,10 @@ public abstract class ProjectManager {
|
|||||||
|
|
||||||
public abstract boolean importProject(long projectID);
|
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 abstract void ensureProjectSaved(long id);
|
||||||
|
|
||||||
public ProjectMetadata getProjectMetadata(long id) {
|
public ProjectMetadata getProjectMetadata(long id) {
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.metaweb.gridworks.commands.project;
|
package com.metaweb.gridworks.commands.project;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
@ -10,12 +8,10 @@ import javax.servlet.ServletException;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.tools.tar.TarEntry;
|
|
||||||
import org.apache.tools.tar.TarOutputStream;
|
import org.apache.tools.tar.TarOutputStream;
|
||||||
|
|
||||||
import com.metaweb.gridworks.ProjectManager;
|
import com.metaweb.gridworks.ProjectManager;
|
||||||
import com.metaweb.gridworks.commands.Command;
|
import com.metaweb.gridworks.commands.Command;
|
||||||
import com.metaweb.gridworks.io.FileProjectManager;
|
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
|
||||||
public class ExportProjectCommand extends Command {
|
public class ExportProjectCommand extends Command {
|
||||||
@ -32,10 +28,7 @@ public class ExportProjectCommand extends Command {
|
|||||||
|
|
||||||
OutputStream os = response.getOutputStream();
|
OutputStream os = response.getOutputStream();
|
||||||
try {
|
try {
|
||||||
gzipTarToOutputStream(//FIXME relies on FileProjectManager
|
gzipTarToOutputStream(project, os);
|
||||||
((FileProjectManager)ProjectManager.singleton).getProjectDir(project.id),
|
|
||||||
os
|
|
||||||
);
|
|
||||||
} finally {
|
} finally {
|
||||||
os.close();
|
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);
|
GZIPOutputStream gos = new GZIPOutputStream(os);
|
||||||
try {
|
try {
|
||||||
tarToOutputStream(dir, gos);
|
tarToOutputStream(project, gos);
|
||||||
} finally {
|
} finally {
|
||||||
gos.close();
|
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);
|
TarOutputStream tos = new TarOutputStream(os);
|
||||||
try {
|
try {
|
||||||
tarDir("", dir, tos);
|
ProjectManager.singleton.exportProject(project.id, tos);
|
||||||
} finally {
|
} finally {
|
||||||
tos.close();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
package com.metaweb.gridworks.commands.project;
|
package com.metaweb.gridworks.commands.project;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
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.FileItemStream;
|
||||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||||
import org.apache.commons.fileupload.util.Streams;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.metaweb.gridworks.ProjectManager;
|
import com.metaweb.gridworks.ProjectManager;
|
||||||
import com.metaweb.gridworks.ProjectMetadata;
|
import com.metaweb.gridworks.ProjectMetadata;
|
||||||
import com.metaweb.gridworks.commands.Command;
|
import com.metaweb.gridworks.commands.Command;
|
||||||
import com.metaweb.gridworks.io.FileProjectManager;
|
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.util.ParsingUtilities;
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
||||||
|
|
||||||
@ -94,7 +88,7 @@ public class ImportProjectCommand extends Command {
|
|||||||
} else {
|
} else {
|
||||||
String fileName = item.getName().toLowerCase();
|
String fileName = item.getName().toLowerCase();
|
||||||
try {
|
try {
|
||||||
internalImportInputStream(projectID, stream, !fileName.endsWith(".tar"));
|
ProjectManager.singleton.importProject(projectID, stream, !fileName.endsWith(".tar"));
|
||||||
} finally {
|
} finally {
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
@ -131,46 +125,11 @@ public class ImportProjectCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
internalImportInputStream(projectID, inputStream, !urlString.endsWith(".tar"));
|
ProjectManager.singleton.importProject(projectID, inputStream, !urlString.endsWith(".tar"));
|
||||||
} finally {
|
} finally {
|
||||||
inputStream.close();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package com.metaweb.gridworks.io;
|
package com.metaweb.gridworks.io;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@ -11,7 +15,11 @@ import java.util.Date;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
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.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
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,
|
* 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.
|
* this method is called before the project is exported to a .tar file.
|
||||||
|
Loading…
Reference in New Issue
Block a user