1 16 package org.springframework.webflow.engine.builder; 17 18 import java.io.IOException ; 19 20 import org.springframework.core.io.Resource; 21 import org.springframework.webflow.definition.FlowDefinition; 22 import org.springframework.webflow.definition.registry.FlowDefinitionConstructionException; 23 import org.springframework.webflow.definition.registry.FlowDefinitionHolder; 24 import org.springframework.webflow.engine.Flow; 25 import org.springframework.webflow.util.ResourceHolder; 26 27 44 public class RefreshableFlowDefinitionHolder implements FlowDefinitionHolder { 45 46 49 private FlowDefinition flowDefinition; 50 51 54 private FlowAssembler assembler; 55 56 60 private long lastModified; 61 62 66 private boolean assembling; 67 68 74 public RefreshableFlowDefinitionHolder(FlowAssembler assembler) { 75 this.assembler = assembler; 76 } 77 78 public String getFlowDefinitionId() { 79 return assembler.getFlowId(); 80 } 81 82 public synchronized FlowDefinition getFlowDefinition() throws FlowDefinitionConstructionException { 83 if (assembling) { 84 return getFlowBuilder().getFlow(); 86 } 87 if (!isAssembled()) { 88 lastModified = calculateLastModified(); 89 assembleFlow(); 90 } 91 else { 92 refreshIfChanged(); 93 } 94 return flowDefinition; 95 } 96 97 public synchronized void refresh() throws FlowBuilderException { 98 assembleFlow(); 99 } 100 101 103 106 protected FlowBuilder getFlowBuilder() { 107 return assembler.getFlowBuilder(); 108 } 109 110 113 protected void refreshIfChanged() { 114 if (this.lastModified == -1) { 115 return; 117 } 118 long calculatedLastModified = calculateLastModified(); 119 if (this.lastModified < calculatedLastModified) { 120 assembleFlow(); 121 this.lastModified = calculatedLastModified; 122 } 123 } 124 125 130 protected long calculateLastModified() { 131 if (getFlowBuilder() instanceof ResourceHolder) { 132 Resource resource = ((ResourceHolder)getFlowBuilder()).getResource(); 133 try { 134 return resource.getFile().lastModified(); 135 } 136 catch (IOException e) { 137 } 139 } 140 return -1; 141 } 142 143 147 protected long getLastModified() { 148 return lastModified; 149 } 150 151 155 protected void assembleFlow() throws FlowBuilderException { 156 try { 157 assembling = true; 158 flowDefinition = assembler.assembleFlow(); 159 } 160 finally { 161 assembling = false; 162 } 163 } 164 165 169 protected boolean isAssembled() { 170 return flowDefinition != null; 171 } 172 173 176 protected boolean isAssembling() { 177 return assembling; 178 } 179 } | Popular Tags |