Reputation: 1605
I ma trying to migrate some old code from WebSphere to Tomcat. The older code used Spring 3.2, now I upgraded the JARs to 5.2.2. But somehow object values just do not persist.
My controller class is:
@Controller
@Scope("session")
public class OperationController {
private GUIDataObject guiDO = null;
/**
* Constructor
*/
public OperationController() {
}
@RequestMapping(value="/readDataSource")
@ResponseBody
public String readDataSource() {
try {
String[] sources = guiDO.getDataSources();
.
.
.
Code to work on Array sources
.
.
.
return "ok";
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
/**
* Set the data sources in the Data Storage Area - these are passed as a "parameter" map
* in the request.
* @param webRequest : WebRequest which parameter map can be pulled from
* @return "ok"
*/
@RequestMapping(value="/setDataSources")
@ResponseBody
public String setDataSources(WebRequest webRequest) {
guiDO.setDatasources(webRequest.getParameterMap());
return "ok";
}
.
.
.
Lots of other code.
.
.
.
}
and the values are stored in the object:
public class GUIDataObject {
private String service;
private String uniqueProcessId;
private String userId;
private String vendor;
// record the data sources to read from
private Map<String, String[]> dataSources = null;
public GUIDataObject(String service, String uniqueProcessId, String userId, String vendor) {
super();
this.service = service;
this.uniqueProcessId = uniqueProcessId;
this.userId = userId;
this.vendor = vendor;
}
public void setDatasources(Map<String, String[]> datasources) {
this.dataSources = datasources;
}
public String[] getDataSources() throws Exception {
if (this.dataSources == null) {
throw new Exception("No datasources have been set from the GUI");
}
if (!this.dataSources.containsKey("source")) {
throw new Exception("No datasources have been set from the GUI");
}
return this.dataSources.get("source");
}
.
.
.
Lots of methods.
.
.
.
}
Now my problem is the dataSources
Map is getting set fine. But when fetching the values they return empty. It errors out in the second if-block, so I can say at least its not null. There are other Maps/Strings in the object as well, but I cant really tell if they are being properly set or not, since, this is the first method that is being hit and it errors out after that. I can see that the values that are initialized in the constructor are being retained just fine. So cant really where it is going wrong.
The same code worked fine on WebSphere and Spring 3.2. Now I am not sure if there are any new configurations that I need to in order to get this to work. Since 3.2 is very very old. Any help on this would be appreciated.
Upvotes: 0
Views: 95
Reputation: 1605
The problem was the way webRequest.getParameterMap()
works in WebSphere and Tomcat. In WebSphere it returns a concrete HashTable
. But in Tomcat, it returns a org.apache.catalina.util.ParameterMap
which is a subclass of HashMap
. And somehow they just don't mix. Even casting throws a ClassCastException
.
I got it to work by changing the dataSources
to a HashMap
.
private HashMap<String, String[]> dataSources = null;
and the set method to:
public void setDatasources(Map<String, String[]> datasources) {
if (this.dataSources == null) {
this.dataSources = new HashMap<String, String[]>();
this.dataSources.putAll(datasources);
} else {
this.dataSources.putAll(datasources);
}
Probably I could have left the dataSources
as a Map
and it still would have worked. But I didn't try it out.
Upvotes: 1