1 package org.oddjob.framework; 2 3 4 import java.util.Date ; 5 6 import org.apache.log4j.MDC; 7 import org.oddjob.Resetable; 8 import org.oddjob.Stateful; 9 import org.oddjob.Structural; 10 import org.oddjob.images.IconHelper; 11 import org.oddjob.logging.Log4jArchiver; 12 import org.oddjob.state.AbstractJobStateListener; 13 import org.oddjob.structural.StatefulChildHelper; 14 import org.oddjob.structural.StructuralListener; 15 import org.oddjob.util.OddjobLockedException; 16 17 23 24 public abstract class StructuralJob extends BasePrimary 25 implements 26 Runnable , Resetable, Stateful, Structural { 27 28 32 protected StatefulChildHelper childHelper; 33 34 35 private volatile boolean executing; 36 37 public StructuralJob() { 38 completeConstruction(); 39 } 40 41 46 private void completeConstruction() { 47 childHelper = new StatefulChildHelper(this); 48 childHelper.addJobStateListener(new AbstractJobStateListener() { 49 protected void jobStateReady(Stateful source, Date time) { 50 if (!executing) { 51 StructuralJob.super.setJobStateReady(); 52 logger().info("Job [" + StructuralJob.this 53 + "] Ready."); 54 } 55 } 56 protected void jobStateComplete(Stateful source, Date time) { 57 if (!executing) { 58 StructuralJob.super.setJobStateComplete(); 59 logger().info("Job [" + StructuralJob.this 60 + "] Complete."); 61 } 62 } 63 protected void jobStateNotComplete(Stateful source, Date time) { 64 if (!executing) { 65 StructuralJob.super.setJobStateNotComplete(); 66 logger().info("Job [" + StructuralJob.this 67 + "] Not Complete."); 68 } 69 } 70 73 protected void jobStateException(Stateful source, Date time, 74 Throwable throwable) { 75 if (!executing) { 76 StructuralJob.super.setJobStateException(throwable); 77 logger().warn("Job [" + StructuralJob.this 78 + "] Exception.", throwable); 79 } 80 } 81 }); 82 } 83 84 85 89 protected boolean independant() { 90 return true; 91 } 92 93 98 abstract protected void execute() throws Throwable ; 99 100 104 public final void run() throws OddjobLockedException { 105 lock.accquire("Job executing."); 106 String oldMDC = (String )MDC.get(Log4jArchiver.MDC); 107 try { 108 MDC.put(Log4jArchiver.MDC, getLogger()); 109 if (!stateHandler.requestJobStateExecuting()) { 110 logger().debug("Can't execute job [" + this + "] because state is [" 111 + stateHandler.getJobState() + "]"); 112 return; 113 } 114 iconHelper.changeIcon(IconHelper.EXECUTING); 115 executing = true; 116 117 if (!configure()) { 119 executing = false; 120 return; 121 } 122 123 logger().info("Executing job [" + this + "]"); 124 try { 125 execute(); 126 synchronized (stateHandler) { 130 executing = false; 131 childHelper.refreshState(); 132 } 133 } 134 catch (Throwable e) { 135 executing = false; 136 logger().warn("Job Exception in [" + this + "]", e); 137 setJobStateException(e); 138 } 139 } 140 finally { 141 if (oldMDC != null) { 142 MDC.put(Log4jArchiver.MDC, oldMDC); 143 } 144 else { 145 MDC.remove(Log4jArchiver.MDC); 146 } 147 lock.release(); 148 } 149 } 150 151 159 public void stop() { 160 if (destroyed) { 161 throw new IllegalStateException ("[" + this + "] destroyed"); 162 } 163 logger().debug("Thread [" + Thread.currentThread().getName() 164 + "] requested [" + getName() + "] stop."); 165 166 if (iconHelper.currentId()== IconHelper.EXECUTING) { 167 iconHelper.changeIcon(IconHelper.STOPPING); 168 } 169 170 stop = true; 171 childHelper.stopChildren(); 172 173 synchronized (this) { 174 notifyAll(); 175 } 176 } 177 178 181 public void softReset() { 182 lock.accquire("Soft Reset in progress."); 183 try { 184 childHelper.softResetChildren(); 185 stop = false; 186 } finally { 187 lock.release(); 188 } 189 } 190 191 194 public void hardReset() { 195 lock.accquire("Hard Reset in progress."); 196 try { 197 childHelper.hardResetChildren(); 198 stop = false; 199 } finally { 200 lock.release(); 201 } 202 } 203 204 210 public void addStructuralListener(StructuralListener listener) { 211 if (destroyed) { 212 throw new IllegalStateException ("[" + this + "] destroyed"); 213 } 214 childHelper.addStructuralListener(listener); 215 } 216 217 222 public void removeStructuralListener(StructuralListener listener) { 223 childHelper.removeStructuralListener(listener); 224 } 225 226 229 public void onDestroy() { 230 childHelper.destroyAll(); 231 } 232 233 } 234 | Popular Tags |