From 292a84dbda355b7e46560234dc52a71c45ac25e5 Mon Sep 17 00:00:00 2001 From: Andreas Romeyke <andreas.romeyke@slub-dresden.de> Date: Fri, 25 Jul 2014 10:59:35 +0000 Subject: [PATCH] - reindented with gvim, to enable better compare with new version --- Makefile | 2 +- .../plugin/storage/nfs/NFSStoragePlugin.java | 324 +++++++++--------- .../plugin/storage/nfs/SLUBStoragePlugin.java | 41 --- 3 files changed, 163 insertions(+), 204 deletions(-) diff --git a/Makefile b/Makefile index de6e476..8c9fa51 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ JAVAPATH=$(wildcard /usr/lib/jvm/java-1.6.0-openjdk-*/bin/) # Verwendete Rosetta-Version -ROSETTAVERSION=3.2.2 +ROSETTAVERSION=4.0.0 # Pfad zum Rosetta-SDK ROSETTASDK=/exlibris/dps/d4_1/system.dir/dps-sdk-${ROSETTAVERSION}/lib/ 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 56b377e..642a174 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 @@ -25,14 +25,14 @@ import java.util.Map; import org.apache.commons.lang.StringUtils; public class NFSStoragePlugin - extends AbstractStorageHandler +extends AbstractStorageHandler { private static final String DIR_PREFIX = "DIR_PREFIX"; private static final String FILE_PER_DIR = "FILE_PER_DIR"; 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); - + public boolean deleteEntity(String storedEntityIdentifier) { File file = new File((String)parameters.get("DIR_ROOT") + storedEntityIdentifier); @@ -46,101 +46,101 @@ public class NFSStoragePlugin } return true; } - + public InputStream retrieveEntity(String storedEntityIdentifier) throws IOException - { - return new FileInputStream((String)parameters.get("DIR_ROOT") + storedEntityIdentifier); - } - - public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) - throws Exception - { - String fileName = createFileName(storedEntityMetadata); - - String relativeDirectoryPath = getStreamRelativePath(); - File destFile = getStreamDirectory(relativeDirectoryPath, fileName); - if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) { - is.close(); - copyStream(storedEntityMetadata.getCurrentFilePath(), destFile.getAbsolutePath()); + return new FileInputStream((String)parameters.get("DIR_ROOT") + storedEntityIdentifier); } - else - { - IOUtil.copy(is, new FileOutputStream(destFile)); - } - String storedEntityIdentifier = relativeDirectoryPath + getNextDir(destFile.getAbsolutePath()) + File.separator + fileName; - if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) + + public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) + throws Exception { - deleteEntity(storedEntityIdentifier); - return null; + String fileName = createFileName(storedEntityMetadata); + + String relativeDirectoryPath = getStreamRelativePath(); + File destFile = getStreamDirectory(relativeDirectoryPath, fileName); + if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) + { + is.close(); + copyStream(storedEntityMetadata.getCurrentFilePath(), destFile.getAbsolutePath()); + } + else + { + IOUtil.copy(is, new FileOutputStream(destFile)); + } + String storedEntityIdentifier = relativeDirectoryPath + getNextDir(destFile.getAbsolutePath()) + File.separator + fileName; + if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) + { + deleteEntity(storedEntityIdentifier); + return null; + } + return storedEntityIdentifier; } - return storedEntityIdentifier; - } - + public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier) throws Exception - { - boolean result = true; - if (fixities != null) { - boolean calcMD5 = false; - boolean calcSHA1 = false; - boolean calcCRC32 = false; - for (Fixity fixity : fixities) + boolean result = true; + if (fixities != null) { - fixity.setResult(null); - if (Fixity.FixityAlgorithm.MD5.toString().equals(fixity.getAlgorithm())) - { - calcMD5 = true; - } - else if (Fixity.FixityAlgorithm.SHA1.toString().equals(fixity.getAlgorithm())) + boolean calcMD5 = false; + boolean calcSHA1 = false; + boolean calcCRC32 = false; + for (Fixity fixity : fixities) { - calcSHA1 = true; - } - else if (Fixity.FixityAlgorithm.CRC32.toString().equals(fixity.getAlgorithm())) - { - calcCRC32 = true; - } - else - { - String oldValue = fixity.getValue(); - fixity.setValue(getChecksumUsingPlugin(getLocalFilePath(storedEntityIdentifier), fixity.getPluginName(), oldValue)); - fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); - result &= fixity.getResult().booleanValue(); + fixity.setResult(null); + if (Fixity.FixityAlgorithm.MD5.toString().equals(fixity.getAlgorithm())) + { + calcMD5 = true; + } + else if (Fixity.FixityAlgorithm.SHA1.toString().equals(fixity.getAlgorithm())) + { + calcSHA1 = true; + } + else if (Fixity.FixityAlgorithm.CRC32.toString().equals(fixity.getAlgorithm())) + { + calcCRC32 = true; + } + else + { + String oldValue = fixity.getValue(); + fixity.setValue(getChecksumUsingPlugin(getLocalFilePath(storedEntityIdentifier), fixity.getPluginName(), oldValue)); + fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); + result &= fixity.getResult().booleanValue(); + } } - } - if ((calcMD5) || (calcSHA1) || (calcCRC32)) - { - InputStream is = null; - try + if ((calcMD5) || (calcSHA1) || (calcCRC32)) { - is = retrieveEntity(storedEntityIdentifier); - checksummer = new Checksummer(is, calcMD5, calcSHA1, calcCRC32); - for (Fixity fixity : fixities) + InputStream is = null; + try { - int checksummerAlgorithmIndex = getChecksummerAlgorithmIndex(fixity.getAlgorithm()); - if (checksummerAlgorithmIndex != -1) + is = retrieveEntity(storedEntityIdentifier); + checksummer = new Checksummer(is, calcMD5, calcSHA1, calcCRC32); + for (Fixity fixity : fixities) { - String oldValue = fixity.getValue(); - fixity.setValue(checksummer.getChecksum(fixity.getAlgorithm())); - fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); - result &= fixity.getResult().booleanValue(); + int checksummerAlgorithmIndex = getChecksummerAlgorithmIndex(fixity.getAlgorithm()); + if (checksummerAlgorithmIndex != -1) + { + String oldValue = fixity.getValue(); + fixity.setValue(checksummer.getChecksum(fixity.getAlgorithm())); + fixity.setResult(Boolean.valueOf((oldValue == null) || (oldValue.equals(fixity.getValue())))); + result &= fixity.getResult().booleanValue(); + } } } - } - finally - { - Checksummer checksummer; - if (is != null) { - is.close(); + finally + { + Checksummer checksummer; + if (is != null) { + is.close(); + } } } } + return result; } - return result; - } - + private int getChecksummerAlgorithmIndex(String algorithm) { try @@ -151,12 +151,12 @@ public class NFSStoragePlugin catch (Exception e) {} return -1; } - + private String getStreamRelativePath() { String relativeDirectoryPath = ""; Date date = new Date(); - + relativeDirectoryPath = relativeDirectoryPath + File.separator; relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("yyyy").format(date); relativeDirectoryPath = relativeDirectoryPath + File.separator; @@ -164,10 +164,10 @@ public class NFSStoragePlugin relativeDirectoryPath = relativeDirectoryPath + File.separator; relativeDirectoryPath = relativeDirectoryPath + new SimpleDateFormat("dd").format(date); relativeDirectoryPath = relativeDirectoryPath + File.separator; - + return relativeDirectoryPath; } - + private File getStreamDirectory(String path, String fileName) { String directoryPrefix = "fileset_"; @@ -181,16 +181,16 @@ public class NFSStoragePlugin File newDir = new File((String)parameters.get("DIR_ROOT") + File.separator + path); newDir.mkdirs(); File destDir = FileUtil.getNextDirectory(newDir, directoryPrefix, maxFilesPerDir); - + return new File(destDir.getAbsolutePath() + File.separator + fileName); } - + private String getNextDir(String fullPath) { String[] dirs = fullPath.split("\\" + File.separator); return dirs[(dirs.length - 2)]; } - + private boolean canHandleSourcePath(String srcPath) { try @@ -201,89 +201,89 @@ public class NFSStoragePlugin catch (Exception e) {} return false; } - + protected void copyStream(String srcPath, String destPath) throws IOException - { - String filesHandlingMethod = (String)parameters.get("FILES_HANDLING_METHOD"); - if ("move".equalsIgnoreCase(filesHandlingMethod)) { - File canonicalSrcFile = getCanonicalFile(srcPath); - FileUtil.moveFile(canonicalSrcFile, new File(destPath)); - } - else if ("soft_link".equalsIgnoreCase(filesHandlingMethod)) - { - softLink(srcPath, destPath); - } - else if ("hard_link".equalsIgnoreCase(filesHandlingMethod)) - { - hardLink(srcPath, destPath); - } - else - { - FileUtil.copyFile(srcPath, destPath); + String filesHandlingMethod = (String)parameters.get("FILES_HANDLING_METHOD"); + if ("move".equalsIgnoreCase(filesHandlingMethod)) + { + File canonicalSrcFile = getCanonicalFile(srcPath); + FileUtil.moveFile(canonicalSrcFile, new File(destPath)); + } + else if ("soft_link".equalsIgnoreCase(filesHandlingMethod)) + { + softLink(srcPath, destPath); + } + else if ("hard_link".equalsIgnoreCase(filesHandlingMethod)) + { + hardLink(srcPath, destPath); + } + else + { + FileUtil.copyFile(srcPath, destPath); + } } - } - + private File getCanonicalFile(String srcPath) throws IOException - { - 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; - } - + { + 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; + } + private void hardLink(String srcPath, String destPath) throws IOException - { - String command = "ln"; - ExecExternalProcess proc = new ExecExternalProcess(); - List<String> args = new LinkedList(); - args.add(srcPath); - args.add(destPath); - int retValue = proc.execExternalProcess(command, args); - if (retValue != 0) { - throw new IOException("ln " + srcPath + " " + destPath + " failed " + proc.getErrorStream() + proc.getInputStream()); + { + String command = "ln"; + ExecExternalProcess proc = new ExecExternalProcess(); + List<String> args = new LinkedList(); + args.add(srcPath); + args.add(destPath); + int retValue = proc.execExternalProcess(command, args); + if (retValue != 0) { + throw new IOException("ln " + srcPath + " " + destPath + " failed " + proc.getErrorStream() + proc.getInputStream()); + } } - } - + private void softLink(String srcPath, String destPath) throws IOException - { - File source = new File(srcPath); - if (!source.exists()) { - throw new IOException("File " + source + " does not exist"); - } - String command = "ln"; - ExecExternalProcess proc = new ExecExternalProcess(); - List<String> args = new ArrayList(); - args.add("-s"); - args.add(srcPath); - args.add(destPath); - int retValue = proc.execExternalProcess(command, args); - if (retValue != 0) { - throw new IOException("ln -s " + srcPath + " " + destPath + " failed " + proc.getErrorStream() + proc.getInputStream()); + { + File source = new File(srcPath); + if (!source.exists()) { + throw new IOException("File " + source + " does not exist"); + } + String command = "ln"; + ExecExternalProcess proc = new ExecExternalProcess(); + List<String> args = new ArrayList(); + args.add("-s"); + args.add(srcPath); + args.add(destPath); + int retValue = proc.execExternalProcess(command, args); + if (retValue != 0) { + throw new IOException("ln -s " + srcPath + " " + destPath + " failed " + proc.getErrorStream() + proc.getInputStream()); + } } - } - + public String getFullFilePath(String storedEntityIdentifier) { return (String)parameters.get("DIR_ROOT") + storedEntityIdentifier; } - + public String getLocalFilePath(String storedEntityIdentifier) { return getFullFilePath(storedEntityIdentifier); } - + public boolean isAvailable() { try { File file = new File((String)parameters.get("DIR_ROOT")); if ((!file.exists()) && - (!file.mkdirs())) + (!file.mkdirs())) { log.error("No access to folder" + (String)parameters.get("DIR_ROOT"), new String[0]); return false; @@ -306,18 +306,17 @@ public class NFSStoragePlugin } return true; } - + public byte[] retrieveEntityByRange(String storedEntityIdentifier, long start, long end) throws Exception - { - bytes = new byte[(int)(end - start + 1L)]; - RandomAccessFile file = null; - try { - file = new RandomAccessFile((String)parameters.get("DIR_ROOT") + storedEntityIdentifier, "r"); - file.seek(start); - file.readFully(bytes, 0, (int)(end - start + 1L)); - + bytes = new byte[(int)(end - start + 1L)]; + RandomAccessFile file = null; + try + { + file = new RandomAccessFile((String)parameters.get("DIR_ROOT") + storedEntityIdentifier, "r"); + file.seek(start); + file.readFully(bytes, 0, (int)(end - start + 1L)); @@ -326,21 +325,22 @@ public class NFSStoragePlugin - return bytes; - } - finally - { - if (file != null) { - try - { - file.close(); - } - catch (Exception e) - { - log.warn("Failed closing file", new String[0]); + + return bytes; + } + finally + { + if (file != null) { + try + { + file.close(); + } + catch (Exception e) + { + log.warn("Failed closing file", new String[0]); + } } } } - } } diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java index f02cab8..920b889 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java @@ -65,47 +65,6 @@ public class SLUBStoragePlugin extends NFSStoragePlugin { log.info("SLUBStoragePlugin instantiated"); } - /** copied from NFS Storage Plugin, enhanced with debugging info, - * stores InputStream on Storage in given format and does fixity check - * to see if written sucessfully - * @param is InputStream - * @param storedEntityMetadata storedEntityMetaData - * @return relative path to file - */ - @Override - public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) throws Exception { - log.info("SLUBStoragePlugin.storeEntity() called"); - String fileName = createFileName(storedEntityMetadata); - log.info("SLUBStoragePlugin.storeEntity() fileName="+fileName); - String relativeDirectoryPath = getStreamRelativePath(storedEntityMetadata); - log.info("SLUBStoragePlugin.storeEntity() relativeDirectoryPath="+relativeDirectoryPath); - File destFile = getStreamDirectory(relativeDirectoryPath, fileName); - log.info("SLUBStoragePlugin.storeEntity() destfile.getAbsolutePath()="+destFile.getAbsolutePath()); - - // debug - // List<Fixity> fixities = getAvailableFixities( storedEntityMetadata ); - - // better move/link - if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath())) { - is.close(); // close input stream so that 'move' can work, we don't use it anyway - copyStream(storedEntityMetadata.getCurrentFilePath(), destFile.getAbsolutePath()); - } - // default way - copy from input stream - else { - IOUtil.copy(is, new FileOutputStream(destFile)); - } - String storedEntityIdentifier = relativeDirectoryPath + File.separator + fileName; - log.info("SLUBStoragePlugin.storeEntity() storedEntityIdentifier="+storedEntityIdentifier); - // check if stored correctly - if(!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) { - log.error("error, SLUBStoragePlugin.storeEntity() has written corrupted files (checked via checkFixity()), storedEntityIdentifier="+storedEntityIdentifier); - deleteEntity(storedEntityIdentifier); // delete corrupt files - return null; - } - // return only relative (not absolute) path - return storedEntityIdentifier; - } - /** prepare right path * path should be of form yyyy/MM/dd/IE-PID/ * we need to findout the associated dnx document (IE), -- GitLab