KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ear > lib > JarList


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: JarList.java,v 1.5 2004/04/19 14:28:04 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ear.lib;
28
29 //java import
30
import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import org.objectweb.jonas_ear.deployment.xml.Web;
38
39 /**
40  * JOnAS Jar list This class provides a way for managing the class-path
41  * dependency libraries.
42  * @author Florent Benoit
43  * @author Ludovic Bert
44  */

45 public class JarList extends Vector JavaDoc {
46
47     /**
48      * Construct an instance of a JarList
49      */

50     public JarList() {
51         super();
52     }
53
54     /**
55      * Construct an instance of a JarList
56      * @param st a String tokenizer
57      */

58     public JarList(StringTokenizer JavaDoc st) {
59         super();
60
61         //add each entries
62
while (st.hasMoreTokens()) {
63             String JavaDoc fileName = st.nextToken();
64             add(fileName);
65         }
66     }
67
68     /**
69      * Construct an instance of a JarList
70      * @param strs an array of files
71      */

72     public JarList(String JavaDoc[] strs) {
73         super();
74
75         for (int i = 0; i < strs.length; i++) {
76             add(strs[i]);
77         }
78     }
79
80     /**
81      * Construct an instance of a JarList with a wars Array
82      * @param wars an array of war file
83      */

84     public JarList(Web[] wars) {
85         super();
86
87         for (int i = 0; i < wars.length; i++) {
88             String JavaDoc s = wars[i].getWebUri();
89             add(s);
90         }
91     }
92
93     /**
94      * Add the specified file to the current list.
95      * @param fileName the name of the file to add.
96      */

97     public void add(String JavaDoc fileName) {
98         if (!contains(fileName) && !fileName.equals("")) {
99             super.add(fileName);
100         }
101     }
102
103     /**
104      * Get the URLs of all the JAR file list append to dirName.
105      * @param dirName the name of the directory where we append the files.
106      * @return the absolute URLs of the JAR files.
107      * @throws JarListException if url are malformed
108      */

109     public URL JavaDoc[] getURLs(String JavaDoc dirName) throws JarListException {
110
111         URL JavaDoc[] urls = new URL JavaDoc[elementCount];
112         for (int i = 0; i < elementCount; i++) {
113             String JavaDoc s = (String JavaDoc) elementData[i];
114             try {
115                 urls[i] = new File JavaDoc(new URL JavaDoc(dirName + File.separator + s).getFile()).getCanonicalFile().toURL();
116             } catch (IOException JavaDoc e) {
117                 throw new JarListException("Error when trying to get the canonical form for the file " + elementData[i]);
118             }
119         }
120         return urls;
121     }
122
123     /**
124      * Merge the specified JarList to the current JarList without adding
125      * duplicate entry.
126      * @param jarList the list to merge.
127      */

128     public void merge(JarList jarList) {
129         for (Enumeration JavaDoc e = jarList.elements(); e.hasMoreElements();) {
130             String JavaDoc s = (String JavaDoc) e.nextElement();
131             //call add which call super.add
132
add(s);
133         }
134     }
135
136     /**
137      * Remove all the entries contained in the jarList to the current jarList.
138      * @param jarList the jarList to remove.
139      */

140     public void remove(JarList jarList) {
141
142         for (Enumeration JavaDoc e = jarList.elements(); e.hasMoreElements();) {
143             String JavaDoc s = (String JavaDoc) e.nextElement();
144             if (contains(s)) {
145                 remove(s);
146             }
147         }
148     }
149
150     /**
151      * Set the relative path of the current list. This is useful in the case of
152      * an EAR file because the class-path is relative to the JAR file and not
153      * the EAR file.
154      * @param path the path for set the relative path
155      */

156     public void setRelativePath(String JavaDoc path) {
157         if (path.equals("")) {
158             return;
159         }
160
161         for (int i = 0; i < elementCount; i++) {
162             elementData[i] = path + File.separator + elementData[i];
163         }
164     }
165 }
Popular Tags