package org.jboss.deployment;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jboss.system.server.ServerConfig;
import org.jboss.util.StringPropertyReplacer;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class NetBootHelper
{
public final static String DEFAULT_NETBOOT_LISTING_URL = "jboss.netboot.listing.url";
protected static org.jboss.logging.Logger log =
org.jboss.logging.Logger.getLogger(NetBootHelper.class);
protected static boolean traceEnabled = log.isTraceEnabled ();
public static String buildDownloadUrlForFile (String baseUrl, String directory, String filename)
{
String part = baseUrl;
if (part.charAt (part.length ()-1) != '/')
part=part + "/";
part = part + directory;
if (part.charAt (part.length ()-1) != '/')
part=part + "/";
part = part + filename;
return part;
}
public static String getDefaultDownloadUrl ()
{
return System.getProperty(ServerConfig.SERVER_HOME_URL);
}
public static String getDefaultListUrl ()
throws IllegalStateException
{
String defaultUrl = System.getProperty (NetBootHelper.DEFAULT_NETBOOT_LISTING_URL);
if (defaultUrl == null)
{
String autofallback = System.getProperty ("jboss.netboot.use.jbossweb");
if (autofallback == null || !autofallback.equalsIgnoreCase ("false"))
{
if (traceEnabled) log.trace ("jboss.netboot.use.jbossweb not defined but fallback activated...");
defaultUrl = System.getProperty(ServerConfig.HOME_URL);
int cropSize = defaultUrl.length ();
if (defaultUrl.endsWith ("/files"))
cropSize-= "/files".length () - 1;
else if (defaultUrl.endsWith ("/files/"))
cropSize-= "/files/".length () - 1;
else
throw new IllegalStateException
("No wildcard permitted in non-file URL deployment when jboss.netboot.listing.url not defined. " +
"You must either use the JBossWeb NetBoot WAR extension, specify individual jars" +
", use the URL:* notation or specify the jboss.netboot.listing.url system property");
defaultUrl = System.getProperty(ServerConfig.HOME_URL).substring (0, cropSize) + "List?";
if (traceEnabled) log.trace ("...using: " + defaultUrl);
}
else
{
if (traceEnabled) log.trace ("jboss.netboot.use.jbossweb not defined and fallback explicitly deactivated");
throw new IllegalStateException
("No wildcard permitted in non-file URL deployment when jboss.netboot.listing.url not defined. " +
"You must either specify individual jars" +
", use the URL:* notation or specify the jboss.netboot.listing.url system property");
}
}
return StringPropertyReplacer.replaceProperties (defaultUrl);
}
public static String buildListUrlForFolder (String baseUrl, String directory)
throws IllegalStateException, UnsupportedEncodingException
{
String listUrl = null;
if (baseUrl == null || "".equals (baseUrl))
{
listUrl = getDefaultListUrl ();
}
else
{
listUrl = baseUrl;
}
return listUrl + "dir=" + java.net.URLEncoder.encode (directory, "UTF-8");
}
public static NetBootFile[] listAllFromDirectory (String lister)
throws Exception
{
return listAllFromDirectory (lister, true, true);
}
public static NetBootFile[] listFilesFromDirectory (String lister)
throws Exception
{
return listAllFromDirectory (lister, false, true);
}
public static NetBootFile[] listDirectoriesFromDirectory (String lister)
throws Exception
{
return listAllFromDirectory (lister, true, false);
}
protected static NetBootFile[] listAllFromDirectory (String lister, boolean doDir, boolean doFiles)
throws Exception
{
if (traceEnabled) log.trace ("Getting directory listing from: " + lister);
ArrayList result = new ArrayList();
InputStream stream = new URL (lister).openStream ();
InputSource is = new InputSource(stream);
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = parser.parse(is);
if (doFiles)
{
Iterator dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "file");
while (dirContentIter.hasNext ())
{
Element item = (Element)dirContentIter.next();
String name = getUniqueChild (item, "name").getFirstChild().getNodeValue();
long lastModified = Long.parseLong(getUniqueChild (item, "modified").getFirstChild().getNodeValue());
long size = Long.parseLong(getUniqueChild (item, "size").getFirstChild().getNodeValue());
result.add (new NetBootFile (name, size, lastModified, false, lister));
}
}
if (doFiles)
{
Iterator dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "directory");
while (dirContentIter.hasNext ())
{
Element item = (Element)dirContentIter.next();
String name = getUniqueChild (item, "name").getFirstChild().getNodeValue();
long lastModified = Long.parseLong(getUniqueChild (item, "modified").getFirstChild().getNodeValue());
long size = Long.parseLong(getUniqueChild (item, "size").getFirstChild().getNodeValue());
result.add (new NetBootFile (name, size, lastModified, true, lister));
}
}
return (NetBootFile[]) result.toArray (new NetBootFile[] {});
}
protected static Element getUniqueChild(Element element, String tagName)
throws DeploymentException
{
Iterator goodChildren = getChildrenByTagName(element, tagName);
if (goodChildren != null && goodChildren.hasNext()) {
Element child = (Element)goodChildren.next();
if (goodChildren.hasNext()) {
throw new DeploymentException
("expected only one " + tagName + " tag");
}
return child;
} else {
throw new DeploymentException
("expected one " + tagName + " tag");
}
}
protected static Iterator getChildrenByTagName(Element element,
String tagName)
{
if (element == null) return null;
NodeList children = element.getChildNodes();
ArrayList goodChildren = new ArrayList();
for (int i=0; i<children.getLength(); i++) {
Node currentChild = children.item(i);
if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
((Element)currentChild).getTagName().equals(tagName)) {
goodChildren.add((Element)currentChild);
}
}
return goodChildren.iterator();
}
}