Merged DEV\EXTENSIONS to HEAD

svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4868 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4869 .
   svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4904 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4938 .
   Module management support
   Modularization of Records Management


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4956 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-01-29 14:43:37 +00:00
parent 0e3da160a2
commit f047c6baaf
42 changed files with 3324 additions and 664 deletions

View File

@@ -0,0 +1,217 @@
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;
/**
* Details of the files installed during a module installation into a WAR
*
* @author Roy Wetherall
*/
public class InstalledFiles
{
/** Modification types */
private static final String MOD_ADD_FILE = "add";
private static final String MOD_UPDATE_FILE = "update";
private static final String MOD_MK_DIR = "mkdir";
/** Delimieter used in the file */
private static final String DELIMITER = "|";
/** War location **/
private String warLocation;
/** Module id **/
private String moduleId;
/** Lists containing the modifications made */
private List<String> adds = new ArrayList<String>();
private Map<String, String> updates = new HashMap<String, String>();
private List<String> mkdirs = new ArrayList<String>();
/**
* Constructor
*
* @param warLocation the war location
* @param moduleId the module id
*/
public InstalledFiles(String warLocation, String moduleId)
{
this.warLocation = warLocation;
this.moduleId = moduleId;
}
/**
* Loads the exisiting information about the installed files from the WAR
*/
public void load()
{
File file = new File(getFileLocation(), ModuleManagementTool.defaultDetector);
if (file.exists() == true)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
try
{
String line = reader.readLine();
while (line != null)
{
String[] modification = line.split("\\" + DELIMITER);
String mod = modification[0];
String location = modification[1];
if (mod.equals(MOD_ADD_FILE) == true)
{
this.adds.add(location);
}
else if (mod.equals(MOD_MK_DIR) == true)
{
this.mkdirs.add(location);
}
else if (mod.equals(MOD_UPDATE_FILE) == true)
{
this.updates.put(location, modification[2]);
}
line = reader.readLine();
}
}
finally
{
reader.close();
}
}
catch(FileNotFoundException exception)
{
throw new ModuleManagementToolException("The module file install file '" + getFileLocation() + "' does not exist", exception);
}
catch(IOException exception)
{
throw new ModuleManagementToolException("Error whilst reading file '" + getFileLocation(), exception);
}
}
}
/**
* Saves the current modification details into the WAR
*/
public void save()
{
try
{
File file = new File(getFileLocation(), ModuleManagementTool.defaultDetector);
if (file.exists() == false)
{
file.createNewFile();
}
FileOutputStream os = new FileOutputStream(file);
try
{
for (String add : this.adds)
{
String output = MOD_ADD_FILE + DELIMITER + add + "\n";
os.write(output.getBytes());
}
for (Map.Entry<String, String> update : this.updates.entrySet())
{
String output = MOD_UPDATE_FILE + DELIMITER + update.getKey() + DELIMITER + update.getValue() + "\n";
os.write(output.getBytes());
}
for (String mkdir : this.mkdirs)
{
String output = MOD_MK_DIR + DELIMITER + mkdir + "\n";
os.write(output.getBytes());
}
}
finally
{
os.close();
}
}
catch(IOException exception)
{
throw new ModuleManagementToolException("Error whilst saving modifications file.", exception);
}
}
/**
* Returns the location of the modifications file based on the module id
*
* @return the file location
*/
private String getFileLocation()
{
return this.warLocation + ModuleManagementTool.MODULE_DIR + "/" + this.moduleId + "/modifications.install";
}
/**
* Get all the added files
*
* @return list of files added to war
*/
public List<String> getAdds()
{
return adds;
}
/**
* Get all the updated files, key is the file that has been updated and the value is the
* location of the backup made before modification took place.
*
* @return map of file locaiton and backup
*/
public Map<String, String> getUpdates()
{
return updates;
}
/**
* Gets a list of the dirs added during install
*
* @return list of directories added
*/
public List<String> getMkdirs()
{
return mkdirs;
}
/**
* Add a file addition
*
* @param location the file added
*/
public void addAdd(String location)
{
this.adds.add(location);
}
/**
* Add a file update
*
* @param location the file updated
* @param backup the backup location
*/
public void addUpdate(String location, String backup)
{
this.updates.put(location, backup);
}
/**
* Add a directory
*
* @param location the directory location
*/
public void addMkdir(String location)
{
this.mkdirs.add(location);
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.module.tool;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.alfresco.repo.module.ModuleDetailsImpl;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
/**
* Module details helper used by the module mangement tool
*
* @author Roy Wetherall
*/
public class ModuleDetailsHelper extends ModuleDetailsImpl
{
/**
* Constructor
*
* @param is input stream
*/
public ModuleDetailsHelper(InputStream is)
{
super(is);
}
/**
* Creates a module details helper object based on a file location.
*
* @param location file location
* @return module details helper object
*/
public static ModuleDetailsHelper create(String location)
{
ModuleDetailsHelper result = null;
try
{
File file = new File(location, ModuleManagementTool.defaultDetector);
if (file.exists() == true)
{
result = new ModuleDetailsHelper(new FileInputStream(file));
}
}
catch (IOException exception)
{
throw new ModuleManagementToolException("Unable to load module details from property file.", exception);
}
return result;
}
/**
* Creates a module details helper object based on a war location and the module id
*
* @param warLocation the war location
* @param moduleId the module id
* @return the module details helper
*/
public static ModuleDetailsHelper create(String warLocation, String moduleId)
{
return ModuleDetailsHelper.create(ModuleDetailsHelper.getFileLocation(warLocation, moduleId));
}
/**
* Gets the file location
*
* @param warLocation the war location
* @param moduleId the module id
* @return the file location
*/
private static String getFileLocation(String warLocation, String moduleId)
{
return warLocation + ModuleManagementTool.MODULE_DIR + "/" + moduleId + "/" + "module.properties";
}
/**
* Saves the module detailsin to the war in the correct location based on the module id
*
* @param warLocation the war location
* @param moduleId the module id
*/
public void save(String warLocation, String moduleId)
{
try
{
File file = new File(getFileLocation(warLocation, moduleId), ModuleManagementTool.defaultDetector);
if (file.exists() == false)
{
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
try
{
Date now = new Date();
this.properties.setProperty(PROP_INSTALL_DATE, now.toString());
this.properties.store(os, null);
}
finally
{
os.close();
}
}
catch (IOException exception)
{
throw new ModuleManagementToolException("Unable to save module details into WAR file.", exception);
}
}
}

View File

@@ -0,0 +1,598 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.module.tool;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import org.alfresco.util.GUID;
import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;
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;
/**
* Module management tool.
*
* Manages the modules installed in a war file. Allows modules to be installed, updated, enabled, disabled and
* uninstalled. Information about the module installed is also available.
*
* @author Roy Wetherall
*/
public class ModuleManagementTool
{
/** Logger */
public static Logger logger = Logger.getLogger("org.alfresco.repo.extension.ModuleManagementTool");
/** Location of the default mapping properties file */
private static final String DEFAULT_FILE_MAPPING_PROPERTIES = "org/alfresco/repo/module/tool/default-file-mapping.properties";
/** Standard directories found in the alfresco war */
public static final String MODULE_DIR = "/WEB-INF/classes/alfresco/module";
public static final String BACKUP_DIR = MODULE_DIR + "/backup";
/** Operations and options supperted via the command line interface to this class */
private static final String OP_INSTALL = "install";
private static final String OP_LIST = "list";
private static final String OPTION_VERBOSE = "-verbose";
private static final String OPTION_FORCE = "-force";
private static final String OPTION_PREVIEW = "-preview";
private static final String OPTION_NOBACKUP = "-nobackup";
/** Default zip detector */
public static ZipDetector defaultDetector = new DefaultRaesZipDetector("amp|war");
/** File mapping properties */
private Properties fileMappingProperties;
/** Indicates the current verbose setting */
private boolean verbose = false;
/**
* Constructor
*/
public ModuleManagementTool()
{
// Load the default file mapping properties
this.fileMappingProperties = new Properties();
InputStream is = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_FILE_MAPPING_PROPERTIES);
try
{
this.fileMappingProperties.load(is);
}
catch (IOException exception)
{
throw new ModuleManagementToolException("Unable to load default extension file mapping properties.", exception);
}
}
/**
* Indicates whether the management tool is currently in verbose reporting mode.
*
* @return true if verbose, false otherwise
*/
public boolean isVerbose()
{
return verbose;
}
/**
* Sets the verbose setting for the mangement tool
*
* @param verbose true if verbose, false otherwise
*/
public void setVerbose(boolean verbose)
{
this.verbose = verbose;
}
/**
* 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)
{
installModule(ampFileLocation, warFileLocation, false, false, true);
}
/**
* Installs a given AMP file into a given WAR file.
*
* @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.
* @param preview indicates whether this should be a preview install. This means that the process of
* installation will be followed and reported, but the WAR file will not be modified.
* @param forceInstall indicates whether the installed files will be replaces reguarless of the currently installed
* version of the AMP. Generally used during development of the AMP.
* @param backupWAR indicates whether we should backup the war we are modifying or not
*/
public void installModule(String ampFileLocation, String warFileLocation, boolean preview, boolean forceInstall, boolean backupWAR)
{
try
{
if (preview == false)
{
// Make sure the module and backup directory exisits in the WAR file
File moduleDir = new File(warFileLocation + MODULE_DIR, defaultDetector);
if (moduleDir.exists() == false)
{
moduleDir.mkdir();
}
File backUpDir = new File(warFileLocation + BACKUP_DIR, defaultDetector);
if (backUpDir.exists() == false)
{
backUpDir.mkdir();
}
// Make a backup of the war we are oging to modify
if (backupWAR == true)
{
java.io.File warFile = new java.io.File(warFileLocation);
if (warFile.exists() == false)
{
throw new ModuleManagementToolException("The war file '" + warFileLocation + "' does not exist.");
}
String backupLocation = warFileLocation + "-" + System.currentTimeMillis() + ".bak";
java.io.File backup = new java.io.File(backupLocation);
FileCopyUtils.copy(warFile, backup);
outputMessage("WAR has been backed up to '" + backupLocation + "'");
}
}
// Get the details of the installing module
ModuleDetailsHelper installingModuleDetails = ModuleDetailsHelper.create(ampFileLocation + "/module.properties");
if (installingModuleDetails.exists() == false)
{
throw new ModuleManagementToolException("No module.properties file has been found in the installing .amp file '" + ampFileLocation + "'");
}
// Get the detail of the installed module
ModuleDetailsHelper installedModuleDetails = ModuleDetailsHelper.create(warFileLocation, installingModuleDetails.getId());
if (installedModuleDetails != null)
{
int compareValue = installedModuleDetails.getVersionNumber().compareTo(installingModuleDetails.getVersionNumber());
if (forceInstall == true || compareValue == -1)
{
if (forceInstall == true)
{
// Warn of forced install
outputMessage("WARNING: The installation of this module is being forced. All files will be removed and replaced reguarless of exiting versions present.");
}
// Trying to update the extension, old files need to cleaned before we proceed
outputMessage("Clearing out files relating to version '" + installedModuleDetails.getVersionNumber().toString() + "' of module '" + installedModuleDetails.getId() + "'");
cleanWAR(warFileLocation, installedModuleDetails.getId(), preview);
}
else if (compareValue == 0)
{
// Trying to install the same extension version again
outputMessage("WARNING: This version of this module is already installed in the WAR");
throw new ModuleManagementToolException("This version of this module is alreay installed. Use the 'force' parameter if you want to overwrite the current installation.");
}
else if (compareValue == 1)
{
// Trying to install an earlier version of the extension
outputMessage("WARNING: A later version of this module is already installed in the WAR");
throw new ModuleManagementToolException("An earlier version of this module is already installed. You must first unistall the current version before installing this version of the module.");
}
}
// TODO check for any additional file mapping propeties supplied in the AEP file
// Copy the files from the AEP file into the WAR file
outputMessage("Adding files relating to version '" + installingModuleDetails.getVersionNumber().toString() + "' of module '" + installingModuleDetails.getId() + "'");
InstalledFiles installedFiles = new InstalledFiles(warFileLocation, installingModuleDetails.getId());
for (Map.Entry<Object, Object> entry : this.fileMappingProperties.entrySet())
{
// Run throught the files one by one figuring out what we are going to do during the copy
copyToWar(ampFileLocation, warFileLocation, (String)entry.getKey(), (String)entry.getValue(), installedFiles, preview);
if (preview == false)
{
// Get a reference to the source folder (if it isn't present dont do anything
File source = new File(ampFileLocation + "/" + entry.getKey(), defaultDetector);
if (source != null && source.list() != null)
{
// Get a reference to the destination folder
File destination = new File(warFileLocation + "/" + entry.getValue(), defaultDetector);
if (destination == null)
{
throw new ModuleManagementToolException("The destination folder '" + entry.getValue() + "' as specified in mapping properties does not exist in the war");
}
// Do the bulk copy since this is quicker than copying file's one by one
destination.copyAllFrom(source);
}
}
}
if (preview == false)
{
// Save the installed file list
installedFiles.save();
// Update the installed module details
installingModuleDetails.save(warFileLocation, installingModuleDetails.getId());
// Update the zip file's
File.update();
}
}
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);
}
}
/**
* Cleans the WAR file of all files relating to the currently installed version of the the AMP.
*
* @param warFileLocatio the war file location
* @param moduleId the module id
* @param preview indicates whether this is a preview installation
*/
private void cleanWAR(String warFileLocation, String moduleId, boolean preview)
{
InstalledFiles installedFiles = new InstalledFiles(warFileLocation, moduleId);
installedFiles.load();
for (String add : installedFiles.getAdds())
{
// Remove file
removeFile(warFileLocation, add, preview);
}
for (String mkdir : installedFiles.getMkdirs())
{
// Remove folder
removeFile(warFileLocation, mkdir, preview);
}
for (Map.Entry<String, String> update : installedFiles.getUpdates().entrySet())
{
if (preview == false)
{
// Recover updated file and delete backups
File modified = new File(warFileLocation + update.getKey(), defaultDetector);
File backup = new File(warFileLocation + update.getValue(), defaultDetector);
modified.copyFrom(backup);
backup.delete();
}
outputMessage("Recovering file '" + update.getKey() + "' from backup '" + update.getValue() + "'", true);
}
}
/**
* Removes a file from the given location in the war file.
*
* @param warLocation the war file location
* @param filePath the path to the file that is to be deleted
* @param preview indicates whether this is a preview install
*/
private void removeFile(String warLocation, String filePath, boolean preview)
{
File removeFile = new File(warLocation + filePath, defaultDetector);
if (removeFile.exists() == true)
{
outputMessage("Removing file '" + filePath + "' from war", true);
if (preview == false)
{
removeFile.delete();
}
}
else
{
outputMessage("The file '" + filePath + "' was expected for removal but was not present in the war", true);
}
}
/**
* Copies a file from the AMP location to the correct location in the WAR, interating on directories where appropraite.
*
* @param ampFileLocation the AMP file location
* @param warFileLocation the WAR file location
* @param sourceDir the directory in the AMP to copy from
* @param destinationDir the directory in the WAR to copy to
* @param installedFiles a list of the currently installed files
* @param preview indicates whether this is a preview install or not
* @throws IOException throws any IOExpceptions thar are raised
*/
private void copyToWar(String ampFileLocation, String warFileLocation, String sourceDir, String destinationDir, InstalledFiles installedFiles, boolean preview)
throws IOException
{
String sourceLocation = ampFileLocation + sourceDir;
File ampConfig = new File(sourceLocation, defaultDetector);
java.io.File[] files = ampConfig.listFiles();
if (files != null)
{
for (java.io.File sourceChild : files)
{
String destinationFileLocation = warFileLocation + destinationDir + "/" + sourceChild.getName();
File destinationChild = new File(destinationFileLocation, defaultDetector);
if (sourceChild.isFile() == true)
{
String backupLocation = null;
boolean createFile = false;
if (destinationChild.exists() == false)
{
createFile = true;
}
else
{
// Backup file about to be updated
backupLocation = BACKUP_DIR + "/" + GUID.generate() + ".bin";
if (preview == false)
{
File backupFile = new File(warFileLocation + backupLocation, defaultDetector);
backupFile.copyFrom(destinationChild);
}
}
if (createFile == true)
{
installedFiles.addAdd(destinationDir + "/" + sourceChild.getName());
this.outputMessage("File '" + destinationDir + "/" + sourceChild.getName() + "' added to war from amp", true);
}
else
{
installedFiles.addUpdate(destinationDir + "/" + sourceChild.getName(), backupLocation);
this.outputMessage("WARNING: The file '" + destinationDir + "/" + sourceChild.getName() + "' is being updated by this module and has been backed-up to '" + backupLocation + "'", true);
}
}
else
{
boolean mkdir = false;
if (destinationChild.exists() == false)
{
mkdir = true;
}
copyToWar(ampFileLocation, warFileLocation, sourceDir + "/" + sourceChild.getName(),
destinationDir + "/" + sourceChild.getName(), installedFiles, preview);
if (mkdir == true)
{
installedFiles.addMkdir(destinationDir + "/" + sourceChild.getName());
this.outputMessage("Directory '" + destinationDir + "/" + sourceChild.getName() + "' added to war", true);
}
}
}
}
}
/**
* @throws UnsupportedOperationException
*/
public void disableModule(String moduleId, String warLocation)
{
throw new UnsupportedOperationException("Disable module is not currently supported");
}
/**
* @throws UnsupportedOperationException
*/
public void enableModule(String moduleId, String warLocation)
{
throw new UnsupportedOperationException("Enable module is not currently supported");
}
/**
* @throws UnsupportedOperationException
*/
public void uninstallModule(String moduleId, String warLocation)
{
throw new UnsupportedOperationException("Uninstall module is not currently supported");
}
/**
* Lists all the currently installed modules in the WAR
*
* @param warLocation the war location
*/
public void listModules(String warLocation)
{
ModuleDetailsHelper moduleDetails = null;
boolean previous = this.verbose;
this.verbose = true;
try
{
File moduleDir = new File(warLocation + MODULE_DIR, defaultDetector);
if (moduleDir.exists() == false)
{
outputMessage("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)
{
File moduleProperties = new File(dir.getPath() + "/module.properties", defaultDetector);
if (moduleProperties.exists() == true)
{
try
{
moduleDetails = new ModuleDetailsHelper(new FileInputStream(moduleProperties));
}
catch (FileNotFoundException exception)
{
throw new ModuleManagementToolException("Unable to open module properties file '" + moduleProperties.getPath() + "'");
}
outputMessage("Module '" + moduleDetails.getId() + "' installed in '" + warLocation + "'");
outputMessage("Title: " + moduleDetails.getTitle(), true);
outputMessage("Version: " + moduleDetails.getVersionNumber(), true);
outputMessage("Install Date: " + moduleDetails.getInstalledDate(), true);
outputMessage("Desription: " + moduleDetails.getDescription(), true);
}
}
}
}
else
{
outputMessage("No modules are installed in this WAR file");
}
}
finally
{
this.verbose = previous;
}
}
/**
* Outputs a message the console (in verbose mode) and the logger.
*
* @param message the message to output
*/
private void outputMessage(String message)
{
outputMessage(message, false);
}
/**
* Outputs a message the console (in verbose mode) and the logger.
*
* @param message the message to output
* @prarm indent indicates that the message should be formated with an indent
*/
private void outputMessage(String message, boolean indent)
{
if (indent == true)
{
message = " - " + message;
}
if (this.verbose == true)
{
System.out.println(message);
}
if (logger.isDebugEnabled() == true)
{
logger.debug(message);
}
}
/**
* Main
*
* @param args command line interface arguments
*/
public static void main(String[] args)
{
if (args.length >= 1)
{
ModuleManagementTool manager = new ModuleManagementTool();
String operation = args[0];
if (operation.equals(OP_INSTALL) == true && args.length >= 3)
{
String aepFileLocation = args[1];
String warFileLocation = args[2];
boolean forceInstall = false;
boolean previewInstall = false;
boolean backup = true;
if (args.length > 3)
{
for (int i = 3; i < args.length; i++)
{
String option = args[i];
if (OPTION_VERBOSE.equals(option) == true)
{
manager.setVerbose(true);
}
else if (OPTION_FORCE.equals(option) == true)
{
forceInstall = true;
}
else if (OPTION_PREVIEW.equals(option) == true)
{
previewInstall = true;
}
else if (OPTION_NOBACKUP.equals(option) == true)
{
backup = false;
}
}
}
// Install the module
manager.installModule(aepFileLocation, warFileLocation, previewInstall, forceInstall, backup);
}
else if (OP_LIST.equals(operation) == true && args.length == 2)
{
// List the installed modules
String warFileLocation = args[1];
manager.listModules(warFileLocation);
}
else
{
outputUsage();
}
}
else
{
outputUsage();
}
}
/**
* Outputs the module management tool usage
*/
private static void outputUsage()
{
System.out.println("Module managment tool available commands:");
System.out.println("-----------------------------------------------------------\n");
System.out.println("install: Installs a AMP file into an Alfresco WAR file, updates if an older version is already installed.");
System.out.println("usage: install AMPFile WARFile options");
System.out.println("valid options: ");
System.out.println(" -verbose : enable verbose output");
System.out.println(" -force : forces installation of AMP regardless of currently installed module version");
System.out.println(" -preview : previews installation of AMP without modifying WAR file");
System.out.println(" -nobackup : indicates that no backup should be made of the WAR\n");
System.out.println("-----------------------------------------------------------\n");
System.out.println("list: Lists all the modules currently installed in an Alfresco WAR file.");
System.out.println("usage: list WARFile\n");
System.out.println("-----------------------------------------------------------\n");
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.module.tool;
import org.alfresco.error.AlfrescoRuntimeException;
/**
* Module Management Tool Exception class
*
* @author Roy Wetherall
*/
public class ModuleManagementToolException extends AlfrescoRuntimeException
{
/**
* Serial version UID
*/
private static final long serialVersionUID = -4329693103965834085L;
public ModuleManagementToolException(String msgId)
{
super(msgId);
}
public ModuleManagementToolException(String msgId, Object[] msgParams)
{
super(msgId, msgParams);
}
public ModuleManagementToolException(String msgId, Object[] msgParams, Throwable cause)
{
super(msgId, msgParams, cause);
}
public ModuleManagementToolException(String msgId, Throwable cause)
{
super(msgId, cause);
}
}

View File

@@ -0,0 +1,235 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
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.springframework.util.FileCopyUtils;
import de.schlichtherle.io.DefaultRaesZipDetector;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.io.ZipDetector;
/**
* Module management tool unit test
*
* @author Roy Wetherall
*/
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.amp");
String ampV2Location = getFileLocation(".amp", "module/test_v2.amp");
System.out.println(warLocation);
// 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");
checkForFileExistance(warLocation, files);
// Check the intstalled files
InstalledFiles installed0 = new InstalledFiles(warLocation, "test");
installed0.load();
assertNotNull(installed0);
assertEquals(7, installed0.getAdds().size());
assertEquals(1, installed0.getMkdirs().size());
assertEquals(1, installed0.getUpdates().size());
String backup = null;
String orig = null;
for (Map.Entry<String, String> update : installed0.getUpdates().entrySet())
{
checkContentsOfFile(warLocation + update.getKey(), "VERSIONONE");
checkContentsOfFile(warLocation + update.getValue(), "ORIGIONAL");
backup = update.getValue();
orig = update.getKey();
}
// Try and install same version
try
{
this.manager.installModule(ampLocation, warLocation);
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(backup);
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());
// Ensure the file has been reverted as it isnt updated in the v2.0
checkContentsOfFile(warLocation + orig, "ORIGIONAL");
// Try and install and earlier version
try
{
this.manager.installModule(ampLocation, warLocation);
fail("An earlier version of this module is already installed so an exception should have been raised since we are not forcing an overwite");
}
catch(ModuleManagementToolException exception)
{
// Pass
}
}
public void testPreviewInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test.amp");
System.out.println(warLocation);
// 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 testForcedInstall()
throws Exception
{
manager.setVerbose(true);
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test.amp");
System.out.println(warLocation);
// Initial install of module
this.manager.installModule(ampLocation, warLocation, false, false, false);
this.manager.installModule(ampLocation, warLocation, false, true, false);
}
public void testList()
throws Exception
{
String warLocation = getFileLocation(".war", "module/test.war");
String ampLocation = getFileLocation(".amp", "module/test.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 = File.createTempFile("moduleManagementToolTest-", extension);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
OutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
return file.getPath();
}
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());
}
}

View File

@@ -0,0 +1,8 @@
# The default AEP => WAR file mappings
/config=/WEB-INF/classes
/lib=/WEB-INF/lib
/licenses=/WEB-INF/licenses
/web/jsp=/jsp
/web/css=/css
/web/images=/images
/web/scripts=/scripts