package net.sourceforge.javautil.deployer.artifact.impl;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.javautil.classloader.impl.ClassContext;
import net.sourceforge.javautil.common.io.IVirtualArtifact;
import net.sourceforge.javautil.deployer.artifact.LinkedDeployment;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeployer;
import net.sourceforge.javautil.deployer.artifact.IVirtualArtifactDeployment;
/**
* The base for most deployment implementations.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public abstract class VirtualArtifactDeploymentBase<A extends IVirtualArtifact> extends VirtualArtifactDeploymentAbstract<A> {
protected A deploymentStructure;
protected Map<String, LinkedDeployment> linked = new LinkedHashMap<String, LinkedDeployment>();
protected ClassContext linkedContext;
public VirtualArtifactDeploymentBase(A deploymentStructure, IVirtualArtifactDeployer deployer, A artifact) {
super(deployer, artifact);
this.deploymentStructure = deploymentStructure;
}
protected void setClassLoader (ClassLoader cl) {
this.classLoader = cl;
if (this.linkedContext != null) this.createLinkedContext(deployer.getDeploymentClassLoader());
}
public A getDeploymentStructure() { return deploymentStructure; }
public LinkedDeployment getLinkedDeployment(IVirtualArtifactDeployment deployment) {
return this.linked.get(this.createUniqueId(deployment.getDeployed()));
}
public List<LinkedDeployment> getLinkedDeployments() { return new ArrayList<LinkedDeployment>(this.linked.values()); }
public void link(IVirtualArtifactDeployment deployment, boolean circular) {
if (this == deployment) return;
if (this.linkedContext == null) this.createLinkedContext(this.getDeployer().getDeploymentClassLoader());
if (!this.linked.containsKey(this.createUniqueId(deployment.getDeployed()))) {
this.linked.put(this.createUniqueId(deployment.getDeployed()), new LinkedDeployment(circular, this, deployment));
if (circular) deployment.link(this, false);
}
}
public void unlink(IVirtualArtifactDeployment deployment) {
if (this == deployment) return;
LinkedDeployment ld = this.linked.remove(this.createUniqueId(deployment.getDeployed()));
if (ld != null && ld.isCircular()) deployment.unlink(this);
if (this.linkedContext != null) {
if (this.linked.size() == 0)
this.classLoader = ((VirtualDeploymentLinkedClassLoaderHeiarchy)this.linkedContext.getHeiarchy()).getClassLoaders()[0];
else
this.createLinkedContext(this.getDeployer().getDeploymentClassLoader());
}
}
public boolean isLinked(IVirtualArtifactDeployment deployment) {
return this.linked.containsKey(this.createUniqueId(deployment.getDeployed()));
}
/**
* This will create a linked context and set the internal class loader to use it instead. By default
* this will also make sure that the current class loader is the parent of the created linked context.
*/
protected void createLinkedContext (ClassLoader parent) {
this.linkedContext = new ClassContext(new VirtualDeploymentLinkedClassLoaderHeiarchy(this, parent, this.classLoader));
this.classLoader = this.linkedContext;
}
}