001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JProperties.java 7255 2005-08-17 15:07:09Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.util;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.net.URL;
032: import java.util.Properties;
033:
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037: import javax.rmi.PortableRemoteObject;
038:
039: import org.objectweb.jonas.adm.AdmInterface;
040:
041: /**
042: * JOnAS property accessor
043: * @author kemlerp
044: */
045: public class JProperties {
046:
047: /**
048: * Name of the JOnAS server used for tests
049: */
050: private static String jonasName = "jonas";
051:
052: /**
053: * Initial context used for lookup
054: */
055: private static Context ictx = null;
056:
057: /**
058: * JOnAS admin used for communicate via JMX to JOnAS
059: */
060: private static AdmInterface admI = null;
061:
062: /**
063: * System properties
064: */
065: private static Properties systEnv = System.getProperties();
066:
067: /**
068: * Separator of file
069: */
070: private static String fileSeparator = systEnv
071: .getProperty("file.separator");
072:
073: /**
074: * -Djonas.test property
075: */
076: private static final String JONAS_TEST = "jonas.test";
077:
078: /**
079: * JONAS_TEST
080: */
081: private static String jonasTest = systEnv.getProperty(JONAS_TEST);
082:
083: /**
084: * -Djonas.base property
085: */
086: private static final String JONAS_BASE = "jonas.base";
087:
088: /**
089: * JONAS_BASE
090: */
091: private static String jonasBase = systEnv.getProperty(JONAS_BASE);
092:
093: /**
094: * resource directory name
095: */
096: private static final String RESOURCE_DIR = "resources";
097:
098: /**
099: * conf directory name
100: */
101: private static final String 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 getInitialContext() throws NamingException {
109: return new InitialContext();
110: }
111:
112: /**
113: * Get list environnement
114: * @return JOnAS protperties
115: * @throws Exception if list environnement cannot be got
116: */
117: public Properties getPropertiesEnv() throws Exception {
118: Properties prop = null;
119:
120: try {
121: if (ictx == null) {
122: ictx = getInitialContext();
123: }
124: if (admI == null) {
125: admI = (AdmInterface) PortableRemoteObject
126: .narrow(ictx.lookup(jonasName + "_Adm"),
127: AdmInterface.class);
128: }
129: prop = admI.listEnv();
130: } catch (Exception e) {
131: throw new Exception("Cannot get list environnement : "
132: + e.getMessage());
133: }
134:
135: return prop;
136: }
137:
138: /**
139: * Get services in jonas properties
140: * @return A table of string with services in jonas properties
141: * @throws Exception if properties cannot be got
142: */
143: public String[] getServices() throws Exception {
144: Properties propertiesEnv = getPropertiesEnv();
145: String services = propertiesEnv.getProperty("jonas.services");
146: return services.split(",");
147: }
148:
149: /**
150: * Search a service in services
151: * @param service The string that is searched
152: * @return true if there is the service in services else false
153: * @throws Exception if services cannot be got
154: */
155: public boolean searchService(String service) throws Exception {
156: String[] services = getServices();
157: boolean found = false;
158: int i = 0;
159:
160: while (!found && i < services.length) {
161: if (services[i].equals(service)) {
162: found = true;
163: }
164: i++;
165: }
166:
167: return found;
168: }
169:
170: /**
171: * Db in service property
172: * @return True if db is a value of service property
173: * @throws Exception if service property cannot be got
174: */
175: public boolean isDb() throws Exception {
176: boolean exist = false;
177: try {
178: if (searchService("db")) {
179: exist = true;
180: }
181: } catch (Exception e) {
182: throw new Exception("Cannot get services : "
183: + e.getMessage());
184: }
185: return exist;
186: }
187:
188: /**
189: * Dbm in service property
190: * @return True if dbm is a value of service property
191: * @throws Exception if service property cannot be got
192: */
193: public boolean isDbm() throws Exception {
194: boolean exist = false;
195: try {
196: if (searchService("dbm")) {
197: exist = true;
198: }
199: } catch (Exception e) {
200: throw new Exception("Cannot get services : "
201: + e.getMessage());
202: }
203: return exist;
204: }
205:
206: /**
207: * Discovery in service property
208: * @return True if discovery is a value of service property
209: * @throws Exception if service property cannot be got
210: */
211: public boolean isDiscovery() throws Exception {
212: boolean exist = false;
213: try {
214: if (searchService("discovery")) {
215: exist = true;
216: }
217: } catch (Exception e) {
218: throw new Exception("Cannot get services : "
219: + e.getMessage());
220: }
221: return exist;
222: }
223:
224: /**
225: * Ear in service property
226: * @return True if ear is a value of service property
227: * @throws Exception if service property cannot be got
228: */
229: public boolean isEar() throws Exception {
230: boolean exist = false;
231: try {
232: if (searchService("ear")) {
233: exist = true;
234: }
235: } catch (Exception e) {
236: throw new Exception("Cannot get services : "
237: + e.getMessage());
238: }
239: return exist;
240: }
241:
242: /**
243: * Ejb in service property
244: * @return True if ejb is a value of service property
245: * @throws Exception if service property cannot be got
246: */
247: public boolean isEjb() throws Exception {
248: boolean exist = false;
249: try {
250: if (searchService("ejb")) {
251: exist = true;
252: }
253: } catch (Exception e) {
254: throw new Exception("Cannot get services : "
255: + e.getMessage());
256: }
257: return exist;
258: }
259:
260: /**
261: * Jms in service property
262: * @return True if jms is a value of service property
263: * @throws Exception if service property cannot be got
264: */
265: public boolean isJms() throws Exception {
266: boolean exist = false;
267: try {
268: if (searchService("jms")) {
269: exist = true;
270: }
271: } catch (Exception e) {
272: throw new Exception("Cannot get services : "
273: + e.getMessage());
274: }
275: return exist;
276: }
277:
278: /**
279: * Jmx in service property
280: * @return True if Jmx is a value of service property
281: * @throws Exception if service property cannot be got
282: */
283: public boolean isJmx() throws Exception {
284: boolean exist = false;
285: try {
286: if (searchService("jmx")) {
287: exist = true;
288: }
289: } catch (Exception e) {
290: throw new Exception("Cannot get services : "
291: + e.getMessage());
292: }
293: return exist;
294: }
295:
296: /**
297: * Jtm in service property
298: * @return True if jtm is a value of service property
299: * @throws Exception if service property cannot be got
300: */
301: public boolean isJtm() throws Exception {
302: boolean exist = false;
303: try {
304: if (searchService("jtm")) {
305: exist = true;
306: }
307: } catch (Exception e) {
308: throw new Exception("Cannot get services : "
309: + e.getMessage());
310: }
311: return exist;
312: }
313:
314: /**
315: * Mail in service property
316: * @return True if db is a value of service property
317: * @throws Exception if service property cannot be got
318: */
319: public boolean isMail() throws Exception {
320: boolean exist = false;
321: try {
322: if (searchService("mail")) {
323: exist = true;
324: }
325: } catch (Exception e) {
326: throw new Exception("Cannot get services : "
327: + e.getMessage());
328: }
329: return exist;
330: }
331:
332: /**
333: * Registry in service property
334: * @return True if registry is a value of service property
335: * @throws Exception if service property cannot be got
336: */
337: public boolean isRegistry() throws Exception {
338: boolean exist = false;
339: try {
340: if (searchService("registry")) {
341: exist = true;
342: }
343: } catch (Exception e) {
344: throw new Exception("Cannot get services : "
345: + e.getMessage());
346: }
347: return exist;
348: }
349:
350: /**
351: * Resource in service property
352: * @return True if resource is a value of service property
353: * @throws Exception if service property cannot be got
354: */
355: public boolean isResource() throws Exception {
356: boolean exist = false;
357: try {
358: if (searchService("resource")) {
359: exist = true;
360: }
361: } catch (Exception e) {
362: throw new Exception("Cannot get services : "
363: + e.getMessage());
364: }
365: return exist;
366: }
367:
368: /**
369: * Security in service property
370: * @return True if security is a value of service property
371: * @throws Exception if service property cannot be got
372: */
373: public boolean isSecurity() throws Exception {
374: boolean exist = false;
375: try {
376: if (searchService("security")) {
377: exist = true;
378: }
379: } catch (Exception e) {
380: throw new Exception("Cannot get services : "
381: + e.getMessage());
382: }
383: return exist;
384: }
385:
386: /**
387: * Web in service property
388: * @return True if web is a value of service property
389: * @throws Exception if service property cannot be got
390: */
391: public boolean isWeb() throws Exception {
392: boolean exist = false;
393: try {
394: if (searchService("web")) {
395: exist = true;
396: }
397: } catch (Exception e) {
398: throw new Exception("Cannot get services : "
399: + e.getMessage());
400: }
401: return exist;
402: }
403:
404: /**
405: * Ws in service property
406: * @return True if ws is a value of service property
407: * @throws Exception if service property cannot be got
408: */
409: public boolean isWs() throws Exception {
410: boolean exist = false;
411: try {
412: if (searchService("ws")) {
413: exist = true;
414: }
415: } catch (Exception e) {
416: throw new Exception("Cannot get services : "
417: + e.getMessage());
418: }
419: return exist;
420: }
421:
422: /**
423: * JMS service location
424: * @return True if it is collocated, else False
425: * @throws Exception if property cannot be got
426: */
427: public boolean isJMSCollocated() throws Exception {
428: Properties propertiesEnv = getPropertiesEnv();
429: boolean collocated = Boolean.valueOf(
430: propertiesEnv
431: .getProperty("jonas.service.jms.collocated"))
432: .booleanValue();
433: return collocated;
434: }
435:
436: /**
437: * Catalina in jonas properties
438: * @return True if Catalina is the web service, else false
439: * @throws Exception if property cannot be got
440: */
441: public boolean isCatalina() throws Exception {
442: Properties propertiesEnv = getPropertiesEnv();
443: String webClass = propertiesEnv
444: .getProperty("jonas.service.web.class");
445: return webClass.endsWith("CatalinaJWebContainerServiceWrapper");
446: }
447:
448: /**
449: * Jtm location
450: * @return True if jtm is remote, False if it is collocated
451: * @throws Exception if property cannot be got
452: */
453: public boolean isJtmRemote() throws Exception {
454: Properties propertiesEnv = getPropertiesEnv();
455: boolean remote = Boolean.valueOf(
456: propertiesEnv.getProperty("jonas.service.jtm.remote"))
457: .booleanValue();
458: return remote;
459: }
460:
461: /**
462: * Get properties from a file in resources directory.
463: * @param fileName name of the property file with ".properties".
464: * @return Properties.
465: * @throws Exception if the file is not found.
466: */
467: public Properties getProperties(String fileName) throws Exception {
468: // Get ClassLoader
469: ClassLoader cl = Thread.currentThread().getContextClassLoader();
470: URL url = cl.getResource(fileName);
471: InputStream is = cl.getResourceAsStream(fileName);
472: Properties configFile = new Properties();
473: configFile.load(is);
474: return configFile;
475: }
476:
477: /**
478: * Get properties from a file in $JONAS_TEST/jonasadmin directory.
479: * @param fileName name of the property file without ".properties".
480: * @return Properties.
481: * @throws FileNotFoundException if the file is not found.
482: */
483: private Properties getCarolProperties()
484: throws FileNotFoundException {
485: // ${jonas.base}/conf/carol.properties
486: if (jonasBase.equalsIgnoreCase("${myenv.JONAS_BASE}")) {
487: throw new FileNotFoundException(
488: "You must add JONAS_BASE in your environnement variables. ");
489: } else {
490: String propFileName = jonasBase + fileSeparator + CONF_DIR
491: + fileSeparator + "carol" + ".properties";
492:
493: File f = null;
494: Properties configFile = new Properties();
495: try {
496: f = new File(propFileName);
497: FileInputStream is = new FileInputStream(f);
498: configFile.load(is);
499: } catch (FileNotFoundException e) {
500: throw new FileNotFoundException(
501: "Cannot find properties for " + propFileName);
502: } catch (IOException e) {
503: System.err.println(e);
504: }
505: return configFile;
506: }
507: }
508:
509: /**
510: * Get registry protocol
511: * @return protocol name (jrmp, jeremie, iiop, cmi)
512: */
513: public String getRegistryProtocol() {
514: String protocol = null;
515: try {
516: protocol = getCarolProperties().getProperty(
517: "carol.protocols");
518: } catch (FileNotFoundException e) {
519: e.printStackTrace();
520: }
521: return protocol;
522: }
523:
524: /**
525: * Get registry url
526: * @return url of the registry
527: */
528: public String getRegistryUrl() {
529: String protocol = null;
530: String url = null;
531: try {
532: protocol = getCarolProperties().getProperty(
533: "carol.protocols");
534:
535: try {
536: url = getCarolProperties().getProperty(
537: "carol." + protocol + ".url");
538: } catch (FileNotFoundException e) {
539: e.printStackTrace();
540: }
541: } catch (FileNotFoundException e) {
542: e.printStackTrace();
543: }
544: return url;
545: }
546:
547: /**
548: * Get Jvm Version
549: * @return the java version
550: */
551: public String getJvmVersion() {
552: return systEnv.getProperty("java.version");
553: }
554:
555: /**
556: * Get Jvm vendor
557: * @return value of the property java.vm.specification.vendor
558: */
559: public String getJvmVendor() {
560: return systEnv.getProperty("java.vm.specification.vendor");
561: }
562: }
|