E. UWS Tools-Box
The class UWSToolBox gathers several usefull functions. All of them are static, so you don't need to create an instance of this class. Some of them have already been used in the previous sections of this tutorial:
-
printURL(UWSUrl) and
printURL(UWSUrl, OutputStream):
They display the content of the given UWSUrl in the given output stream.
-
publishErrorSummary(AbstractJob, String, ErrorType) and
publishErrorSummary(AbstractJob, Exception, ErrorType, String, String, String)
They set the phase to ERROR and set an error summary of the given job in function of the given parameters.
![](images/descriptionToolsBox.png)
Error tools
The both error publication functions use actually writeErrorFile(Exception error, String dirPath, String fileName, boolean overwrite) which only writes the stack trace of the given exception in the specified file. The second writeErrorFile function calls this one but with true as last parameter which means that if the specified file exists, it will be overwritten.
URL tools
In addition to the printURL methods, this class provides the following functions to manipulate more easily URLs:
-
getServerResource(String, HttpServletRequest):
It returns the full URL to access to the server resource (a directory, a file, an image, a html page, ...) which corresponds to the given URI.
-
getParamsMap(HttpServletRequest):
It extracts the parameters map from the given HTTP request. The keys (parameter name) and values (parameter value) of the returned map are of type String. (note: HttpServletRequest.getParameterMap() returns a map whose the values are an array of String. Hence this tool function !)
-
getParamsMap(HttpServletRequest, String):
It does the same thing than getParamsMap(HttpServletRequest) and add the given owner ID, if it is not already into the parameters map.
-
getQueryPart(Map):
It generates the query part of an URL and returns it. In a URL, this part starts with ?. The parameters follows the syntax name=value and are separated by a &.
-
getParameters(String):
It does the contrary of getQueryPart(Map). The given query part is parsed so that parameters can be returned in a map (keys are parameter names whereas values are parameter values).
Saving & Restoring a UWS
At each stop (or even during a simple restart) of Tomcat, the value of all class attributes are lost. Consequently all pending/running/stopped jobs are destroyed. To avoid this effect you can save the UWS in the destroy() function, by using saveUWS(AbstractUWs, File, boolean). Below is an example:
public void destroy() { // Save the current state of this UWS: UWSToolBox.saveUWS(uws, restoreFile, true); super.destroy(); }
To restore a UWS you just have to call restoreUWS(File, boolean) in the init(ServletConfig) function:
public void init(ServletConfig config) throws ServletException { // Restore the last saved UWS: restoreFile = new File(config.getServletContext().getRealPath("/"), "uwsRestore"); uws = (BasicUWS<JobChrono>) UWSToolBox.restoreUWS(restoreFile, true); super.init(config); }
Notes:
- All the UWS objects of the library are serializable (they implement the interface java.io.Serializable). The functions UWSToolBox.saveUWS and UWSToolBox.restoreUWS use this particularity to save the UWS in the specified file and then to restore it.
- Another solution is to use the Tomcat Persistent Manager which uses also the java Serialization mechanism. However according to the Tomcat website this functionnality "should be considered experimental" !