KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > NetBootHelper


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.deployment;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.UnsupportedEncodingException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18
19 import org.jboss.system.server.ServerConfig;
20 import org.jboss.util.StringPropertyReplacer;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25
26 /**
27  * Static helper methods for NetBoot features
28  *
29  * @see org.jboss.deployment.NetBootFile
30  * @see org.jboss.deployment.SARDeployer
31  * @see org.jboss.deployment.scanner.HttpURLDeploymentScanner
32  *
33  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
34  * @version $Revision: 1.6.6.1 $
35  *
36  * <p><b>Revisions:</b>
37  *
38  * <p><b>7 novembre 2002 Sacha Labourey:</b>
39  * <ul>
40  * <li> First implementation </li>
41  * </ul>
42  */

43
44 public class NetBootHelper
45 {
46    
47    // Constants -----------------------------------------------------
48

49    public final static String JavaDoc DEFAULT_NETBOOT_LISTING_URL = "jboss.netboot.listing.url";
50    
51    // Attributes ----------------------------------------------------
52

53    // Static --------------------------------------------------------
54

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    // Constructors --------------------------------------------------
60

61    // Public --------------------------------------------------------
62

63    public static String JavaDoc buildDownloadUrlForFile (String JavaDoc baseUrl, String JavaDoc directory, String JavaDoc filename)
64    {
65       String JavaDoc 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 JavaDoc getDefaultDownloadUrl ()
79    {
80       return System.getProperty(ServerConfig.SERVER_HOME_URL);
81    }
82    
83    public static String JavaDoc getDefaultListUrl ()
84       throws IllegalStateException JavaDoc
85    {
86       String JavaDoc defaultUrl = System.getProperty (NetBootHelper.DEFAULT_NETBOOT_LISTING_URL);
87       if (defaultUrl == null)
88       {
89          // No default listing URL is provided!
90
// We have a defaulting mode that can be used: if, on the server side,
91
// the JBossWeb NetBoot war is used, we can automatically use it
92
// The jboss.netboot.use.jbossweb System property can be set to false to
93
// disable this automatic behaviour
94
//
95
String JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc buildListUrlForFolder (String JavaDoc baseUrl, String JavaDoc directory)
127       throws IllegalStateException JavaDoc, UnsupportedEncodingException JavaDoc
128    {
129       String JavaDoc listUrl = null;
130       
131       if (baseUrl == null || "".equals (baseUrl))
132       {
133          // If not supplied, we provide the default URL
134
//
135
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 JavaDoc lister)
146       throws Exception JavaDoc
147    {
148       return listAllFromDirectory (lister, true, true);
149    }
150    
151    public static NetBootFile[] listFilesFromDirectory (String JavaDoc lister)
152       throws Exception JavaDoc
153    {
154       return listAllFromDirectory (lister, false, true);
155    }
156    
157    public static NetBootFile[] listDirectoriesFromDirectory (String JavaDoc lister)
158       throws Exception JavaDoc
159    {
160       return listAllFromDirectory (lister, true, false);
161    }
162    
163    // Z implementation ----------------------------------------------
164

165    // Y overrides ---------------------------------------------------
166

167    // Package protected ---------------------------------------------
168

169    // Protected -----------------------------------------------------
170

171    /**
172     *
173     * The is expected document we should receive in result:
174     *
175     * <!ELEMENT directory (file*, sub-directory*)>
176     * <!ELEMENT file (name, modified, size)>
177     * <!ELEMENT sub-directory (name, modified)>
178     * <!ELEMENT name (#PCDATA)>
179     * <!ELEMENT modified (#PCDATA)>
180     * <!ELEMENT size (#PCDATA)>
181     *
182     * In this case we are only interested in file, not directories
183     *
184     */

185    protected static NetBootFile[] listAllFromDirectory (String JavaDoc lister, boolean doDir, boolean doFiles)
186       throws Exception JavaDoc
187    {
188       if (traceEnabled) log.trace ("Getting directory listing from: " + lister);
189       
190       ArrayList JavaDoc result = new ArrayList JavaDoc();
191       // We now download the XML description from the remote URL
192
//
193
InputStream JavaDoc stream = new URL JavaDoc (lister).openStream ();
194       InputSource JavaDoc is = new InputSource JavaDoc(stream);
195
196       DocumentBuilder JavaDoc parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
197       org.w3c.dom.Document JavaDoc doc = parser.parse(is);
198       
199       if (doFiles)
200       {
201          Iterator JavaDoc dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "file");
202          while (dirContentIter.hasNext ())
203          {
204             Element JavaDoc item = (Element JavaDoc)dirContentIter.next();
205             String JavaDoc 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 JavaDoc dirContentIter = getChildrenByTagName(doc.getDocumentElement(), "directory");
216          while (dirContentIter.hasNext ())
217          {
218             Element JavaDoc item = (Element JavaDoc)dirContentIter.next();
219             String JavaDoc 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    /**
231     * from org.jboss.metadata.MetaData which is not in the System module
232     * (which is understandable)
233     */

234    protected static Element JavaDoc getUniqueChild(Element JavaDoc element, String JavaDoc tagName)
235       throws DeploymentException
236    {
237       Iterator JavaDoc goodChildren = getChildrenByTagName(element, tagName);
238
239       if (goodChildren != null && goodChildren.hasNext()) {
240          Element JavaDoc child = (Element JavaDoc)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 JavaDoc getChildrenByTagName(Element JavaDoc element,
253                                                String JavaDoc tagName)
254    {
255       if (element == null) return null;
256       // getElementsByTagName gives the corresponding elements in the whole
257
// descendance. We want only children
258

259       NodeList JavaDoc children = element.getChildNodes();
260       ArrayList JavaDoc goodChildren = new ArrayList JavaDoc();
261       for (int i=0; i<children.getLength(); i++) {
262          Node JavaDoc currentChild = children.item(i);
263          if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
264              ((Element JavaDoc)currentChild).getTagName().equals(tagName)) {
265             goodChildren.add((Element JavaDoc)currentChild);
266          }
267       }
268       return goodChildren.iterator();
269    }
270
271    // Private -------------------------------------------------------
272

273    // Inner classes -------------------------------------------------
274

275 }
276
Popular Tags