1 package org.oddjob.framework; 2 3 4 import java.beans.PropertyChangeListener ; 5 import java.beans.PropertyChangeSupport ; 6 7 import org.apache.log4j.Logger; 8 import org.oddjob.Iconic; 9 import org.oddjob.arooa.ArooaRuntime; 10 import org.oddjob.arooa.Location; 11 import org.oddjob.arooa.RuntimeConfiguration; 12 import org.oddjob.images.IconHelper; 13 import org.oddjob.images.IconListener; 14 import org.oddjob.images.IconTip; 15 import org.oddjob.state.JobStateEvent; 16 import org.oddjob.state.JobStateHandler; 17 import org.oddjob.state.JobStateListener; 18 import org.oddjob.state.StateTransform; 19 import org.oddjob.util.Lock; 20 21 27 28 public abstract class BaseComponent implements Iconic { 29 30 31 protected Location location; 32 33 34 private ArooaRuntime arooaRuntime; 35 36 39 protected PropertyChangeSupport changes = new PropertyChangeSupport (this); 40 41 44 protected IconHelper iconHelper = new IconHelper(this); 45 46 49 protected JobStateHandler stateHandler = new JobStateHandler(this); 50 51 55 protected volatile boolean destroyed; 56 57 62 protected Lock lock = new Lock(); 63 64 protected abstract Logger logger(); 65 66 public void arooaRuntime(ArooaRuntime arooaRuntime) { 67 this.arooaRuntime = arooaRuntime; 68 } 69 70 public Location getLocation() { 71 return this.location; 72 } 73 74 protected RuntimeConfiguration arooaRuntime() { 75 return arooaRuntime; 76 } 77 78 protected void setJobStateReady() { 79 iconHelper.changeIcon(IconHelper.READY); 80 stateHandler.setJobStateReady(); 81 } 82 83 protected void setJobStateComplete() { 84 iconHelper.changeIcon(IconHelper.COMPLETE); 85 stateHandler.setJobStateComplete(); 86 } 87 88 protected void setJobStateNotComplete() { 89 iconHelper.changeIcon(IconHelper.NOT_COMPLETE); 90 stateHandler.setJobStateNotComplete(); 91 } 92 93 99 protected void setJobStateException(Throwable ex) { 100 stateHandler.setJobStateException(ex); 101 iconHelper.changeIcon(IconHelper.EXCEPTION); 102 } 103 104 110 protected boolean configure() { 111 if (arooaRuntime != null) { 113 try { 114 logger().debug("Configuring job [" + this + "]"); 115 arooaRuntime.configure(); 116 } catch (Exception e) { 117 logger().error("Configuration exception for job: " 118 + this + ", location: " + getLocation(), e); 119 setJobStateException(e); 120 return false; 121 } 122 } 123 return true; 124 } 125 126 protected boolean canSoftReset() { 127 return StateTransform.SOFT_RESET.canReset(stateHandler.getJobState()); 128 } 129 130 protected boolean canHardReset() { 131 return StateTransform.HARD_RESET.canReset(stateHandler.getJobState()); 132 } 133 134 143 public JobStateEvent lastJobStateEvent() { 144 if (destroyed) { 145 throw new IllegalStateException ("[" + this + "] destroyed"); 146 } 147 return stateHandler.getLastEvent(); 148 } 149 150 153 public void addJobStateListener(JobStateListener listener) { 154 if (destroyed) { 155 throw new IllegalStateException ("[" + this + "] destroyed"); 156 } 157 stateHandler.addJobStateListener(listener); 158 } 159 160 163 public void removeJobStateListener(JobStateListener listener){ 164 stateHandler.removeJobStateListener(listener); 165 } 166 167 172 public void addPropertyChangeListener(PropertyChangeListener l) { 173 if (destroyed) { 174 throw new IllegalStateException ("[" + this + "] destroyed"); 175 } 176 changes.addPropertyChangeListener(l); 177 } 178 179 184 public void removePropertyChangeListener(PropertyChangeListener l) { 185 changes.removePropertyChangeListener(l); 186 } 187 188 192 public IconTip iconForId(String iconId) { 193 return iconHelper.iconForId(iconId); 194 } 195 196 202 public void addIconListener(IconListener listener) { 203 if (destroyed) { 204 throw new IllegalStateException ("[" + this + "] destroyed"); 205 } 206 iconHelper.addIconListener(listener); 207 } 208 209 215 public void removeIconListener(IconListener listener) { 216 iconHelper.removeIconListener(listener); 217 } 218 219 223 public final void destroy() { 224 lock.accquire("Destruction in progress."); 225 try { 226 if (destroyed) { 227 throw new IllegalStateException ("[" + this + "] destroyed"); 228 } 229 logger().debug("Destroying [" + this + "]."); 230 onDestroy(); 231 destroyed = true; 232 logger().debug("Destroyed [" + this + "]"); 233 } 234 finally { 235 lock.release(); 236 } 237 } 238 239 243 public void onDestroy() { } 244 } 245 | Popular Tags |