1 16 package org.apache.cocoon.webapps.portal.components; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.xml.transform.sax.SAXResult ; 23 import javax.xml.transform.sax.TransformerHandler ; 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 ; 46 import org.w3c.dom.NodeList ; 47 48 49 55 public final class CopletThread implements Runnable { 56 57 private Logger logger; 58 private String copletID; 59 private Map objectModel; 60 private Object [] loadedCoplet; 61 private ComponentManager manager; 62 private SourceResolver resolver; 63 private XPathProcessor processor; 64 65 78 public void init(String copletID, 79 Map objectModel, 80 Logger logger, 81 Object [] 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 97 public void run() { 98 XMLSerializer compiler = null; 99 Element copletConf = (Element )this.loadedCoplet[1]; 100 SourceParameters p = (SourceParameters)loadedCoplet[2]; 101 102 try { 103 String resource = null; 107 boolean showCustomizePage = p.getParameterAsBoolean(PortalConstants.PARAMETER_CUSTOMIZE, false); 108 if (showCustomizePage) { 109 final String value = DOMUtil.getValueOf(copletConf, "customization/@uri", (String )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 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 transformations = DOMUtil.selectNodeList(copletConf, 130 "transformation/stylesheet", this.processor); 131 XSLTProcessor xslt = null; 132 ArrayList transformers = null; 133 ArrayList 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 (); 140 sources = new ArrayList (); 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 handler = xslt.getTransformerHandler(source); 149 150 final SAXResult result = new SAXResult (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 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 Map info = new HashMap (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 local) { 210 this.logger.error("Exception during processing of coplet: " + copletID, local); 213 } catch (Throwable local) { 214 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 } } | Popular Tags |