KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > util > JProperties


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 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 1any 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: JProperties.java,v 1.5 2005/07/25 12:29:58 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.util;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39
40 import org.objectweb.jonas.adm.AdmInterface;
41
42 /**
43  * JOnAS property accessor
44  * @author kemlerp
45  */

46 public class JProperties {
47
48     /**
49      * Name of the JOnAS server used for tests
50      */

51     private static String JavaDoc jonasName = "jonas";
52
53     /**
54      * Initial context used for lookup
55      */

56     private static Context JavaDoc ictx = null;
57
58     /**
59      * JOnAS admin used for communicate via JMX to JOnAS
60      */

61     private static AdmInterface admI = null;
62
63     /**
64      * System properties
65      */

66     private static Properties JavaDoc systEnv = System.getProperties();
67
68     /**
69      * Separator of file
70      */

71     private static String JavaDoc fileSeparator = systEnv.getProperty("file.separator");
72
73     /**
74      * -Djonas.test property
75      */

76     private static final String JavaDoc JONAS_TEST = "jonas.test";
77
78     /**
79      * JONAS_TEST
80      */

81     private static String JavaDoc jonasTest = systEnv.getProperty(JONAS_TEST);
82
83     /**
84      * -Djonas.base property
85      */

86     private static final String JavaDoc JONAS_BASE = "jonas.base";
87
88     /**
89      * JONAS_BASE
90      */

91     private static String JavaDoc jonasBase = systEnv.getProperty(JONAS_BASE);
92
93     /**
94      * resource directory name
95      */

96     private static final String JavaDoc RESOURCE_DIR = "resources";
97
98     /**
99      * conf directory name
100      */

101     private static final String JavaDoc CONF_DIR = "conf";
102
103     /**
104      * Get initialContext
105      * @return the initialContext
106      * @throws NamingException if the initial context can't be retrieved
107      */

108     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
109         return new InitialContext JavaDoc();
110     }
111
112     /**
113      * Get list environnement
114      * @return JOnAS protperties
115      * @throws Exception if list environnement cannot be got
116      */

117     public Properties JavaDoc getPropertiesEnv() throws Exception JavaDoc {
118         Properties JavaDoc prop = null;
119
120         try {
121             if (ictx == null) {
122                 ictx = getInitialContext();
123             }
124             if (admI == null) {
125                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
126             }
127             prop = admI.listEnv();
128         } catch (Exception JavaDoc e) {
129             throw new Exception JavaDoc("Cannot get list environnement : " + e.getMessage());
130         }
131
132         return prop;
133     }
134
135     /**
136      * Get services in jonas properties
137      * @return A table of string with services in jonas properties
138      * @throws Exception if properties cannot be got
139      */

140     public String JavaDoc[] getServices() throws Exception JavaDoc {
141         Properties JavaDoc propertiesEnv = getPropertiesEnv();
142         String JavaDoc services = propertiesEnv.getProperty("jonas.services");
143         return services.split(",");
144     }
145
146     /**
147      * Search a service in services
148      * @param service The string that is searched
149      * @return true if there is the service in services else false
150      * @throws Exception if services cannot be got
151      */

152     public boolean searchService(String JavaDoc service) throws Exception JavaDoc {
153         String JavaDoc[] services = getServices();
154         boolean found = false;
155         int i = 0;
156
157         while (!found && i < services.length) {
158             if (services[i].equals(service)) {
159                 found = true;
160             }
161             i++;
162         }
163
164         return found;
165     }
166
167     /**
168      * Db in service property
169      * @return True if db is a value of service property
170      * @throws Exception if service property cannot be got
171      */

172     public boolean isDb() throws Exception JavaDoc {
173         boolean exist = false;
174         try {
175             if (searchService("db")) {
176                 exist = true;
177             }
178         } catch (Exception JavaDoc e) {
179             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
180         }
181         return exist;
182     }
183
184     /**
185      * Dbm in service property
186      * @return True if dbm is a value of service property
187      * @throws Exception if service property cannot be got
188      */

189     public boolean isDbm() throws Exception JavaDoc {
190         boolean exist = false;
191         try {
192             if (searchService("dbm")) {
193                 exist = true;
194             }
195         } catch (Exception JavaDoc e) {
196             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
197         }
198         return exist;
199     }
200
201     /**
202      * Discovery in service property
203      * @return True if discovery is a value of service property
204      * @throws Exception if service property cannot be got
205      */

206     public boolean isDiscovery() throws Exception JavaDoc {
207         boolean exist = false;
208         try {
209             if (searchService("discovery")) {
210                 exist = true;
211             }
212         } catch (Exception JavaDoc e) {
213             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
214         }
215         return exist;
216     }
217
218     /**
219      * Ear in service property
220      * @return True if ear is a value of service property
221      * @throws Exception if service property cannot be got
222      */

223     public boolean isEar() throws Exception JavaDoc {
224         boolean exist = false;
225         try {
226             if (searchService("ear")) {
227                 exist = true;
228             }
229         } catch (Exception JavaDoc e) {
230             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
231         }
232         return exist;
233     }
234
235     /**
236      * Ejb in service property
237      * @return True if ejb is a value of service property
238      * @throws Exception if service property cannot be got
239      */

240     public boolean isEjb() throws Exception JavaDoc {
241         boolean exist = false;
242         try {
243             if (searchService("ejb")) {
244                 exist = true;
245             }
246         } catch (Exception JavaDoc e) {
247             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
248         }
249         return exist;
250     }
251
252     /**
253      * Jms in service property
254      * @return True if jms is a value of service property
255      * @throws Exception if service property cannot be got
256      */

257     public boolean isJms() throws Exception JavaDoc {
258         boolean exist = false;
259         try {
260             if (searchService("jms")) {
261                 exist = true;
262             }
263         } catch (Exception JavaDoc e) {
264             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
265         }
266         return exist;
267     }
268
269     /**
270      * Jmx in service property
271      * @return True if Jmx is a value of service property
272      * @throws Exception if service property cannot be got
273      */

274     public boolean isJmx() throws Exception JavaDoc {
275         boolean exist = false;
276         try {
277             if (searchService("jmx")) {
278                 exist = true;
279             }
280         } catch (Exception JavaDoc e) {
281             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
282         }
283         return exist;
284     }
285
286     /**
287      * Jtm in service property
288      * @return True if jtm is a value of service property
289      * @throws Exception if service property cannot be got
290      */

291     public boolean isJtm() throws Exception JavaDoc {
292         boolean exist = false;
293         try {
294             if (searchService("jtm")) {
295                 exist = true;
296             }
297         } catch (Exception JavaDoc e) {
298             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
299         }
300         return exist;
301     }
302
303     /**
304      * Mail in service property
305      * @return True if db is a value of service property
306      * @throws Exception if service property cannot be got
307      */

308     public boolean isMail() throws Exception JavaDoc {
309         boolean exist = false;
310         try {
311             if (searchService("mail")) {
312                 exist = true;
313             }
314         } catch (Exception JavaDoc e) {
315             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
316         }
317         return exist;
318     }
319
320     /**
321      * Registry in service property
322      * @return True if registry is a value of service property
323      * @throws Exception if service property cannot be got
324      */

325     public boolean isRegistry() throws Exception JavaDoc {
326         boolean exist = false;
327         try {
328             if (searchService("registry")) {
329                 exist = true;
330             }
331         } catch (Exception JavaDoc e) {
332             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
333         }
334         return exist;
335     }
336
337     /**
338      * Resource in service property
339      * @return True if resource is a value of service property
340      * @throws Exception if service property cannot be got
341      */

342     public boolean isResource() throws Exception JavaDoc {
343         boolean exist = false;
344         try {
345             if (searchService("resource")) {
346                 exist = true;
347             }
348         } catch (Exception JavaDoc e) {
349             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
350         }
351         return exist;
352     }
353
354     /**
355      * Security in service property
356      * @return True if security is a value of service property
357      * @throws Exception if service property cannot be got
358      */

359     public boolean isSecurity() throws Exception JavaDoc {
360         boolean exist = false;
361         try {
362             if (searchService("security")) {
363                 exist = true;
364             }
365         } catch (Exception JavaDoc e) {
366             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
367         }
368         return exist;
369     }
370
371     /**
372      * Web in service property
373      * @return True if web is a value of service property
374      * @throws Exception if service property cannot be got
375      */

376     public boolean isWeb() throws Exception JavaDoc {
377         boolean exist = false;
378         try {
379             if (searchService("web")) {
380                 exist = true;
381             }
382         } catch (Exception JavaDoc e) {
383             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
384         }
385         return exist;
386     }
387
388     /**
389      * Ws in service property
390      * @return True if ws is a value of service property
391      * @throws Exception if service property cannot be got
392      */

393     public boolean isWs() throws Exception JavaDoc {
394         boolean exist = false;
395         try {
396             if (searchService("ws")) {
397                 exist = true;
398             }
399         } catch (Exception JavaDoc e) {
400             throw new Exception JavaDoc("Cannot get services : " + e.getMessage());
401         }
402         return exist;
403     }
404
405     /**
406      * Joram platform in jonas properties
407      * @return True if Joram is in jonas
408      * @throws Exception if property cannot be got
409      */

410     public boolean isJoram() throws Exception JavaDoc {
411         Properties JavaDoc propertiesEnv = getPropertiesEnv();
412         String JavaDoc mom = propertiesEnv.getProperty("jonas.service.jms.mom");
413         return mom.endsWith("JmsAdminForJoram");
414     }
415
416     /**
417      * Catalina in jonas properties
418      * @return True if Catalina is the web service, else false
419      * @throws Exception if property cannot be got
420      */

421     public boolean isCatalina() throws Exception JavaDoc {
422         Properties JavaDoc propertiesEnv = getPropertiesEnv();
423         String JavaDoc webClass = propertiesEnv.getProperty("jonas.service.web.class");
424         return webClass.endsWith("CatalinaJWebContainerServiceWrapper");
425     }
426
427     /**
428      * Get properties from a file in resources directory.
429      * @param fileName name of the property file with ".properties".
430      * @return Properties.
431      * @throws Exception if the file is not found.
432      */

433     public Properties JavaDoc getProperties(String JavaDoc fileName) throws Exception JavaDoc {
434         // Get ClassLoader
435
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
436         URL JavaDoc url = cl.getResource(fileName);
437         InputStream JavaDoc is = cl.getResourceAsStream(fileName);
438         Properties JavaDoc configFile = new Properties JavaDoc();
439         configFile.load(is);
440         return configFile;
441     }
442
443     /**
444      * Get properties from a file in $JONAS_TEST/jonasadmin directory.
445      * @param fileName name of the property file without ".properties".
446      * @return Properties.
447      * @throws FileNotFoundException if the file is not found.
448      */

449     private Properties JavaDoc getCarolProperties() throws FileNotFoundException JavaDoc {
450         // ${jonas.base}/conf/carol.properties
451
if (jonasBase.equalsIgnoreCase("${myenv.JONAS_BASE}")) {
452             throw new FileNotFoundException JavaDoc("You must add JONAS_BASE in your environnement variables. ");
453         } else {
454             String JavaDoc propFileName = jonasBase + fileSeparator + CONF_DIR + fileSeparator + "carol" + ".properties";
455
456             File JavaDoc f = null;
457             Properties JavaDoc configFile = new Properties JavaDoc();
458             try {
459                 f = new File JavaDoc(propFileName);
460                 FileInputStream JavaDoc is = new FileInputStream JavaDoc(f);
461                 configFile.load(is);
462             } catch (FileNotFoundException JavaDoc e) {
463                 throw new FileNotFoundException JavaDoc("Cannot find properties for " + propFileName);
464             } catch (IOException JavaDoc e) {
465                 System.err.println(e);
466             }
467             return configFile;
468         }
469     }
470
471     /**
472      * Get registry protocol
473      * @return protocol name (jrmp, jeremie, iiop, cmi)
474      */

475     public String JavaDoc getRegistryProtocol() {
476         String JavaDoc protocol = null;
477         try {
478             protocol = getCarolProperties().getProperty("carol.protocols");
479         } catch (FileNotFoundException JavaDoc e) {
480             e.printStackTrace();
481         }
482         return protocol;
483     }
484
485     /**
486      * Get registry url
487      * @return url of the registry
488      */

489     public String JavaDoc getRegistryUrl() {
490         String JavaDoc protocol = null;
491         String JavaDoc url = null;
492         try {
493             protocol = getCarolProperties().getProperty("carol.protocols");
494
495             try {
496                 url = getCarolProperties().getProperty("carol." + protocol + ".url");
497             } catch (FileNotFoundException JavaDoc e) {
498                 e.printStackTrace();
499             }
500         } catch (FileNotFoundException JavaDoc e) {
501             e.printStackTrace();
502         }
503         return url;
504     }
505
506     /**
507      * Get Jvm Version
508      * @return the java version
509      */

510     public String JavaDoc getJvmVersion() {
511         return systEnv.getProperty("java.version");
512     }
513
514     /**
515      * Get Jvm vendor
516      * @return value of the property java.vm.specification.vendor
517      */

518     public String JavaDoc getJvmVendor() {
519         return systEnv.getProperty("java.vm.specification.vendor");
520     }
521 }
Popular Tags