1 17 package org.alfresco.repo.admin.patch; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 import java.util.Collections ; 22 import java.util.List ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.repo.transaction.TransactionUtil; 26 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork; 27 import org.alfresco.service.cmr.admin.PatchException; 28 import org.alfresco.service.transaction.TransactionService; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 38 public abstract class AbstractPatch implements Patch 39 { 40 47 public static final String ERR_PROPERTY_NOT_SET = "patch.general.property_not_set"; 48 49 private static Log logger = LogFactory.getLog(AbstractPatch.class); 50 51 private String id; 52 private int fixesFromSchema; 53 private int fixesToSchema; 54 private int targetSchema; 55 private String description; 56 57 private List <Patch> dependsOn; 58 59 private boolean applied; 60 61 private PatchService patchService; 62 private TransactionService transactionService; 63 64 public AbstractPatch() 65 { 66 this.fixesFromSchema = -1; 67 this.fixesToSchema = -1; 68 this.targetSchema = -1; 69 this.applied = false; 70 this.dependsOn = Collections.emptyList(); 71 } 72 73 @Override 74 public String toString() 75 { 76 StringBuilder sb = new StringBuilder (256); 77 sb.append("Patch") 78 .append("[ id=").append(id) 79 .append(", description=").append(description) 80 .append(", fixesFromSchema=").append(fixesFromSchema) 81 .append(", fixesToSchema=").append(fixesToSchema) 82 .append(", targetSchema=").append(targetSchema) 83 .append("]"); 84 return sb.toString(); 85 } 86 87 90 public void setPatchService(PatchService patchService) 91 { 92 this.patchService = patchService; 93 } 94 95 98 public void setTransactionService(TransactionService transactionService) 99 { 100 this.transactionService = transactionService; 101 } 102 103 106 public void init() 107 { 108 if (patchService == null) 109 { 110 throw new AlfrescoRuntimeException("Mandatory property not set: patchService"); 111 } 112 patchService.registerPatch(this); 113 } 114 115 public String getId() 116 { 117 return id; 118 } 119 120 124 public void setId(String id) 125 { 126 this.id = id; 127 } 128 129 public int getFixesFromSchema() 130 { 131 return fixesFromSchema; 132 } 133 134 139 public void setFixesFromSchema(int version) 140 { 141 if (version < 0) 142 { 143 throw new IllegalArgumentException ("The 'fixesFromSchema' property may not be less than 0"); 144 } 145 this.fixesFromSchema = version; 146 if (fixesToSchema < fixesFromSchema) 148 { 149 setFixesToSchema(this.fixesFromSchema); 150 } 151 } 152 153 public int getFixesToSchema() 154 { 155 return fixesToSchema; 156 } 157 158 164 public void setFixesToSchema(int version) 165 { 166 if (version < fixesFromSchema) 167 { 168 throw new IllegalArgumentException ("'fixesToSchema' must be greater than or equal to 'fixesFromSchema'"); 169 } 170 this.fixesToSchema = version; 171 } 172 173 public int getTargetSchema() 174 { 175 return targetSchema; 176 } 177 178 186 public void setTargetSchema(int version) 187 { 188 if (version <= fixesToSchema) 189 { 190 throw new IllegalArgumentException ("'targetSchema' must be greater than 'fixesToSchema'"); 191 } 192 this.targetSchema = version; 193 } 194 195 public String getDescription() 196 { 197 return description; 198 } 199 200 203 public void setDescription(String description) 204 { 205 this.description = description; 206 } 207 208 public List <Patch> getDependsOn() 209 { 210 return this.dependsOn; 211 } 212 218 public void setDependsOn(List <Patch> dependsOn) 219 { 220 this.dependsOn = dependsOn; 221 } 222 223 public boolean applies(int version) 224 { 225 return ((this.fixesFromSchema <= version) && (version <= fixesToSchema)); 226 } 227 228 234 protected final void checkPropertyNotNull(Object value, String name) 235 { 236 if (value == null) 237 { 238 throw new PatchException(ERR_PROPERTY_NOT_SET, "bootstrapView", this); 239 } 240 } 241 242 247 protected void checkProperties() 248 { 249 checkPropertyNotNull(id, "id"); 251 checkPropertyNotNull(description, "description"); 252 checkPropertyNotNull(transactionService, "transactionService"); 253 if (fixesFromSchema == -1 || fixesToSchema == -1 || targetSchema == -1) 254 { 255 throw new AlfrescoRuntimeException( 256 "Patch properties 'fixesFromSchema', 'fixesToSchema' and 'targetSchema' have not all been set on this patch: \n" + 257 " patch: " + this); 258 } 259 } 260 261 266 public synchronized String apply() throws PatchException 267 { 268 if (applied) 270 { 271 throw new AlfrescoRuntimeException("The patch has already been executed: \n" + 272 " patch: " + this); 273 } 274 checkProperties(); 276 try 278 { 279 TransactionWork<String > patchWork = new TransactionWork<String >() 280 { 281 public String doWork() throws Exception 282 { 283 String report = applyInternal(); 284 return report; 286 }; 287 }; 288 String report = TransactionUtil.executeInNonPropagatingUserTransaction(transactionService, patchWork); 289 applied = true; 291 if (logger.isDebugEnabled()) 293 { 294 logger.debug("Patch successfully applied: \n" + 295 " patch: " + this + "\n" + 296 " report: " + report); 297 } 298 return report; 299 } 300 catch (PatchException e) 301 { 302 throw e; 304 } 305 catch (Throwable e) 306 { 307 Throwable cause = e.getCause(); 309 if (cause != null && cause instanceof PatchException) 310 { 311 throw (PatchException) cause; 312 } 313 String report = makeReport(e); 315 throw new PatchException(report); 317 } 318 } 319 320 326 private String makeReport(Throwable e) 327 { 328 StringWriter stringWriter = new StringWriter (1024); 329 PrintWriter printWriter = new PrintWriter (stringWriter, true); 330 try 331 { 332 e.printStackTrace(printWriter); 333 return stringWriter.toString(); 334 } 335 finally 336 { 337 printWriter.close(); 338 } 339 } 340 341 349 protected abstract String applyInternal() throws Exception ; 350 } 351 | Popular Tags |