All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.se.xslt.XsltSuManager Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.xslt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.jbidescriptor.generated.Jbi;
import org.ow2.petals.component.framework.jbidescriptor.generated.Provides;
import org.ow2.petals.component.framework.se.AbstractServiceEngine;
import org.ow2.petals.component.framework.se.ServiceEngineServiceUnitManager;
import org.ow2.petals.component.framework.su.ServiceUnitDataHandler;
import org.ow2.petals.se.xslt.model.XslParameterBean;
import org.ow2.petals.se.xslt.model.XsltConfiguration;
import org.ow2.petals.se.xslt.model.XsltConfigurationHandler;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ebmwebsourcing.easycommons.lang.StringHelper;

/**
 * @author Roland Naudin - EBM WebSourcing
 * @author Vincent Zurczak - EBM WebSourcing
 */
public class XsltSuManager extends ServiceEngineServiceUnitManager {

    /**
     * The SU parameter that defines the style sheet path.
     */
    private static final String CONFIG_XSL_PATH = "stylesheet";

    /**
     * The SU parameter that defines the transformer factory class name.
     */
    private static final String CONFIG_TRANSFORMER_FACTORY_CLASS_NAME = "xslt-engine-factory-class-name";
    
    /**
     * The SU parameter that defines the transformer pool minimum size.
     */
    private static final String CONFIG_TRANSFORMER_POOL_SIZE_MIN = "xslt-engine-pool-size-min";
    
    /**
     * The default minimum size of the transformer pool.
     */
    private static final int DEFAULT_TRANSFORMER_POOL_SIZE_MIN = 1;
    
    /**
     * The SU parameter that defines the transformer pool maximum size.
     */
    private static final String CONFIG_TRANSFORMER_POOL_SIZE_MAX = "xslt-engine-pool-size-max";
    
    /**
     * The default maximum size of the transformer pool.
     */
    private static final int DEFAULT_TRANSFORMER_POOL_SIZE_MAX = 1;
    
    /**
     * The SU parameter that defines the name of output attachments.
     */
    private static final String CONFIG_OUTPUT_ATTACHMENT_NAME = "output-attachment-name";

    /**
     * The default name of output attachments.
     */
    private static final String DEFAULT_OUTPUT_ATTACHMENT_NAME = "petals-se-xslt.out";
    
    /**
     * The SU parameter that defines the list of XSL parameters.
     */
    private static final String CONFIG_XSL_PARAMETERS = "xsl-parameters";

    /**
     * The SU parameter that defines a XSL parameter.
     */
    private static final String CONFIG_XSL_PARAMETER = "xsl-parameter";

    /**
     * A map associating service-unit names and XSLT configuration.
     */
    private final Map suNameToXsltConfigurationHandler =
        new ConcurrentHashMap ();



    /**
     * Default constructor.
     * @param component the XSLT component
     */
    public XsltSuManager(final AbstractServiceEngine component) {
        super( component );
    }

    @Override
    protected void doDeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {

        final Jbi jbiDescriptor = suDH.getDescriptor();
        final String serviceUnitName = suDH.getName();

        // Check that there is only one Provides section in the SU
        if (!jbiDescriptor.getServices().getConsumes().isEmpty())
            throw new PEtALSCDKException( "'Consumes' sections are not supported by this component." );

        final List providesList = jbiDescriptor.getServices().getProvides();
        if (providesList.size() != 1)
            throw new PEtALSCDKException( "Only one 'Provides' section is allowed by this component." );


        // Check that the required parameters are defined
        Provides provides = providesList.get( 0 );
        final SuConfigurationParameters extensions = suDH.getConfigurationExtensions(provides);

        String xslPath = extensions.get( CONFIG_XSL_PATH );
        if( xslPath != null )
            xslPath = xslPath.trim();

        if( StringHelper.isNullOrEmpty( xslPath ))
            throw new PEtALSCDKException( "The '" + CONFIG_XSL_PATH + "' parameter is not defined or is empty." );

        // The transformer factory class is optional.
        // We simply set a default value and log its absence if it is not present
        String transformerFactoryClassName = extensions.get( CONFIG_TRANSFORMER_FACTORY_CLASS_NAME );
        if( StringHelper.isNullOrEmpty( transformerFactoryClassName )) {
            transformerFactoryClassName = null;
            
            String msg = serviceUnitName
                    + ": the '"
                    + CONFIG_TRANSFORMER_FACTORY_CLASS_NAME
                    + "' parameter is not defined or is empty. The system transformer factory is used.";
            
            if(this.logger != null && this.logger.isLoggable( Level.FINE )) {
                this.logger.fine( msg );
            }
        }
        
        // The transformer pool minimum size is optional.
        // We simply set a default value if it is not present
        int transformerPoolSizeMin = getIntParameterValue(this.logger, serviceUnitName, extensions,
                CONFIG_TRANSFORMER_POOL_SIZE_MIN, DEFAULT_TRANSFORMER_POOL_SIZE_MIN);
        
        // The transformer pool maximum size is optional.
        // We simply set a default value if it is not present
        int transformerPoolSizeMax = getIntParameterValue(this.logger, serviceUnitName, extensions,
                 CONFIG_TRANSFORMER_POOL_SIZE_MAX, DEFAULT_TRANSFORMER_POOL_SIZE_MAX);
        
        // The attachment parameter is optional.
        // Set a default value and simply log its absence if it is not present 
        String outputAttachmentName = extensions.get( CONFIG_OUTPUT_ATTACHMENT_NAME );
        if( outputAttachmentName != null ) {
            outputAttachmentName = outputAttachmentName.trim();
        }
        if( StringHelper.isNullOrEmpty( outputAttachmentName )) {
            outputAttachmentName = DEFAULT_OUTPUT_ATTACHMENT_NAME;
            
            if(this.logger != null && this.logger.isLoggable( Level.FINE )) {
                String msg = serviceUnitName + ": the '" + CONFIG_OUTPUT_ATTACHMENT_NAME
                + "' parameter is not defined or is empty.";
                this.logger.fine( msg );
            }
        }

        // Get the XSL parameters
        // Get them from the JBI descriptor, the parameters being complex
        Map xslParamNameToXslParamBean = new HashMap();
        for (Element elt : provides.getAny()) {
            if (!elt.getNodeName().endsWith(CONFIG_XSL_PARAMETERS))
                continue;

            NodeList children = elt.getElementsByTagNameNS("*", CONFIG_XSL_PARAMETER);
            if (this.logger != null && this.logger.isLoggable(Level.FINE)) {
                String msg = serviceUnitName + ": " + children.getLength();
                String suffix;
                if (children.getLength() < 2)
                    suffix = " XSL parameter was found.";
                else
                    suffix = " XSL parameters were found.";

                this.logger.fine(msg + suffix);
            }

            for (int i = 0; i < children.getLength(); i++) {
                Element child = (Element) children.item(i);
                String name = child.getAttribute("name");
                boolean overridable = Boolean.valueOf(child.getAttribute("overridable"));

                String value = null;
                if (child.getTextContent() != null)
                    value = child.getTextContent().trim();

                if (!StringHelper.isNullOrEmpty(name) && !StringHelper.isNullOrEmpty(value)) {
                    XslParameterBean xslParamBean = new XslParameterBean(name, value, overridable);
                    xslParamNameToXslParamBean.put(name, xslParamBean);

                    if (this.logger != null && this.logger.isLoggable(Level.FINE)) {
                        String msg = serviceUnitName + ": a XSL parameter was found (" + name
                        + " = " + value + ").";
                        this.logger.fine(msg);
                    }
                }
            }
        }


        // Create a model bean to avoid the use of the extensions
        // Who knows if the CDK will not clean up the configuration later on...
        XsltConfiguration config = new XsltConfiguration(
                xslPath,
                transformerFactoryClassName,
                transformerPoolSizeMin,
                transformerPoolSizeMax,
                suDH.getInstallRoot(),
                outputAttachmentName,
                provides.getEndpointName(),
                serviceUnitName, xslParamNameToXslParamBean);

        // Create a handler and register these elements
        XsltConfigurationHandler handler = new XsltConfigurationHandler( config, this.logger );

        handler.check(this.logger);
        
        handler.start(this.logger);

        getComponent().registerXsltConfigurationHandler(config.getEndpointName(), handler);
        this.suNameToXsltConfigurationHandler.put( serviceUnitName, handler );
    }

    private static final int getIntParameterValue(Logger logger, String serviceUnitName,
            final SuConfigurationParameters extensions, String parameterName, int defaultValue) {
        int value;
        
        String valueStr = extensions.get( parameterName );
        if(!StringHelper.isNullOrEmpty(valueStr)) {
            try {
                value = Integer.parseInt(valueStr);
            } catch(NumberFormatException e) {
                value = defaultValue;
                String msg = serviceUnitName + ": the '" + parameterName
                + "' parameter is not an integer. The default value '" + defaultValue + "' is used";
                logger.warning( msg );
            }
        } else {
            value = defaultValue;
        }
        
        return value;
    }

    @Override
    protected void doUndeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {
        XsltConfigurationHandler handler = this.suNameToXsltConfigurationHandler.remove(suDH.getName());
        getComponent().removeXsltConfigurationHandler(handler.getEndpointName());
        handler.stop();
    }

    @Override
    protected XsltSe getComponent() {
        return (XsltSe) super.getComponent();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy