Merged V3.4-BUG-FIX to HEAD

39015: Upgrade of truezip to 7.5.5 see ALF-14247
   39361: Added truezip-swing jar.  It seems that Truezip needs Swing :(


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@39832 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Samuel Langlois
2012-07-26 16:41:46 +00:00
parent faa9ba7118
commit 68b4af4b11
8 changed files with 770 additions and 718 deletions

View File

@@ -21,15 +21,14 @@ package org.alfresco.repo.module.tool;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileOutputStream;
import de.schlichtherle.truezip.file.TFileReader;
/**
* Details of the files installed during a module installation into a WAR
@@ -74,12 +73,12 @@ public class InstalledFiles
*/
public void load()
{
File file = new File(getFileLocation(), ModuleManagementTool.DETECTOR_AMP_AND_WAR);
TFile file = new TFile(getFileLocation());
if (file.exists() == true)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedReader reader = new BufferedReader(new TFileReader(file));
try
{
String line = reader.readLine();
@@ -130,12 +129,12 @@ public class InstalledFiles
{
try
{
File file = new File(getFileLocation(), ModuleManagementTool.DETECTOR_AMP_AND_WAR);
TFile file = new TFile(getFileLocation());
if (file.exists() == false)
{
file.createNewFile();
}
FileOutputStream os = new FileOutputStream(file);
TFileOutputStream os = new TFileOutputStream(file);
try
{
for (String add : this.adds)

View File

@@ -18,6 +18,7 @@
*/
package org.alfresco.repo.module.tool;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -26,9 +27,9 @@ import java.util.Properties;
import org.alfresco.repo.module.ModuleDetailsImpl;
import org.alfresco.service.cmr.module.ModuleDetails;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileInputStream;
import de.schlichtherle.truezip.file.TFileOutputStream;
/**
* Module details helper used by the module mangement tool
@@ -62,23 +63,35 @@ public class ModuleDetailsHelper
*
* @param location file location
* @return Returns the module details or null if the location points to nothing
* @throws IOException
*/
public static ModuleDetails createModuleDetailsFromPropertyLocation(String location)
public static ModuleDetails createModuleDetailsFromPropertyLocation(String location) throws IOException
{
ModuleDetails result = null;
TFileInputStream is;
try
{
File file = new File(location, ModuleManagementTool.DETECTOR_AMP_AND_WAR);
if (file.exists())
{
InputStream is = new FileInputStream(file);
result = createModuleDetailsFromPropertiesStream(is);
}
is = new TFileInputStream(location);
}
catch (FileNotFoundException error)
{
throw new ModuleManagementToolException("Unable to load module details from property file.", error);
}
try
{
result = createModuleDetailsFromPropertiesStream(is);
}
catch (IOException exception)
{
throw new ModuleManagementToolException("Unable to load module details from property file.", exception);
throw new ModuleManagementToolException(
"Unable to load module details from property file.", exception);
}
finally
{
is.close(); // ALWAYS close the stream!
}
return result;
}
@@ -89,8 +102,9 @@ public class ModuleDetailsHelper
* @param moduleId the module id
* @return Returns the module details for the given module ID as it occurs in the WAR, or <tt>null</tt>
* if there are no module details available.
* @throws IOException
*/
public static ModuleDetails createModuleDetailsFromWarAndId(String warLocation, String moduleId)
public static ModuleDetails createModuleDetailsFromWarAndId(String warLocation, String moduleId) throws IOException
{
String modulePropertiesFileLocation = ModuleDetailsHelper.getModulePropertiesFileLocation(warLocation, moduleId);
return ModuleDetailsHelper.createModuleDetailsFromPropertyLocation(modulePropertiesFileLocation);
@@ -102,10 +116,10 @@ public class ModuleDetailsHelper
* @return Returns a file handle to the module properties file within the given WAR.
* The file may or may not exist.
*/
public static File getModuleDetailsFileFromWarAndId(String warLocation, String moduleId)
public static TFile getModuleDetailsFileFromWarAndId(String warLocation, String moduleId)
{
String location = ModuleDetailsHelper.getModulePropertiesFileLocation(warLocation, moduleId);
File file = new File(location, ModuleManagementTool.DETECTOR_AMP_AND_WAR);
TFile file = new TFile(location);
return file;
}
@@ -143,7 +157,7 @@ public class ModuleDetailsHelper
try
{
String modulePropertiesFileLocation = getModulePropertiesFileLocation(warLocation, moduleId);
File file = new File(modulePropertiesFileLocation, ModuleManagementTool.DETECTOR_AMP_AND_WAR);
TFile file = new TFile(modulePropertiesFileLocation);
if (file.exists() == false)
{
file.createNewFile();
@@ -151,7 +165,7 @@ public class ModuleDetailsHelper
// Get all the module properties
Properties moduleProperties = moduleDetails.getProperties();
OutputStream os = new FileOutputStream(file);
OutputStream os = new TFileOutputStream(file);
try
{
moduleProperties.store(os, null);

View File

@@ -19,11 +19,8 @@
package org.alfresco.repo.module.tool;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
@@ -35,12 +32,13 @@ import org.alfresco.service.cmr.module.ModuleInstallState;
import org.alfresco.util.VersionNumber;
import org.safehaus.uuid.UUIDGenerator;
import de.schlichtherle.io.DefaultRaesZipDetector;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.ZipControllerException;
import de.schlichtherle.io.ZipDetector;
import de.schlichtherle.io.ZipWarningException;
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.ZipDriver;
import de.schlichtherle.truezip.socket.sl.IOPoolLocator;
/**
* Module management tool.
@@ -83,9 +81,6 @@ public class ModuleManagementTool implements LogOutput
private static final int ERROR_EXIT_CODE = 1;
private static final int SUCCESS_EXIT_CODE = 0;
/** Default zip detector */
public static final ZipDetector DETECTOR_AMP_AND_WAR = new DefaultRaesZipDetector("amp|war");
/** File mapping properties */
private Properties defaultFileMappingProperties;
@@ -99,6 +94,9 @@ public class ModuleManagementTool implements LogOutput
*/
public ModuleManagementTool()
{
TConfig config = TConfig.get();
config.setArchiveDetector(new TArchiveDetector("war|amp", new ZipDriver(IOPoolLocator.SINGLETON)));
// Load the default file mapping properties
this.defaultFileMappingProperties = new Properties();
InputStream is = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_FILE_MAPPING_PROPERTIES);
@@ -134,21 +132,22 @@ public class ModuleManagementTool implements LogOutput
/**
* Installs all modules within a folder into the given WAR file.
* @throws IOException
*
* @see #installModule(String, String, boolean, boolean, boolean)
*/
public void installModules(String directory, String warFileLocation)
public void installModules(String directory, String warFileLocation) throws IOException
{
installModules(directory, warFileLocation, false, false, true);
}
public void installModules(String directoryLocation, String warFileLocation, boolean preview, boolean forceInstall, boolean backupWAR)
public void installModules(String directoryLocation, String warFileLocation, boolean preview, boolean forceInstall, boolean backupWAR) throws IOException
{
java.io.File dir = new java.io.File(directoryLocation);
if (dir.exists() == true)
{
if (backupWAR) {
backupWar(warFileLocation,true);
backupWar(new TFile(warFileLocation),true);
backupWAR = false; //Set it to false so a backup doesn't occur again.
}
installModules(dir, warFileLocation, preview, forceInstall,backupWAR);
@@ -207,21 +206,20 @@ public class ModuleManagementTool implements LogOutput
try
{
outputVerboseMessage("Installing AMP '" + ampFileLocation + "' into WAR '" + warFileLocation + "'");
java.io.File theWar = new File(warFileLocation, DETECTOR_AMP_AND_WAR);
if (!theWar.exists())
TFile warFile = new TFile(warFileLocation);
if (!warFile.exists())
{
throw new ModuleManagementToolException("The war file '" + warFileLocation + "' does not exist.");
throw new ModuleManagementToolException("The war file '" + warFile + "' does not exist.");
}
if (preview == false)
{
// Make sure the module and backup directory exisits in the WAR file
File moduleDir = new File(warFileLocation + WarHelper.MODULE_NAMESPACE_DIR, DETECTOR_AMP_AND_WAR);
TFile moduleDir = new TFile(warFileLocation + WarHelper.MODULE_NAMESPACE_DIR);
if (moduleDir.exists() == false)
{
moduleDir.mkdir();
}
backupWar(warFileLocation, backupWAR);
backupWar(warFile, backupWAR);
}
// Get the details of the installing module
@@ -235,12 +233,12 @@ public class ModuleManagementTool implements LogOutput
VersionNumber installingVersion = installingModuleDetails.getVersion();
//A series of checks
warHelper.checkCompatibleVersion(theWar, installingModuleDetails);
warHelper.checkCompatibleEdition(theWar, installingModuleDetails);
warHelper.checkModuleDependencies(theWar, installingModuleDetails);
warHelper.checkCompatibleVersion(warFile, installingModuleDetails);
warHelper.checkCompatibleEdition(warFile, installingModuleDetails);
warHelper.checkModuleDependencies(warFile, installingModuleDetails);
// Try to find an installed module by the ID
ModuleDetails installedModuleDetails = warHelper.getModuleDetailsOrAlias(theWar, installingModuleDetails);
ModuleDetails installedModuleDetails = warHelper.getModuleDetailsOrAlias(warFile, installingModuleDetails);
uninstallIfNecessary(warFileLocation, installedModuleDetails, preview, forceInstall, installingVersion);
@@ -256,12 +254,9 @@ public class ModuleManagementTool implements LogOutput
{
for (Entry<Object, Object> entry : directoryChanges.entrySet())
{
File destination = new File((String) entry.getValue(), DETECTOR_AMP_AND_WAR);
File source = new File((String) entry.getKey(), DETECTOR_AMP_AND_WAR);
//Do the bulk copy since this is quicker than copying files one by one
//The changes aren't actuall "committed" until the File.update() is called (below)
destination.copyAllFrom(source);
TFile source = new TFile((String) entry.getKey());
TFile destination = new TFile((String) entry.getValue());
source.cp_rp(destination);
}
}
@@ -273,36 +268,24 @@ public class ModuleManagementTool implements LogOutput
installingModuleDetails.setInstallDate(new Date());
ModuleDetailsHelper.saveModuleDetails(warFileLocation, installingModuleDetails);
// Update the zip files
File.update();
// Set the modified date
java.io.File warFile = new java.io.File(warFileLocation);
if (warFile.exists())
{
warFile.setLastModified(System.currentTimeMillis());
}
// Update the zip filessync
TVFS.umount();
}
}
catch (ZipWarningException ignore)
{
// Only instances of the class ZipWarningException exist in the chain of
// exceptions. We choose to ignore this.
}
catch (ZipControllerException exception)
{
// At least one exception occured which is not just a ZipWarningException.
// This is a severe situation that needs to be handled.
throw new ModuleManagementToolException("A Zip error was encountered during deployment of the AEP into the WAR", exception);
}
catch (IOException exception)
{
throw new ModuleManagementToolException("An IO error was encountered during deployment of the AEP into the WAR", exception);
}
throw new ModuleManagementToolException("An IO error was encountered during deployment of the AMP into the WAR", exception);
}
}
private void uninstallIfNecessary(String warFileLocation, ModuleDetails installedModuleDetails, boolean preview,
boolean forceInstall, VersionNumber installingVersion)
boolean forceInstall, VersionNumber installingVersion) throws IOException
{
// Now clean up the old instance
if (installedModuleDetails != null)
@@ -392,6 +375,28 @@ public class ModuleManagementTool implements LogOutput
// Get a reference to the source folder (if it isn't present don't do anything)
File source = new File(ampFileLocation + "/" + mappingSource, DETECTOR_AMP_AND_WAR);
if (source != null && source.list() != null)
{
// The file mappings are expected to start with "/"
String mappingSource = (String) entry.getKey();
if (mappingSource.length() == 0 || !mappingSource.startsWith("/"))
{
throw new AlfrescoRuntimeException("File mapping sources must start with '/', but was: " + mappingSource);
}
String mappingTarget = (String) entry.getValue();
if (mappingTarget.length() == 0 || !mappingTarget.startsWith("/"))
{
throw new AlfrescoRuntimeException("File mapping targets must start with '/' but was '" + mappingTarget + "'");
}
mappingSource = mappingSource.trim(); //trim whitespace
mappingTarget = mappingTarget.trim(); //trim whitespace
// Run throught the files one by one figuring out what we are going to do during the copy
calculateCopyToWar(ampFileLocation, warFileLocation, mappingSource, mappingTarget, installedFiles, preview, forceInstall);
// Get a reference to the source folder (if it isn't present don't do anything)
TFile source = new TFile(ampFileLocation + "/" + mappingSource);
if (source != null && source.list() != null)
{
// Add to the list of directory changes so we can implement the changes later.
String sourceDir = ampFileLocation + mappingSource;
@@ -404,30 +409,30 @@ public class ModuleManagementTool implements LogOutput
return dirChanges;
}
private void backupWar(String warFileLocation, boolean backupWAR)
private void backupWar(TFile warFile, boolean backupWAR) throws IOException
{
// Make a backup of the war we are going to modify
if (backupWAR == true)
{
File backUpDir = new File(warFileLocation + BACKUP_DIR, DETECTOR_AMP_AND_WAR);
if (backUpDir.exists() == false)
{
backUpDir.mkdir();
}
java.io.File warFile = new java.io.File(warFileLocation);
String backupLocation = warFileLocation + "-" + System.currentTimeMillis() + ".bak";
java.io.File backup = new java.io.File(backupLocation);
try
{
copyFile(warFile, backup);
}
catch (IOException exception)
{
throw new ModuleManagementToolException("An IO error was encountered when backing up the WAR", exception);
}
outputVerboseMessage("WAR has been backed up to '" + backupLocation + "'");
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 + "'");
}
}
@@ -436,7 +441,7 @@ public class ModuleManagementTool implements LogOutput
*/
private Properties getCustomFileMappings(String ampFileLocation)
{
File file = new File(ampFileLocation + "/" + FILE_MAPPING_PROPERTIES, ModuleManagementTool.DETECTOR_AMP_AND_WAR);
TFile file = new TFile(ampFileLocation + "/" + FILE_MAPPING_PROPERTIES);
if (!file.exists())
{
// Nothing there
@@ -446,7 +451,7 @@ public class ModuleManagementTool implements LogOutput
InputStream is = null;
try
{
is = new BufferedInputStream(new FileInputStream(file));
is = new BufferedInputStream(new TFileInputStream(file));
mappingProperties.load(is);
}
catch (IOException exception)
@@ -470,8 +475,9 @@ public class ModuleManagementTool implements LogOutput
* @param moduleId the module id
* @param preview indicates whether this is a preview installation
* @param purge Fully delete all files (including those marked "PRESERVED")
* @throws IOException
*/
public void uninstallModule(String moduleId,String warFileLocation, boolean preview, boolean purge)
public void uninstallModule(String moduleId,String warFileLocation, boolean preview, boolean purge) throws IOException
{
InstalledFiles installedFiles = new InstalledFiles(warFileLocation, moduleId);
installedFiles.load();
@@ -491,10 +497,10 @@ public class ModuleManagementTool implements LogOutput
if (preview == false)
{
// Recover updated file and delete backups
File modified = new File(warFileLocation + update.getKey(), DETECTOR_AMP_AND_WAR);
File backup = new File(warFileLocation + update.getValue(), DETECTOR_AMP_AND_WAR);
modified.copyFrom(backup);
backup.delete();
TFile modified = new TFile(warFileLocation + update.getKey());
TFile backup = new TFile(warFileLocation + update.getValue());
backup.cp_rp(modified);
backup.deleteOnExit();
}
outputVerboseMessage("Recovering file '" + update.getKey() + "' from backup '" + update.getValue() + "'", true);
@@ -516,7 +522,7 @@ public class ModuleManagementTool implements LogOutput
*/
private void removeFile(String warLocation, String filePath, boolean preview)
{
File removeFile = new File(warLocation + filePath, DETECTOR_AMP_AND_WAR);
TFile removeFile = new TFile(warLocation + filePath);
if (removeFile.exists() == true)
{
outputVerboseMessage("Removing file '" + filePath + "' from war", true);
@@ -567,7 +573,7 @@ public class ModuleManagementTool implements LogOutput
}
String sourceLocation = ampFileLocation + sourceDir;
File ampConfig = new File(sourceLocation, DETECTOR_AMP_AND_WAR);
TFile ampConfig = new TFile(sourceLocation);
java.io.File[] files = ampConfig.listFiles();
if (files != null)
@@ -575,7 +581,7 @@ public class ModuleManagementTool implements LogOutput
for (java.io.File sourceChild : files)
{
String destinationFileLocation = warFileLocation + destinationDir + "/" + sourceChild.getName();
File destinationChild = new File(destinationFileLocation, DETECTOR_AMP_AND_WAR);
TFile destinationChild = new TFile(destinationFileLocation);
if (sourceChild.isFile() == true)
{
String backupLocation = null;
@@ -592,8 +598,8 @@ public class ModuleManagementTool implements LogOutput
backupLocation = BACKUP_DIR + "/" + generateGuid() + ".bin";
if (preview == false)
{
File backupFile = new File(warFileLocation + backupLocation, DETECTOR_AMP_AND_WAR);
backupFile.copyFrom(destinationChild);
TFile backupFile = new TFile(warFileLocation + backupLocation);
destinationChild.cp_rp(backupFile);
}
} else {
//Not a forced install, there is an existing file in the war, lets rollback the transaction,
@@ -666,7 +672,7 @@ public class ModuleManagementTool implements LogOutput
try
{
File moduleDir = new File(warLocation + WarHelper.MODULE_NAMESPACE_DIR, DETECTOR_AMP_AND_WAR);
TFile moduleDir = new TFile(warLocation + WarHelper.MODULE_NAMESPACE_DIR);
if (moduleDir.exists() == false)
{
outputVerboseMessage("No modules are installed in this WAR file");
@@ -679,20 +685,27 @@ public class ModuleManagementTool implements LogOutput
{
if (dir.isDirectory() == true)
{
File moduleProperties = new File(dir.getPath() + WarHelper.MODULE_CONFIG_IN_WAR, DETECTOR_AMP_AND_WAR);
TFile moduleProperties = new TFile(dir.getPath() + WarHelper.MODULE_CONFIG_IN_WAR);
if (moduleProperties.exists() == true)
{
InputStream is = null;
try
{
moduleFound = true;
InputStream is = new FileInputStream(moduleProperties);
is = new TFileInputStream(moduleProperties);
moduleDetails = ModuleDetailsHelper.createModuleDetailsFromPropertiesStream(is);
}
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.getVersion(), true);
@@ -848,15 +861,22 @@ public class ModuleManagementTool implements LogOutput
}
}
if (directory == false)
try
{
// Install the module
manager.installModule(aepFileLocation, warFileLocation, previewInstall, forceInstall, backup);
if (directory == false)
{
// Install the module
manager.installModule(aepFileLocation, warFileLocation, previewInstall, forceInstall, backup);
}
else
{
// Install the modules from the directory
manager.installModules(aepFileLocation, warFileLocation, previewInstall, forceInstall, backup);
}
}
else
catch (IOException error)
{
// Install the modules from the directory
manager.installModules(aepFileLocation, warFileLocation, previewInstall, forceInstall, backup);
throw new ModuleManagementToolException(error.getMessage());
}
System.exit(SUCCESS_EXIT_CODE);
@@ -921,6 +941,11 @@ public class ModuleManagementTool implements LogOutput
manager.outputErrorMessage(e.getMessage());
System.exit(ERROR_EXIT_CODE);
}
catch (IOException error)
{
manager.outputErrorMessage(error.getMessage());
System.exit(ERROR_EXIT_CODE);
}
}
/**
@@ -930,38 +955,7 @@ public class ModuleManagementTool implements LogOutput
{
return UUIDGenerator.getInstance().generateTimeBasedUUID().toString();
}
/**
* Code borrowed directly from the Springframework FileCopyUtils.
*/
private static void copyFile(java.io.File in, java.io.File out) throws IOException
{
InputStream is = new BufferedInputStream(new FileInputStream(in));
OutputStream os = new BufferedOutputStream(new FileOutputStream(out));
try
{
int byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
os.flush();
}
finally
{
if (is != null)
{
try { is.close(); } catch (Throwable e) { e.printStackTrace(); }
}
if (os != null)
{
try { os.close(); } catch (Throwable e) { e.printStackTrace(); }
}
}
}
/**
* Outputs the module management tool usage
*/

View File

@@ -1,376 +1,420 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.module.tool;
import java.io.BufferedReader;
import java.io.File;
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 java.util.Map;
import junit.framework.TestCase;
import org.alfresco.util.TempFileProvider;
import org.springframework.util.FileCopyUtils;
import de.schlichtherle.io.DefaultRaesZipDetector;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.io.ZipDetector;
/**
* @see org.alfresco.repo.module.tool.ModuleManagementTool
*
* @author Roy Wetherall
* @author Derek Hulley
*/
public class ModuleManagementToolTest extends TestCase
{
private ModuleManagementTool manager = new ModuleManagementTool();
ZipDetector defaultDetector = new DefaultRaesZipDetector("amp|war");
public void testBasicInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
String ampV2Location = getFileLocation(".amp", "module/test_v2.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation);
// Check that the war has been modified correctly
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files.add("/WEB-INF/classes/alfresco/module/test");
files.add("/WEB-INF/licenses/license.txt");
files.add("/scripts/test.js");
files.add("/images/test.jpg");
files.add("/jsp/test.jsp");
files.add("/css/test.css");
files.add("/extra.txt");
checkForFileExistance(warLocation, files);
// Check the intstalled files
InstalledFiles installed0 = new InstalledFiles(warLocation, "test");
installed0.load();
assertNotNull(installed0);
assertEquals(9, installed0.getAdds().size());
assertEquals(1, installed0.getMkdirs().size());
// Try and install same version
try
{
this.manager.installModule(ampLocation, warLocation);
// Already installed is now a Warning rather than an Error and is now non fatal
// fail("The module is already installed so an exception should have been raised since we are not forcing an overwite");
}
catch(ModuleManagementToolException exception)
{
// Pass
}
// Install a later version
this.manager.installModule(ampV2Location, warLocation);
// Check that the war has been modified correctly
List<String> files2 = new ArrayList<String>(12);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files2.add("/WEB-INF/lib/test.jar");
files2.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files2.add("/WEB-INF/classes/alfresco/module/test");
files2.add("/WEB-INF/licenses/license.txt");
files2.add("/scripts/test2.js");
files2.add("/scripts/test3.js");
files2.add("/images/test.jpg");
files2.add("/css/test.css");
files2.add("/WEB-INF/classes/alfresco/module/test/version2");
files2.add("/WEB-INF/classes/alfresco/module/test/version2/version2-context.xml");
checkForFileExistance(warLocation, files2);
List<String> files3 = new ArrayList<String>(2);
files3.add("/scripts/test.js");
files3.add("/jsp/test.jsp");
files3.add("/extra.txt");
checkForFileNonExistance(warLocation, files3);
// Check the intstalled files
InstalledFiles installed1 = new InstalledFiles(warLocation, "test");
installed1.load();
assertNotNull(installed1);
assertEquals(8, installed1.getAdds().size());
assertEquals(1, installed1.getMkdirs().size());
assertEquals(0, installed1.getUpdates().size());
/**
* Try and install an earlier version over a later version
*/
try
{
this.manager.installModule(ampLocation, warLocation);
//fail("A later version of this module is already installed so an exception should have been raised since we are not forcing an overwite");
//this is now a warning rather than an error
// Check that the war has not been modified
checkForFileExistance(warLocation, files2);
checkForFileNonExistance(warLocation, files3);
}
catch(ModuleManagementToolException exception)
{
// Pass
}
}
public void testDependencySuccess() throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String testAmpV1Location = getFileLocation(".amp", "module/test_v1.amp");
String testAmpV2Location = getFileLocation(".amp", "module/test_v2.amp");
String testAmpDepV1Location = getFileLocation(".amp", "module/dependent_on_test_v1.amp");
String testAmpDepV2Location = getFileLocation(".amp", "module/dependent_on_test_v2.amp");
// Install V1
this.manager.installModule(testAmpV1Location, warLocation, false, false, false);
// Install the module dependent on test_v1
this.manager.installModule(testAmpDepV1Location, warLocation, false, false, false);
try
{
// Attempt to upgrade the dependent module
this.manager.installModule(testAmpDepV2Location, warLocation, false, false, false);
fail("Failed to detect inadequate dependency on the test amp");
}
catch (ModuleManagementToolException e)
{
System.out.println("Expected: " + e.getMessage());
}
// Install the test_v2
this.manager.installModule(testAmpV2Location, warLocation, false, false, false);
// The dependent module should now go in
this.manager.installModule(testAmpDepV2Location, warLocation, false, false, false);
}
public void testPreviewInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, true, false, true);
// TODO need to prove that the war file has not been updated in any way
}
public void testUninstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, false);
this.manager.listModules(warLocation);
this.manager.uninstallModule("test", warLocation, false, false);
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
checkForFileNonExistance(warLocation, files);
}
public void testForcedInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, false);
this.manager.installModule(ampLocation, warLocation, false, true, false);
}
public void testInstallFromDir()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
String ampV2Location = getFileLocation(".amp", "module/test_v2.amp");
int index = ampV2Location.lastIndexOf(File.separator);
System.out.println(index);
String directoryLocation = ampV2Location.substring(0, index);
try
{
this.manager.installModules(directoryLocation, warLocation);
}
catch (ModuleManagementToolException exception)
{
exception.printStackTrace();
System.out.println("Expected failure: " + exception.getMessage());
}
}
public void testExistingFilesInWar() throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war"); //Version 4.0.1
String ampLocation = getFileLocation(".amp", "module/test_v4.amp");
try
{
this.manager.installModule(ampLocation, warLocation, false, false, true);
}
catch(ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().contains("will overwrite an existing file in the war"));
}
this.manager.installModule(ampLocation, warLocation, false, true, true); //Now force it
checkContentsOfFile(warLocation + "/jsp/relogin.jsp", "VERSIONONE");
checkContentsOfFile(warLocation + "/css/main.css", "p{margin-bottom:1em;}");
}
public void testWhiteSpaceInCustomMapping()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v3.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, true);
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files.add("/images/test.jpg");
files.add("/css/test.css");
files.add("/extra.txt");
checkForFileExistance(warLocation, files);
}
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);
}
private String getFileLocation(String extension, String location)
throws IOException
{
File file = TempFileProvider.createTempFile("moduleManagementToolTest-", extension);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
assertNotNull(is);
OutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
return file.getPath();
}
public void testNoWar() throws Exception
{
String noWar = "noWar";
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
try
{
this.manager.installModule(ampLocation, noWar,false,false, false);
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("does not exist."));
}
try
{
this.manager.installModule(ampLocation, noWar,false,false, true); //backup war
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("does not exist."));
}
}
private void checkForFileExistance(String warLocation, List<String> files)
{
for (String file : files)
{
File file0 = new de.schlichtherle.io.File(warLocation + file, this.defaultDetector);
assertTrue("The file/dir " + file + " does not exist in the WAR.", file0.exists());
}
}
private void checkForFileNonExistance(String warLocation, List<String> files)
{
for (String file : files)
{
File file0 = new de.schlichtherle.io.File(warLocation + file, this.defaultDetector);
assertFalse("The file/dir " + file + " does exist in the WAR.", file0.exists());
}
}
private void checkContentsOfFile(String location, String expectedContents)
throws IOException
{
File file = new de.schlichtherle.io.File(location, this.defaultDetector);
assertTrue(file.exists());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = reader.readLine();
assertNotNull(line);
assertEquals(expectedContents, line.trim());
}
}
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
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;
/**
* @see org.alfresco.repo.module.tool.ModuleManagementTool
*
* @author Roy Wetherall
* @author Derek Hulley
*/
public class ModuleManagementToolTest extends TestCase
{
private ModuleManagementTool manager = new ModuleManagementTool();
static final int BUFFER = 2048;
public void testBasicInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
String ampV2Location = getFileLocation(".amp", "module/test_v2.amp");
installerSharedTests(warLocation, ampLocation, ampV2Location);
}
private void installerSharedTests(String warLocation, String ampLocation, String ampV2Location)
{
// Initial install of module
this.manager.installModule(ampLocation, warLocation);
// Check that the war has been modified correctly
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files.add("/WEB-INF/classes/alfresco/module/test");
files.add("/WEB-INF/licenses/license.txt");
files.add("/scripts/test.js");
files.add("/images/test.jpg");
files.add("/jsp/test.jsp");
files.add("/css/test.css");
files.add("/extra.txt");
checkForFileExistance(warLocation, files);
// Check the intstalled files
InstalledFiles installed0 = new InstalledFiles(warLocation, "test");
installed0.load();
assertNotNull(installed0);
assertEquals(9, installed0.getAdds().size());
assertEquals(1, installed0.getMkdirs().size());
// Try and install same version
try
{
this.manager.installModule(ampLocation, warLocation);
// Already installed is now a Warning rather than an Error and is now non fatal
// fail("The module is already installed so an exception should have been raised since we are not forcing an overwite");
}
catch(ModuleManagementToolException exception)
{
// Pass
}
// Install a later version
this.manager.installModule(ampV2Location, warLocation);
// Check that the war has been modified correctly
List<String> files2 = new ArrayList<String>(12);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files2.add("/WEB-INF/lib/test.jar");
files2.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files2.add("/WEB-INF/classes/alfresco/module/test");
files2.add("/WEB-INF/licenses/license.txt");
files2.add("/scripts/test2.js");
files2.add("/scripts/test3.js");
files2.add("/images/test.jpg");
files2.add("/css/test.css");
files2.add("/WEB-INF/classes/alfresco/module/test/version2");
files2.add("/WEB-INF/classes/alfresco/module/test/version2/version2-context.xml");
checkForFileExistance(warLocation, files2);
List<String> files3 = new ArrayList<String>(2);
files3.add("/scripts/test.js");
files3.add("/jsp/test.jsp");
files3.add("/extra.txt");
checkForFileNonExistance(warLocation, files3);
// Check the intstalled files
InstalledFiles installed1 = new InstalledFiles(warLocation, "test");
installed1.load();
assertNotNull(installed1);
assertEquals(8, installed1.getAdds().size());
assertEquals(1, installed1.getMkdirs().size());
assertEquals(0, installed1.getUpdates().size());
/**
* Try and install an earlier version over a later version
*/
try
{
this.manager.installModule(ampLocation, warLocation);
//fail("A later version of this module is already installed so an exception should have been raised since we are not forcing an overwite");
//this is now a warning rather than an error
// Check that the war has not been modified
checkForFileExistance(warLocation, files2);
checkForFileNonExistance(warLocation, files3);
}
catch(ModuleManagementToolException exception)
{
// Pass
}
}
public void testBasicFolderInstall() throws Exception
{
manager.setVerbose(true);
String warDirectory = extractToDir(".war", "module/test.war");
String ampDirectory = extractToDir(".amp", "module/test_v1.amp");
String ampV2Directory = getFileLocation(".amp", "module/test_v2.amp");
assertNotNull(warDirectory);
assertNotNull(ampDirectory);
assertNotNull(ampV2Directory);
installerSharedTests(warDirectory, ampDirectory, ampV2Directory);
}
public void testDependencySuccess() throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String testAmpV1Location = getFileLocation(".amp", "module/test_v1.amp");
String testAmpV2Location = getFileLocation(".amp", "module/test_v2.amp");
String testAmpDepV1Location = getFileLocation(".amp", "module/dependent_on_test_v1.amp");
String testAmpDepV2Location = getFileLocation(".amp", "module/dependent_on_test_v2.amp");
// Install V1
this.manager.installModule(testAmpV1Location, warLocation, false, false, false);
// Install the module dependent on test_v1
this.manager.installModule(testAmpDepV1Location, warLocation, false, false, false);
try
{
// Attempt to upgrade the dependent module
this.manager.installModule(testAmpDepV2Location, warLocation, false, false, false);
fail("Failed to detect inadequate dependency on the test amp");
}
catch (ModuleManagementToolException e)
{
System.out.println("Expected: " + e.getMessage());
}
// Install the test_v2
this.manager.installModule(testAmpV2Location, warLocation, false, false, false);
// The dependent module should now go in
this.manager.installModule(testAmpDepV2Location, warLocation, false, false, false);
}
public void testPreviewInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, true, false, true);
// TODO need to prove that the war file has not been updated in any way
}
public void testUninstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, false);
this.manager.listModules(warLocation);
this.manager.uninstallModule("test", warLocation, false, false);
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
checkForFileNonExistance(warLocation, files);
}
public void testForcedInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, false);
this.manager.installModule(ampLocation, warLocation, false, true, false);
}
public void testInstallFromDir()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
String ampV2Location = getFileLocation(".amp", "module/test_v2.amp");
int index = ampV2Location.lastIndexOf(File.separator);
System.out.println(index);
String directoryLocation = ampV2Location.substring(0, index);
try
{
this.manager.installModules(directoryLocation, warLocation);
}
catch (ModuleManagementToolException exception)
{
exception.printStackTrace();
System.out.println("Expected failure: " + exception.getMessage());
}
}
public void testExistingFilesInWar() throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war"); //Version 4.0.1
String ampLocation = getFileLocation(".amp", "module/test_v4.amp");
try
{
this.manager.installModule(ampLocation, warLocation, false, false, true);
}
catch(ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().contains("will overwrite an existing file in the war"));
}
this.manager.installModule(ampLocation, warLocation, false, true, true); //Now force it
checkContentsOfFile(warLocation + "/jsp/relogin.jsp", "VERSIONONE");
checkContentsOfFile(warLocation + "/css/main.css", "p{margin-bottom:1em;}");
}
public void testWhiteSpaceInCustomMapping()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test_v3.amp");
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, true);
List<String> files = new ArrayList<String>(10);
files.add("/WEB-INF/classes/alfresco/module/test/module.properties");
files.add("/WEB-INF/classes/alfresco/module/test/modifications.install");
files.add("/WEB-INF/lib/test.jar");
files.add("/WEB-INF/classes/alfresco/module/test/module-context.xml");
files.add("/images/test.jpg");
files.add("/css/test.css");
files.add("/extra.txt");
checkForFileExistance(warLocation, files);
}
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);
}
private String getFileLocation(String extension, String location)
throws IOException
{
File file = TempFileProvider.createTempFile("moduleManagementToolTest-", extension);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
assertNotNull(is);
OutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
return file.getPath();
}
private String extractToDir(String extension, String location)
{
File tmpDir = TempFileProvider.getTempDir();
try {
TFile zipFile = new TFile(this.getClass().getClassLoader().getResource(location).getPath());
TFile outDir = new TFile(tmpDir.getAbsolutePath()+"/moduleManagementToolTestDir"+System.currentTimeMillis());
outDir.mkdir();
zipFile.cp_rp(outDir);
TVFS.umount(zipFile);
return outDir.getPath();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void testNoWar() throws Exception
{
String noWar = "noWar";
String ampLocation = getFileLocation(".amp", "module/test_v1.amp");
try
{
this.manager.installModule(ampLocation, noWar,false,false, false);
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("does not exist."));
}
try
{
this.manager.installModule(ampLocation, noWar,false,false, true); //backup war
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("does not exist."));
}
}
private void checkForFileExistance(String warLocation, List<String> files)
{
for (String file : files)
{
File file0 = new TFile(warLocation + file);
assertTrue("The file/dir " + file + " does not exist in the WAR.", file0.exists());
}
}
private void checkForFileNonExistance(String warLocation, List<String> files)
{
for (String file : files)
{
File file0 = new TFile(warLocation + file);
assertFalse("The file/dir " + file + " does exist in the WAR.", file0.exists());
}
}
private void checkContentsOfFile(String location, String expectedContents)
throws IOException
{
File file = new TFile(location);
assertTrue(file.exists());
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(new TFileInputStream(file)));
String line = reader.readLine();
assertNotNull(line);
assertEquals(expectedContents, line.trim());
}
finally
{
if (reader != null)
{
try { reader.close(); } catch (Throwable e ) {}
}
}
}
}

View File

@@ -1,9 +1,9 @@
package org.alfresco.repo.module.tool;
import java.io.File;
import org.alfresco.service.cmr.module.ModuleDetails;
import de.schlichtherle.truezip.file.TFile;
/**
* Performs various actions on a war file or exploded war directory
*
@@ -20,21 +20,21 @@ public interface WarHelper
* @param installingModuleDetails
* @return ModuleDetails
*/
public ModuleDetails getModuleDetailsOrAlias(File war, ModuleDetails installingModuleDetails);
public ModuleDetails getModuleDetailsOrAlias(TFile war, ModuleDetails installingModuleDetails);
/**
* Checks the dependencies of this module
* @param war
* @param installingModuleDetails
*/
public void checkModuleDependencies(File war, ModuleDetails installingModuleDetails);
public void checkModuleDependencies(TFile war, ModuleDetails installingModuleDetails);
/**
* Checks to see if the module is compatible with the version of Alfresco.
*
* @param war a valid war file or exploded directory from a war
*/
public void checkCompatibleVersion(File war, ModuleDetails installingModuleDetails);
public void checkCompatibleVersion(TFile war, ModuleDetails installingModuleDetails);
/**
* This checks to see if the module that is being installed is compatible with the war.
@@ -43,6 +43,6 @@ public interface WarHelper
* @param war a valid war file or exploded directory from a war
* @param installingModuleDetails
*/
public void checkCompatibleEdition(File war, ModuleDetails installingModuleDetails);
public void checkCompatibleEdition(TFile war, ModuleDetails installingModuleDetails);
}

View File

@@ -1,6 +1,5 @@
package org.alfresco.repo.module.tool;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -12,7 +11,9 @@ import org.alfresco.service.cmr.module.ModuleDependency;
import org.alfresco.service.cmr.module.ModuleDetails;
import org.alfresco.util.VersionNumber;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileInputStream;
/**
* Performs logic for the Module Management Tool.
@@ -33,10 +34,10 @@ public class WarHelperImpl implements WarHelper
}
@Override
public void checkCompatibleVersion(File war, ModuleDetails installingModuleDetails)
public void checkCompatibleVersion(TFile war, ModuleDetails installingModuleDetails)
{
//Version check
File propsFile = getFile(war, VERSION_PROPERTIES);
TFile propsFile = new TFile(war+VERSION_PROPERTIES);
if (propsFile != null && propsFile.exists())
{
Properties warVers = loadProperties(propsFile);
@@ -55,14 +56,14 @@ public class WarHelperImpl implements WarHelper
}
@Override
public void checkCompatibleEdition(File war, ModuleDetails installingModuleDetails)
public void checkCompatibleEdition(TFile war, ModuleDetails installingModuleDetails)
{
List<String> installableEditions = installingModuleDetails.getEditions();
if (installableEditions != null && installableEditions.size() > 0) {
File propsFile = getFile(war, VERSION_PROPERTIES);
TFile propsFile = new TFile(war+VERSION_PROPERTIES);
if (propsFile != null && propsFile.exists())
{
Properties warVers = loadProperties(propsFile);
@@ -84,7 +85,7 @@ public class WarHelperImpl implements WarHelper
}
@Override
public void checkModuleDependencies(File war, ModuleDetails installingModuleDetails)
public void checkModuleDependencies(TFile war, ModuleDetails installingModuleDetails)
{
// Check that the target war has the necessary dependencies for this install
List<ModuleDependency> installingModuleDependencies = installingModuleDetails.getDependencies();
@@ -107,7 +108,7 @@ public class WarHelperImpl implements WarHelper
}
@Override
public ModuleDetails getModuleDetailsOrAlias(File war, ModuleDetails installingModuleDetails)
public ModuleDetails getModuleDetailsOrAlias(TFile war, ModuleDetails installingModuleDetails)
{
ModuleDetails installedModuleDetails = getModuleDetails(war, installingModuleDetails.getId());
if (installedModuleDetails == null)
@@ -137,10 +138,10 @@ public class WarHelperImpl implements WarHelper
* @param moduleId
* @return
*/
protected ModuleDetails getModuleDetails(File war, String moduleId)
protected ModuleDetails getModuleDetails(TFile war, String moduleId)
{
ModuleDetails moduleDets = null;
File theFile = getModuleDetailsFile(war, moduleId);
TFile theFile = getModuleDetailsFile(war, moduleId);
if (theFile != null && theFile.exists())
{
moduleDets = new ModuleDetailsImpl(loadProperties(theFile));
@@ -153,14 +154,15 @@ public class WarHelperImpl implements WarHelper
* @param propertiesPath Path to the properties file (including .properties)
* @return Properties object or null
*/
private Properties loadProperties(File propertiesFile)
private Properties loadProperties(TFile propertiesFile)
{
Properties result = null;
InputStream is = null;
try
{
if (propertiesFile.exists())
{
InputStream is = new FileInputStream(propertiesFile);
is = new TFileInputStream(propertiesFile);
result = new Properties();
result.load(is);
}
@@ -169,21 +171,21 @@ public class WarHelperImpl implements WarHelper
{
throw new ModuleManagementToolException("Unable to load properties from the war file; "+propertiesFile.getPath(), exception);
}
finally
{
if (is != null)
{
try { is.close(); } catch (Throwable e ) {}
}
}
return result;
}
private File getModuleDetailsFile(File war, String moduleId)
private TFile getModuleDetailsFile(TFile war, String moduleId)
{
return getFile(war,MODULE_NAMESPACE_DIR+ "/" + moduleId+MODULE_CONFIG_IN_WAR);
return new TFile(war.getAbsolutePath()+MODULE_NAMESPACE_DIR+ "/" + moduleId+MODULE_CONFIG_IN_WAR);
}
private File getFile(File war, String pathToFileIncludingExtension)
{
return new de.schlichtherle.io.File(war,pathToFileIncludingExtension, ModuleManagementTool.DETECTOR_AMP_AND_WAR);
}
}

View File

@@ -1,167 +1,167 @@
package org.alfresco.repo.module.tool;
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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.alfresco.repo.module.ModuleDetailsImpl;
import org.alfresco.service.cmr.module.ModuleDetails;
import org.alfresco.util.TempFileProvider;
import org.alfresco.util.VersionNumber;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;
import de.schlichtherle.io.FileOutputStream;
/**
* Tests the war helper.
*
* @author Gethin James
*/
public class WarHelperImplTest extends WarHelperImpl
{
public WarHelperImplTest()
{
super(new LogOutput()
{
@Override
public void info(Object message)
{
System.out.println(message);
}
});
}
@Test
public void testCheckCompatibleVersion()
{
File theWar = getFile(".war", "module/test.war"); //Version 4.1.0
ModuleDetails installingModuleDetails = new ModuleDetailsImpl("test_it", new VersionNumber("9999"), "Test Mod", "Testing module");
installingModuleDetails.setRepoVersionMin(new VersionNumber("10.1"));
try
{
this.checkCompatibleVersion(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("must be installed on a repo version greater than 10.1"));
}
installingModuleDetails.setRepoVersionMin(new VersionNumber("1.1"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMax(new VersionNumber("3.0"));
try
{
this.checkCompatibleVersion(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("cannot be installed on a repo version greater than 3.0"));
}
installingModuleDetails.setRepoVersionMax(new VersionNumber("99"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMin(new VersionNumber("4.1.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.1.0")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMin(new VersionNumber("3.4.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.1.0")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
try
{
installingModuleDetails.setRepoVersionMin(new VersionNumber("3.4.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.0.999")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
fail("Should not pass as current version is 4.1.0 and the max value is 4.0.999"); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("cannot be installed on a repo version greater than 4.0.999"));
}
}
@Test
public void testCheckCompatibleEdition()
{
Properties props = new Properties();
props.setProperty(ModuleDetails.PROP_ID, "TestComp");
props.setProperty(ModuleDetails.PROP_VERSION, "9999");
props.setProperty(ModuleDetails.PROP_TITLE, "Test for Compatiblity");
props.setProperty(ModuleDetails.PROP_DESCRIPTION, "Test for Compatible Editions");
ModuleDetails installingModuleDetails = new ModuleDetailsImpl(props);
File theWar = getFile(".war", "module/test.war"); //Community Edition
//Test for no edition specified
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
//Test for invalid edition
props.setProperty(ModuleDetails.PROP_EDITIONS, "CommuniT");
installingModuleDetails = new ModuleDetailsImpl(props);
try
{
this.checkCompatibleEdition(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("can only be installed in one of the following editions[CommuniT]"));
}
props.setProperty(ModuleDetails.PROP_EDITIONS, ("CoMMunity")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
props.setProperty(ModuleDetails.PROP_EDITIONS, ("enterprise,community,bob")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
props.setProperty(ModuleDetails.PROP_EDITIONS, ("enterprise,Community")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
}
@Test
public void testNoVersionProperties()
{
File theWar = getFile(".war", "module/empty.war");
ModuleDetails installingModuleDetails = new ModuleDetailsImpl("test_it", new VersionNumber("9999"), "Test Mod", "Testing module");
installingModuleDetails.setRepoVersionMin(new VersionNumber("10.1"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
}
private File getFile(String extension, String location)
{
File file = TempFileProvider.createTempFile("moduleManagementToolTest-", extension);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
assertNotNull(is);
OutputStream os;
try
{
os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
}
catch (IOException error)
{
error.printStackTrace();
}
return file;
}
}
package org.alfresco.repo.module.tool;
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.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.TFile;
/**
* Tests the war helper.
*
* @author Gethin James
*/
public class WarHelperImplTest extends WarHelperImpl
{
public WarHelperImplTest()
{
super(new LogOutput()
{
@Override
public void info(Object message)
{
System.out.println(message);
}
});
}
@Test
public void testCheckCompatibleVersion()
{
TFile theWar = getFile(".war", "module/test.war"); //Version 4.1.0
ModuleDetails installingModuleDetails = new ModuleDetailsImpl("test_it", new VersionNumber("9999"), "Test Mod", "Testing module");
installingModuleDetails.setRepoVersionMin(new VersionNumber("10.1"));
try
{
this.checkCompatibleVersion(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("must be installed on a repo version greater than 10.1"));
}
installingModuleDetails.setRepoVersionMin(new VersionNumber("1.1"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMax(new VersionNumber("3.0"));
try
{
this.checkCompatibleVersion(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("cannot be installed on a repo version greater than 3.0"));
}
installingModuleDetails.setRepoVersionMax(new VersionNumber("99"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMin(new VersionNumber("4.1.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.1.0")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
installingModuleDetails.setRepoVersionMin(new VersionNumber("3.4.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.1.0")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
try
{
installingModuleDetails.setRepoVersionMin(new VersionNumber("3.4.0")); //current war version
installingModuleDetails.setRepoVersionMax(new VersionNumber("4.0.999")); //current war version
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
fail("Should not pass as current version is 4.1.0 and the max value is 4.0.999"); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("cannot be installed on a repo version greater than 4.0.999"));
}
}
@Test
public void testCheckCompatibleEdition()
{
Properties props = new Properties();
props.setProperty(ModuleDetails.PROP_ID, "TestComp");
props.setProperty(ModuleDetails.PROP_VERSION, "9999");
props.setProperty(ModuleDetails.PROP_TITLE, "Test for Compatiblity");
props.setProperty(ModuleDetails.PROP_DESCRIPTION, "Test for Compatible Editions");
ModuleDetails installingModuleDetails = new ModuleDetailsImpl(props);
TFile theWar = getFile(".war", "module/test.war"); //Community Edition
//Test for no edition specified
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
//Test for invalid edition
props.setProperty(ModuleDetails.PROP_EDITIONS, "CommuniT");
installingModuleDetails = new ModuleDetailsImpl(props);
try
{
this.checkCompatibleEdition(theWar, installingModuleDetails);
fail(); //should never get here
}
catch (ModuleManagementToolException exception)
{
assertTrue(exception.getMessage().endsWith("can only be installed in one of the following editions[CommuniT]"));
}
props.setProperty(ModuleDetails.PROP_EDITIONS, ("CoMMunity")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
props.setProperty(ModuleDetails.PROP_EDITIONS, ("enterprise,community,bob")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
props.setProperty(ModuleDetails.PROP_EDITIONS, ("enterprise,Community")); //should ignore case
installingModuleDetails = new ModuleDetailsImpl(props);
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
}
@Test
public void testNoVersionProperties()
{
TFile theWar = getFile(".war", "module/empty.war");
ModuleDetails installingModuleDetails = new ModuleDetailsImpl("test_it", new VersionNumber("9999"), "Test Mod", "Testing module");
installingModuleDetails.setRepoVersionMin(new VersionNumber("10.1"));
this.checkCompatibleVersion(theWar, installingModuleDetails); //does not throw exception
this.checkCompatibleEdition(theWar, installingModuleDetails); //does not throw exception
}
private TFile getFile(String extension, String location)
{
File file = TempFileProvider.createTempFile("moduleManagementToolTest-", extension);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
assertNotNull(is);
OutputStream os;
try
{
os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
}
catch (IOException error)
{
error.printStackTrace();
}
return new TFile(file);
}
}