KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > common > JModule


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  * --------------------------------------------------------------------------
22  * $Id:
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.common;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 /**
36  * This class implements utility methods for the manipulation of application modules
37  * @author Adriana Danes
38  */

39 public class JModule {
40     /**
41      * file extension containing the different module types
42      */

43     public static final String JavaDoc EJBJAR_EXTENSION = "jar";
44     public static final String JavaDoc RAR_EXTENSION = "rar";
45     public static final String JavaDoc WAR_EXTENSION = "war";
46     public static final String JavaDoc EAR_EXTENSION = "ear";
47
48     /**
49      * Child directory of a expanded container
50      */

51     public static final String JavaDoc EJBJAR_CHILD_DIR = "META-INF";
52     public static final String JavaDoc RAR_CHILD_DIR = "META-INF";
53     public static final String JavaDoc WAR_CHILD_DIR = "WEB-INF";
54     public static final String JavaDoc EAR_CHILD_DIR = "META-INF";
55
56     /**
57      * xml file in a child directory of a expanded container
58      */

59     public static final String JavaDoc EJBJAR_CONFIRM_FILE = "ejb-jar.xml";
60     public static final String JavaDoc RAR_CONFIRM_FILE = "ra.xml";
61     public static final String JavaDoc WAR_CONFIRM_FILE = "web.xml";
62     public static final String JavaDoc EAR_CONFIRM_FILE = "application.xml";
63
64     /**
65      * properties file extension
66      */

67     public static final String JavaDoc PROPS_EXTENSION = "properties";
68
69
70     /**
71      * name of the directory containig configuration
72      */

73     public static final String JavaDoc CONF_DIR = "conf";
74
75     public static ArrayList JavaDoc getFilesInDir(String JavaDoc dirName, String JavaDoc extension) throws Exception JavaDoc {
76         ArrayList JavaDoc al = new ArrayList JavaDoc();
77         File JavaDoc file = new File JavaDoc(dirName);
78         String JavaDoc[] files = file.list();
79         int pos;
80         String JavaDoc fileName;
81         if (files != null) {
82             for (int i = 0; i < files.length; i++) {
83                 file = new File JavaDoc(dirName, files[i]);
84                 if (file.isFile() == true) {
85                     fileName = file.getName().toLowerCase();
86                     pos = fileName.lastIndexOf(extension);
87                     if (pos > -1) {
88                         if (pos == (fileName.length() - extension.length())) {
89                             al.add(file.getName());
90                         }
91                     }
92                 }
93             }
94         }
95         Collections.sort(al);
96         return al;
97     }
98
99     public static ArrayList JavaDoc getDatasourcePropsInDir() throws Exception JavaDoc {
100         // Get all files include in configuration directory
101
ArrayList JavaDoc al = getFilesInDir(JProp.getJonasBase() + File.separator + CONF_DIR, PROPS_EXTENSION);
102         // Keep all datasources properties
103
String JavaDoc sPath;
104         Properties JavaDoc oProps = new Properties JavaDoc();
105         boolean bDelete;
106         int i = 0;
107         while (i < al.size()) {
108             sPath = JProp.getJonasBase() + File.separator + CONF_DIR + File.separator
109                 + al.get(i).toString();
110             bDelete = true;
111             try {
112                 oProps.clear();
113                 oProps.load(new FileInputStream JavaDoc(sPath));
114                 // Detect datasource property
115
if (oProps.getProperty("datasource.name") != null) {
116                     // Remove Extension
117
int iPos = al.get(i).toString().toLowerCase().lastIndexOf(".properties");
118                     al.set(i, al.get(i).toString().substring(0, iPos));
119                     // Next
120
i++;
121                     bDelete = false;
122                 }
123             }
124             catch (Exception JavaDoc e) {
125                 // none
126
}
127             finally {
128                 // Remove no-datasource
129
if (bDelete == true) {
130                     al.remove(i);
131                 }
132             }
133         }
134         return al;
135     }
136
137     public static ArrayList JavaDoc getMailFactoryPropsInDir(String JavaDoc type) throws Exception JavaDoc {
138         // Get all files include in configuration directory
139
ArrayList JavaDoc al = getFilesInDir(JProp.getJonasBase() + File.separator + CONF_DIR, PROPS_EXTENSION);
140         // Keep all datasources properties
141
String JavaDoc sPath;
142         Properties JavaDoc oProps = new Properties JavaDoc();
143         boolean bDelete;
144         int i = 0;
145         while (i < al.size()) {
146             sPath = JProp.getJonasBase() + File.separator + CONF_DIR + File.separator
147                 + al.get(i).toString();
148             bDelete = true;
149             try {
150                 oProps.clear();
151                 oProps.load(new FileInputStream JavaDoc(sPath));
152                 // Detect mail factory property
153
if (oProps.getProperty("mail.factory.name") != null) {
154                     boolean bToReturn;
155                     if (type == null) {
156                         // any type OK
157
bToReturn = true;
158                     } else {
159                         // Check type
160
if (oProps.getProperty("mail.factory.type").equals(type) == true) {
161                             bToReturn = true;
162                         } else {
163                             bToReturn = false;
164                         }
165                     }
166                     if (bToReturn == true) {
167                         // Remove Extension
168
int iPos = al.get(i).toString().toLowerCase().lastIndexOf(".properties");
169                         al.set(i, al.get(i).toString().substring(0, iPos));
170                         // Next
171
i++;
172                         bDelete = false;
173                     }
174                 }
175             }
176             catch (Exception JavaDoc e) {
177                 // none
178
}
179             finally {
180                 // Remove no-mail factory
181
if (bDelete == true) {
182                     al.remove(i);
183                 }
184             }
185         }
186         return al;
187     }
188     public static ArrayList JavaDoc getMailFactoryPropsInDir() throws Exception JavaDoc {
189         return getMailFactoryPropsInDir(null);
190     }
191
192     public static void prefixAutoload(String JavaDoc autoloaddir, ArrayList JavaDoc p_List) {
193         String JavaDoc sDir = autoloaddir + File.separator;// "autoloaddir/"
194
for (int i = 0; i < p_List.size(); i++) {
195             p_List.set(i, sDir + p_List.get(i));
196         }
197     }
198
199     /**
200      * Return the list of all installed containers in a directory.
201      * The container may be : <br>
202      * - a archive file container with a known extension : jar, war, ear, rar<br>
203      * - a directory where the container is expanded<br>
204      * To control if a directory contains a expanded container, it's necessary
205      * to provide the child directory and the xml file that confirm the expanded container.<br>
206      * Parameter example to retrieve :<br>
207      * - web application : war, WEB-INF, web.xml
208      * - application : ear, META-INF, application.xml
209      * - ejb container : jar, META-INF, ejb-jar.xml
210      * - resource : rar, META-INF, ra.xml
211      *
212      * @param p_DirName The name of the directory to find
213      * @param p_Extension The extension of container (jar, war, ear, rar)
214      * @param p_DirChildConfirm The child directory that confirm the container
215      * @param p_FileConfirm The xml file inside the child directory that confirm the container
216      * @return A list of absolute path container
217      * @throws Exception
218      */

219     public static ArrayList JavaDoc getInstalledContainersInDir(String JavaDoc p_DirName, String JavaDoc p_Extension
220         , String JavaDoc p_DirChildConfirm, String JavaDoc p_FileConfirm)
221         throws Exception JavaDoc {
222
223         // Return variable
224
ArrayList JavaDoc al = new ArrayList JavaDoc();
225         // Variables used in loop
226
int iPos;
227         String JavaDoc sFilename;
228         File JavaDoc oDirChild;
229         File JavaDoc oFileConfirm;
230         // Search all files in directory
231
File JavaDoc oFile = new File JavaDoc(p_DirName);
232         String JavaDoc[] asFiles = oFile.list();
233
234         if (asFiles != null) {
235             // Loop for each found file
236
for (int i = 0; i < asFiles.length; i++) {
237                 oFile = new File JavaDoc(p_DirName, asFiles[i]);
238                 // Detect file with extension
239
if (oFile.isFile() == true) {
240                     sFilename = oFile.getName().toLowerCase();
241                     iPos = sFilename.lastIndexOf(p_Extension);
242                     if (iPos > -1) {
243                         if (iPos == (sFilename.length() - p_Extension.length())) {
244                             al.add(oFile.getCanonicalFile().toURL().getPath());
245                         }
246                     }
247                 }
248                 // Detect directory of expanded container
249
else if (oFile.isDirectory() == true) {
250                     oDirChild = new File JavaDoc(oFile, p_DirChildConfirm);
251                     if ((oDirChild.exists() == true) && (oDirChild.isDirectory() == true)) {
252                         oFileConfirm = new File JavaDoc(oDirChild, p_FileConfirm);
253                         if (oFileConfirm.exists() == true) {
254                             al.add(oFile.getCanonicalFile().toURL().getPath());
255                         }
256                     }
257                 }
258             }
259         }
260         return al;
261     }
262
263 }
264
Popular Tags