From ca2aec7d2fcc285976fab2b859154898365662db Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Thu, 16 Jan 2014 16:32:03 +0000
Subject: [PATCH] - fixed path to plugin in Makefile - fixed classpath in
 Makefile - moved java-file to class-dirname structure - added PLUGIN-INF/ 
 with metadata - adapted source of java file to import right classes

---
 Makefile                                      |  9 +-
 PLUGIN-INF/metadata_SLUBStoragepPlugin.xml    | 94 +++++++++++++++++++
 java/SLUBStoragePlugin.java                   |  0
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 92 ++++++++++++++++++
 4 files changed, 192 insertions(+), 3 deletions(-)
 create mode 100644 PLUGIN-INF/metadata_SLUBStoragepPlugin.xml
 delete mode 100644 java/SLUBStoragePlugin.java
 create mode 100644 java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java

diff --git a/Makefile b/Makefile
index b65a5cc..2cfad67 100644
--- a/Makefile
+++ b/Makefile
@@ -7,19 +7,21 @@
 JAVAPATH=$(wildcard /usr/lib/jvm/java-1.6.0-openjdk-*/bin/)
 
 # Verwendete Rosetta-Version
-ROSETTAVERSION=3.2.1
+ROSETTAVERSION=3.2.0
 
 # Pfad zum Rosetta-SDK
 ROSETTASDK=/exlibris/dps/d4_1/system.dir/dps-sdk-${ROSETTAVERSION}/lib/
 # Pfad zum Rosetta-SDK, Deposit-Module
 ROSETTASDKDEPOSIT=${ROSETTASDK}/../dps-sdk-projects/dps-sdk-deposit/lib
+ROSETTASDKPLUGINS=${ROSETTASDK}/../../bundled_plugins/
+
 
 # classpath
-CLASSPATH=${ROSETTASDKDEPOSIT}/../src/:${ROSETTASDKDEPOSIT}/xmlbeans-2.3.0.jar:${ROSETTASDKDEPOSIT}/dps-sdk-${ROSETTAVERSION}.jar:${ROSETTASDKDEPOSIT}/log4j-1.2.14.jar 
+CLASSPATH=${ROSETTASDKDEPOSIT}/../src/:${ROSETTASDKDEPOSIT}/xmlbeans-2.3.0.jar:${ROSETTASDKDEPOSIT}/dps-sdk-${ROSETTAVERSION}.jar:${ROSETTASDKDEPOSIT}/log4j-1.2.14.jar:${ROSETTASDKPLUGINS}/NFSStoragePlugin.jar
 
 # sources
 
-SOURCES=java/SLUBStoragePlugin.java
+SOURCES=java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
 OBJS=$(SOURCES:.java=.class)
 
 
@@ -42,6 +44,7 @@ jarclean:
 clean: jarclean
 	@rm -Rf doc/
 	@rm -f *.class *.jar *.sh
+	@rm -f $(OBJS)
 
 distclean: clean
 	@rm -f *~
diff --git a/PLUGIN-INF/metadata_SLUBStoragepPlugin.xml b/PLUGIN-INF/metadata_SLUBStoragepPlugin.xml
new file mode 100644
index 0000000..9eea254
--- /dev/null
+++ b/PLUGIN-INF/metadata_SLUBStoragepPlugin.xml
@@ -0,0 +1,94 @@
+<pl:metadata-config xmlns:pl="http://www.exlibrisgroup.com/Plugins/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
+  <pl:pluginTypeName>StoragePlugin</pl:pluginTypeName>
+  <pl:deployName>SLUBStoragePlugin</pl:deployName>
+ <pl:className>org.slub.rosetta.dps.repository.plugin.storage.nfs.SLUBStoragePlugin</pl:className>
+  <pl:initParameters>
+  <fr:x_form  xmlns:fr="http://com/exlibris/digitool/common/forms/xmlbeans">
+    <form_name>SLUBStoragePlugin_Params</form_name>
+    <description>SLUBTreeStoragePlugin_parameters_form</description>
+    <version>0.1</version>
+    <grid_x>1</grid_x>
+    <md_format>1</md_format>
+        <x_fields>
+        <x_field>
+            <field_name>DIR_ROOT</field_name>
+            <label>ui.storage.nfs.rootDir</label>
+            <ui_tool_tip>ui.storage.nfs.rootDir.tooltip</ui_tool_tip>
+            <single>false</single>
+            <mandatory>true</mandatory>
+            <x_logic_type>String</x_logic_type>
+            <x_ui_type>TextField</x_ui_type>
+            <css_class>width40</css_class>
+            <x_options></x_options>
+        </x_field>
+        <x_field>
+            <field_name>MAX_SIZE_IN_MB</field_name>
+            <label>ui.storage.nfs.storageMaxCapacity</label>
+            <ui_tool_tip>ui.storage.nfs.storageMaxCapacity.tooltip</ui_tool_tip>
+            <single>false</single>
+            <mandatory>true</mandatory>
+            <validator_class_name>com.exlibris.core.infra.web.utils.formBuilder.validator.NumericFormValidator</validator_class_name>
+            <x_logic_type>String</x_logic_type>
+            <x_ui_type>TextField</x_ui_type>
+            <css_class>width40</css_class>
+            <x_options></x_options>
+        </x_field>
+        <x_field>
+            <field_name>BLOCK_SIZE</field_name>
+            <label>ui.storage.nfs.fileBlockSize</label>
+            <ui_tool_tip>ui.storage.nfs.fileBlockSize</ui_tool_tip>
+            <single>false</single>
+            <default_value>8</default_value>
+            <mandatory>true</mandatory>
+            <validator_class_name>com.exlibris.core.infra.web.utils.formBuilder.validator.NumericFormValidator</validator_class_name>
+            <x_logic_type>String</x_logic_type>
+            <x_ui_type>TextField</x_ui_type>
+            <css_class>width40</css_class>
+            <x_options></x_options>
+        </x_field>
+        <x_field>
+            <field_name>FILES_HANDLING_METHOD</field_name>
+            <label>ui.storage.nfs.filesHandlingMethod</label>
+            <ui_tool_tip>filesHandlingMethod.filesHandlingMethod</ui_tool_tip>
+            <single>false</single>
+            <default_value>copy</default_value>
+            <pos_x>1</pos_x>
+            <pos_y>3</pos_y>
+            <mandatory>true</mandatory>
+            <x_logic_type>String</x_logic_type>
+            <x_ui_type>Radio</x_ui_type>
+            <populator_class_name>com.exlibris.core.infra.web.utils.formBuilder.populators.CodeTablePopulator</populator_class_name>
+            <x_populator_parameters>
+                <x_populator_parameter>
+                    <parameter>table</parameter>
+                    <value>FILES_HANDLING_METHOD_CODE_TABLE_PLACE_HOLDER</value>
+                </x_populator_parameter>
+            </x_populator_parameters>
+            <x_options></x_options>
+        </x_field>
+    </x_fields>
+</fr:x_form>
+</pl:initParameters>
+  <pl:description>SLUB Storage Plugin</pl:description>
+  <pl:version>1.1</pl:version>
+  <pl:materialType>DIGITAL</pl:materialType>
+  <pl:module>Repository</pl:module>
+  <pl:generalType>TASK</pl:generalType>
+  <pl:publicApi>N</pl:publicApi>
+  <pl:implType>java</pl:implType>
+  <pl:contactPerson>
+    <contact_info xmlns="http://www.exlibrisgroup.com/Plugins/1.0/">
+      <contactType>admin</contactType>
+      <firstName></firstName>
+      <lastName>SLUB Dresden</lastName>
+      <telephone1></telephone1>
+      <telephone2></telephone2>
+      <email>rosetta@slb-dresden.de</email>
+      <address1>Zellescher Weg 17</address1>
+      <address2></address2>
+      <city>Dresden</city>
+      <zipCode>01069</zipCode>
+      <country>Germany</country>
+    </contact_info>
+  </pl:contactPerson>
+</pl:metadata-config>
diff --git a/java/SLUBStoragePlugin.java b/java/SLUBStoragePlugin.java
deleted file mode 100644
index e69de29..0000000
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
new file mode 100644
index 0000000..f9d9e67
--- /dev/null
+++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
@@ -0,0 +1,92 @@
+//package com.exlibris.dps.repository.plugin.storage.nfs;
+package org.slub.rosetta.dps.repository.plugin.storage.nfs;
+ 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+import com.exlibris.dps.repository.plugin.storage.nfs.NFSStoragePlugin;
+import com.exlibris.core.infra.common.util.IOUtil;
+import com.exlibris.core.sdk.storage.containers.StoredEntityMetaData;
+import com.exlibris.digitool.common.storage.Fixity;
+import com.exlibris.digitool.infrastructure.utils.Checksummer;
+ 
+public class SLUBStoragePlugin extends NFSStoragePlugin {
+ 
+    private static final String DIR_ROOT = "DIR_ROOT";
+ 
+    public SLUBStoragePlugin() {
+        super();
+    }
+ 
+ 
+    @Override
+    public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) throws Exception {
+ 
+        String fileName = createFileName(storedEntityMetadata);
+ 
+        String relativeDirectoryPath = getStreamRelativePath(storedEntityMetadata);
+        File destFile = getStreamDirectory(relativeDirectoryPath, fileName);
+ 
+        // 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;
+ 
+        if(!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) {
+            deleteEntity(storedEntityIdentifier); // delete corrupt files
+            return null;
+        }
+ 
+        // return only relative (not absolute) path
+        return storedEntityIdentifier;
+    }
+ 
+ 
+    private String getStreamRelativePath(StoredEntityMetaData storedEntityMetaData ) throws Exception {
+ 
+        String relativeDirectoryPath = File.separator;
+        String pid = storedEntityMetaData.getEntityPid();
+        Checksummer checksummer = new Checksummer(pid, true, false, false);
+        Fixity fixity = new Fixity(Fixity.FixityAlgorithm.MD5.toString(), checksummer.getMD5());
+        String value = fixity.getValue();
+ 
+        for(int i=0 ; i<value.length() && i<32 ; i= i+2) {
+            if(i+1 < value.length()) {
+                relativeDirectoryPath += value.substring(i, i+2) + File.separator;
+            } else {
+                relativeDirectoryPath += value.substring(i, i+1) + File.separator;
+            }
+        }
+ 
+        if(32 < value.length()) {
+            relativeDirectoryPath += value.substring(32);
+        }
+ 
+        return relativeDirectoryPath;
+    }
+ 
+    private File getStreamDirectory(String path, String fileName) {
+ 
+        File newDir = new File(parameters.get(DIR_ROOT) + File.separator + path);
+        newDir.mkdirs();
+        return new File(newDir.getAbsolutePath() + File.separator + fileName);
+    }
+ 
+    private boolean canHandleSourcePath(String srcPath) {
+        try {
+            File file = new File(srcPath);
+            return file.canRead();
+        }
+        catch (Exception e) {
+            return false;
+        }
+    }
+ 
+}
-- 
GitLab