void saveResult(Result result) throws IOException { File file = new File(patchDir, result.getPatch().getId() + ".patch.result"); Properties props = new Properties(); FileOutputStream fos = new FileOutputStream(file); try { props.put(DATE, Long.toString(result.getDate())); props.put(UPDATES + "." + COUNT, Integer.toString(result.getUpdates().size())); int i = 0; for (BundleUpdate update : result.getUpdates()) { props.put( UPDATES + "." + Integer.toString(i) + "." + SYMBOLIC_NAME, update.getSymbolicName()); props.put(UPDATES + "." + Integer.toString(i) + "." + NEW_VERSION, update.getNewVersion()); props.put( UPDATES + "." + Integer.toString(i) + "." + NEW_LOCATION, update.getNewLocation()); props.put( UPDATES + "." + Integer.toString(i) + "." + OLD_VERSION, update.getPreviousVersion()); props.put( UPDATES + "." + Integer.toString(i) + "." + OLD_LOCATION, update.getPreviousLocation()); i++; } props.put(STARTUP, ((ResultImpl) result).getStartup()); String overrides = ((ResultImpl) result).getOverrides(); if (overrides != null) { props.put(OVERRIDES, overrides); } props.store(fos, "Installation results for patch " + result.getPatch().getId()); } finally { close(fos); } }
void rollback(Patch patch, boolean force) throws PatchException { Result result = patch.getResult(); if (result == null) { throw new PatchException("Patch " + patch.getId() + " is not installed"); } Bundle[] allBundles = bundleContext.getBundles(); List<BundleUpdate> badUpdates = new ArrayList<BundleUpdate>(); for (BundleUpdate update : result.getUpdates()) { boolean found = false; Version v = Version.parseVersion(update.getNewVersion()); for (Bundle bundle : allBundles) { if (stripSymbolicName(bundle.getSymbolicName()) .equals(stripSymbolicName(update.getSymbolicName())) && bundle.getVersion().equals(v)) { found = true; break; } } if (!found) { badUpdates.add(update); } } if (!badUpdates.isEmpty() && !force) { StringBuilder sb = new StringBuilder(); sb.append("Unable to rollback patch ") .append(patch.getId()) .append(" because of the following missing bundles:\n"); for (BundleUpdate up : badUpdates) { sb.append("\t") .append(up.getSymbolicName()) .append("/") .append(up.getNewVersion()) .append("\n"); } throw new PatchException(sb.toString()); } Map<Bundle, String> toUpdate = new HashMap<Bundle, String>(); for (BundleUpdate update : result.getUpdates()) { Version v = Version.parseVersion(update.getNewVersion()); for (Bundle bundle : allBundles) { if (stripSymbolicName(bundle.getSymbolicName()) .equals(stripSymbolicName(update.getSymbolicName())) && bundle.getVersion().equals(v)) { toUpdate.put(bundle, update.getPreviousLocation()); } } } try { applyChanges(toUpdate); writeFully( new File(System.getProperty("karaf.base"), "etc/startup.properties"), ((ResultImpl) result).getStartup()); writeFully( new File(System.getProperty("karaf.base"), "etc/overrides.properties"), ((ResultImpl) result).getOverrides()); } catch (Exception e) { throw new PatchException( "Unable to rollback patch " + patch.getId() + ": " + e.getMessage(), e); } ((PatchImpl) patch).setResult(null); File file = new File(patchDir, result.getPatch().getId() + ".patch.result"); file.delete(); }