view flys-artifacts/src/main/java/de/intevation/flys/artifacts/AbstractStaticStateArtifact.java @ 5779:ebec12def170

Datacage: Add a pool of builders to make it multi threadable. XML DOM is not thread safe. Therefore the old implementation only allowed one thread to use the builder at a time. As the complexity of the configuration has increased over time this has become a bottleneck of the whole application because it took quiet some time to build a result. Furthermore the builder code path is visited very frequent. So many concurrent requests were piled up resulting in long waits for the users. To mitigate this problem a round robin pool of builders is used now. Each of the pooled builders has an independent copy of the XML template and can be run in parallel. The number of builders is determined by the system property 'flys.datacage.pool.size'. It defaults to 4.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 21 Apr 2013 12:48:09 +0200
parents 79878efbdf07
children
line wrap: on
line source
package de.intevation.flys.artifacts;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.ArtifactNamespaceContext;

import de.intevation.artifacts.common.utils.XMLUtils;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.data.StateData;
import de.intevation.artifactdatabase.state.State;

import de.intevation.flys.artifacts.states.StaticState;

/**
 * A abstract baseclass for Artifacts which are using only one static state.
 *
 * This class is intended to be used without the config/stateengine to generate
 * the static state.
 *
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public abstract class AbstractStaticStateArtifact extends StaticFLYSArtifact {

    private transient StaticState staticstate;

    private static final Logger logger =
        Logger.getLogger(AbstractStaticStateArtifact.class);

    /**
     * Get a list containing the one and only State.
     * @param  context ignored.
     * @return list with one and only state.
     */
    @Override
    protected List<State> getStates(Object context) {
        ArrayList<State> states = new ArrayList<State>();
        states.add(getStaticState());
        return states;
    }


    /**
     * Get the "current" state.
     * @param cc ignored.
     * @return always the set static state.
     */
    @Override
    public State getCurrentState(Object cc) {
        return getStaticState();
    }

    /**
     * A child class must override this method to set its static state
     */
    protected abstract void initStaticState();

    protected void setStaticState(StaticState state) {
        this.staticstate = state;
    }

    protected StaticState getStaticState() {
        if (staticstate == null) {
            initStaticState();
        }
        return staticstate;
    }

    /**
     * Get the state.
     * @param context ignored.
     * @param stateID ignored.
     * @return the state.
     */
    @Override
    protected State getState(Object context, String stateID) {
        return getStaticState();
    }

    @Override
    public Document describe(Document data, CallContext cc) {
        logger.debug("Describe artifact: " + identifier());

        Document desc = XMLUtils.newDocument();

        ElementCreator creator = new ElementCreator(
            desc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root = ProtocolUtils.createRootNode(creator);
        desc.appendChild(root);

        Element name = ProtocolUtils.createArtNode(
            creator, "name",
            new String[] { "value" },
            new String[] { getName() });

        root.appendChild(name);
        root.appendChild(createOutputModes(cc, desc, creator));

        ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());

        // Add the data to an anonymous state.
        Collection<StateData> datas = getAllData();
        if (datas.size() > 0) {
            Element ui = creator.create("ui");
            Element staticE = creator.create("static");

            StaticState current = getStaticState();
            Element state = current.describeStatic(this, desc, root, cc, null);
            staticE.appendChild(state);

            for (StateData dataItem : datas) {
                Element itemelent = creator.create("data");
                creator.addAttr(itemelent, "name", dataItem.getName(), true);
                creator.addAttr(itemelent, "type", dataItem.getType(), true);
                state.appendChild(itemelent);
                Element valuement = creator.create("item");
                creator.addAttr(valuement, "label", dataItem.getDescription(), true);
                creator.addAttr(valuement, "value", dataItem.getValue().toString(), true);
                itemelent.appendChild(valuement);
            }

            ui.appendChild(staticE);
            root.appendChild(ui);
        }

        return desc;
    }
}

http://dive4elements.wald.intevation.org