1 16 package org.springframework.webflow.definition.registry; 17 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.Map ; 21 import java.util.TreeMap ; 22 23 import org.springframework.core.style.ToStringCreator; 24 import org.springframework.util.Assert; 25 import org.springframework.webflow.definition.FlowDefinition; 26 27 35 public class FlowDefinitionRegistryImpl implements FlowDefinitionRegistry { 36 37 40 private Map flowDefinitions = new TreeMap (); 41 42 45 private FlowDefinitionRegistry parent; 46 47 49 public String [] getFlowDefinitionIds() { 50 return (String [])flowDefinitions.keySet().toArray(new String [flowDefinitions.size()]); 51 } 52 53 public int getFlowDefinitionCount() { 54 return flowDefinitions.size(); 55 } 56 57 public boolean containsFlowDefinition(String id) { 58 Assert.hasText(id, "The flow id is required"); 59 return flowDefinitions.get(id) != null; 60 } 61 62 public void refresh() throws FlowDefinitionConstructionException { 63 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 64 try { 65 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 67 LinkedList needsReindexing = new LinkedList (); 68 Iterator it = flowDefinitions.entrySet().iterator(); 69 while (it.hasNext()) { 70 Map.Entry entry = (Map.Entry )it.next(); 71 String key = (String )entry.getKey(); 72 FlowDefinitionHolder holder = (FlowDefinitionHolder)entry.getValue(); 73 holder.refresh(); 74 if (!holder.getFlowDefinitionId().equals(key)) { 75 needsReindexing.add(new Indexed(key, holder)); 76 } 77 } 78 it = needsReindexing.iterator(); 79 while (it.hasNext()) { 80 Indexed indexed = (Indexed)it.next(); 81 reindex(indexed.holder, indexed.key); 82 } 83 } 84 finally { 85 Thread.currentThread().setContextClassLoader(loader); 86 } 87 } 88 89 public void refresh(String flowId) 90 throws NoSuchFlowDefinitionException, FlowDefinitionConstructionException { 91 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 92 try { 93 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 95 FlowDefinitionHolder holder = getFlowDefinitionHolder(flowId); 96 holder.refresh(); 97 if (!holder.getFlowDefinitionId().equals(flowId)) { 98 reindex(holder, flowId); 99 } 100 } 101 finally { 102 Thread.currentThread().setContextClassLoader(loader); 103 } 104 } 105 106 108 public FlowDefinition getFlowDefinition(String id) 109 throws NoSuchFlowDefinitionException, FlowDefinitionConstructionException { 110 Assert.hasText(id, 111 "Unable to load a flow definition: no flow id was provided. Please provide a valid flow identifier."); 112 try { 113 return getFlowDefinitionHolder(id).getFlowDefinition(); 114 } 115 catch (NoSuchFlowDefinitionException e) { 116 if (parent != null) { 117 return parent.getFlowDefinition(id); 119 } 120 throw e; 121 } 122 } 123 124 126 public void setParent(FlowDefinitionRegistry parent) { 127 this.parent = parent; 128 } 129 130 public FlowDefinition[] getFlowDefinitions() throws FlowDefinitionConstructionException { 131 FlowDefinition[] flows = new FlowDefinition[flowDefinitions.size()]; 132 Iterator it = flowDefinitions.values().iterator(); 133 int i = 0; 134 while (it.hasNext()) { 135 FlowDefinitionHolder holder = (FlowDefinitionHolder)it.next(); 136 flows[i] = holder.getFlowDefinition(); 137 i++; 138 } 139 return flows; 140 } 141 142 public void registerFlowDefinition(FlowDefinitionHolder flowHolder) { 143 Assert.notNull(flowHolder, "The flow definition holder to register is required"); 144 index(flowHolder); 145 } 146 147 152 public void removeFlowDefinition(String id) { 153 Assert.hasText(id, "The flow id is required"); 154 flowDefinitions.remove(id); 155 } 156 157 159 164 private void reindex(FlowDefinitionHolder holder, String oldId) { 165 flowDefinitions.remove(oldId); 166 index(holder); 167 } 168 169 173 private void index(FlowDefinitionHolder holder) { 174 Assert.hasText(holder.getFlowDefinitionId(), "The flow holder to index must return a non-blank flow id"); 175 flowDefinitions.put(holder.getFlowDefinitionId(), holder); 176 } 177 178 182 private FlowDefinitionHolder getFlowDefinitionHolder(String id) throws NoSuchFlowDefinitionException { 183 FlowDefinitionHolder flowHolder = (FlowDefinitionHolder)flowDefinitions.get(id); 184 if (flowHolder == null) { 185 throw new NoSuchFlowDefinitionException(id, getFlowDefinitionIds()); 186 } 187 return flowHolder; 188 } 189 190 196 private static class Indexed { 197 198 private String key; 199 200 private FlowDefinitionHolder holder; 201 202 public Indexed(String key, FlowDefinitionHolder holder) { 203 this.key = key; 204 this.holder = holder; 205 } 206 } 207 208 public String toString() { 209 return new ToStringCreator(this).append("flowDefinitions", flowDefinitions).append("parent", parent).toString(); 210 } 211 } | Popular Tags |