Patch checkpoint.

Service descriptor changes including introduction of schema number.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2161 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-01-20 14:45:16 +00:00
parent b4220a973b
commit 02dc9ced9b
20 changed files with 407 additions and 359 deletions

View File

@@ -21,12 +21,9 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.domain.AppliedPatch;
import org.alfresco.service.cmr.admin.PatchException;
import org.alfresco.service.cmr.admin.PatchInfo;
import org.alfresco.service.descriptor.Descriptor;
import org.alfresco.service.descriptor.DescriptorService;
import org.apache.commons.logging.Log;
@@ -50,6 +47,11 @@ public class PatchServiceImpl implements PatchService
private PatchDaoService patchDaoService;
private List<Patch> patches;
public PatchServiceImpl()
{
this.patches = new ArrayList<Patch>(10);
}
public void setDescriptorService(DescriptorService descriptorService)
{
this.descriptorService = descriptorService;
@@ -60,29 +62,11 @@ public class PatchServiceImpl implements PatchService
this.patchDaoService = patchDaoService;
}
public void setPatches(List<Patch> patches)
public void registerPatch(Patch patch)
{
this.patches = patches;
patches.add(patch);
}
public List<PatchInfo> getAppliedPatches()
{
// get all the persisted patches
List<AppliedPatch> appliedPatches = patchDaoService.getAppliedPatches();
List<PatchInfo> patchInfos = new ArrayList<PatchInfo>(appliedPatches.size());
for (AppliedPatch patch : appliedPatches)
{
PatchInfo patchInfo = new PatchInfo(patch);
patchInfos.add(patchInfo);
}
// done
if (logger.isDebugEnabled())
{
logger.debug("Retrieved list of " + patchInfos.size() + " applied patches: \n");
}
return patchInfos;
}
public boolean applyOutstandingPatches()
{
// construct a map of all known patches by ID
@@ -92,23 +76,18 @@ public class PatchServiceImpl implements PatchService
allPatchesById.put(patch.getId(), patch);
}
// construct a list of executed patches by ID
Map<String, PatchInfo> appliedPatchInfosById = new HashMap<String, PatchInfo>(23);
List<PatchInfo> appliedPatches = getAppliedPatches();
for (PatchInfo patchInfo : appliedPatches)
Map<String, AppliedPatch> appliedPatchesById = new HashMap<String, AppliedPatch>(23);
List<AppliedPatch> appliedPatches = patchDaoService.getAppliedPatches();
for (AppliedPatch appliedPatch : appliedPatches)
{
// ignore unsuccessful attempts - we need to try them again
if (!patchInfo.getSucceeded())
{
continue;
}
appliedPatchInfosById.put(patchInfo.getId(), patchInfo);
appliedPatchesById.put(appliedPatch.getId(), appliedPatch);
}
// go through all the patches and apply them where necessary
boolean success = true;
for (Patch patch : allPatchesById.values())
{
// apply the patch
success = applyPatchAndDependencies(patch, appliedPatchInfosById);
success = applyPatchAndDependencies(patch, appliedPatchesById);
if (!success)
{
// we failed to apply a patch or one of its dependencies - terminate
@@ -126,15 +105,15 @@ public class PatchServiceImpl implements PatchService
* @param patchInfos all the executed patch data. If there was a failure, then this
* is the list of successful executions only.
* @param patch the patch (containing dependencies) to apply
* @param appliedPatchInfosById already applied patches
* @param appliedPatchesById already applied patches keyed by their ID
* @return Returns true if the patch and all its dependencies were successfully applied.
*/
private boolean applyPatchAndDependencies(Patch patch, Map<String, PatchInfo> appliedPatchInfosById)
private boolean applyPatchAndDependencies(Patch patch, Map<String, AppliedPatch> appliedPatchesById)
{
String id = patch.getId();
// check if it has already been done
PatchInfo patchInfo = appliedPatchInfosById.get(id);
if (patchInfo != null && patchInfo.getSucceeded())
AppliedPatch appliedPatch = appliedPatchesById.get(id);
if (appliedPatch != null && appliedPatch.getSucceeded())
{
// this has already been done
return true;
@@ -144,7 +123,7 @@ public class PatchServiceImpl implements PatchService
List<Patch> dependencies = patch.getDependsOn();
for (Patch dependencyPatch : dependencies)
{
boolean success = applyPatchAndDependencies(dependencyPatch, appliedPatchInfosById);
boolean success = applyPatchAndDependencies(dependencyPatch, appliedPatchesById);
if (!success)
{
// a patch failed to be applied
@@ -152,8 +131,8 @@ public class PatchServiceImpl implements PatchService
}
}
// all the dependencies were successful
patchInfo = applyPatch(patch);
if (!patchInfo.getSucceeded())
appliedPatch = applyPatch(patch);
if (!appliedPatch.getSucceeded())
{
// this was a failure
return false;
@@ -161,26 +140,24 @@ public class PatchServiceImpl implements PatchService
else
{
// it was successful - add it to the map of successful patches
appliedPatchInfosById.put(id, patchInfo);
appliedPatchesById.put(id, appliedPatch);
return true;
}
}
private PatchInfo applyPatch(Patch patch)
private AppliedPatch applyPatch(Patch patch)
{
// get the patch from the DAO
AppliedPatch appliedPatch = patchDaoService.getAppliedPatch(patch.getId());
if (appliedPatch != null && appliedPatch.getSucceeded())
{
// it has already been applied
PatchInfo patchInfo = new PatchInfo(appliedPatch);
// done
if (logger.isDebugEnabled())
{
logger.debug("Patch was already successfully applied: \n" +
" patch: " + patchInfo);
" patch: " + appliedPatch);
}
return patchInfo;
return appliedPatch;
}
// the execution report
String report = null;
@@ -211,28 +188,29 @@ public class PatchServiceImpl implements PatchService
success = false;
}
}
// create a record for the execution
appliedPatch = patchDaoService.newAppliedPatch(patch.getId());
// fill in the record's details
appliedPatch.setDescription(patch.getDescription());
appliedPatch.setApplyToVersion(patch.getApplyToVersion());
appliedPatch.setSucceeded(success);
appliedPatch.setAppliedOnVersion(repoDescriptor.getVersion());
appliedPatch.setAppliedOnDate(new Date());
appliedPatch.setReport(report);
// create the info for returning
PatchInfo patchInfo = new PatchInfo(appliedPatch);
appliedPatch.setFixesFromSchema(patch.getFixesFromSchema());
appliedPatch.setFixesToSchema(patch.getFixesToSchema());
appliedPatch.setTargetSchema(patch.getTargetSchema()); // the schema the server is expecting
appliedPatch.setAppliedToSchema(repoDescriptor.getSchema()); // the old schema of the repo
appliedPatch.setAppliedOnDate(new Date()); // the date applied
appliedPatch.setSucceeded(success); // whether or not the patch succeeded
appliedPatch.setReport(report); // additional, human-readable, status
// done
if (logger.isDebugEnabled())
{
logger.debug("Applied patch: \n" + patchInfo);
logger.debug("Applied patch: \n" + appliedPatch);
}
return patchInfo;
return appliedPatch;
}
/**
* Check whether or not the patch should be applied to the repository, given the descriptor.
* This helper is required to
* Check whether the patch is applicable to the particular version of the repository.
*
* @param repoDescriptor contains the version details of the repository
* @param patch the patch whos version must be checked
@@ -240,76 +218,15 @@ public class PatchServiceImpl implements PatchService
*/
private boolean applies(Descriptor repoDescriptor, Patch patch)
{
// resolve the version numbers of the repo
int[] repoVersions = new int[] {0, 0, 0};
try
{
if (repoDescriptor.getVersionMajor() != null)
{
repoVersions[0] = Integer.parseInt(repoDescriptor.getVersionMajor());
}
if (repoDescriptor.getVersionMinor() != null)
{
repoVersions[1] = Integer.parseInt(repoDescriptor.getVersionMinor());
}
if (repoDescriptor.getVersionRevision() != null)
{
repoVersions[2] = Integer.parseInt(repoDescriptor.getVersionRevision());
}
}
catch (NumberFormatException e)
{
throw new AlfrescoRuntimeException("Non-numeric repository version are not allowed: " +
repoDescriptor.getVersion());
}
// by default, the patch won't apply. All the revision numbers will have to be greater
// than that of the repo to apply
int[] applyToVersions = new int[] {0, 0, 0};
try
{
// break the patch version up
String applyToVersion = patch.getApplyToVersion();
StringTokenizer tokens = new StringTokenizer(applyToVersion, ".");
for (int i = 0; i < 3 && tokens.hasMoreTokens(); i++)
{
String versionStr = tokens.nextToken();
applyToVersions[i] = Integer.parseInt(versionStr);
}
}
catch (NumberFormatException e)
{
throw new AlfrescoRuntimeException("Non-numeric patch apply version: " + patch);
}
// compare each version number
// in the event of a match, we don't apply as the patch version is the
boolean apply = true;
for (int i = 0; i < 3; i++)
{
if (repoVersions[i] < applyToVersions[i])
{
// repo is old and patch is newer - we definitely need to apply
apply = true;
break;
}
else if (repoVersions[i] > applyToVersions[i])
{
// the repo version number is high enough (repo is new enough) that patch doesn't apply
apply = false;
break;
}
else
{
// so far, the two match
}
}
int repoSchema = repoDescriptor.getSchema();
// does the patch apply?
boolean apply = patch.applies(repoSchema);
// done
if (logger.isDebugEnabled())
{
logger.debug("Patch version number check against repo version: \n" +
" repo version: " + repoDescriptor.getVersion() + "\n" +
logger.debug("Patch schema version number check against repo version: \n" +
" repo schema version: " + repoDescriptor.getVersion() + "\n" +
" patch: " + patch);
}
return apply;