KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > portal > components > CopletThread


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.webapps.portal.components;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.xml.transform.sax.SAXResult JavaDoc;
23 import javax.xml.transform.sax.TransformerHandler JavaDoc;
24
25 import org.apache.avalon.framework.component.Component;
26 import org.apache.avalon.framework.component.ComponentManager;
27 import org.apache.avalon.framework.logger.Logger;
28 import org.apache.cocoon.components.sax.XMLSerializer;
29 import org.apache.cocoon.components.source.SourceUtil;
30 import org.apache.cocoon.environment.ObjectModelHelper;
31 import org.apache.cocoon.environment.Request;
32 import org.apache.cocoon.webapps.portal.PortalConstants;
33 import org.apache.cocoon.webapps.portal.context.SessionContextImpl;
34 import org.apache.cocoon.xml.ContentHandlerWrapper;
35 import org.apache.cocoon.xml.IncludeXMLConsumer;
36 import org.apache.cocoon.xml.XMLConsumer;
37 import org.apache.cocoon.xml.dom.DOMUtil;
38
39 import org.apache.excalibur.source.Source;
40 import org.apache.excalibur.source.SourceParameters;
41 import org.apache.excalibur.source.SourceResolver;
42 import org.apache.excalibur.xml.xpath.XPathProcessor;
43 import org.apache.excalibur.xml.xslt.XSLTProcessor;
44
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47
48
49 /**
50  * This is the thread for loading one coplet in the background.
51  *
52  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
53  * @version CVS $Id: CopletThread.java 30932 2004-07-29 17:35:38Z vgritsenko $
54 */

55 public final class CopletThread implements Runnable JavaDoc {
56
57     private Logger logger;
58     private String JavaDoc copletID;
59     private Map JavaDoc objectModel;
60     private Object JavaDoc[] loadedCoplet;
61     private ComponentManager manager;
62     private SourceResolver resolver;
63     private XPathProcessor processor;
64     
65     /**
66      * Initialise all instance variables.
67      * The main information is the loadedCoplet array:
68      * 0 : contains the result of the coplet loading, <code>null</code>or
69      * the compiled sax events
70      * 1 : The coplet configuration element from the coplet profile
71      * 2 : The resource parameters
72      * 3 : The coplet element
73      * 4 : Current time
74      * 5 : The timeout
75      * 6 : The thread (this)
76      * 7 : The status profile
77      */

78     public void init(String JavaDoc copletID,
79                      Map JavaDoc objectModel,
80                      Logger logger,
81                      Object JavaDoc[] loadedCoplet,
82                      ComponentManager manager,
83                      SourceResolver resolver,
84                      XPathProcessor processor) {
85         this.copletID = copletID;
86         this.objectModel = objectModel;
87         this.logger = logger;
88         this.loadedCoplet = loadedCoplet;
89         this.manager = manager;
90         this.resolver = resolver;
91         this.processor = processor;
92     }
93
94     /**
95      * Process one coplet
96      */

97     public void run() {
98         XMLSerializer compiler = null;
99         Element JavaDoc copletConf = (Element JavaDoc)this.loadedCoplet[1];
100         SourceParameters p = (SourceParameters)loadedCoplet[2];
101
102         try {
103             // Determine the resource to load
104
// If the coplet is customizable and has no customization info
105
// the customization resource is loaded, otherwise the resource
106
String JavaDoc resource = null;
107             boolean showCustomizePage = p.getParameterAsBoolean(PortalConstants.PARAMETER_CUSTOMIZE, false);
108             if (showCustomizePage) {
109                 final String JavaDoc value = DOMUtil.getValueOf(copletConf, "customization/@uri", (String JavaDoc)null, this.processor);
110                 if (value == null) {
111                     this.logger.error("The coplet '"+this.copletID+"' is customizable but has no customization info.");
112                 }
113                 resource = value;
114             }
115             if (resource == null) {
116                 resource = DOMUtil.getValueOf(copletConf, "resource/@uri", this.processor);
117             }
118             boolean handlesSizable = DOMUtil.getValueAsBooleanOf(copletConf, "configuration/handlesSizable", false, this.processor);
119
120             if (!handlesSizable && !p.getParameter("size", "max").equals("max")) {
121                 // do nothing here
122
loadedCoplet[0] = new byte[0];
123             } else {
124
125                 compiler = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
126                 compiler.startDocument();
127
128                 XMLConsumer nextConsumer = compiler;
129                 NodeList JavaDoc transformations = DOMUtil.selectNodeList(copletConf,
130                                                         "transformation/stylesheet", this.processor);
131                 XSLTProcessor xslt = null;
132                 ArrayList JavaDoc transformers = null;
133                 ArrayList JavaDoc sources = null;
134                 Request request = ObjectModelHelper.getRequest(this.objectModel);
135                 XMLConsumer stylesheet =null;
136                 
137                 try {
138                     if (transformations != null && transformations.getLength() > 0) {
139                         transformers = new ArrayList JavaDoc();
140                         sources = new ArrayList JavaDoc();
141                         
142                         nextConsumer = new IncludeXMLConsumer(nextConsumer);
143                         for(int k = transformations.getLength()-1; k >=0; k--) {
144                             xslt = (XSLTProcessor)this.manager.lookup(XSLTProcessor.ROLE);
145                             transformers.add(xslt);
146                             Source source = this.resolver.resolveURI(DOMUtil.getValueOfNode(transformations.item(k)));
147                             sources.add(source);
148                             TransformerHandler JavaDoc handler = xslt.getTransformerHandler(source);
149
150                             final SAXResult JavaDoc result = new SAXResult JavaDoc(nextConsumer);
151                             result.setLexicalHandler(nextConsumer);
152                             handler.setResult(result);
153                             nextConsumer = new ContentHandlerWrapper(handler, handler);
154                             stylesheet = nextConsumer;
155                         }
156                         stylesheet.startDocument();
157                     }
158                     boolean includeFragment = true;
159                     boolean handlesParameters = DOMUtil.getValueAsBooleanOf(copletConf, "configuration/handlesParameters", true, this.processor);
160                     String JavaDoc size = p.getParameter("size", "max");
161                     includeFragment = size.equals("max");
162                     if (!includeFragment) {
163                         if (this.logger.isWarnEnabled()) {
164                             this.logger.warn("Minimized coplet '"+copletID+"' not handled correctly.");
165                         }
166                     }
167                     if ( includeFragment ) {
168                         if (this.logger.isDebugEnabled() ) {
169                             this.logger.debug("portal: Loading coplet " + copletID);
170                         }
171                         // add the parameters to the request attributes
172
Map JavaDoc info = new HashMap JavaDoc(3);
173                         SessionContextImpl.copletInfo.set(info);
174                         info.put(PortalConstants.COPLETINFO_PARAMETERS, p);
175                         info.put(PortalConstants.COPLETINFO_PORTALURI, request.getRequestURI());
176                         info.put(PortalConstants.COPLETINFO_STATUSPROFILE, loadedCoplet[7]);
177                         XMLConsumer xc = new IncludeXMLConsumer(nextConsumer);
178                         Source source = null;
179                         try {
180                             source = SourceUtil.getSource(resource,
181                                                           null,
182                                                           (handlesParameters ? p : null),
183                                                           resolver);
184                             SourceUtil.toSAX(source, xc);
185                         } finally {
186                             resolver.release(source);
187                         }
188
189                         if (this.logger.isDebugEnabled()) {
190                             this.logger.debug("portal: Loaded coplet " + copletID);
191                         }
192                     }
193                     if ( stylesheet != null ) {
194                         stylesheet.endDocument();
195                     }
196                 } finally {
197                     SessionContextImpl.copletInfo.set(null);
198                     if ( transformers != null ) {
199                         for(int i=0; i < transformers.size(); i++) {
200                             this.manager.release( (Component)transformers.get(i));
201                             this.resolver.release( (Source)sources.get(i));
202                         }
203                     }
204                 }
205                 nextConsumer = null;
206                 compiler.endDocument();
207                 loadedCoplet[0] = compiler.getSAXFragment();
208             }
209         } catch (Exception JavaDoc local) {
210             // this exception is ignored and an error message is included
211
// later on when the coplet is processed
212
this.logger.error("Exception during processing of coplet: " + copletID, local);
213         } catch (Throwable JavaDoc local) {
214             // this exception is ignored and an error message is included
215
// later on when the coplet is processed
216
this.logger.error("Exception during processing of coplet: " + copletID, local);
217         } finally {
218             if (compiler != null) {
219                 this.manager.release(compiler);
220             }
221         }
222         loadedCoplet[6] = null;
223         copletID = null;
224         copletConf = null;
225         this.logger = null;
226         objectModel = null;
227         p = null;
228         loadedCoplet = null;
229         manager = null;
230         resolver = null;
231     } // END run
232
} // END CLASS
233
Popular Tags