1 17 package org.alfresco.repo.admin.patch; 18 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.i18n.I18NUtil; 26 import org.alfresco.repo.domain.AppliedPatch; 27 import org.alfresco.service.cmr.admin.PatchException; 28 import org.alfresco.service.descriptor.Descriptor; 29 import org.alfresco.service.descriptor.DescriptorService; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 34 43 public class PatchServiceImpl implements PatchService 44 { 45 private static final String MSG_NOT_RELEVANT = "patch.service.not_relevant"; 46 47 private static final Date ZERO_DATE = new Date (0L); 48 private static final Date INFINITE_DATE = new Date (Long.MAX_VALUE); 49 50 private static Log logger = LogFactory.getLog(PatchServiceImpl.class); 51 52 private DescriptorService descriptorService; 53 private PatchDaoService patchDaoService; 54 private List <Patch> patches; 55 56 public PatchServiceImpl() 57 { 58 this.patches = new ArrayList <Patch>(10); 59 } 60 61 public void setDescriptorService(DescriptorService descriptorService) 62 { 63 this.descriptorService = descriptorService; 64 } 65 66 public void setPatchDaoService(PatchDaoService patchDaoService) 67 { 68 this.patchDaoService = patchDaoService; 69 } 70 71 public void registerPatch(Patch patch) 72 { 73 patches.add(patch); 74 } 75 76 public boolean applyOutstandingPatches() 77 { 78 Map <String , Patch> allPatchesById = new HashMap <String , Patch>(23); 80 for (Patch patch : patches) 81 { 82 allPatchesById.put(patch.getId(), patch); 83 } 84 Map <String , AppliedPatch> appliedPatchesById = new HashMap <String , AppliedPatch>(23); 86 List <AppliedPatch> appliedPatches = patchDaoService.getAppliedPatches(); 87 for (AppliedPatch appliedPatch : appliedPatches) 88 { 89 appliedPatchesById.put(appliedPatch.getId(), appliedPatch); 90 } 91 boolean success = true; 93 for (Patch patch : allPatchesById.values()) 94 { 95 success = applyPatchAndDependencies(patch, appliedPatchesById); 97 if (!success) 98 { 99 break; 101 } 102 } 103 return success; 105 } 106 107 117 private boolean applyPatchAndDependencies(Patch patch, Map <String , AppliedPatch> appliedPatchesById) 118 { 119 String id = patch.getId(); 120 AppliedPatch appliedPatch = appliedPatchesById.get(id); 122 if (appliedPatch != null && appliedPatch.getSucceeded()) 123 { 124 return true; 126 } 127 128 List <Patch> dependencies = patch.getDependsOn(); 130 for (Patch dependencyPatch : dependencies) 131 { 132 boolean success = applyPatchAndDependencies(dependencyPatch, appliedPatchesById); 133 if (!success) 134 { 135 return false; 137 } 138 } 139 appliedPatch = applyPatch(patch); 141 if (!appliedPatch.getSucceeded()) 142 { 143 return false; 145 } 146 else 147 { 148 appliedPatchesById.put(id, appliedPatch); 150 return true; 151 } 152 } 153 154 private AppliedPatch applyPatch(Patch patch) 155 { 156 AppliedPatch appliedPatch = patchDaoService.getAppliedPatch(patch.getId()); 158 if (appliedPatch != null && appliedPatch.getSucceeded()) 159 { 160 if (logger.isDebugEnabled()) 162 { 163 logger.debug("Patch was already successfully applied: \n" + 164 " patch: " + appliedPatch); 165 } 166 return appliedPatch; 167 } 168 String report = null; 170 boolean success = false; 171 Descriptor repoDescriptor = descriptorService.getInstalledRepositoryDescriptor(); 173 boolean applies = applies(repoDescriptor, patch); 174 if (!applies) 175 { 176 report = I18NUtil.getMessage(MSG_NOT_RELEVANT, repoDescriptor.getSchema()); 178 success = true; } 180 else 181 { 182 try 184 { 185 report = patch.apply(); 186 success = true; 187 } 188 catch (PatchException e) 189 { 190 report = e.getMessage(); 192 success = false; 193 } 194 } 195 196 Descriptor serverDescriptor = descriptorService.getServerDescriptor(); 197 String server = (serverDescriptor.getVersion() + " - " + serverDescriptor.getEdition()); 198 199 if (appliedPatch == null) 201 { 202 appliedPatch = patchDaoService.newAppliedPatch(patch.getId()); 203 } 204 String patchDescription = I18NUtil.getMessage(patch.getDescription()); 206 if (patchDescription == null) 207 { 208 logger.warn("Patch description is not available: " + patch); 209 patchDescription = "No patch description available"; 210 } 211 appliedPatch.setDescription(patchDescription); 212 appliedPatch.setFixesFromSchema(patch.getFixesFromSchema()); 213 appliedPatch.setFixesToSchema(patch.getFixesToSchema()); 214 appliedPatch.setTargetSchema(patch.getTargetSchema()); appliedPatch.setAppliedToSchema(repoDescriptor.getSchema()); appliedPatch.setAppliedToServer(server); appliedPatch.setAppliedOnDate(new Date ()); appliedPatch.setSucceeded(success); appliedPatch.setWasExecuted(applies); appliedPatch.setReport(report); 222 if (logger.isDebugEnabled()) 224 { 225 logger.debug("Applied patch: \n" + appliedPatch); 226 } 227 return appliedPatch; 228 } 229 230 237 private boolean applies(Descriptor repoDescriptor, Patch patch) 238 { 239 int repoSchema = repoDescriptor.getSchema(); 240 boolean apply = patch.applies(repoSchema); 242 243 if (logger.isDebugEnabled()) 245 { 246 logger.debug("Patch schema version number check against repo version: \n" + 247 " repo schema version: " + repoDescriptor.getVersion() + "\n" + 248 " patch: " + patch); 249 } 250 return apply; 251 } 252 253 @SuppressWarnings ("unchecked") 254 public List <PatchInfo> getPatches(Date fromDate, Date toDate) 255 { 256 if (fromDate == null) 257 { 258 fromDate = ZERO_DATE; 259 } 260 if (toDate == null) 261 { 262 toDate = INFINITE_DATE; 263 } 264 List <? extends PatchInfo> appliedPatches = patchDaoService.getAppliedPatches(fromDate, toDate); 265 for (PatchInfo appliedPatch : appliedPatches) 267 { 268 patchDaoService.detach((AppliedPatch)appliedPatch); 269 } 270 return (List <PatchInfo>) appliedPatches; 272 } 273 } 274 | Popular Tags |