1 package org.oddjob.framework; 2 3 4 import java.util.EventObject ; 5 6 import org.apache.log4j.Logger; 7 import org.oddjob.arooa.ArooaConstants; 8 import org.oddjob.arooa.ArooaContext; 9 import org.oddjob.arooa.ArooaRuntime; 10 import org.oddjob.arooa.ConfigurationListener; 11 import org.oddjob.arooa.Location; 12 import org.oddjob.images.IconHelper; 13 import org.xml.sax.Locator ; 14 15 21 22 public abstract class BasePrimary extends BaseComponent { 23 24 private static int instanceCount; 25 26 27 private Logger theLogger; 28 29 34 private String name; 35 36 41 protected transient volatile boolean stop; 42 43 48 protected Logger logger() { 49 if (theLogger == null) { 50 int count = 0; 51 synchronized (BaseComponent.class) { 52 count = instanceCount++; 53 } 54 theLogger = Logger.getLogger(this.getClass().getName() 55 + "." + count); 56 } 57 return theLogger; 58 } 59 60 73 public boolean setContext(ArooaContext context) { 74 Locator locator = context.getLocator(); 75 if (locator != null) { 76 Location location = new Location(locator.getSystemId(), 77 locator.getLineNumber(), 78 locator.getColumnNumber()); 79 this.location = location; 80 } 81 ArooaRuntime arooaRuntime = (ArooaRuntime) context.get(ArooaConstants.CURRENTLY_CONFIGURING); 82 if (arooaRuntime != null) { 83 arooaRuntime.addConfigurationListener(new ConfigurationListener() { 84 public void configurationComplete(EventObject event) { 85 BasePrimary.this.configurationComplete(); 86 } 87 }); 88 } 89 arooaRuntime(arooaRuntime); 90 return independant(); 91 } 92 93 protected boolean independant() { 94 return false; 95 } 96 97 protected void setJobStateReady() { 98 super.setJobStateReady(); 99 stop = false; 100 } 101 102 107 protected void sleep(long waitTime) { 108 if (destroyed) { 109 throw new IllegalStateException ("[" + this + "] destroyed"); 110 } 111 iconHelper.changeIcon(IconHelper.SLEEPING); 112 logger().debug("[" + this + "] Sleeping for [" + waitTime + "] milli seconds." ); 113 try { 114 synchronized (this) { 115 wait(waitTime); 116 } 117 } catch (InterruptedException e) { 118 logger().debug("Sleep interupted."); 119 } 120 iconHelper.changeIcon(IconHelper.EXECUTING); 121 } 122 123 124 128 protected void configurationComplete() { 129 130 } 131 132 137 synchronized public void setName(String name) { 138 if (destroyed) { 139 throw new IllegalStateException ("[" + this + "] destroyed"); 140 } 141 String old = this.name; 142 this.name = name; 143 changes.firePropertyChange("name", old, name); 144 } 145 146 151 synchronized public String getName() { 152 if (name == null && arooaRuntime() != null) { 153 return arooaRuntime().getAttribute("name"); 154 } 155 return name; 156 } 157 158 161 public String getLogger() { 162 return logger().getName(); 163 } 164 165 168 public void setLogger(String logger) { 169 if (logger == null) { 170 return; 171 } 172 if (theLogger != null) { 173 theLogger.debug("Logger being replaced by [" + logger + "]"); 174 } 175 theLogger = Logger.getLogger(logger); 176 } 177 178 181 public String toString() { 182 if (getName() == null) { 183 return getClass().getName(); 184 } 185 else { 186 return getName(); 187 } 188 } 189 190 } 191 | Popular Tags |