package org.jboss.deployment.cache;
import java.net.URL;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.system.MissingAttributeException;
import org.jboss.deployment.Deployer;
import org.jboss.deployment.DeploymentException;
import org.jboss.util.NullArgumentException;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanProxyInstance;
public class DeploymentCache
extends ServiceMBeanSupport
implements Deployer, DeploymentCacheMBean
{
protected Deployer deployer;
protected DeploymentStore store;
public void setDeployer(final ObjectName deployerName)
{
if (deployerName == null)
throw new NullArgumentException("deployerName");
deployer = (Deployer)
MBeanProxyExt.create(Deployer.class, deployerName, server);
}
public ObjectName getDeployer()
{
return ((MBeanProxyInstance)deployer).getMBeanProxyObjectName();
}
public void setStore(final ObjectName storeName)
{
if (storeName == null)
throw new NullArgumentException("storeName");
store = (DeploymentStore)
MBeanProxyExt.create(DeploymentStore.class, storeName, server);
}
public ObjectName getStore()
{
return ((MBeanProxyInstance)store).getMBeanProxyObjectName();
}
protected boolean isInvalid(final URL orig, final URL stored)
throws Exception
{
boolean trace = log.isTraceEnabled();
long omod = orig.openConnection().getLastModified();
long smod = stored.openConnection().getLastModified();
if (trace) {
log.trace("Modfication times (orig, stored): " + omod + ", " + smod);
}
return omod > smod;
}
public void deploy(final URL url) throws DeploymentException
{
boolean debug = log.isDebugEnabled();
try {
URL storedURL = store.get(url);
if (storedURL != null) {
if (isInvalid(url, storedURL)) {
log.info("Cached deployment is invalid; refreshing store for URL: " + url);
storedURL = store.put(url);
}
else {
if (debug) {
log.debug("Using cached deployment URL: " + storedURL);
}
}
}
else {
log.info("Deployment not in cache; adding URL to store: " + url);
storedURL = store.put(url);
}
deployer.deploy(storedURL);
}
catch (Exception e) {
throw new DeploymentException(e);
}
}
public void undeploy(final URL url) throws DeploymentException
{
boolean debug = log.isDebugEnabled();
try {
URL storedURL = store.get(url);
if (storedURL != null) {
deployer.undeploy(storedURL);
}
else {
if (debug) {
log.debug("Not found in store; ignoring URL: " + url);
}
}
}
catch (Exception e) {
throw new DeploymentException(e);
}
}
public boolean isDeployed(final URL url)
{
try {
URL storedURL = store.get(url);
return storedURL != null && deployer.isDeployed(url);
}
catch (Exception e) {
return false;
}
}
protected void createService() throws Exception
{
}
protected void startService() throws Exception
{
if (deployer == null)
throw new MissingAttributeException("Deployer");
if (store == null)
throw new MissingAttributeException("Store");
}
protected void stopService() throws Exception
{
}
protected void destroyService() throws Exception
{
deployer = null;
store = null;
}
}