KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > JiveGlobals


1 /**
2  * $RCSfile: JiveGlobals.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/05/31 03:25:19 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import org.dom4j.Document;
15 import org.dom4j.io.SAXReader;
16
17 import javax.naming.InitialContext JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.util.*;
23
24 /**
25  * Controls Jive properties. Jive properties are only meant to be set and retrieved
26  * by core Jive classes.
27  * <p/>
28  * The location of the home directory should be specified one of
29  * three ways:
30  * <ol>
31  * <li>Set a Java system property named <tt>home</tt> with the full path to your
32  * home directory.
33  * <li>Indicate its value in the <tt>messenger_init.xml</tt> file. This
34  * is a simple xml file that should look something like:<br>
35  * <tt><home>c:\JiveMessenger</home></tt> (Windows) <br>
36  * or <br>
37  * <tt><home>/var/JiveMessenger</home></tt> (Unix) <p>
38  * <p/>
39  * The file must be in your classpath so that it can be loaded by Java's classloader.
40  * <li>Use another class in your VM to set the <tt>JiveGlobals.home</tt> variable.
41  * This must be done before the rest of Jive starts up, for example: in a servlet that
42  * is set to run as soon as the appserver starts up.
43  * </ol>
44  * <p/>
45  * All property names must be in the form <code>prop.name</code> - parts of the name must
46  * be seperated by ".". The value can be any valid String, including strings with line breaks.
47  */

48 public class JiveGlobals {
49
50     private static String JavaDoc JIVE_CONFIG_FILENAME = null;
51
52     /**
53      * Location of the jiveHome directory. All configuration files should be
54      * located here.
55      */

56     public static String JavaDoc home = null;
57
58     public static boolean failedLoading = false;
59
60     private static XMLProperties xmlProperties = null;
61     private static JiveProperties properties = null;
62
63     private static Locale locale = null;
64     private static TimeZone timeZone = null;
65     private static DateFormat JavaDoc dateFormat = null;
66     private static DateFormat JavaDoc dateTimeFormat = null;
67     private static DateFormat JavaDoc timeFormat = null;
68
69     /**
70      * Returns the global Locale used by Jive. A locale specifies language
71      * and country codes, and is used for internationalization. The default
72      * locale is system dependant - Locale.getDefault().
73      *
74      * @return the global locale used by Jive.
75      */

76     public static Locale getLocale() {
77         if (locale == null) {
78             if (xmlProperties != null) {
79                 String JavaDoc [] localeArray;
80                 String JavaDoc localeProperty = (String JavaDoc) xmlProperties.getProperty("locale");
81                 if (localeProperty != null) {
82                     localeArray = localeProperty.split("_");
83                 }
84                 else {
85                     localeArray = new String JavaDoc[] {"", ""};
86                 }
87
88                 String JavaDoc language = localeArray[0];
89                 if (language == null) {
90                     language = "";
91                 }
92                 String JavaDoc country = "";
93                 if (localeArray.length == 2) {
94                     country = localeArray[1];
95                 }
96                 // If no locale info is specified, return the system default Locale.
97
if (language.equals("") && country.equals("")) {
98                     locale = Locale.getDefault();
99                 }
100                 else {
101                     locale = new Locale(language, country);
102                 }
103             }
104             else {
105                 return Locale.getDefault();
106             }
107         }
108         return locale;
109     }
110
111     /**
112      * Sets the global locale used by Jive. A locale specifies language
113      * and country codes, and is used for formatting dates and numbers.
114      * The default locale is Locale.US.
115      *
116      * @param newLocale the global Locale for Jive.
117      */

118     public static void setLocale(Locale newLocale) {
119         locale = newLocale;
120         // Save values to Jive properties.
121
setXMLProperty("locale", locale.toString());
122
123         // Reset the date formatter objects
124
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
125         dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
126         dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
127                 DateFormat.MEDIUM, locale);
128         timeFormat.setTimeZone(timeZone);
129         dateFormat.setTimeZone(timeZone);
130         dateTimeFormat.setTimeZone(timeZone);
131     }
132
133     /**
134      * Returns the global TimeZone used by Jive. The default is the VM's
135      * default time zone.
136      *
137      * @return the global time zone used by Jive.
138      */

139     public static TimeZone getTimeZone() {
140         if (timeZone == null) {
141             if (properties != null) {
142                 String JavaDoc timeZoneID = (String JavaDoc)properties.get("locale.timeZone");
143                 if (timeZoneID == null) {
144                     timeZone = TimeZone.getDefault();
145                 }
146                 else {
147                     timeZone = TimeZone.getTimeZone(timeZoneID);
148                 }
149             }
150             else {
151                 return TimeZone.getDefault();
152             }
153         }
154         return timeZone;
155     }
156
157     /**
158      * Sets the global time zone used by Jive. The default time zone is the VM's
159      * time zone.
160      */

161     public static void setTimeZone(TimeZone newTimeZone) {
162         timeZone = newTimeZone;
163         timeFormat.setTimeZone(timeZone);
164         dateFormat.setTimeZone(timeZone);
165         dateTimeFormat.setTimeZone(timeZone);
166         setProperty("locale.timeZone", timeZone.getID());
167     }
168
169     /**
170      * Formats a Date object to return a time using the global locale.
171      *
172      * @param date the Date to format.
173      * @return a String representing the time.
174      */

175     public static String JavaDoc formatTime(Date date) {
176         if (timeFormat == null) {
177             if (properties != null) {
178                 timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale());
179                 timeFormat.setTimeZone(getTimeZone());
180             }
181             else {
182                 DateFormat JavaDoc instance = DateFormat.getTimeInstance(DateFormat.SHORT, getLocale());
183                 instance.setTimeZone(getTimeZone());
184                 return instance.format(date);
185             }
186         }
187         return timeFormat.format(date);
188     }
189
190     /**
191      * Formats a Date object to return a date using the global locale.
192      *
193      * @param date the Date to format.
194      * @return a String representing the date.
195      */

196     public static String JavaDoc formatDate(Date date) {
197         if (dateFormat == null) {
198             if (properties != null) {
199                 dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
200                 dateFormat.setTimeZone(getTimeZone());
201             }
202             else {
203                 DateFormat JavaDoc instance = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
204                 instance.setTimeZone(getTimeZone());
205                 return instance.format(date);
206             }
207         }
208         return dateFormat.format(date);
209     }
210
211     /**
212      * Formats a Date object to return a date and time using the global locale.
213      *
214      * @param date the Date to format.
215      * @return a String representing the date and time.
216      */

217     public static String JavaDoc formatDateTime(Date date) {
218         if (dateTimeFormat == null) {
219             if (properties != null) {
220                 dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
221                         DateFormat.MEDIUM, getLocale());
222                 dateTimeFormat.setTimeZone(getTimeZone());
223             }
224             else {
225                 DateFormat JavaDoc instance = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
226                         DateFormat.MEDIUM, getLocale());
227                 instance.setTimeZone(getTimeZone());
228                 return instance.format(date);
229             }
230         }
231         return dateTimeFormat.format(date);
232     }
233
234     /**
235      * Returns the location of the <code>home</code> directory.
236      *
237      * @return the location of the home dir.
238      */

239     public static String JavaDoc getHomeDirectory() {
240         if (home == null) {
241             loadSetupProperties();
242         }
243         return home;
244     }
245
246     /**
247      * Returns a local property. Local properties are stored in the file defined in
248      * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
249      * Properties are always specified as "foo.bar.prop", which would map to
250      * the following entry in the XML file:
251      * <pre>
252      * &lt;foo&gt;
253      * &lt;bar&gt;
254      * &lt;prop&gt;some value&lt;/prop&gt;
255      * &lt;/bar&gt;
256      * &lt;/foo&gt;
257      * </pre>
258      *
259      * @param name the name of the property to return.
260      * @return the property value specified by name.
261      */

262     public static String JavaDoc getXMLProperty(String JavaDoc name) {
263         if (xmlProperties == null) {
264             loadSetupProperties();
265         }
266
267         // home not loaded?
268
if (xmlProperties == null) {
269             return null;
270         }
271
272         return xmlProperties.getProperty(name);
273     }
274
275     /**
276      * Returns a local property. Local properties are stored in the file defined in
277      * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
278      * Properties are always specified as "foo.bar.prop", which would map to
279      * the following entry in the XML file:
280      * <pre>
281      * &lt;foo&gt;
282      * &lt;bar&gt;
283      * &lt;prop&gt;some value&lt;/prop&gt;
284      * &lt;/bar&gt;
285      * &lt;/foo&gt;
286      * </pre>
287      *
288      * If the specified property can't be found, the <tt>defaultValue</tt> will be returned.
289      *
290      * @param name the name of the property to return.
291      * @param defaultValue the default value for the property.
292      * @return the property value specified by name.
293      */

294     public static String JavaDoc getXMLProperty(String JavaDoc name, String JavaDoc defaultValue) {
295         if (xmlProperties == null) {
296             loadSetupProperties();
297         }
298
299         // home not loaded?
300
if (xmlProperties == null) {
301             return null;
302         }
303
304         String JavaDoc value = xmlProperties.getProperty(name);
305         if (value == null) {
306             value = defaultValue;
307         }
308         return value;
309     }
310
311     /**
312      * Returns an integer value local property. Local properties are stored in the file defined in
313      * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
314      * Properties are always specified as "foo.bar.prop", which would map to
315      * the following entry in the XML file:
316      * <pre>
317      * &lt;foo&gt;
318      * &lt;bar&gt;
319      * &lt;prop&gt;some value&lt;/prop&gt;
320      * &lt;/bar&gt;
321      * &lt;/foo&gt;
322      * </pre>
323      *
324      * If the specified property can't be found, or if the value is not a number, the
325      * <tt>defaultValue</tt> will be returned.
326      *
327      * @param name the name of the property to return.
328      * @param defaultValue value returned if the property could not be loaded or was not
329      * a number.
330      * @return the property value specified by name or <tt>defaultValue</tt>.
331      */

332     public static int getXMLProperty(String JavaDoc name, int defaultValue) {
333         String JavaDoc value = getXMLProperty(name);
334         if (value != null) {
335             try {
336                 return Integer.parseInt(value);
337             }
338             catch (NumberFormatException JavaDoc nfe) { }
339         }
340         return defaultValue;
341     }
342
343     /**
344      * Sets a local property. If the property doesn't already exists, a new
345      * one will be created. Local properties are stored in the file defined in
346      * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
347      * Properties are always specified as "foo.bar.prop", which would map to
348      * the following entry in the XML file:
349      * <pre>
350      * &lt;foo&gt;
351      * &lt;bar&gt;
352      * &lt;prop&gt;some value&lt;/prop&gt;
353      * &lt;/bar&gt;
354      * &lt;/foo&gt;
355      * </pre>
356      *
357      * @param name the name of the property being set.
358      * @param value the value of the property being set.
359      */

360     public static void setXMLProperty(String JavaDoc name, String JavaDoc value) {
361         if (xmlProperties == null) {
362             loadSetupProperties();
363         }
364
365         // jiveHome not loaded?
366
if (xmlProperties != null) {
367             xmlProperties.setProperty(name, value);
368         }
369     }
370
371     /**
372      * Sets multiple local properties at once. If a property doesn't already exists, a new
373      * one will be created. Local properties are stored in the file defined in
374      * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
375      * Properties are always specified as "foo.bar.prop", which would map to
376      * the following entry in the XML file:
377      * <pre>
378      * &lt;foo&gt;
379      * &lt;bar&gt;
380      * &lt;prop&gt;some value&lt;/prop&gt;
381      * &lt;/bar&gt;
382      * &lt;/foo&gt;
383      * </pre>
384      *
385      * @param propertyMap a map of properties, keyed on property name.
386      */

387     public static void setXMLProperties(Map propertyMap) {
388         if (xmlProperties == null) {
389             loadSetupProperties();
390         }
391
392         if (xmlProperties != null) {
393             xmlProperties.setProperties(propertyMap);
394         }
395     }
396
397     /**
398      * Return all immediate children property values of a parent local property as a list of strings,
399      * or an empty list if there are no children. For example, given
400      * the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, <tt>X.Y.C</tt> and <tt>X.Y.C.D</tt>, then
401      * the immediate child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
402      * <tt>C</tt> (the value of <tt>C.D</tt> would not be returned using this method).<p>
403      *
404      * Local properties are stored in the file defined in <tt>JIVE_CONFIG_FILENAME</tt> that exists
405      * in the <tt>home</tt> directory. Properties are always specified as "foo.bar.prop",
406      * which would map to the following entry in the XML file:
407      * <pre>
408      * &lt;foo&gt;
409      * &lt;bar&gt;
410      * &lt;prop&gt;some value&lt;/prop&gt;
411      * &lt;/bar&gt;
412      * &lt;/foo&gt;
413      * </pre>
414      *
415      *
416      * @param parent the name of the parent property to return the children for.
417      * @return all child property values for the given parent.
418      */

419     public static List getXMLProperties(String JavaDoc parent) {
420         if (xmlProperties == null) {
421             loadSetupProperties();
422         }
423
424         // jiveHome not loaded?
425
if (xmlProperties == null) {
426             return Collections.EMPTY_LIST;
427         }
428
429         String JavaDoc[] propNames = xmlProperties.getChildrenProperties(parent);
430         List values = new ArrayList();
431         for (int i = 0; i < propNames.length; i++) {
432             String JavaDoc propName = propNames[i];
433             String JavaDoc value = getProperty(parent + "." + propName);
434             if (value != null) {
435                 values.add(value);
436             }
437         }
438
439         return values;
440     }
441
442     /**
443      * Deletes a locale property. If the property doesn't exist, the method
444      * does nothing.
445      *
446      * @param name the name of the property to delete.
447      */

448     public static void deleteXMLProperty(String JavaDoc name) {
449         if (xmlProperties == null) {
450             loadSetupProperties();
451         }
452         xmlProperties.deleteProperty(name);
453     }
454
455     /**
456      * Returns a Jive property.
457      *
458      * @param name the name of the property to return.
459      * @return the property value specified by name.
460      */

461     public static String JavaDoc getProperty(String JavaDoc name) {
462         if (properties == null) {
463             if (isSetupMode()) {
464                 return null;
465             }
466             properties = JiveProperties.getInstance();
467         }
468         return (String JavaDoc)properties.get(name);
469     }
470
471     /**
472      * Returns a Jive property. If the specified property doesn't exist, the
473      * <tt>defaultValue</tt> will be returned.
474      *
475      * @param name the name of the property to return.
476      * @param defaultValue value returned if the property doesn't exist.
477      * @return the property value specified by name.
478      */

479     public static String JavaDoc getProperty(String JavaDoc name, String JavaDoc defaultValue) {
480         if (properties == null) {
481             if (isSetupMode()) {
482                 return defaultValue;
483             }
484             properties = JiveProperties.getInstance();
485         }
486         String JavaDoc value = (String JavaDoc)properties.get(name);
487         if (value != null) {
488             return value;
489         }
490         else {
491             return defaultValue;
492         }
493     }
494
495     /**
496      * Returns an integer value Jive property. If the specified property doesn't exist, the
497      * <tt>defaultValue</tt> will be returned.
498      *
499      * @param name the name of the property to return.
500      * @param defaultValue value returned if the property doesn't exist or was not
501      * a number.
502      * @return the property value specified by name or <tt>defaultValue</tt>.
503      */

504     public static int getIntProperty(String JavaDoc name, int defaultValue) {
505         String JavaDoc value = getProperty(name);
506         if (value != null) {
507             try {
508                 return Integer.parseInt(value);
509             }
510             catch (NumberFormatException JavaDoc nfe) { }
511         }
512         return defaultValue;
513     }
514
515     /**
516      * Returns a boolean value Jive property.
517      *
518      * @param name the name of the property to return.
519      * @return true if the property value exists and is set to <tt>"true"</tt> (ignoring case).
520      * Otherwise <tt>false</tt> is returned.
521      */

522     public static boolean getBooleanProperty(String JavaDoc name) {
523         return Boolean.valueOf(getProperty(name)).booleanValue();
524     }
525
526     /**
527      * Returns a boolean value Jive property. If the property doesn't exist, the <tt>defaultValue</tt>
528      * will be returned.
529      *
530      * If the specified property can't be found, or if the value is not a number, the
531      * <tt>defaultValue</tt> will be returned.
532      *
533      * @param name the name of the property to return.
534      * @param defaultValue value returned if the property doesn't exist.
535      * @return true if the property value exists and is set to <tt>"true"</tt> (ignoring case).
536      * Otherwise <tt>false</tt> is returned.
537      */

538     public static boolean getBooleanProperty(String JavaDoc name, boolean defaultValue) {
539         String JavaDoc value = getProperty(name);
540         if (value != null) {
541             return Boolean.valueOf(getProperty(name)).booleanValue();
542         }
543         else {
544             return defaultValue;
545         }
546     }
547
548     /**
549      * Return all immediate children property names of a parent Jive property as a list of strings,
550      * or an empty list if there are no children. For example, given
551      * the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, <tt>X.Y.C</tt> and <tt>X.Y.C.D</tt>, then
552      * the immediate child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
553      * <tt>C</tt> (<tt>C.D</tt> would not be returned using this method).<p>
554      *
555      * @return a List of all immediate children property names (Strings).
556      */

557     public static List<String JavaDoc> getPropertyNames(String JavaDoc parent) {
558         if (properties == null) {
559             if (isSetupMode()) {
560                 return new ArrayList<String JavaDoc>();
561             }
562             properties = JiveProperties.getInstance();
563         }
564         return new ArrayList<String JavaDoc>(properties.getChildrenNames(parent));
565     }
566
567     /**
568      * Return all immediate children property values of a parent Jive property as a list of strings,
569      * or an empty list if there are no children. For example, given
570      * the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, <tt>X.Y.C</tt> and <tt>X.Y.C.D</tt>, then
571      * the immediate child properties of <tt>X.Y</tt> are <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and
572      * <tt>X.Y.C</tt> (the value of <tt>X.Y.C.D</tt> would not be returned using this method).<p>
573      *
574      * @param parent the name of the parent property to return the children for.
575      * @return all child property values for the given parent.
576      */

577     public static List<String JavaDoc> getProperties(String JavaDoc parent) {
578         if (properties == null) {
579             if (isSetupMode()) {
580                 return new ArrayList<String JavaDoc>();
581             }
582             properties = JiveProperties.getInstance();
583         }
584
585         Collection<String JavaDoc> propertyNames = properties.getChildrenNames(parent);
586         List<String JavaDoc> values = new ArrayList<String JavaDoc>();
587         for (Iterator i=propertyNames.iterator(); i.hasNext(); ) {
588             String JavaDoc propName = (String JavaDoc)i.next();
589             String JavaDoc value = getProperty(propName);
590             if (value != null) {
591                 values.add(value);
592             }
593         }
594
595         return values;
596     }
597
598     /**
599      * Returns all Jive property names.
600      *
601      * @return a List of all property names (Strings).
602      */

603     public static List<String JavaDoc> getPropertyNames() {
604         if (properties == null) {
605             if (isSetupMode()) {
606                 return new ArrayList<String JavaDoc>();
607             }
608             properties = JiveProperties.getInstance();
609         }
610         return new ArrayList<String JavaDoc>(properties.getPropertyNames());
611     }
612
613     /**
614      * Sets a Jive property. If the property doesn't already exists, a new
615      * one will be created.
616      *
617      * @param name the name of the property being set.
618      * @param value the value of the property being set.
619      */

620     public static void setProperty(String JavaDoc name, String JavaDoc value) {
621         if (properties == null) {
622             if (isSetupMode()) {
623                 return;
624             }
625             properties = JiveProperties.getInstance();
626         }
627         properties.put(name, value);
628     }
629
630    /**
631      * Sets multiple Jive properties at once. If a property doesn't already exists, a new
632      * one will be created.
633      *
634      * @param propertyMap a map of properties, keyed on property name.
635      */

636     public static void setProperties(Map propertyMap) {
637         if (properties == null) {
638             if (isSetupMode()) {
639                 return;
640             }
641             properties = JiveProperties.getInstance();
642         }
643
644         properties.putAll(propertyMap);
645     }
646
647     /**
648      * Deletes a Jive property. If the property doesn't exist, the method
649      * does nothing. All children of the property will be deleted as well.
650      *
651      * @param name the name of the property to delete.
652      */

653     public static void deleteProperty(String JavaDoc name) {
654         if (properties == null) {
655             if (isSetupMode()) {
656                 return;
657             }
658             properties = JiveProperties.getInstance();;
659         }
660         properties.remove(name);
661     }
662
663    /**
664     * Allows the name of the local config file name to be changed. The
665     * default is "jive-messenger.xml".
666     *
667     * @param configName the name of the config file.
668     */

669     public static void setConfigName(String JavaDoc configName) {
670         JIVE_CONFIG_FILENAME = configName;
671     }
672
673     /**
674      * Returns the name of the local config file name.
675      *
676      * @return the name of the config file.
677      */

678     static String JavaDoc getConfigName() {
679         if (JIVE_CONFIG_FILENAME == null) {
680             JIVE_CONFIG_FILENAME = "jive-messenger.xml";
681         };
682         return JIVE_CONFIG_FILENAME;
683     }
684
685     /**
686      * Returns true if in setup mode.
687      *
688      * @return true if in setup mode.
689      */

690     private static boolean isSetupMode() {
691         return !(Boolean.valueOf(JiveGlobals.getXMLProperty("setup")).booleanValue());
692     }
693
694     /**
695      * Loads properties if necessary. Property loading must be done lazily so
696      * that we give outside classes a chance to set <tt>home</tt>.
697      */

698     private synchronized static void loadSetupProperties() {
699         if (failedLoading) {
700             return;
701         }
702         if (xmlProperties == null) {
703             // If jiveHome is still null, no outside process has set it and
704
// we have to attempt to load the value from jive_init.xml,
705
// which must be in the classpath.
706
if (home == null) {
707                 home = new InitPropLoader().getHome();
708             }
709             // If that failed, try loading it from JNDI
710
if (home == null) {
711                 try {
712                     InitialContext JavaDoc context = new InitialContext JavaDoc();
713                     home = (String JavaDoc)context.lookup("java:comp/env/home");
714                 }
715                 catch (Exception JavaDoc e) { }
716             }
717             // Finally, try to load it jiveHome as a system property.
718
if (home == null) {
719                 home = System.getProperty("home");
720             }
721
722             if(home == null){
723                 try {
724                     home = new File JavaDoc("..").getCanonicalPath();
725                     if(!new File JavaDoc(home, "conf/" + getConfigName()).exists()){
726                         home = null;
727                     }
728                 }
729                 catch (IOException JavaDoc e) {
730                     e.printStackTrace();
731                 }
732             }
733
734              if(home == null){
735                 try {
736                     home = new File JavaDoc("").getCanonicalPath();
737                     if(!new File JavaDoc(home, "conf/" + getConfigName()).exists()){
738                         home = null;
739                     }
740                 }
741                 catch (IOException JavaDoc e) {
742                     e.printStackTrace();
743                 }
744             }
745
746             // If still null, finding home failed.
747
if (home == null) {
748                 failedLoading = true;
749                 StringBuilder JavaDoc msg = new StringBuilder JavaDoc();
750                 msg.append("Critical Error! The home directory could not be loaded, \n");
751                 msg.append("which will prevent the application from working correctly.\n\n");
752                 msg.append("You must set home in one of four ways:\n");
753                 msg.append(" 1) Add a messenger_init.xml file to your classpath, which points \n ");
754                 msg.append(" to home.\n");
755                 msg.append(" 3) Set the JNDI value \"java:comp/env/home\" with a String \n");
756                 msg.append(" that points to your home directory. \n");
757                 msg.append(" 4) Set the Java system property \"home\".\n\n");
758                 msg.append("Further instructions for setting home can be found in the \n");
759                 msg.append("installation documentation.");
760                 System.err.println(msg.toString());
761                 return;
762             }
763             // Create a manager with the full path to the xml config file.
764
try {
765                 // Do a permission check on the jiveHome directory:
766
File JavaDoc mh = new File JavaDoc(home);
767                 if (!mh.exists()) {
768                     Log.error("Error - the specified home directory does not exist (" + home + ")");
769                 }
770                 else {
771                     if (!mh.canRead() || !mh.canWrite()) {
772                         Log.error("Error - the user running this application can not read " +
773                                 "and write to the specified home directory (" + home + "). " +
774                                 "Please grant the executing user read and write permissions.");
775                     }
776                 }
777                 xmlProperties = new XMLProperties(home + File.separator + "conf" +
778                         File.separator + getConfigName());
779             }
780             catch (IOException JavaDoc ioe) {
781                 Log.error(ioe);
782                 failedLoading = true;
783                 return;
784             }
785         }
786     }
787 }
788
789 /**
790  * A very small class to load the file defined in JiveGlobals.JIVE_CONFIG_FILENAME. The class is
791  * needed since loading files from the classpath in a static context often
792  * fails.
793  */

794 class InitPropLoader {
795
796     public String JavaDoc getHome() {
797         String JavaDoc home = null;
798         InputStream JavaDoc in = null;
799         try {
800             in = getClass().getResourceAsStream("/messenger_init.xml");
801             if (in != null) {
802                 SAXReader reader = new SAXReader();
803                 Document doc = reader.read(in);
804                 home = doc.getRootElement().getText();
805             }
806         }
807         catch (Exception JavaDoc e) {
808             Log.error("Error loading messenger_init.xml to find home.", e);
809         }
810         finally {
811             try { if (in != null) { in.close(); } }
812             catch (Exception JavaDoc e) { }
813         }
814         if (home != null) {
815             home = home.trim();
816             // Remove trailing slashes.
817
while (home.endsWith("/") || home.endsWith("\\")) {
818                 home = home.substring(0, home.length() - 1);
819             }
820         }
821         if ("".equals(home)) {
822             home = null;
823         }
824         return home;
825     }
826 }
Popular Tags