1 7 8 package org.jboss.deployment; 9 10 import java.io.InputStream ; 11 import java.io.UnsupportedEncodingException ; 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 19 import org.jboss.system.server.ServerConfig; 20 import org.jboss.util.StringPropertyReplacer; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import org.xml.sax.InputSource ; 25 26 43 44 public class NetBootHelper 45 { 46 47 49 public final static String DEFAULT_NETBOOT_LISTING_URL = "jboss.netboot.listing.url"; 50 51 53 55 protected static org.jboss.logging.Logger log = 56 org.jboss.logging.Logger.getLogger(NetBootHelper.class); 57 protected static boolean traceEnabled = log.isTraceEnabled (); 58 59 61 63 public static String buildDownloadUrlForFile (String baseUrl, String directory, String filename) 64 { 65 String part = baseUrl; 66 if (part.charAt (part.length ()-1) != '/') 67 part=part + "/"; 68 69 part = part + directory; 70 if (part.charAt (part.length ()-1) != '/') 71 part=part + "/"; 72 73 part = part + filename; 74 75 return part; 76 } 77 78 public static String getDefaultDownloadUrl () 79 { 80 return System.getProperty(ServerConfig.SERVER_HOME_URL); 81 } 82 83 public static String getDefaultListUrl () 84 throws IllegalStateException 85 { 86 String defaultUrl = System.getProperty (NetBootHelper.DEFAULT_NETBOOT_LISTING_URL); 87 if (defaultUrl == null) 88 { 89 String autofallback = System.getProperty ("jboss.netboot.use.jbossweb"); 96 if (autofallback == null || !autofallback.equalsIgnoreCase ("false")) 97 { 98 if (traceEnabled) log.trace ("jboss.netboot.use.jbossweb not defined but fallback activated..."); 99 defaultUrl = System.getProperty(ServerConfig.HOME_URL); 100 int cropSize = defaultUrl.length (); 101 if (defaultUrl.endsWith ("/files")) 102 cropSize-= "/files".length () - 1; 103 else if (defaultUrl.endsWith ("/files/")) 104 cropSize-= "/files/".length () - 1; 105 else 106 throw new IllegalStateException 107 ("No wildcard permitted in non-file URL deployment when jboss.netboot.listing.url not defined. " + 108 "You must either use the JBossWeb NetBoot WAR extension, specify individual jars" + 109 ", use the URL:* notation or specify the jboss.netboot.listing.url system property"); 110 defaultUrl = System.getProperty(ServerConfig.HOME_URL).substring (0, cropSize) + "List?"; 111 112 if (traceEnabled) log.trace ("...using: " + defaultUrl); 113 } 114 else 115 { 116 if (traceEnabled) log.trace ("jboss.netboot.use.jbossweb not defined and fallback explicitly deactivated"); 117 throw new IllegalStateException 118 ("No wildcard permitted in non-file URL deployment when jboss.netboot.listing.url not defined. " + 119 "You must either specify individual jars" + 120 ", use the URL:* notation or specify the jboss.netboot.listing.url system property"); 121 } 122 } 123 return StringPropertyReplacer.replaceProperties (defaultUrl); 124 } 125 126 public static String buildListUrlForFolder (String baseUrl, String directory) 127 throws IllegalStateException , UnsupportedEncodingException 128 { 129 String listUrl = null; 130 131 if (baseUrl == null || "".equals (baseUrl)) 132 { 133 listUrl = getDefaultListUrl (); 136 } 137 else 138 { 139 listUrl = baseUrl; 140 } 141 142 return listUrl + "dir=" + java.net.URLEncoder.encode (directory, "UTF-8"); 143 } 144 145 public static NetBootFile[] listAllFromDirectory (String lister) 146 throws Exception 147 { 148 return listAllFromDirectory (lister, true, true); 149 } 150 151 public static NetBootFile[] listFilesFromDirectory (String lister) 152 throws Exception 153 { 154 return listAllFromDirectory (lister, false, true); 155 } 156 157 public static NetBootFile[] listDirectoriesFromDirectory (String lister) 158 throws Exception 159 { 160 return listAllFromDirectory (lister, true, false); 161 } 162 163 165 167 169 171 185 protected static NetBootFile[] listAllFromDirectory (String lister, boolean doDir, boolean doFiles) 186 throws Exception 187 { 188 if (traceEnabled) log.trace ("Getting directory listing from: " + lister); 189 190 ArrayList result = new ArrayList (); 191 InputStream stream = new URL (lister).openStream (); 194 InputSource is = new InputSource (stream); 195 196 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 197 org.w3c.dom.Document doc = parser.parse(is); 198 199 if (doFiles) 200 { 201 Iterator dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "file"); 202 while (dirContentIter.hasNext ()) 203 { 204 Element item = (Element )dirContentIter.next(); 205 String name = getUniqueChild (item, "name").getFirstChild().getNodeValue(); 206 long lastModified = Long.parseLong(getUniqueChild (item, "modified").getFirstChild().getNodeValue()); 207 long size = Long.parseLong(getUniqueChild (item, "size").getFirstChild().getNodeValue()); 208 209 result.add (new NetBootFile (name, size, lastModified, false, lister)); 210 } 211 } 212 213 if (doFiles) 214 { 215 Iterator dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "directory"); 216 while (dirContentIter.hasNext ()) 217 { 218 Element item = (Element )dirContentIter.next(); 219 String name = getUniqueChild (item, "name").getFirstChild().getNodeValue(); 220 long lastModified = Long.parseLong(getUniqueChild (item, "modified").getFirstChild().getNodeValue()); 221 long size = Long.parseLong(getUniqueChild (item, "size").getFirstChild().getNodeValue()); 222 223 result.add (new NetBootFile (name, size, lastModified, true, lister)); 224 } 225 } 226 227 return (NetBootFile[]) result.toArray (new NetBootFile[] {}); 228 } 229 230 234 protected static Element getUniqueChild(Element element, String tagName) 235 throws DeploymentException 236 { 237 Iterator goodChildren = getChildrenByTagName(element, tagName); 238 239 if (goodChildren != null && goodChildren.hasNext()) { 240 Element child = (Element )goodChildren.next(); 241 if (goodChildren.hasNext()) { 242 throw new DeploymentException 243 ("expected only one " + tagName + " tag"); 244 } 245 return child; 246 } else { 247 throw new DeploymentException 248 ("expected one " + tagName + " tag"); 249 } 250 } 251 252 protected static Iterator getChildrenByTagName(Element element, 253 String tagName) 254 { 255 if (element == null) return null; 256 259 NodeList children = element.getChildNodes(); 260 ArrayList goodChildren = new ArrayList (); 261 for (int i=0; i<children.getLength(); i++) { 262 Node currentChild = children.item(i); 263 if (currentChild.getNodeType() == Node.ELEMENT_NODE && 264 ((Element )currentChild).getTagName().equals(tagName)) { 265 goodChildren.add((Element )currentChild); 266 } 267 } 268 return goodChildren.iterator(); 269 } 270 271 273 275 } 276 | Popular Tags |