/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.sunshine.sunspot.sunlet;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;
import org.apache.excalibur.source.SourceParameters;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.sax.XMLSerializer;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Response;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.Transformer;
import org.apache.cocoon.xml.XMLConsumer;
import org.apache.log.Logger;
import org.apache.cocoon.sunshine.SunShine;
import org.apache.cocoon.sunshine.connector.Resource;
import org.apache.cocoon.sunshine.connector.ResourceConnector;
import org.apache.cocoon.xml.IncludeXMLConsumer;
import org.apache.cocoon.sunshine.xml.XMLUtil;
import org.apache.cocoon.sunshine.sunspot.Constants;
import org.apache.cocoon.sunshine.sunspot.SunSpot;
import org.apache.cocoon.sunshine.sunspot.context.SessionContextImpl;
/**
* This is the thread for loading one sunlet in the background.
*
* @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
* @version CVS $Id: SunLetThread.java,v 1.3.2.1 2002/06/07 09:34:26 cziegeler Exp $
*/
public final class SunLetThread implements Runnable {
private Logger logger;
private String sunletID;
private Map objectModel;
private Response response;
private ResourceConnector resourceConnector;
private Object[] loadedSunlet;
private ComponentManager manager;
private SourceResolver resolver;
/**
* Initialise all instance variables.
* The main information is the loadedSunlet array:
* 0 : contains the result of the sunlet loading, <code>null</code>or
* the compiled sax events
* 1 : The sunlet configuration element from the sunlet profile
* 2 : The resource parameters
* 3 : The sunlet element
* 4 : Current time
* 5 : The timeout
* 6 : The thread (this)
* 7 : The status profile
*/
public void init(String sunletID,
Map objectModel,
Logger logger,
Response response,
ResourceConnector resourceConnector,
Object[] loadedSunlet,
ComponentManager manager,
SourceResolver resolver) {
this.sunletID = sunletID;
this.objectModel = objectModel;
this.logger = logger;
this.response = response;
this.resourceConnector = resourceConnector;
this.loadedSunlet = loadedSunlet;
this.manager = manager;
this.resolver = resolver;
}
/**
* Process one sunlet
*/
public void run() {
XMLSerializer compiler = null;
Element sunletConf = (Element)this.loadedSunlet[1];
SourceParameters p = (SourceParameters)loadedSunlet[2];
try {
// Determine the resource to load
// If the sunLet is customizable and has no customization info
// the customization resource is loaded, otherwise the resource
Resource resource = null;
boolean showCustomizePage = p.getParameterAsBoolean(Constants.PARAMETER_CUSTOMIZE, false);
if (showCustomizePage == true) {
final String value = XMLUtil.getValueOf(sunletConf, "customization/@uri", null);
if (value == null) {
this.logger.error("The sunLet '"+this.sunletID+"' is customizable but has no customization info.");
}
resource = new Resource(this.resolver, value);
}
if (resource == null) {
resource = new Resource(this.resolver, XMLUtil.getValueOf(sunletConf, "resource/@uri"));
}
boolean handlesSizable = XMLUtil.getValueAsBooleanOf(sunletConf, "configuration/handlesSizable", false);
if (handlesSizable == false && p.getParameter("size", "max").equals("max") == false) {
// do nothing here
loadedSunlet[0] = new byte[0];
} else {
compiler = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
compiler.startDocument();
XMLConsumer nextConsumer = compiler;
NodeList transformations = XMLUtil.selectNodeList(sunletConf,
"transformation/stylesheet");
Transformer xslT = null;
ArrayList transformers = new ArrayList();
ComponentSelector selector = null;
Request request = ObjectModelHelper.getRequest(this.objectModel);
try {
if (transformations != null && transformations.getLength() > 0) {
selector = (ComponentSelector) this.manager.lookup(Transformer.ROLE + "Selector");
nextConsumer = new IncludeXMLConsumer(nextConsumer);
for(int k = transformations.getLength()-1; k >=0; k--) {
xslT = (Transformer)selector.select("xslt");
transformers.add(xslT);
xslT.setup(resolver,
objectModel,
XMLUtil.getValueOfNode(transformations.item(k)),
new Parameters());
xslT.setConsumer(nextConsumer);
nextConsumer = xslT;
}
nextConsumer.startDocument();
}
switch (resource.getResourceType()) {
case ResourceConnector.RESOURCE_TYPE_CLASS: {
Sunlet theSunlet;
try {
Class loaderClass = Class.forName(resource.getResourceIdentifier());
theSunlet = (Sunlet)loaderClass.newInstance();
} catch (ClassNotFoundException cnfException) {
throw new ProcessingException("getSunlet: Class not found: " + resource.getResourceIdentifier(), cnfException);
} catch (IllegalAccessException iaException) {
throw new ProcessingException("getSunlet: Illegal Access: " + resource.getResourceIdentifier(), iaException);
} catch (InstantiationException iException) {
throw new ProcessingException("getSunlet: Instantion exception: " + resource.getResourceIdentifier(), iException);
}
theSunlet.init(objectModel, p);
theSunlet.execute(nextConsumer, nextConsumer, objectModel, p);
break;
}
default: {
boolean includeFragment = true;
boolean handlesParameters = XMLUtil.getValueAsBooleanOf(sunletConf, "configuration/handlesParameters", true);
String size = p.getParameter("size", "max");
if (resource.getResourceType() == ResourceConnector.RESOURCE_TYPE_FILE) {
// files have no possibility to evaluate minimized, so if they are not maximized
// they will be neglected
includeFragment = size.equals("max");
} else {
includeFragment = size.equals("max");
if (includeFragment == false) {
if (this.logger.isWarnEnabled() == true) {
this.logger.warn("Minimized sunlet '"+sunletID+"' not handled correctly.");
}
}
}
if ( includeFragment == true) {
String res;
if (resource.getResourceType() == ResourceConnector.RESOURCE_TYPE_URI) {
if (this.response != null) {
res = this.response.encodeURL(resource.getResourceIdentifier());
} else {
res = resource.getResourceIdentifier();
}
} else {
res = resource.getResourceIdentifier();
}
if (this.logger.isDebugEnabled() == true) {
this.logger.debug("sunSpot Loading sunlet " + sunletID);
}
// add the parameters to the request attributes
Map info = new HashMap(3);
SessionContextImpl.sunLetInfo.set(info);
info.put(Constants.SUNLETINFO_PARAMETERS, p);
info.put(Constants.SUNLETINFO_PORTALURI, request.getRequestURI());
info.put(Constants.SUNLETINFO_STATUSPROFILE, loadedSunlet[7]);
this.resourceConnector.streamXML(resource.getResourceType(), null,
res, (handlesParameters == true ? p : null),
new IncludeXMLConsumer(nextConsumer), null);
if (this.logger.isDebugEnabled() == true) {
this.logger.debug("sunSpot: Loaded sunlet " + sunletID);
}
}
}
}
if (xslT != null) {
xslT.endDocument();
xslT = null;
}
} finally {
SessionContextImpl.sunLetInfo.set(null);
if (selector != null) {
for(int i=0; i<transformers.size(); i++) {
selector.release((Component)transformers.get(i));
}
this.manager.release((Component)selector);
}
}
transformers.clear();
nextConsumer = null;
compiler.endDocument();
loadedSunlet[0] = compiler.getSAXFragment();
}
} catch (Exception local) {
// this exception is ignored and an error message is included
// later on when the sunlet is processed
this.logger.error("Exception during processing of sunlet: " + sunletID, local);
} catch (Throwable local) {
// this exception is ignored and an error message is included
// later on when the sunlet is processed
this.logger.error("Exception during processing of sunlet: " + sunletID, local);
} finally {
if (compiler != null) this.manager.release((Component)compiler);
}
loadedSunlet[6] = null;
sunletID = null;
sunletConf = null;
this.logger = null;
objectModel = null;
p = null;
response = null;
resourceConnector = null;
loadedSunlet = null;
manager = null;
resolver = null;
} // END run
} // END CLASS