diff --git a/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java b/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java index 7fd3dd2b8d..55763ca9fe 100644 --- a/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java +++ b/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java @@ -397,7 +397,7 @@ public class ModuleManagementTool implements LogOutput * @see ModuleManagementTool#installModule(String, String, boolean, boolean, boolean) * * @param warFile the location of the AMP file to be installed - * @param warFileLocation the location of the WAR file into which the AMP file is to be installed + * @param backupWAR true if you want it to perform the backup */ private void backupWar(TFile warFile, boolean backupWAR) throws IOException { @@ -405,24 +405,7 @@ public class ModuleManagementTool implements LogOutput // Make a backup of the war we are going to modify if (backupWAR == true) { - - String backupLocation = warFile.getAbsolutePath()+"-" + System.currentTimeMillis() + ".bak"; - - if (warFile.isArchive()) - { - outputVerboseMessage("Backing up WAR file..."); - TFile source = new TFile(warFile.getAbsolutePath(), TArchiveDetector.NULL); - TFile backup = new TFile(backupLocation, TArchiveDetector.NULL); - source.cp_rp(backup); //Just copy the file - } - else - { - outputVerboseMessage("Backing up war DIRECTORY..."); - TFile backup = new TFile(backupLocation); - warFile.cp_rp(backup); //Copy the directory - } - outputVerboseMessage("WAR has been backed up to '" + backupLocation + "'"); - + warHelper.backup(warFile); } } diff --git a/source/java/org/alfresco/repo/module/tool/WarHelper.java b/source/java/org/alfresco/repo/module/tool/WarHelper.java index 8bd7de8ef3..f7e0daacbb 100644 --- a/source/java/org/alfresco/repo/module/tool/WarHelper.java +++ b/source/java/org/alfresco/repo/module/tool/WarHelper.java @@ -4,7 +4,9 @@ import org.alfresco.service.cmr.module.ModuleDetails; import de.schlichtherle.truezip.file.TFile; +import java.io.IOException; import java.util.List; +import java.util.jar.Manifest; /** * Performs various actions on a war file or exploded war directory @@ -37,7 +39,7 @@ public interface WarHelper * @param war a valid war file or exploded directory from a war */ public void checkCompatibleVersion(TFile war, ModuleDetails installingModuleDetails); - + /** * This checks to see if the module that is being installed is compatible with the war. * If not module edition is specfied then it will just return. However, if an edition is specified and it doesn't match @@ -65,4 +67,23 @@ public interface WarHelper * @throws ModuleManagementToolException */ List listModules(TFile war); + + /** + * Backs up a given file or directory. + * + * @since 5.1 + * @param file the file to backup + * @return the absolute path to the backup file. + */ + public String backup(TFile file) throws IOException; + + /** + * Finds a war manifest file. + * @since 5.1 + * @param war the war + * @return Manifest + * @throws ModuleManagementToolException + */ + public Manifest findManifest(TFile war) throws ModuleManagementToolException; + } diff --git a/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java b/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java index e635217b3e..a0e0d97294 100644 --- a/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java +++ b/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java @@ -1,5 +1,6 @@ package org.alfresco.repo.module.tool; +import de.schlichtherle.truezip.file.TArchiveDetector; import de.schlichtherle.truezip.file.TFile; import de.schlichtherle.truezip.file.TFileInputStream; import org.alfresco.error.AlfrescoRuntimeException; @@ -110,20 +111,33 @@ public class WarHelperImpl implements WarHelper * @throws ModuleManagementToolException */ protected String findManifestArtibute(TFile war, String attributeName) throws ModuleManagementToolException { - - InputStream is = null; - - try - { - is = new TFileInputStream(war+MANIFEST_FILE); - Manifest manifest = new Manifest(is); - Attributes attribs = manifest.getMainAttributes(); - return attribs.getValue(attributeName); - } - catch (IOException e) - { - throw new ModuleManagementToolException("Unabled to read a manifest for the war file: "+ war); - } + + Manifest manifest = findManifest(war); + Attributes attribs = manifest.getMainAttributes(); + return attribs.getValue(attributeName); + } + + /** + * Finds a single attribute from a war manifest file. + * @param war the war + * @return Manifest + * @throws ModuleManagementToolException + */ + @Override + public Manifest findManifest(TFile war) throws ModuleManagementToolException { + + InputStream is = null; + + try + { + is = new TFileInputStream(war+MANIFEST_FILE); + Manifest manifest = new Manifest(is); + return manifest; + } + catch (IOException e) + { + throw new ModuleManagementToolException("Unabled to read a manifest for the war file: "+ war); + } finally { if (is != null) @@ -131,8 +145,7 @@ public class WarHelperImpl implements WarHelper try { is.close(); } catch (Throwable e ) {} } } - } - + } /** * Compares the version information with the module details to see if their valid. If they are invalid then it throws an exception. @@ -335,6 +348,36 @@ public class WarHelperImpl implements WarHelper return moduleDetails; } + /** + * Backs up a given file or directory. + * + * @param file the file to backup + * @return the absolute path to the backup file. + */ + @Override + public String backup(TFile file) throws IOException + { + + String backupLocation = file.getAbsolutePath()+"-" + System.currentTimeMillis() + ".bak"; + + if (file.isArchive()) + { + log.info("Backing up file..."); + TFile source = new TFile(file.getAbsolutePath(), TArchiveDetector.NULL); + TFile backup = new TFile(backupLocation, TArchiveDetector.NULL); + source.cp_rp(backup); //Just copy the file + } + else + { + log.info("Backing up DIRECTORY..."); + TFile backup = new TFile(backupLocation); + file.cp_rp(backup); //Copy the directory + } + log.info("The back up is at '" + backupLocation + "'"); + + return backupLocation; + } + /** * Gets the module details for the specified module from the war. * @param war a valid war file or exploded directory from a war diff --git a/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java b/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java index e71426aad5..fc3ee2b851 100644 --- a/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java +++ b/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java @@ -28,6 +28,7 @@ import org.springframework.util.FileCopyUtils; import java.io.*; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -387,7 +388,7 @@ public class ModuleManagementToolTest extends TestCase assertEquals(details.size(), 3); //Sort them by installation date - details.sort(new Comparator() { + Collections.sort(details, new Comparator() { @Override public int compare(ModuleDetails a, ModuleDetails b) { return a.getInstallDate().compareTo(b.getInstallDate()); diff --git a/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java b/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java index 63be7979ea..9b6685f92c 100644 --- a/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java +++ b/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java @@ -17,6 +17,7 @@ import org.springframework.util.FileCopyUtils; import java.io.*; import java.util.List; import java.util.Properties; +import java.util.jar.Manifest; import static org.junit.Assert.*; @@ -290,6 +291,25 @@ public class WarHelperImplTest extends WarHelperImpl } + + @Test + public void testfindManifest() throws Exception { + //Now check the compatible versions using the manifest + TFile theWar = getFile(".war", "module/share-3.4.11.war"); + Manifest manifest = this.findManifest(theWar); + + assertNotNull(manifest); + assertEquals("Alfresco Share Enterprise", manifest.getMainAttributes().getValue(MANIFEST_IMPLEMENTATION_TITLE)); + assertEquals("3.4.11", manifest.getMainAttributes().getValue(MANIFEST_SPECIFICATION_VERSION)); + + theWar = getFile(".war", "module/alfresco-4.2.a.war"); + manifest = this.findManifest(theWar); + + assertNotNull(manifest); + assertEquals("Alfresco Repository Community", manifest.getMainAttributes().getValue(MANIFEST_IMPLEMENTATION_TITLE)); + assertEquals("4.2.a", manifest.getMainAttributes().getValue(MANIFEST_SPECIFICATION_VERSION)); + } + @Test public void testListModules() throws Exception {