uk.ac.soton.itinnovation.grid.service.utils
Class ConfigUtils

java.lang.Object
  extended by uk.ac.soton.itinnovation.grid.service.utils.ConfigUtils

public class ConfigUtils
extends Object

Utility class for JSP configuration pages.


Constructor Summary
ConfigUtils(String[] propFiles)
          Read a set of property files.
 
Method Summary
static void copyFile(File src, File dest)
          Deprecated. use FileUtils.copyFile(File, File)
static String escapeAttribute(String attr)
          Escape HTML characters & ' and "
static String escapeHTML(Object object)
          Escape HTML characters & and <.
static String escapeTargetAttr(String value)
          Escape and encode a value for use in an HTML (href) attribute.
static String formatException(Throwable ex)
          Escape HTML characters in exception message.
static String get(Map map, String name)
          Get a property from a form submission's parameters.
 String get(String name)
          Return the value of the property read from the files given to the constructor.
 File getConfigDir()
          Get the configured configuration directory.
static File getHibernateDatabaseDir()
          Get the configured hibernate database directory or null if it has not been configured.
static boolean getNeedRestart()
           
static String getRealPath()
           
static String getRestartHint()
          Convenience wrapper for getRestartHint(boolean) with mainLink = true.
static String getRestartHint(boolean mainLink)
          Tell the user how to restart the container.
static String getServerInfo()
           
static String getServerName()
           
static String getTomcatVersion()
          Get the version number of tomcat as a string.
static String getWebAppName(HttpServletRequest request)
          Returns the name of the webapp.
 String input(String name, String value)
          Convenience wrapper for input(String, String, int) when the size is unspecified.
 String input(String name, String value, int size)
          Create a text input form field.
static boolean isJBoss()
           
static boolean isTomcat()
           
static void setJBoss()
           
static void setNeedRestart()
          Set a flag that can only be cleared by restarting the webapp.
static void setRealPath(String rp)
          Set the realPath.
static void setServerInfo(String si)
          Set the serverInfo String, e.g.
static void setServerName(String sn)
           
static void setTomcat()
           
static String stackTrace(Throwable ex)
          Format a stack trace for display in a web-admin page.
static void writeConfig(File file, String contents)
          Create a file with the given contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConfigUtils

public ConfigUtils(String[] propFiles)
            throws Exception
Read a set of property files. The values are then used as defaults when using input(java.lang.String, java.lang.String, int)

Throws:
Exception
Method Detail

setNeedRestart

public static void setNeedRestart()
Set a flag that can only be cleared by restarting the webapp.


getNeedRestart

public static boolean getNeedRestart()

get

public String get(String name)
           throws Stop
Return the value of the property read from the files given to the constructor.

Throws:
Stop

get

public static String get(Map map,
                         String name)
                  throws Stop
Get a property from a form submission's parameters.

Parameters:
map - a mapping of names to arrays of values
name - the name of the parameter
Throws:
Stop - if the parameter isn't in the map

writeConfig

public static void writeConfig(File file,
                               String contents)
                        throws Stop
Create a file with the given contents.

Parameters:
file - the name of the file to create
contents - the contents to write. Any backslashes are escaped.
Throws:
Stop - if the directory containing the file does not exist
Stop - if the file cannot be written

input

public String input(String name,
                    String value,
                    int size)
Create a text input form field.

Parameters:
name - the form control's name
value - a default value, if none was read by ConfigUtils(String[]).
size - the default field size, or 0 to leave unset
Returns:
an HTML formatted string

input

public String input(String name,
                    String value)
Convenience wrapper for input(String, String, int) when the size is unspecified.


getConfigDir

public File getConfigDir()
Get the configured configuration directory.

Returns:
the directory that should be used to store configuration files

getRestartHint

public static String getRestartHint()
Convenience wrapper for getRestartHint(boolean) with mainLink = true.


getRestartHint

public static String getRestartHint(boolean mainLink)
Tell the user how to restart the container. This tries to detect the user's system and give them the correct hint for it.

Parameters:
mainLink - whether to include a 'click here to continue' link to main.jsp.
Returns:
an HTML formatted message

copyFile

public static void copyFile(File src,
                            File dest)
                     throws Exception
Deprecated. use FileUtils.copyFile(File, File)

Copy a file.

Throws:
Exception

getWebAppName

public static String getWebAppName(HttpServletRequest request)
Returns the name of the webapp.


formatException

public static String formatException(Throwable ex)
Escape HTML characters in exception message.


escapeTargetAttr

public static String escapeTargetAttr(String value)
Escape and encode a value for use in an HTML (href) attribute. The value is first encoded (so that " " becomes "+", etc), then the result is escaped for use in an attribute (using escapeAttribute(java.lang.String)).

Note: By default, tomcat is configured to interpret query parameters as being encoding using iso-8859-1, but it can be configured to use other character sets by putting a "URIEncoding" attribute on the <Connector> in tomcat's configuration. Ideally, we would use this encoding but it's difficult to extract it, so we always use UTF-8 for now and you have to update your configuration manually.

Parameters:
value - the value to encode
Returns:
escaped and encoded string

escapeAttribute

public static String escapeAttribute(String attr)
Escape HTML characters & ' and "

Parameters:
attr - string to convert
Returns:
escaped string

escapeHTML

public static String escapeHTML(Object object)
Escape HTML characters & and <.

Parameters:
object - to convert to string, or null
Returns:
A string without any < or & characters.

stackTrace

public static String stackTrace(Throwable ex)
Format a stack trace for display in a web-admin page. Usage:
 try {
   // body of page
 } catch (Throwable ex) {
   out.write(ConfigUtils.stackTrace(ex));
 }
 

Parameters:
ex - a throwable. A full stack trace is shown, unless ex is a Stop, in which case only the cause (if any) is shown. Any hint from the Stop is also displayed.
Returns:
an HTML-safe string to insert into the web-page
See Also:
Stop

setRealPath

public static void setRealPath(String rp)
Set the realPath. This is the path of the root of the webapp.


getRealPath

public static String getRealPath()

setServerInfo

public static void setServerInfo(String si)
Set the serverInfo String, e.g. "Apache Tomcat/5.0.30". WARNING: The JBoss serverInfo is also an "Apache Tomcat" string.


getServerInfo

public static String getServerInfo()

getTomcatVersion

public static String getTomcatVersion()
Get the version number of tomcat as a string. For example, this could be 5.0.28 or 5.5.23. Note that null is returned if the servlet container does not appear to be any version of tomcat.

Returns:
the tomcat version number as a string or null if the servlet container does not appear to be tomcat, as determined with isTomcat()
See Also:
isTomcat()

setServerName

public static void setServerName(String sn)

getServerName

public static String getServerName()

setTomcat

public static void setTomcat()

isTomcat

public static boolean isTomcat()

setJBoss

public static void setJBoss()

isJBoss

public static boolean isJBoss()

getHibernateDatabaseDir

public static File getHibernateDatabaseDir()
Get the configured hibernate database directory or null if it has not been configured.



Copyright © 2001-2008 University of Southampton IT Innovation Centre. All Rights Reserved.