/*
* 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 .
*/
package org.alfresco.repo.module.tool;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.cmr.module.ModuleDetails;
import org.alfresco.service.cmr.module.ModuleInstallState;
import org.alfresco.util.VersionNumber;
import org.safehaus.uuid.UUIDGenerator;
import de.schlichtherle.truezip.file.TArchiveDetector;
import de.schlichtherle.truezip.file.TConfig;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileInputStream;
import de.schlichtherle.truezip.file.TVFS;
import de.schlichtherle.truezip.fs.archive.zip.ZipDriver;
import de.schlichtherle.truezip.socket.sl.IOPoolLocator;
/**
* 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.
*
* @since 2.0
*
* @author Roy Wetherall
* @author Derek Hulley
*/
public class ModuleManagementTool implements LogOutput
{
/** Location of the default mapping properties file */
private static final String DEFAULT_FILE_MAPPING_PROPERTIES = "org/alfresco/repo/module/tool/default-file-mapping.properties";
/** Location of the AMP-specific mappings file */
private static final String FILE_MAPPING_PROPERTIES = "file-mapping.properties";
/**
* The property to add to a custom {@link #FILE_MAPPING_PROPERTIES file-mapping.properties} to inherit the default values.
* The default is true.
*/
private static final String PROP_INHERIT_DEFAULT = "include.default";
/** Standard directories found in the alfresco war */
public static final String BACKUP_DIR = WarHelper.MODULE_NAMESPACE_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_UNINSTALL = "uninstall";
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";
private static final String OPTION_DIRECTORY = "-directory";
private static final String OPTION_PURGE = "-purge";
private static final String OPTION_HELP = "-help";
private static final int ERROR_EXIT_CODE = 1;
private static final int SUCCESS_EXIT_CODE = 0;
/** File mapping properties */
private Properties defaultFileMappingProperties;
/** Indicates the current verbose setting */
private boolean verbose = false;
WarHelper warHelper = new WarHelperImpl(this);
/**
* Constructor
*/
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);
try
{
this.defaultFileMappingProperties.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 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) throws IOException
{
installModules(directory, warFileLocation, false, false, true);
}
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(new TFile(warFileLocation),true);
backupWAR = false; //Set it to false so a backup doesn't occur again.
}
installModules(dir, warFileLocation, preview, forceInstall,backupWAR);
}
else
{
throw new ModuleManagementToolException("Invalid directory '" + directoryLocation + "'");
}
}
private void installModules(java.io.File dir, String warFileLocation, boolean preview, boolean forceInstall, boolean backupWAR)
{
java.io.File[] children = dir.listFiles();
if (children != null)
{
for (java.io.File child : children)
{
if (child.isFile() == true && child.getName().toLowerCase().endsWith(".amp") == true)
{
installModule(child.getPath(), warFileLocation, preview, forceInstall, backupWAR);
}
else
{
installModules(child, warFileLocation, preview, forceInstall, backupWAR);
}
}
}
}
/**
* 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 replaced regardless 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
{
outputVerboseMessage("Installing AMP '" + ampFileLocation + "' into WAR '" + warFileLocation + "'");
TFile warFile = new TFile(warFileLocation);
if (!warFile.exists())
{
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
TFile moduleDir = new TFile(warFileLocation + WarHelper.MODULE_NAMESPACE_DIR);
if (moduleDir.exists() == false)
{
moduleDir.mkdir();
}
backupWar(warFile, backupWAR);
}
// Get the details of the installing module
String propertiesLocation = ampFileLocation + "/module.properties";
ModuleDetails installingModuleDetails = ModuleDetailsHelper.createModuleDetailsFromPropertyLocation(propertiesLocation);
if (installingModuleDetails == null)
{
throw new ModuleManagementToolException("No module.properties file has been found in the installing .amp file '" + ampFileLocation + "'");
}
String installingId = installingModuleDetails.getId();
VersionNumber installingVersion = installingModuleDetails.getVersion();
//A series of checks
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(warFile, installingModuleDetails);
//Check module directory exists
TFile moduleInstallDirectory = new TFile(warFileLocation + WarHelper.MODULE_NAMESPACE_DIR+ "/" + installingId);
if (preview == false && moduleInstallDirectory.exists() == false)
{
moduleInstallDirectory.mkdir();
}
uninstallIfNecessary(warFileLocation, installedModuleDetails, preview, forceInstall, installingVersion);
outputVerboseMessage("Adding files relating to version '" + installingVersion + "' of module '" + installingId + "'");
InstalledFiles installedFiles = new InstalledFiles(warFileLocation, installingId);
Properties directoryChanges = calculateChanges(ampFileLocation, warFileLocation, preview, forceInstall, installedFiles);
if (preview == false)
{
//Now actually do the changes
if (directoryChanges != null && directoryChanges.size() > 0)
{
for (Entry