1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.excalibur.source.Source; 23 import org.apache.excalibur.source.SourceResolver; 24 import org.apache.excalibur.source.SourceValidity; 25 import org.apache.excalibur.source.impl.validity.AbstractAggregatedValidity; 26 27 33 public class MultiSourceValidity extends AbstractAggregatedValidity 34 implements SourceValidity { 35 36 37 private long expiry; 38 39 40 private long delay; 41 42 43 private List uris = new ArrayList (); 44 45 46 private boolean isClosed = false; 47 48 49 private transient SourceResolver resolver; 50 51 54 public static final int CHECK_ALWAYS = -1; 55 56 68 public MultiSourceValidity(SourceResolver resolver, long delay) { 69 70 this.resolver = resolver; 71 this.expiry = System.currentTimeMillis() + delay; 72 this.delay = delay; 73 } 74 75 81 public void addSource(Source src) { 82 if (this.uris != null) { 83 SourceValidity validity = src.getValidity(); 84 if (validity == null) { 85 86 this.uris = null; 87 } else { 88 89 super.add(validity); 90 this.uris.add(src.getURI()); 91 } 92 } 93 } 94 95 100 public void close() { 101 this.isClosed = true; 102 this.resolver = null; 103 } 104 105 110 public int isValid() { 111 if (System.currentTimeMillis() <= expiry) { 112 113 return SourceValidity.VALID; 114 } 115 116 117 expiry = System.currentTimeMillis() + delay; 118 119 if (uris == null || !isClosed) { 120 121 return SourceValidity.INVALID; 122 } else { 123 124 return computeStatus(null); 125 } 126 } 127 128 134 public int isValid(SourceValidity newValidity) { 135 if (uris == null || !isClosed) { 136 137 return SourceValidity.INVALID; 138 } 139 140 141 if (newValidity instanceof MultiSourceValidity) { 142 return computeStatus(((MultiSourceValidity)newValidity).resolver); 143 } else { 144 145 return SourceValidity.INVALID; 146 } 147 } 148 149 156 private int computeStatus(SourceResolver resolver) { 157 158 List validities = super.getValidities(); 159 for (int i = 0; i < validities.size(); i++) { 160 161 162 SourceValidity validity = (SourceValidity) validities.get(i); 163 switch (validity.isValid()) { 164 165 166 case SourceValidity.VALID: 167 break; 168 169 170 case SourceValidity.INVALID: 171 return SourceValidity.INVALID; 172 173 174 case SourceValidity.UNKNOWN: 175 176 if (resolver == null) { 177 return SourceValidity.UNKNOWN; 178 } 179 180 181 Source newSrc = null; 182 int newValidity = SourceValidity.INVALID; 183 try { 184 newSrc = resolver.resolveURI((String ) this.uris.get(i)); 185 newValidity = validity.isValid(newSrc.getValidity()); 186 } catch(IOException ioe) { 187 188 newValidity = SourceValidity.INVALID; 189 } finally { 190 191 if (newSrc != null) { 192 resolver.release(newSrc); 193 } 194 } 195 196 197 if (newValidity == SourceValidity.VALID) { 198 break; 199 } 200 201 202 return SourceValidity.INVALID; 203 204 205 default: 206 return SourceValidity.INVALID; 207 } 208 } 209 210 211 return SourceValidity.VALID; 212 } 213 } 214 | Popular Tags |