diff --git a/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java b/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java index 7a323c9c8f..7fd3dd2b8d 100644 --- a/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java +++ b/source/java/org/alfresco/repo/module/tool/ModuleManagementTool.java @@ -18,28 +18,20 @@ */ package org.alfresco.repo.module.tool; -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Date; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; - +import de.schlichtherle.truezip.file.*; +import de.schlichtherle.truezip.fs.archive.zip.JarDriver; +import de.schlichtherle.truezip.socket.sl.IOPoolLocator; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.module.ModuleVersionNumber; import org.alfresco.service.cmr.module.ModuleDetails; import org.alfresco.service.cmr.module.ModuleInstallState; -import org.alfresco.util.VersionNumber; import org.safehaus.uuid.UUIDGenerator; -import de.schlichtherle.truezip.file.TArchiveDetector; -import de.schlichtherle.truezip.file.TConfig; -import de.schlichtherle.truezip.file.TFile; -import de.schlichtherle.truezip.file.TFileInputStream; -import de.schlichtherle.truezip.file.TVFS; -import de.schlichtherle.truezip.fs.archive.zip.JarDriver; -import de.schlichtherle.truezip.socket.sl.IOPoolLocator; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.*; +import java.util.Map.Entry; /** * Module management tool. @@ -178,13 +170,13 @@ public class ModuleManagementTool implements LogOutput } } - /** - * Installs a given AMP file into a given WAR file. - * - * @see ModuleManagementTool#installModule(String, String, boolean, boolean, boolean) - * - * @param ampFileLocation 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 + /** + * Installs a given AMP file into a given WAR file. + * + * @see ModuleManagementTool#installModule(String, String, boolean, boolean, boolean) + * + * @param ampFileLocation 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 */ public void installModule(String ampFileLocation, String warFileLocation) { @@ -399,6 +391,14 @@ public class ModuleManagementTool implements LogOutput return dirChanges; } + /** + * Backsup a given WAR file. + * + * @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 + */ private void backupWar(TFile warFile, boolean backupWAR) throws IOException { @@ -664,69 +664,27 @@ public class ModuleManagementTool implements LogOutput */ public void listModules(String warLocation) { - ModuleDetails moduleDetails = null; boolean previous = this.verbose; this.verbose = true; - boolean moduleFound = false; - + try { - TFile moduleDir = new TFile(warLocation + WarHelper.MODULE_NAMESPACE_DIR); - if (moduleDir.exists() == false) + List modulesFound = warHelper.listModules(new TFile(warLocation)); + + if (modulesFound.size() < 1) { outputVerboseMessage("No modules are installed in this WAR file"); } - - java.io.File[] dirs = moduleDir.listFiles(); - if (dirs != null && dirs.length != 0) - { - for (java.io.File dir : dirs) - { - if (dir.isDirectory() == true) - { - TFile moduleProperties = new TFile(dir.getPath() + WarHelper.MODULE_CONFIG_IN_WAR); - if (moduleProperties.exists() == true) - { - InputStream is = null; - try - { - moduleFound = true; - is = new TFileInputStream(moduleProperties); - moduleDetails = ModuleDetailsHelper.createModuleDetailsFromPropertiesStream(is); - } - catch (AlfrescoRuntimeException exception) - { - throw new ModuleManagementToolException("Unable to open module properties file '" + moduleProperties.getPath() + "' " + exception.getMessage(), exception); - } - catch (IOException exception) - { - throw new ModuleManagementToolException("Unable to open module properties file '" + moduleProperties.getPath() + "'", exception); - } - finally - { - if (is != null) - { - try { is.close(); } catch (Throwable e ) {} - } - } - outputVerboseMessage("Module '" + moduleDetails.getId() + "' installed in '" + warLocation + "'"); - outputVerboseMessage(" Title: " + moduleDetails.getTitle(), true); - outputVerboseMessage(" Version: " + moduleDetails.getModuleVersionNumber(), true); - outputVerboseMessage(" Install Date: " + moduleDetails.getInstallDate(), true); - outputVerboseMessage(" Description: " + moduleDetails.getDescription(), true); - } - } - } + + for (Iterator iterator = modulesFound.iterator(); iterator.hasNext(); ) { + ModuleDetails moduleDetails = iterator.next(); + outputVerboseMessage("Module '" + moduleDetails.getId() + "' installed in '" + warLocation + "'"); + outputVerboseMessage(" Title: " + moduleDetails.getTitle(), true); + outputVerboseMessage(" Version: " + moduleDetails.getModuleVersionNumber(), true); + outputVerboseMessage(" Install Date: " + moduleDetails.getInstallDate(), true); + outputVerboseMessage(" Description: " + moduleDetails.getDescription(), true); } - else - { - outputVerboseMessage("No modules are installed in this WAR file"); - } - if (!moduleFound) - { - outputVerboseMessage("No modules were found in this WAR file"); - } - + } finally { diff --git a/source/java/org/alfresco/repo/module/tool/WarHelper.java b/source/java/org/alfresco/repo/module/tool/WarHelper.java index 2b02946a93..8bd7de8ef3 100644 --- a/source/java/org/alfresco/repo/module/tool/WarHelper.java +++ b/source/java/org/alfresco/repo/module/tool/WarHelper.java @@ -4,6 +4,8 @@ import org.alfresco.service.cmr.module.ModuleDetails; import de.schlichtherle.truezip.file.TFile; +import java.util.List; + /** * Performs various actions on a war file or exploded war directory * @@ -54,5 +56,13 @@ public interface WarHelper * @return boolean - true if it is a share war */ public boolean isShareWar(TFile war); - + + /** + * Lists all the currently installed modules in the WAR + * @since 5.1 + * @param war the war + * @return List an unordered list of module details. + * @throws ModuleManagementToolException + */ + List listModules(TFile war); } diff --git a/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java b/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java index b6b54f9789..e635217b3e 100644 --- a/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java +++ b/source/java/org/alfresco/repo/module/tool/WarHelperImpl.java @@ -1,5 +1,13 @@ package org.alfresco.repo.module.tool; +import de.schlichtherle.truezip.file.TFile; +import de.schlichtherle.truezip.file.TFileInputStream; +import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.repo.module.ModuleDetailsImpl; +import org.alfresco.service.cmr.module.ModuleDependency; +import org.alfresco.service.cmr.module.ModuleDetails; +import org.alfresco.util.VersionNumber; + import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -8,14 +16,6 @@ import java.util.Properties; import java.util.jar.Attributes; import java.util.jar.Manifest; -import org.alfresco.repo.module.ModuleDetailsImpl; -import org.alfresco.service.cmr.module.ModuleDependency; -import org.alfresco.service.cmr.module.ModuleDetails; -import org.alfresco.util.VersionNumber; - -import de.schlichtherle.truezip.file.TFile; -import de.schlichtherle.truezip.file.TFileInputStream; - /** * Performs logic for the Module Management Tool. @@ -138,7 +138,7 @@ public class WarHelperImpl implements WarHelper * Compares the version information with the module details to see if their valid. If they are invalid then it throws an exception. * @param warVersion * @param installingModuleDetails - * @throws ModuleManagementToolException + * @throws ModuleManagementToolException/home/gethin/development/projects/updatetool/code/update-tool */ private void checkVersions(VersionNumber warVersion, ModuleDetails installingModuleDetails) throws ModuleManagementToolException { @@ -276,6 +276,65 @@ public class WarHelperImpl implements WarHelper return false; //default } + + /** + * Lists all the currently installed modules in the WAR + * + * @param war the war + * @throws ModuleManagementToolException + */ + @Override + public List listModules(TFile war) + { + List moduleDetails = new ArrayList<>(); + boolean moduleFound = false; + + TFile moduleDir = new TFile(war, WarHelper.MODULE_NAMESPACE_DIR); + if (moduleDir.exists() == false) + { + return moduleDetails; //empty + } + + java.io.File[] dirs = moduleDir.listFiles(); + if (dirs != null && dirs.length != 0) + { + for (java.io.File dir : dirs) + { + if (dir.isDirectory() == true) + { + TFile moduleProperties = new TFile(dir.getPath() + WarHelper.MODULE_CONFIG_IN_WAR); + if (moduleProperties.exists() == true) + { + InputStream is = null; + try + { + moduleFound = true; + is = new TFileInputStream(moduleProperties); + moduleDetails.add(ModuleDetailsHelper.createModuleDetailsFromPropertiesStream(is)); + } + catch (AlfrescoRuntimeException exception) + { + throw new ModuleManagementToolException("Unable to open module properties file '" + moduleProperties.getPath() + "' " + exception.getMessage(), exception); + } + catch (IOException exception) + { + throw new ModuleManagementToolException("Unable to open module properties file '" + moduleProperties.getPath() + "'", exception); + } + finally + { + if (is != null) + { + try { is.close(); } catch (Throwable e ) {} + } + } + } + } + } + } + + return moduleDetails; + } + /** * 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 8b7f417398..e71426aad5 100644 --- a/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java +++ b/source/test-java/org/alfresco/repo/module/tool/ModuleManagementToolTest.java @@ -18,24 +18,18 @@ */ package org.alfresco.repo.module.tool; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; - -import junit.framework.TestCase; - -import org.alfresco.util.TempFileProvider; -import org.springframework.util.FileCopyUtils; - import de.schlichtherle.truezip.file.TFile; import de.schlichtherle.truezip.file.TFileInputStream; import de.schlichtherle.truezip.file.TVFS; +import junit.framework.TestCase; +import org.alfresco.service.cmr.module.ModuleDetails; +import org.alfresco.util.TempFileProvider; +import org.springframework.util.FileCopyUtils; + +import java.io.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; /** * @see org.alfresco.repo.module.tool.ModuleManagementTool @@ -346,19 +340,74 @@ public class ModuleManagementToolTest extends TestCase checkForFileExistance(warLocation, files); } - public void testList() - throws Exception + public void testList() throws Exception { String warLocation = getFileLocation(".war", "module/test.war"); - String ampLocation = getFileLocation(".amp", "module/test_v1.amp"); - this.manager.listModules(warLocation); - - this.manager.installModule(ampLocation, warLocation); - - this.manager.listModules(warLocation); } - + + public void testListAndInstall() throws Exception { + + String warLocation = getFileLocation(".war", "module/test.war"); + String ampLocation = getFileLocation(".amp", "module/test_v1.amp"); + String ampV2Location = getFileLocation(".amp", "module/test_v2.amp"); + + TFile war = new TFile(warLocation); + + List details = this.manager.warHelper.listModules(war); + assertNotNull(details); + assertEquals(details.size(), 0); + + this.manager.installModule(ampLocation, warLocation); + + details = this.manager.warHelper.listModules(war); + assertNotNull(details); + assertEquals(details.size(), 1); + ModuleDetails aModule = details.get(0); + assertEquals("test", aModule.getId()); + assertEquals("1.0", aModule.getModuleVersionNumber().toString()); + + this.manager.installModule(ampV2Location, warLocation); + + details = this.manager.warHelper.listModules(war); + assertNotNull(details); + assertEquals(details.size(), 1); + aModule = details.get(0); + assertEquals("test", aModule.getId()); + assertEquals("2.0", aModule.getModuleVersionNumber().toString()); + + String testAmpDepV2Location = getFileLocation(".amp", "module/dependent_on_test_v2.amp"); + String testAmp7 = getFileLocation(".amp", "module/test_v7.amp"); + + this.manager.installModule(testAmpDepV2Location, warLocation, false, true, false); + this.manager.installModule(testAmp7, warLocation, false, true, false); + + details = this.manager.warHelper.listModules(war); + assertNotNull(details); + assertEquals(details.size(), 3); + + //Sort them by installation date + details.sort(new Comparator() { + @Override + public int compare(ModuleDetails a, ModuleDetails b) { + return a.getInstallDate().compareTo(b.getInstallDate()); + } + }); + + ModuleDetails installedModule = details.get(0); + assertEquals("test", installedModule.getId()); + assertEquals("2.0", installedModule.getModuleVersionNumber().toString()); + + installedModule = details.get(1); + assertEquals("org.alfresco.module.test.dependent", installedModule.getId()); + assertEquals("2.0", installedModule.getModuleVersionNumber().toString()); + + installedModule = details.get(2); + assertEquals("forcedtest", installedModule.getId()); + assertEquals("1.0", installedModule.getModuleVersionNumber().toString()); + + } + private String getFileLocation(String extension, String location) throws IOException { 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 02dcdaef37..63be7979ea 100644 --- a/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java +++ b/source/test-java/org/alfresco/repo/module/tool/WarHelperImplTest.java @@ -1,30 +1,24 @@ package org.alfresco.repo.module.tool; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Properties; - -import org.alfresco.repo.module.ModuleDetailsImpl; -import org.alfresco.repo.module.ModuleVersionNumber; -import org.alfresco.service.cmr.module.ModuleDetails; -import org.alfresco.util.TempFileProvider; -import org.alfresco.util.VersionNumber; -import org.junit.Test; -import org.springframework.util.FileCopyUtils; - import de.schlichtherle.truezip.file.TArchiveDetector; import de.schlichtherle.truezip.file.TConfig; import de.schlichtherle.truezip.file.TFile; import de.schlichtherle.truezip.fs.archive.zip.ZipDriver; import de.schlichtherle.truezip.socket.sl.IOPoolLocator; +import org.alfresco.repo.module.ModuleDetailsImpl; +import org.alfresco.repo.module.ModuleVersionNumber; +import org.alfresco.service.cmr.module.ModuleDetails; +import org.alfresco.service.cmr.module.ModuleInstallState; +import org.alfresco.util.TempFileProvider; +import org.alfresco.util.VersionNumber; +import org.junit.Test; +import org.springframework.util.FileCopyUtils; + +import java.io.*; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.*; /** * Tests the war helper. @@ -295,7 +289,26 @@ public class WarHelperImplTest extends WarHelperImpl } } - + + @Test + public void testListModules() throws Exception + { + TFile theWar = getFile(".war", "module/test.war"); + + List details = this.listModules(theWar); + assertNotNull(details); + assertEquals(details.size(), 0); + + theWar = getFile(".war", "module/share-4.2.a.war"); + details = this.listModules(theWar); + assertNotNull(details); + assertEquals(details.size(), 1); + ModuleDetails aModule = details.get(0); + assertEquals("alfresco-mm-share", aModule.getId()); + assertEquals("0.1.5.6", aModule.getModuleVersionNumber().toString()); + assertEquals(ModuleInstallState.INSTALLED, aModule.getInstallState()); + + } private Properties dummyModuleProperties() { Properties props = new Properties();