package net.sourceforge.javautil.deployer.artifact.impl;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import net.sourceforge.javautil.common.io.IVirtualArtifact;
import net.sourceforge.javautil.common.proxy.CollectionTargetProxy;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeployer;
import net.sourceforge.javautil.deployer.artifact.VirtualArtifactDeployerEvent;
import net.sourceforge.javautil.deployer.artifact.VirtualArtifactDeployerException;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeployerListener;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeployment;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeploymentPattern;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactPatternDeployer;
/**
* The base for most pattern-based deployer implementations.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public abstract class VirtualArtifactPatternDeployerAbstract<A extends IVirtualArtifact, DU extends IVirtualArtifactDeployment<A>, DEP extends IVirtualArtifactDeployer>
extends VirtualArtifactDeployerBase<A, DU> implements IVirtualArtifactPatternDeployer<A, DU, DEP> {
protected final List<IVirtualArtifactDeploymentPattern> patterns = new ArrayList<IVirtualArtifactDeploymentPattern>();
public boolean handlesDeployment(IVirtualArtifact artifact) { return this.findPattern(artifact) != null; }
public void add (IVirtualArtifactDeploymentPattern<? extends A, ? extends DU, ? extends DEP> pattern) { this.patterns.add(pattern); }
/**
* Facility method. Implementations can call this in their {@link #deploy(IVirtualArtifact)} method
* to create the actual deployment.
*
* @param artifact
* @return
*/
protected DU createDeployment (A artifact) {
if (this.isDeployed(artifact)) throw new VirtualArtifactDeployerException(this, artifact, "Artifact already deployed: " + artifact);
IVirtualArtifactDeploymentPattern pattern = this.findPattern(artifact);
if (pattern == null)
throw new VirtualArtifactDeployerException(this, artifact, "Could not deploy this artifact");
return (DU) pattern.createDeployment(this, artifact);
}
/**
* @param artifact The artifact in question
* @return The first pattern that matches the artifact, or null if none matched
*/
protected IVirtualArtifactDeploymentPattern<A,DU,DEP> findPattern (IVirtualArtifact artifact) {
for (IVirtualArtifactDeploymentPattern pattern : this.patterns) {
if (pattern.matches(artifact)) return pattern;
}
return null;
}
}