Commit 5074170f authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Removed logic to search for home folder in JiveGlobals (settings are now...

Removed logic to search for home folder in JiveGlobals (settings are now injected from outside). JM-371

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2706 b35dd754-fafc-0310-a699-88a17e54d16e
parent 357b0d00
...@@ -177,9 +177,6 @@ public class XMPPServer { ...@@ -177,9 +177,6 @@ public class XMPPServer {
} }
private void initialize() throws FileNotFoundException { private void initialize() throws FileNotFoundException {
// Set the name of the config file
JiveGlobals.setConfigName("jive-messenger.xml");
locateMessenger(); locateMessenger();
name = JiveGlobals.getProperty("xmpp.domain"); name = JiveGlobals.getProperty("xmpp.domain");
...@@ -260,7 +257,7 @@ public class XMPPServer { ...@@ -260,7 +257,7 @@ public class XMPPServer {
// Log that the server has been started // Log that the server has been started
List params = new ArrayList(); List<String> params = new ArrayList<String>();
params.add(version.getVersionString()); params.add(version.getVersionString());
params.add(JiveGlobals.formatDateTime(new Date())); params.add(JiveGlobals.formatDateTime(new Date()));
String startupBanner = LocaleUtils.getLocalizedString("startup.name", params); String startupBanner = LocaleUtils.getLocalizedString("startup.name", params);
...@@ -586,7 +583,10 @@ public class XMPPServer { ...@@ -586,7 +583,10 @@ public class XMPPServer {
throw new FileNotFoundException(); throw new FileNotFoundException();
} }
else { else {
JiveGlobals.home = messengerHome.toString(); // Set the home directory for the config file
JiveGlobals.setHomeDirectory(messengerHome.toString());
// Set the name of the config file
JiveGlobals.setConfigName(jiveConfigName);
} }
} }
......
...@@ -11,13 +11,8 @@ ...@@ -11,13 +11,8 @@
package org.jivesoftware.util; package org.jivesoftware.util;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import javax.naming.InitialContext;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.*; import java.util.*;
...@@ -47,13 +42,13 @@ import java.util.*; ...@@ -47,13 +42,13 @@ import java.util.*;
*/ */
public class JiveGlobals { public class JiveGlobals {
private static String JIVE_CONFIG_FILENAME = null; private static String JIVE_CONFIG_FILENAME = "conf" + File.separator + "jive-messenger.xml";
/** /**
* Location of the jiveHome directory. All configuration files should be * Location of the jiveHome directory. All configuration files should be
* located here. * located here.
*/ */
public static String home = null; private static String home = null;
public static boolean failedLoading = false; public static boolean failedLoading = false;
...@@ -237,12 +232,35 @@ public class JiveGlobals { ...@@ -237,12 +232,35 @@ public class JiveGlobals {
* @return the location of the home dir. * @return the location of the home dir.
*/ */
public static String getHomeDirectory() { public static String getHomeDirectory() {
if (home == null) { if (xmlProperties == null) {
loadSetupProperties(); loadSetupProperties();
} }
return home; return home;
} }
/**
* Sets the location of the <code>home</code> directory. The directory must exist and the
* user running the application must have read and write permissions over the specified
* directory.
*
* @param pathname the location of the home dir.
*/
public static void setHomeDirectory(String pathname) {
File mh = new File(pathname);
// Do a permission check on the new home directory
if (!mh.exists()) {
Log.error("Error - the specified home directory does not exist (" + pathname + ")");
}
else if (!mh.canRead() || !mh.canWrite()) {
Log.error("Error - the user running this application can not read " +
"and write to the specified home directory (" + pathname + "). " +
"Please grant the executing user read and write permissions.");
}
else {
home = pathname;
}
}
/** /**
* Returns a local property. Local properties are stored in the file defined in * Returns a local property. Local properties are stored in the file defined in
* <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory. * <tt>JIVE_CONFIG_FILENAME</tt> that exists in the <tt>home</tt> directory.
...@@ -676,9 +694,6 @@ public class JiveGlobals { ...@@ -676,9 +694,6 @@ public class JiveGlobals {
* @return the name of the config file. * @return the name of the config file.
*/ */
static String getConfigName() { static String getConfigName() {
if (JIVE_CONFIG_FILENAME == null) {
JIVE_CONFIG_FILENAME = "jive-messenger.xml";
};
return JIVE_CONFIG_FILENAME; return JIVE_CONFIG_FILENAME;
} }
...@@ -696,86 +711,20 @@ public class JiveGlobals { ...@@ -696,86 +711,20 @@ public class JiveGlobals {
* that we give outside classes a chance to set <tt>home</tt>. * that we give outside classes a chance to set <tt>home</tt>.
*/ */
private synchronized static void loadSetupProperties() { private synchronized static void loadSetupProperties() {
if (failedLoading) {
return;
}
if (xmlProperties == null) { if (xmlProperties == null) {
// If jiveHome is still null, no outside process has set it and // If home is null then log that the application will not work correctly
// we have to attempt to load the value from jive_init.xml, if (home == null && !failedLoading) {
// which must be in the classpath.
if (home == null) {
home = new InitPropLoader().getHome();
}
// If that failed, try loading it from JNDI
if (home == null) {
try {
InitialContext context = new InitialContext();
home = (String)context.lookup("java:comp/env/home");
}
catch (Exception e) { }
}
// Finally, try to load it jiveHome as a system property.
if (home == null) {
home = System.getProperty("home");
}
if(home == null){
try {
home = new File("..").getCanonicalPath();
if(!new File(home, "conf/" + getConfigName()).exists()){
home = null;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
if(home == null){
try {
home = new File("").getCanonicalPath();
if(!new File(home, "conf/" + getConfigName()).exists()){
home = null;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
// If still null, finding home failed.
if (home == null) {
failedLoading = true; failedLoading = true;
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
msg.append("Critical Error! The home directory could not be loaded, \n"); msg.append("Critical Error! The home directory has not been configured, \n");
msg.append("which will prevent the application from working correctly.\n\n"); msg.append("which will prevent the application from working correctly.\n\n");
msg.append("You must set home in one of four ways:\n");
msg.append(" 1) Add a messenger_init.xml file to your classpath, which points \n ");
msg.append(" to home.\n");
msg.append(" 3) Set the JNDI value \"java:comp/env/home\" with a String \n");
msg.append(" that points to your home directory. \n");
msg.append(" 4) Set the Java system property \"home\".\n\n");
msg.append("Further instructions for setting home can be found in the \n");
msg.append("installation documentation.");
System.err.println(msg.toString()); System.err.println(msg.toString());
return; return;
} }
// Create a manager with the full path to the xml config file. // Create a manager with the full path to the xml config file.
try {
// Do a permission check on the jiveHome directory:
File mh = new File(home);
if (!mh.exists()) {
Log.error("Error - the specified home directory does not exist (" + home + ")");
}
else { else {
if (!mh.canRead() || !mh.canWrite()) { try {
Log.error("Error - the user running this application can not read " + xmlProperties = new XMLProperties(home + File.separator + getConfigName());
"and write to the specified home directory (" + home + "). " +
"Please grant the executing user read and write permissions.");
}
}
xmlProperties = new XMLProperties(home + File.separator + "conf" +
File.separator + getConfigName());
} }
catch (IOException ioe) { catch (IOException ioe) {
Log.error(ioe); Log.error(ioe);
...@@ -784,43 +733,5 @@ public class JiveGlobals { ...@@ -784,43 +733,5 @@ public class JiveGlobals {
} }
} }
} }
}
/**
* A very small class to load the file defined in JiveGlobals.JIVE_CONFIG_FILENAME. The class is
* needed since loading files from the classpath in a static context often
* fails.
*/
class InitPropLoader {
public String getHome() {
String home = null;
InputStream in = null;
try {
in = getClass().getResourceAsStream("/messenger_init.xml");
if (in != null) {
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
home = doc.getRootElement().getText();
}
}
catch (Exception e) {
Log.error("Error loading messenger_init.xml to find home.", e);
}
finally {
try { if (in != null) { in.close(); } }
catch (Exception e) { }
}
if (home != null) {
home = home.trim();
// Remove trailing slashes.
while (home.endsWith("/") || home.endsWith("\\")) {
home = home.substring(0, home.length() - 1);
}
}
if ("".equals(home)) {
home = null;
}
return home;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment