KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > MultiSourceValidity


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
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 /**
28  * <p>An aggregated {@link SourceValidity} for multiple sources.</p>
29  *
30  * @author <a HREF="http://www.apache.org/~sylvain">Sylvain Wallez</a>
31  * @version $Id: MultiSourceValidity.java 158097 2005-03-18 16:53:15Z vgritsenko $
32  */

33 public class MultiSourceValidity extends AbstractAggregatedValidity
34                                  implements SourceValidity {
35
36     /** <p>When validity expiration is performed.</p> */
37     private long expiry;
38
39     /** <p>The delay from <b>now</b> used to calculate the expiration time.</p> */
40     private long delay;
41
42     /** <p>An ordered list of URIs which should be checked.</p> */
43     private List JavaDoc uris = new ArrayList JavaDoc();
44
45     /** <p>Is this instance is closed (accepts modifications or is validable)? */
46     private boolean isClosed = false;
47
48     /** <p>The {@link SourceResolver} to use (transient not to be serialized). */
49     private transient SourceResolver resolver;
50
51     /**
52      * <p>The delay value indicating to check always.</p>
53      */

54     public static final int CHECK_ALWAYS = -1;
55
56     /**
57      * <p>Create a new {@link MultiSourceValidity} instance.</p>
58      *
59      * <p>If the number of milliseconds is less than <b>zero</b>, or it's sum with
60      * the number of <b>now</b> milliseconds is greater than the biggest long
61      * representable, the expiration date will be set to {@link Long#MAX_VALUE}
62      * milliseconds from the epoch.</p>
63      *
64      * @param resolver the {@link SourceResolver} used to access the sources.
65      * @param delay the number of milliseconds from <b>now</b> defining for how long
66      * this instance will be valid.
67      */

68     public MultiSourceValidity(SourceResolver resolver, long delay) {
69         /* Calculate the initial expiration time and calculate the delay */
70         this.resolver = resolver;
71         this.expiry = System.currentTimeMillis() + delay;
72         this.delay = delay;
73     }
74
75     /**
76      * <p>Add a {@link Source} to the list of {@link Source}s monitored by this
77      * instance.</p>
78      *
79      * @param src a <b>non-null</b> {@link Source}.
80      */

81     public void addSource(Source src) {
82         if (this.uris != null) {
83             SourceValidity validity = src.getValidity();
84             if (validity == null) {
85                 /* The source has no validity: this will be always be invalid. */
86                 this.uris = null;
87             } else {
88                 /* Add the validity and URI to the list */
89                 super.add(validity);
90                 this.uris.add(src.getURI());
91             }
92         }
93     }
94
95     /**
96      * <p>Close this instance, or in other words declare that no other sources will
97      * be added to this {@link MultiSourceValidity} and that checkings can be now
98      * performed.</p>
99      */

100     public void close() {
101         this.isClosed = true;
102         this.resolver = null;
103     }
104
105     /**
106      * <p>Check the validity of this {@link SourceValidity} instance.</p>
107      *
108      * @see SourceValidity#isValid()
109      */

110     public int isValid() {
111         if (System.currentTimeMillis() <= expiry) {
112             /* Validity not expired, so, don't even check */
113             return SourceValidity.VALID;
114         }
115
116         /* Re-calculate the expiry time based on the current time */
117         expiry = System.currentTimeMillis() + delay;
118
119         if (uris == null || !isClosed) {
120             /* We have not been closed (yet) or we were forced to be invalid */
121             return SourceValidity.INVALID;
122         } else {
123             /* Compute the status of all the sources listed in this instance */
124             return computeStatus(null);
125         }
126     }
127
128     /**
129      * <p>Check the validity of this instance comparing it with a (recently acquired)
130      * new {@link SourceValidity} object.</p>
131      *
132      * @see SourceValidity#isValid(SourceValidity)
133      */

134     public int isValid(SourceValidity newValidity) {
135         if (uris == null || !isClosed) {
136             /* We have not been closed (yet) or we were forced to be invalid */
137             return SourceValidity.INVALID;
138         }
139
140         /* Perform a simple class check and compute the validity of the sources */
141         if (newValidity instanceof MultiSourceValidity) {
142             return computeStatus(((MultiSourceValidity)newValidity).resolver);
143         } else {
144             /* The supplied validity is not an instance of ourselves, forget it */
145             return SourceValidity.INVALID;
146         }
147     }
148
149     /**
150      * <p>Compute the status of this instance by checking every source.</p>
151      *
152      * @param resolver The {@link SourceResolver} to use to access sources.
153      * @return {@link SourceValidity.VALID}, {@link SourceValidity.INVALID} or
154      * {@link SourceValidity.UNKNOWN} depending on the status.
155      */

156     private int computeStatus(SourceResolver resolver) {
157         /* Get the validities and analyse them one by one */
158         List JavaDoc validities = super.getValidities();
159         for (int i = 0; i < validities.size(); i++) {
160
161             /* Check the validity status */
162             SourceValidity validity = (SourceValidity) validities.get(i);
163             switch (validity.isValid()) {
164
165                 /* The current source is valid: just continue to next source */
166                 case SourceValidity.VALID:
167                     break;
168
169                 /* The current source is invalid: stop examining */
170                 case SourceValidity.INVALID:
171                     return SourceValidity.INVALID;
172
173                 /* The source validity is not known: check with the new source */
174                 case SourceValidity.UNKNOWN:
175                     /* We have no resolver: definitely don't know */
176                     if (resolver == null) {
177                         return SourceValidity.UNKNOWN;
178                     }
179
180                     /* Check the new source by asking to the resolver */
181                     Source newSrc = null;
182                     int newValidity = SourceValidity.INVALID;
183                     try {
184                         newSrc = resolver.resolveURI((String JavaDoc) this.uris.get(i));
185                         newValidity = validity.isValid(newSrc.getValidity());
186                     } catch(IOException JavaDoc ioe) {
187                         /* Swallow the IOException, but set the new validity */
188                         newValidity = SourceValidity.INVALID;
189                     } finally {
190                         /* Make sure that the source is released */
191                         if (newSrc != null) {
192                             resolver.release(newSrc);
193                         }
194                     }
195
196                     /* If the source is still valid, go to the next one */
197                     if (newValidity == SourceValidity.VALID) {
198                         break;
199                     }
200
201                     /* The source is not valid (or unknown), we invalidate the lot */
202                     return SourceValidity.INVALID;
203
204                 /* We got something _really_ odd out tof the validity, dunno. */
205                 default:
206                     return SourceValidity.INVALID;
207             }
208         }
209
210         /* All items checked successfully */
211         return SourceValidity.VALID;
212     }
213 }
214
Popular Tags