KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > PreemptiveLoader


1 /*
2  * Copyright 1999-2004 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.transformation.helpers;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.cocoon.caching.CachedResponse;
26 import org.apache.cocoon.components.sax.XMLSerializer;
27 import org.apache.cocoon.components.source.SourceUtil;
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceResolver;
30 import org.apache.excalibur.source.SourceValidity;
31 import org.apache.excalibur.source.impl.validity.ExpiresValidity;
32
33 /**
34  * The preemptive loader is a singleton that runs in the background
35  * and loads content into the cache.
36  *
37  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
38  * @version CVS $Id: PreemptiveLoader.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  * @since 2.1
40  */

41 public final class PreemptiveLoader {
42
43     private static final PreemptiveLoader instance = new PreemptiveLoader();
44     
45     /** The list of proxies currently used for caching */
46     private Map JavaDoc cacheStorageProxyMap = new HashMap JavaDoc(20);
47     /** The list of URIs to load */
48     private List JavaDoc loadList = new ArrayList JavaDoc(50);
49     /** Is this thread still alive? */
50     boolean alive = false;
51     
52     /**
53      * Return singleton.
54      * @return PreemptiveLoader
55      */

56     static PreemptiveLoader getInstance() {
57         return instance;
58     }
59     
60     /**
61      * Add a new task
62      * @param proxy The cache to store the content
63      * @param uri The absolute URI to load
64      * @param expires The expires information used for the cache
65      */

66     public void add(IncludeCacheStorageProxy proxy, String JavaDoc uri, long expires) {
67         boolean addItem = true;
68         List JavaDoc uriList = (List JavaDoc)this.cacheStorageProxyMap.get(proxy);
69         if ( null == uriList ) {
70              uriList = new ArrayList JavaDoc(50);
71              this.cacheStorageProxyMap.put(proxy, uriList);
72         } else {
73             synchronized (uriList) {
74                 // nothing to do: uri is alredy in list
75
if (uriList.contains(uri)) {
76                    addItem = false;
77                }
78             }
79         }
80         if ( addItem ) {
81             uriList.add(uri);
82             this.loadList.add(new Object JavaDoc[] {proxy, uri, new Long JavaDoc(expires), uriList});
83         }
84
85         synchronized (this.cacheStorageProxyMap) {
86             this.cacheStorageProxyMap.notify();
87         }
88     }
89     
90     /**
91      * Start the preemptive loading
92      * @param manager A component manager
93      * @param resolver A source resolver
94      * @param logger A logger
95      */

96     public void process(ServiceManager manager,
97                          SourceResolver resolver,
98                          Logger logger) {
99         this.alive = true;
100         if (logger.isDebugEnabled()) {
101             logger.debug("PreemptiveLoader: Starting preemptive loading");
102         }
103
104         while (this.alive) {
105             while (this.loadList.size() > 0) {
106                 Object JavaDoc[] object = (Object JavaDoc[])this.loadList.get(0);
107                 final String JavaDoc uri = (String JavaDoc)object[1];
108                 this.loadList.remove(0);
109                 synchronized (object[3]) {
110                     ((List JavaDoc)object[3]).remove(uri);
111                 }
112                 
113                 Source source = null;
114                 XMLSerializer serializer = null;
115
116                 try {
117                     if (logger.isDebugEnabled()) {
118                         logger.debug("PreemptiveLoader: Loading " + uri);
119                     }
120
121                     source = resolver.resolveURI(uri);
122                     serializer = (XMLSerializer)manager.lookup(XMLSerializer.ROLE);
123                 
124                     SourceUtil.toSAX(source, serializer);
125                 
126                     SourceValidity[] validities = new SourceValidity[1];
127                     validities[0] = new ExpiresValidity(((Long JavaDoc)object[2]).longValue() * 1000); // milliseconds!
128
CachedResponse response = new CachedResponse(validities,
129                                                                  (byte[])serializer.getSAXFragment());
130                     ((IncludeCacheStorageProxy)object[0]).put(uri, response);
131                      
132                 } catch (Exception JavaDoc ignore) {
133                     // all exceptions are ignored!
134
} finally {
135                     resolver.release( source );
136                     manager.release( serializer );
137                 }
138                 if (logger.isDebugEnabled()) {
139                     logger.debug("PreemptiveLoader: Finished loading " + uri);
140                 }
141             }
142             synchronized (this.cacheStorageProxyMap) {
143                 try {
144                     this.cacheStorageProxyMap.wait();
145                 } catch (InterruptedException JavaDoc e) {
146                 }
147             }
148         }
149         if (logger.isDebugEnabled()) {
150             logger.debug("PreemptiveLoader: Finished preemptive loading");
151         }
152     }
153     
154     /**
155      * Stop the loading.
156      * The loader stops when all tasks from the queue are processed.
157      */

158     synchronized public void stop() {
159         this.alive = false;
160         synchronized (this.cacheStorageProxyMap) {
161             this.cacheStorageProxyMap.notify();
162         }
163     }
164 }
165
Popular Tags