package org.jboss.deployment;
import java.io.File;
import java.io.FileFilter;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JARDeployer
extends SubDeployerSupport
implements JARDeployerMBean
{
private String[] descriptorNames = {
".xml"
};
public String[] getDescriptorNames()
{
return descriptorNames;
}
public void setDescriptorNames(String[] descriptorNames)
{
this.descriptorNames = descriptorNames;
}
protected void stopService()
{
}
public boolean accepts(DeploymentInfo di)
{
boolean trace = log.isTraceEnabled();
try
{
if (di.isXML || di.isScript)
{
return false;
}
URL wdDir = di.localCl.findResource("WEB-INF/");
if (wdDir != null)
{
return false;
}
URL ddDir;
try
{
ddDir = di.localCl.findResource("META-INF/");
if (ddDir == null)
{
log.debug("No META-INF or WEB-INF resource found, assuming it if for us");
return true;
}
}
catch (ClassCastException e)
{
ddDir = new URL(di.url, "META-INF/");
}
if (ddDir.getProtocol().equals("file"))
{
log.trace("File protocol: "+ddDir);
File file = new File(ddDir.getFile());
if (!file.exists())
{
log.warn("File not found: " + file);
return true;
}
File[] entries = file.listFiles(
new FileFilter()
{
public boolean accept(File pathname)
{
boolean accept = false;
String name = pathname.getName();
for(int n = 0; accept == false && n < descriptorNames.length; n ++)
{
String d = descriptorNames[n];
accept = name.endsWith(d);
}
return accept;
}
}
);
log.debug("XML entries found: " + entries.length);
return entries.length == 0;
} else if (ddDir.getProtocol().equals("jar") == true)
{
log.trace("jar protocol: " + ddDir);
JarFile jarFile = null;
try
{
URLConnection con = ddDir.openConnection();
JarURLConnection jarConn = (JarURLConnection) con;
jarConn.setUseCaches(false);
jarFile = jarConn.getJarFile();
}
catch (Exception e)
{
log.warn("Looking inside jar failed; ignoring", e);
if( jarFile != null )
jarFile.close();
jarFile = null;
return false;
}
boolean accepts = true;
for (Enumeration e = jarFile.entries(); accepts == true && e.hasMoreElements();)
{
JarEntry entry = (JarEntry)e.nextElement();
String name = entry.getName();
if (trace)
{
log.trace("Looking at entry: " + name);
}
if (name.startsWith("META-INF/"))
{
for(int n = 0; accepts == true && n < descriptorNames.length; n ++)
{
String d = descriptorNames[n];
accepts = name.endsWith(d) == false;
}
} } jarFile.close();
jarFile = null;
log.debug("No xml files found");
return accepts;
} else
{
log.debug("Unrecognized protocol: " + ddDir.getProtocol());
}
return false;
}
catch (Exception e)
{
return false;
}
}
}