view artifact-database/src/main/java/de/intevation/artifactdatabase/ProxyArtifact.java @ 93:e27cf9c84eb8

Unified imports. artifacts/trunk@849 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Mar 2010 15:57:40 +0000
parents 68285f7bc476
children 933bbc9fc11f
line wrap: on
line source
package de.intevation.artifactdatabase;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

/**
 * The proxy artifact is a wrapper around another artifact. It simply forwards
 * the interface calls to this underlaying artifact.
 * The reason for using proxy artifacts is enable the workflow to exchange
 * artifacts at any time by something else without losing the concrete
 * artifact. From the outside it always looks like there is only one
 * distinct artifact.<br>
 *
 * An inner artifact is able to replace itself by indirectly hand over
 * the replacement via the call context to the proxy artifact.<br>
 * To do so the proxied artifact has to call
 * <code>callContext.getContextValue(EPLACE_PROXY, replacement);</code>.
 * After the current call (describe, feed, advance and out) of the proxied
 * artifact is finished the proxy artifact replaces the former proxied artifact
 * with the replacement.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class ProxyArtifact
extends      DefaultArtifact
{
    /**
     * Key to signal that the proxied artifact should be replaced.
     */
    public static final Object REPLACE_PROXY = new Object();

    private static Logger logger = Logger.getLogger(ProxyArtifact.class);

    /**
     * The proxied artifact.
     */
    protected Artifact proxied;

    /**
     * Default constructor.
     */
    public ProxyArtifact() {
    }

    /**
     * Constructor to create a new proxy artifact around a given artifact.
     * @param proxied The artifact to be proxied.
     */
    public ProxyArtifact(Artifact proxied) {
        this.proxied = proxied;
    }

    /**
     * The currently proxied artifact.
     * @return The proxied artifact.
     */
    public Artifact getProxied() {
        return proxied;
    }

    /**
     * Explicitly set the proxied artifacts.
     * @param proxied
     */
    public void setProxied(Artifact proxied) {
        this.proxied = proxied;
    }

    @Override
    public void setIdentifier(String identifier) {
        this.identifier = identifier;

        if (proxied != null)
            proxied.setIdentifier(identifier);
    }

    /**
     * Method to check if the current proxied artifact should be replaced
     * by a new one coming from the call context.
     * @param callContext
     */
    protected void checkReplacement(CallContext callContext) {
        Object replacement = callContext.getContextValue(REPLACE_PROXY);
        if (replacement instanceof Artifact) {
            setProxied((Artifact)replacement);
        }
    }

    @Override
    public String hash() {
        return proxied != null
            ? proxied.hash()
            : super.hash();
    }

    @Override
    public Document describe(Document data, CallContext context) {
        try {
            return proxied != null
                ? proxied.describe(data, context)
                : super.describe(data, context);
        }
        finally {
            checkReplacement(context);
        }
    }

    @Override
    public Document advance(Document target, CallContext context) {
        try {
            return proxied != null
                ? proxied.advance(target, context)
                : super.advance(target, context);
        }
        finally {
            checkReplacement(context);
        }
    }

    @Override
    public Document feed(Document target, CallContext context) {
        try {
            return proxied != null
                ? proxied.feed(target, context)
                : super.feed(target, context);
        }
        finally {
            checkReplacement(context);
        }
    }

    @Override
    public void out(
        Document     format,
        OutputStream out,
        CallContext  context
    )
    throws IOException
    {
        try {
            if (proxied != null) {
                proxied.out(format, out, context);
            }
            else {
                super.out(format, out, context);
            }
        }
        finally {
            checkReplacement(context);
        }
    }

    @Override
    public void endOfLife(Object context) {
        if (proxied != null) {
            proxied.endOfLife(context);
        }
        else {
            super.endOfLife(context);
        }
    }

    @Override
    public void cleanup(Object context) {
        if (proxied != null)
            proxied.cleanup(context);
        else
            super.cleanup(context);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org