diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/NFSStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/NFSStoragePlugin.java index 642a1745716ce2c6eebb1f5fb9f94e6d543b5413..4c798a3da6d7b1775abe554228786f2083e92da3 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/NFSStoragePlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/NFSStoragePlugin.java @@ -6,6 +6,7 @@ import com.exlibris.core.infra.common.util.IOUtil; import com.exlibris.core.infra.svc.api.scriptRunner.ExecExternalProcess; import com.exlibris.core.sdk.storage.containers.StoredEntityMetaData; import com.exlibris.core.sdk.storage.handler.AbstractStorageHandler; +import com.exlibris.core.sdk.storage.handler.StorageUtil; import com.exlibris.core.sdk.utils.FileUtil; import com.exlibris.digitool.common.storage.Fixity; import com.exlibris.digitool.common.storage.Fixity.FixityAlgorithm; @@ -19,6 +20,7 @@ import java.io.RandomAccessFile; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -32,10 +34,12 @@ extends AbstractStorageHandler private static final String DIR_ROOT = "DIR_ROOT"; private static final String FILES_HANDLING_METHOD = "FILES_HANDLING_METHOD"; private static final ExLogger log = ExLogger.getExLogger(NFSStoragePlugin.class); + private final String RELATIVE_DIRECTORY_PATH = "relativeDirectoryPath"; + private final String DEST_FILE_PATH = "destFilePath"; public boolean deleteEntity(String storedEntityIdentifier) { - File file = new File((String)parameters.get("DIR_ROOT") + storedEntityIdentifier); + File file = new File((String)this.parameters.get("DIR_ROOT") + storedEntityIdentifier); try { return file.delete(); @@ -50,35 +54,73 @@ extends AbstractStorageHandler public InputStream retrieveEntity(String storedEntityIdentifier) throws IOException { - return new FileInputStream((String)parameters.get("DIR_ROOT") + storedEntityIdentifier); + return retrieveEntity(storedEntityIdentifier, true); + } + + public InputStream retrieveEntity(String storedEntityIdentifier, boolean isRelative) + throws IOException + { + return new FileInputStream((isRelative ? (String)this.parameters.get("DIR_ROOT") : "") + storedEntityIdentifier); } public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) throws Exception { - String fileName = createFileName(storedEntityMetadata); + String existsDescPath = getFilePathInDescIfExists(storedEntityMetadata); - String relativeDirectoryPath = getStreamRelativePath(); - File destFile = getStreamDirectory(relativeDirectoryPath, fileName); - if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) - { - is.close(); - copyStream(storedEntityMetadata.getCurrentFilePath(), destFile.getAbsolutePath()); - } - else + String destFilePath = null; + + boolean isCopyFileNeeded = true; + if (existsDescPath != null) { - IOUtil.copy(is, new FileOutputStream(destFile)); + destFilePath = existsDescPath; + isCopyFileNeeded = !checkFixity(storedEntityMetadata.getFixities(), destFilePath, false); } - String storedEntityIdentifier = relativeDirectoryPath + getNextDir(destFile.getAbsolutePath()) + File.separator + fileName; - if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) + Map<String, String> paths = getStoreEntityIdentifier(storedEntityMetadata, destFilePath); + String storedEntityIdentifier = (String)paths.get("relativeDirectoryPath"); + destFilePath = (String)paths.get("destFilePath"); + if (isCopyFileNeeded) { - deleteEntity(storedEntityIdentifier); - return null; + if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) + { + if (is != null) { + is.close(); + } + copyStream(storedEntityMetadata, destFilePath); + } + else + { + log.info("Cannot handle source path: " + storedEntityMetadata.getCurrentFilePath()); + if (is == null) + { + log.warn("InputStream is null", new String[0]); + return null; + } + FileOutputStream output = null; + try + { + output = new FileOutputStream(new File(destFilePath)); + IOUtil.copy(is, output); + } + finally + { + IOUtil.closeQuietly(output); + } + } + if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) { + return null; + } } return storedEntityIdentifier; } public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier) + throws Exception + { + return checkFixity(fixities, storedEntityIdentifier, true); + } + + public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier, boolean isRelativePath) throws Exception { boolean result = true; @@ -105,7 +147,7 @@ extends AbstractStorageHandler else { String oldValue = fixity.getValue(); - fixity.setValue(getChecksumUsingPlugin(getLocalFilePath(storedEntityIdentifier), fixity.getPluginName(), oldValue)); + fixity.setValue(getChecksumUsingPlugin(isRelativePath ? getLocalFilePath(storedEntityIdentifier) : storedEntityIdentifier, fixity.getPluginName(), oldValue)); fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); result &= fixity.getResult().booleanValue(); } @@ -115,7 +157,7 @@ extends AbstractStorageHandler InputStream is = null; try { - is = retrieveEntity(storedEntityIdentifier); + is = retrieveEntity(storedEntityIdentifier, isRelativePath); checksummer = new Checksummer(is, calcMD5, calcSHA1, calcCRC32); for (Fixity fixity : fixities) { @@ -124,7 +166,7 @@ extends AbstractStorageHandler { String oldValue = fixity.getValue(); fixity.setValue(checksummer.getChecksum(fixity.getAlgorithm())); - fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); + fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equalsIgnoreCase(fixity.getValue())))); result &= fixity.getResult().booleanValue(); } } @@ -152,33 +194,48 @@ extends AbstractStorageHandler return -1; } - private String getStreamRelativePath() + private String getStreamRelativePath(String destFilePath) { - String relativeDirectoryPath = ""; - Date date = new Date(); - - relativeDirectoryPath = relativeDirectoryPath + File.separator; - relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("yyyy").format(date); - relativeDirectoryPath = relativeDirectoryPath + File.separator; - relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("MM").format(date); - relativeDirectoryPath = relativeDirectoryPath + File.separator; - relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("dd").format(date); - relativeDirectoryPath = relativeDirectoryPath + File.separator; - - return relativeDirectoryPath; + StringBuffer relativeDirectoryPath = new StringBuffer(); + + String year = null;String month = null;String day = null; + if (destFilePath == null) + { + Date date = new Date(); + year = new SimpleDateFormat("yyyy").format(date); + month = new SimpleDateFormat("MM").format(date); + day = new SimpleDateFormat("dd").format(date); + } + else + { + String nextDir = getNextDir(destFilePath); + String[] splitted = destFilePath.split(nextDir); + splitted = StringUtils.split(splitted[0], File.separator); + day = splitted[(splitted.length - 1)]; + month = splitted[(splitted.length - 2)]; + year = splitted[(splitted.length - 3)]; + } + relativeDirectoryPath.append(File.separator).append(year).append(File.separator).append(month).append(File.separator).append(day).append(File.separator); + + + + + + + return relativeDirectoryPath.toString(); } private File getStreamDirectory(String path, String fileName) { String directoryPrefix = "fileset_"; int maxFilesPerDir = 100; - if (!StringUtils.isEmpty((String)parameters.get("DIR_PREFIX"))) { - directoryPrefix = (String)parameters.get("DIR_PREFIX"); + if (!StringUtils.isEmpty((String)this.parameters.get("DIR_PREFIX"))) { + directoryPrefix = (String)this.parameters.get("DIR_PREFIX"); } - if (!StringUtils.isEmpty((String)parameters.get("FILE_PER_DIR"))) { - maxFilesPerDir = Integer.valueOf((String)parameters.get("FILE_PER_DIR")).intValue(); + if (!StringUtils.isEmpty((String)this.parameters.get("FILE_PER_DIR"))) { + maxFilesPerDir = Integer.valueOf((String)this.parameters.get("FILE_PER_DIR")).intValue(); } - File newDir = new File((String)parameters.get("DIR_ROOT") + File.separator + path); + File newDir = new File((String)this.parameters.get("DIR_ROOT") + File.separator + path); newDir.mkdirs(); File destDir = FileUtil.getNextDirectory(newDir, directoryPrefix, maxFilesPerDir); @@ -202,14 +259,19 @@ extends AbstractStorageHandler return false; } - protected void copyStream(String srcPath, String destPath) + protected void copyStream(StoredEntityMetaData storedEntityMetadata, String destPath) throws IOException { - String filesHandlingMethod = (String)parameters.get("FILES_HANDLING_METHOD"); + String filesHandlingMethod = (String)this.parameters.get("FILES_HANDLING_METHOD"); + String srcPath = storedEntityMetadata.getCurrentFilePath(); + + String pid = storedEntityMetadata.getEntityPid(); + String iePid = storedEntityMetadata.getIePid(); if ("move".equalsIgnoreCase(filesHandlingMethod)) { File canonicalSrcFile = getCanonicalFile(srcPath); FileUtil.moveFile(canonicalSrcFile, new File(destPath)); + saveDestPathsTmpFile(iePid, pid, destPath); } else if ("soft_link".equalsIgnoreCase(filesHandlingMethod)) { @@ -222,17 +284,47 @@ extends AbstractStorageHandler else { FileUtil.copyFile(srcPath, destPath); + saveDestPathsTmpFile(iePid, pid, destPath); } } + private void saveDestPathsTmpFile(String folder, String key, String path) + { + if (folder == null) { + return; + } + String tmpFilePath = getTempStorageDirectory(false) + "destPath"; + + + File destPathDir = new File(getTempStorageDirectory(false) + "destPath" + File.separator); + if (!destPathDir.exists()) { + destPathDir.mkdirs(); + } + StorageUtil.saveDestPathToTmpFile(folder, tmpFilePath, key, path); + } + + protected String getFilePathInDescIfExists(StoredEntityMetaData storedEntityMetadata) + { + String tmpFilePath = getTempStorageDirectory(false) + "destPath"; + if (storedEntityMetadata.getIePid() == null) { + return null; + } + String existsDescPath = StorageUtil.readDestPathFromTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid()); + return existsDescPath; + } + private File getCanonicalFile(String srcPath) - throws IOException + { + String fileName = srcPath.split("\\" + File.separator)[(srcPath.split("\\" + File.separator).length - 1)]; + File canonicalSrcDir = null; + try { - String fileName = srcPath.split("\\" + File.separator)[(srcPath.split("\\" + File.separator).length - 1)]; - File canonicalSrcDir = new File(srcPath).getParentFile().getCanonicalFile(); - File canonicalSrcFile = new File(canonicalSrcDir, fileName).getCanonicalFile(); - return canonicalSrcFile; + canonicalSrcDir = new File(srcPath).getParentFile().getCanonicalFile(); + return new File(canonicalSrcDir, fileName).getCanonicalFile(); } + catch (IOException e) {} + return null; + } private void hardLink(String srcPath, String destPath) throws IOException @@ -269,7 +361,7 @@ extends AbstractStorageHandler public String getFullFilePath(String storedEntityIdentifier) { - return (String)parameters.get("DIR_ROOT") + storedEntityIdentifier; + return (String)this.parameters.get("DIR_ROOT") + storedEntityIdentifier; } public String getLocalFilePath(String storedEntityIdentifier) @@ -281,21 +373,21 @@ extends AbstractStorageHandler { try { - File file = new File((String)parameters.get("DIR_ROOT")); - if ((!file.exists()) && + File file = new File((String)this.parameters.get("DIR_ROOT")); + if ((!file.exists()) && (!file.mkdirs())) { - log.error("No access to folder" + (String)parameters.get("DIR_ROOT"), new String[0]); + log.error("No access to folder" + (String)this.parameters.get("DIR_ROOT"), new String[0]); return false; } if (!file.canRead()) { - log.error("No read access to folder: " + (String)parameters.get("DIR_ROOT"), new String[0]); + log.error("No read access to folder: " + (String)this.parameters.get("DIR_ROOT"), new String[0]); return false; } if (!file.canWrite()) { - log.error("No write access to folder: " + (String)parameters.get("DIR_ROOT"), new String[0]); + log.error("No write access to folder: " + (String)this.parameters.get("DIR_ROOT"), new String[0]); return false; } } @@ -314,7 +406,7 @@ extends AbstractStorageHandler RandomAccessFile file = null; try { - file = new RandomAccessFile((String)parameters.get("DIR_ROOT") + storedEntityIdentifier, "r"); + file = new RandomAccessFile((String)this.parameters.get("DIR_ROOT") + storedEntityIdentifier, "r"); file.seek(start); file.readFully(bytes, 0, (int)(end - start + 1L)); @@ -342,5 +434,20 @@ extends AbstractStorageHandler } } } -} + private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, String destFilePath) + { + Map<String, String> paths = new HashMap(); + + String fileName = createFileName(storedEntityMetadata); + String relativeDirectoryPath = getStreamRelativePath(destFilePath); + if (destFilePath == null) + { + File destFile = getStreamDirectory(relativeDirectoryPath, fileName); + destFilePath = destFile.getAbsolutePath(); + } + paths.put("destFilePath", destFilePath); + paths.put("relativeDirectoryPath", relativeDirectoryPath + getNextDir(destFilePath) + File.separator + fileName); + return paths; + } +}