1 4 package org.oddjob.webapp.model; 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import org.oddjob.Iconic; 12 import org.oddjob.Stateful; 13 import org.oddjob.Structural; 14 import org.oddjob.images.IconEvent; 15 import org.oddjob.images.IconListener; 16 import org.oddjob.logging.ConsoleArchiver; 17 import org.oddjob.logging.LogArchiver; 18 import org.oddjob.logging.LogLevel; 19 import org.oddjob.logging.LogListener; 20 import org.oddjob.monitor.model.Describer; 21 import org.oddjob.monitor.model.ExplorerModel; 22 import org.oddjob.state.JobStateEvent; 23 import org.oddjob.state.JobStateListener; 24 import org.oddjob.util.ThreadManager; 25 26 31 public class JobInfoLookup { 32 33 34 private final IconRegistry iconRegistry; 35 36 37 private Map jobs = new HashMap (); 38 39 40 private String rootRefId; 41 42 43 private final ExplorerModel explorerModel = new ExplorerModel(); 44 45 50 public JobInfoLookup(IconRegistry iconRegistry) { 51 if (iconRegistry == null) { 52 throw new NullPointerException ("Icon Registry must not be null"); 53 } 54 this.iconRegistry = iconRegistry; 55 } 56 57 public void setThreadManager(ThreadManager threadManager) { 58 explorerModel.setThreadManager(threadManager); 59 } 60 61 68 public void setRoot(Object root) { 69 if (root == null) { 70 throw new NullPointerException ("Root node must not be null"); 71 } 72 if (rootRefId != null) { 73 throw new IllegalStateException ("Can't change root."); 74 } 75 76 explorerModel.setRoot(root); 77 78 TreeNode rootNode = new TreeNode(explorerModel, this); 79 if (root instanceof Structural) { 80 ((Structural) root).addStructuralListener(rootNode); 81 } 82 83 jobs.put(rootNode.getRefId(), rootNode); 84 85 this.rootRefId = rootNode.getRefId(); 86 } 87 88 93 public String getRootRefId() { 94 return rootRefId; 95 } 96 97 102 public void setLogFormat(String logFormat) { 103 explorerModel.setLogFormat(logFormat); 104 } 105 106 112 public JobStateEvent stateFor(String refId) { 113 Object object = objectFor(refId); 114 if (object == null) { 115 throw new IllegalStateException ("[" + refId + "] does not exist!"); 116 } 117 118 if (!(object instanceof Stateful)) { 119 return null; 120 } 121 122 class JSL implements JobStateListener { 123 JobStateEvent lastEvent; 124 public void jobStateChange(org.oddjob.state.JobStateEvent event) { 125 lastEvent = event; 126 }; 127 } 128 JSL jsl = new JSL(); 129 130 Stateful stateful = (Stateful) object; 131 stateful.addJobStateListener(jsl); 132 stateful.removeJobStateListener(jsl); 133 return jsl.lastEvent; 134 } 135 136 142 public NodeInfo nodeInfoFor(String refId) { 143 TreeNode treeNode = (TreeNode) jobs.get(refId); 144 Object object = objectFor(refId); 145 146 String iconId = null; 147 148 if (object instanceof Iconic) { 149 class IL implements IconListener { 150 IconEvent lastEvent; 151 public void iconEvent(IconEvent event) { 152 lastEvent = event; 153 } 154 } 155 IL il = new IL(); 156 157 ((Iconic) object).addIconListener(il); 158 ((Iconic) object).removeIconListener(il); 159 iconId = il.lastEvent.getIconId(); 160 iconRegistry.register(iconId, (Iconic) object); 161 } 162 163 return new NodeInfo(treeNode.getNodeName(), 164 treeNode.getChildRefIds(), 165 iconId); 166 } 167 168 174 public Map propertiesFor(String refId) { 175 Object object = objectFor(refId); 176 return Describer.describe(object); 177 } 178 179 185 public List consoleEventsFor(String refId) { 186 TreeNode treeNode = (TreeNode) jobs.get(refId); 187 if (treeNode == null) { 188 throw new IllegalStateException ("[" + refId + "] does not exist!"); 189 } 190 191 class LL implements LogListener { 192 List list = new ArrayList (); 193 public void logEvent(org.oddjob.logging.LogEvent logEvent) { 194 list.add(logEvent); 195 } 196 } 197 198 LL ll = new LL(); 199 Object object = treeNode.getComponent(); 200 ConsoleArchiver consoleArchiver = treeNode.getExplorerContext().getConsoleArchiver(); 201 consoleArchiver.addConsoleListener(ll, object, -1, 1000); 202 consoleArchiver.removeConsoleListener(ll, object); 203 return ll.list; 204 } 205 206 212 public List logEventsFor(String refId) { 213 TreeNode treeNode = (TreeNode) jobs.get(refId); 214 if (treeNode == null) { 215 throw new IllegalStateException ("[" + refId + "] does not exist!"); 216 } 217 218 class LL implements LogListener { 219 List list = new ArrayList (); 220 public void logEvent(org.oddjob.logging.LogEvent logEvent) { 221 list.add(logEvent); 222 } 223 } 224 LL ll = new LL(); 225 226 Object object = treeNode.getComponent(); 227 LogArchiver logArchiver = treeNode.getExplorerContext().getLogArchiver(); 228 logArchiver.addLogListener(ll, object, LogLevel.DEBUG, -1, 1000); 229 logArchiver.removeLogListener(ll, object); 230 return ll.list; 231 } 232 233 public WebJobActions actionsFor(String refId) { 234 TreeNode treeNode = (TreeNode) jobs.get(refId); 235 if (treeNode == null) { 236 throw new IllegalStateException ("[" + refId + "] does not exist!"); 237 } 238 WebJobActions actions = new WebJobActions(); 239 actions.select(treeNode.getComponent(), treeNode.getExplorerContext()); 240 return actions; 241 } 242 243 253 Object objectFor(String refId) throws IllegalStateException { 254 TreeNode treeNode = (TreeNode) jobs.get(refId); 255 if (treeNode == null) { 256 throw new IllegalStateException ("[" + refId + "] does not exist!"); 257 } 258 return treeNode.getComponent(); 259 } 260 261 266 void addChild(TreeNode treeNode) { 267 jobs.put(treeNode.getRefId(), treeNode); 268 } 269 270 275 void removeChild(String refId) { 276 jobs.remove(refId); 277 } 278 279 283 public void destroy() { 284 explorerModel.destroy(); 285 jobs.clear(); 286 } 287 } 288 | Popular Tags |