1 package org.oddjob.jobs; 2 3 import org.oddjob.Stateful; 4 import org.oddjob.Stoppable; 5 import org.oddjob.arooa.ArooaConstants; 6 import org.oddjob.arooa.ArooaContext; 7 import org.oddjob.arooa.RuntimeConfiguration; 8 import org.oddjob.framework.SimpleJob; 9 import org.oddjob.state.JobState; 10 import org.oddjob.state.JobStateEvent; 11 import org.oddjob.state.JobStateListener; 12 13 34 35 public class WaitJob extends SimpleJob 36 implements Stoppable { 37 private static final long serialVersionUID = 20051130; 38 39 private static final long DEFAULT_WAIT_SLEEP = 1000; 40 41 46 private long pause; 47 48 53 private Object forProperty; 54 55 56 private RuntimeConfiguration rtc; 57 58 private JobState state; 59 60 private boolean not; 61 62 67 public void setPause(long delay) { 68 this.pause = delay; 69 } 70 71 76 public long getPause() { 77 return pause; 78 } 79 80 public boolean setContext(ArooaContext arooaContext) { 81 rtc = (RuntimeConfiguration) arooaContext.get( 82 ArooaConstants.CURRENTLY_CONFIGURING); 83 return super.setContext(arooaContext); 84 } 85 86 90 public int execute() throws Exception { 91 String forAttribute = null; 92 if (rtc != null) { 93 forAttribute = rtc.getAttribute("for"); 94 } 95 96 if (state != null) { 97 if (forProperty == null) { 98 throw new IllegalStateException ("'for' property must set."); 99 } 100 if (!(forProperty instanceof Stateful)) { 101 throw new IllegalStateException ("'for' property must Stateful."); 102 } 103 waitForState(); 104 } 105 else if (forAttribute != null) { 106 logger().debug("Waiting for property [" + forAttribute + "]"); 107 waitFor(); 108 } 109 else { 110 simpleWait(); 111 } 112 return 0; 113 } 114 115 protected void simpleWait() { 116 sleep(pause); 117 } 118 119 protected void waitFor() { 120 long sleep = pause; 121 if (sleep == 0) { 122 sleep = DEFAULT_WAIT_SLEEP; 123 } 124 while (!stop && forProperty == null) { 125 sleep(sleep); 126 rtc.configure(); 127 } 128 } 129 130 protected void waitForState() { 131 class Listener implements JobStateListener { 132 JobState state; 133 synchronized public void jobStateChange(JobStateEvent event) { 134 state = event.getJobState(); 135 logger().debug("State " + state); 136 synchronized (WaitJob.this) { 137 WaitJob.this.notifyAll(); 138 } 139 } 140 synchronized JobState getState() { 141 return state; 142 } 143 144 } 145 Listener l = new Listener(); 146 ((Stateful) forProperty).addJobStateListener(l); 147 148 while (true) { 149 JobState now = l.getState(); 150 if (now == state && !not) { 151 break; 152 } 153 if (now != state && not) { 154 break; 155 } 156 logger().debug("Waiting for state " + 157 (not ? "!" : "") + state + ", currently " 158 + now); 159 sleep(1000); 160 } 161 ((Stateful) forProperty).removeJobStateListener(l); 162 } 163 164 public Object getFor() { 165 return forProperty; 166 } 167 168 public void setFor(Object forProperty) { 169 this.forProperty = forProperty; 170 } 171 172 public String getState() { 173 if (state == null) { 174 return null; 175 } 176 return state.toString(); 177 } 178 179 public void setState(String state) { 180 if (state.startsWith("!")) { 181 not = true; 182 state = state.substring(state.indexOf("!") + 1); 183 } 184 this.state = JobState.stateFor(state); 185 if (this.state == null) { 186 throw new IllegalArgumentException ("Unknown state requested [" + state + "]"); 187 } 188 } 189 } 190 | Popular Tags |