/** * Complete the current operation and persist the current state to the disk. This will also * trigger the invalidation of outdated modules. * * @param modification the current modification * @param callback the completion callback */ private void complete( final InstallationManager.InstallationModification modification, final FinalizeCallback callback) { final List<File> processed = new ArrayList<File>(); try { try { // Update the state to invalidate and process module resources if (stateUpdater.compareAndSet(this, State.PREPARED, State.INVALIDATE) && mode == PatchingTaskContext.Mode.APPLY) { // Only invalidate modules when applying patches; on rollback files are immediately // restored for (final File invalidation : moduleInvalidations) { processed.add(invalidation); PatchModuleInvalidationUtils.processFile(invalidation, mode); } } modification.complete(); callback.completed(this); state = State.COMPLETED; } catch (Exception e) { this.moduleInvalidations.clear(); this.moduleInvalidations.addAll(processed); throw new RuntimeException(e); } } finally { if (state != State.COMPLETED) { try { modification.cancel(); } finally { try { undoChanges(); } finally { callback.operationCancelled(this); } } } else { try { if (checkForGarbageOnRestart) { final File cleanupMarker = new File(installedImage.getInstallationMetadata(), "cleanup-patching-dirs"); cleanupMarker.createNewFile(); } } catch (IOException e) { PatchLogger.ROOT_LOGGER.infof(e, "failed to create cleanup marker"); } } } }
/** * Cancel the current patch and undo the changes. * * @param callback the finalize callback */ protected void cancel(final FinalizeCallback callback) { try { undoChanges(); } finally { callback.operationCancelled(this); } }
/** * Finalize the patch. * * @param callback the finalize callback * @return the result * @throws Exception */ protected PatchingResult finalize(final FinalizeCallback callback) throws Exception { assert state == State.NEW; final Patch original = callback.getPatch(); final Patch.PatchType patchType = original.getIdentity().getPatchType(); final String patchId; if (patchType == Patch.PatchType.CUMULATIVE) { patchId = modification.getCumulativePatchID(); } else { patchId = original.getPatchId(); } try { // The processed patch, based on the recorded changes final Patch processedPatch = createProcessedPatch(original); // The rollback containing all the recorded rollback actions final RollbackPatch rollbackPatch = createRollbackPatch(patchId, patchType); callback.finishPatch(processedPatch, rollbackPatch, this); } catch (Exception e) { if (undoChanges()) { callback.operationCancelled(this); } throw e; } state = State.PREPARED; return new PatchingResult() { @Override public String getPatchId() { return original.getPatchId(); } @Override public PatchInfo getPatchInfo() { return new PatchInfo() { @Override public String getVersion() { return identityEntry.getResultingVersion(); } @Override public String getCumulativePatchID() { return identityEntry.delegate.getModifiedState().getCumulativePatchID(); } @Override public List<String> getPatchIDs() { return identityEntry.delegate.getModifiedState().getPatchIDs(); } }; } @Override public void commit() { if (state == State.PREPARED) { complete(modification, callback); } else { undoChanges(); throw new IllegalStateException(); } } @Override public void rollback() { if (undoChanges()) { try { callback.operationCancelled(IdentityPatchContext.this); } finally { modification.cancel(); } } } }; }