Hi,

Thank you for contributing the fixes back to Tuscany. A bit more help from you will be greatly appreciated:

1) Open a JIRA at https://issues.apache.org/jira/browse/TUSCANY
2) Create a svn patch (you can run "svn diff > xyz.patch" command under sca/ folder)
3) Attach the patch the JIRA and grant Apache license to it

Thanks,
Raymond

From: ZhiYong Mao
Sent: Tuesday, August 26, 2008 9:10 PM
To: user@tuscany.apache.org
Subject: I have tried to inject HTTP request or session into service Java bean


***********************
Warning: Your file, tuscany-1.2.1-fix.zip, contains more than 32 files after decompression and cannot be scanned. Warning: Your file, tuscany-1.2.1-fix.zip, contains more than 32 files after decompression and cannot be scanned.
***********************



Hi

I have tried to inject HTTP request or session into service Java bean and it works fine now, here are the latest binary package and source code changed by us: (See attached file: tuscany-1.2.1-fix.zip)(See attached file: tuscany-1.2.1-fix-src.zip)
The details changed are as follows:
1.===============org.apache.tuscany.sca.databinding.json.JSON2JavaBean.java====================
(1)Line 55:
SerializerState state = new SerializerState();
return serializer.unmarshall(state, context.getTargetDataType().getPhysical(), source);
=======>:
/**************modify by Jacky Mao******************************
* Inject HttpServletRequest or HttpSession into parameters in methods
* So it is not necessary to transform these 2 parameter types
*/
if(source instanceof javax.servlet.http.HttpServletRequest ||
source instanceof javax.servlet.http.HttpSession){
return source;
}else{
SerializerState state = new SerializerState();
return serializer.unmarshall(state, context.getTargetDataType().getPhysical(), source);
}
2.========org.apache.tuscany.sca.binding.jsonrpc.JSONRPCServiceServlet.java==========
(1)Line 181:
JSONRPCBridge jsonrpcBridge = new JSONRPCBridge();
jsonrpcBridge.registerObject("Service", serviceInstance, serviceInterface);
session.setAttribute("JSONRPCBridge", jsonrpcBridge);
========>(I think it is better like this):
JSONRPCBridge jsonrpcBridge = (JSONRPCBridge) session.getAttribute("JSONRPCBridge");
if(jsonrpcBridge==null){
jsonrpcBridge=new JSONRPCBridge();
session.setAttribute("JSONRPCBridge", jsonrpcBridge);
}
jsonrpcBridge.registerObject("Service", serviceInstance, serviceInterface);
(2)Line 217:
// Extract the arguments
JSONArray array = jsonReq.getJSONArray("params");
args = new Object[array.length()];
for (int i = 0; i < args.length; i++) {
args[i] = array.get(i);
}
=======>(add HttpServletRequest or HttpSession into args array at appropriate position)
/*****************modify by Jacky Mao********************************
* Inject HttpServletRequest or HttpSession into parameters in methods
*******************************************************************/
//1.find current method
Method methods[]=serviceInterface.getMethods();
Method currentMethod=null;
for(int i=0;i<methods.length;i++){
if(method.equals("Service."+methods[i].getName())){
currentMethod=methods[i];
break;
}
}
//2.find index of the parameter which is HttpServletRequest or HttpSession
int paramIndex=-1;
Object paramValue=null;
if(currentMethod!=null){
Class<?>[] clazz=currentMethod.getParameterTypes();
LocalArgResolver lar=new LocalArgResolver();
for(int i=0;i<clazz.length;i++){
Object obj=lar.getArgValue(clazz[i], request);
if(obj!=null){
paramIndex=i;
paramValue=obj;
break;//just support one(HttpServletRequest or HttpSession)
}
}
}
//3.Extract the arguments and add HttpServletRequest or HttpSession
// into arguments if it is necessary.
JSONArray array = jsonReq.getJSONArray("params");
if(paramIndex>=0){
args = new Object[array.length()+1];
}else{
args = new Object[array.length()];
}
boolean added=false;
for (int i = 0; i < args.length; i++) {
if(i==paramIndex){
args[i]=paramValue;
added=true;
}else{
if(added){
args[i] = array.get(i-1);
}else{
args[i] = array.get(i);
}
}
}
/*****************************************************************************/
(3)Line 244
jsonResponse.put("error", e.getCause());
=======>(It is for fixing a small bug:"error parsing result")
/*****modify by Jacky Mao************************
* add json error object into error so that
* the client javascript can parse them correctly.
*/
Throwable t=e.getCause();
JSONObject jsonError = new JSONObject();
jsonError.put("code", response.SC_INTERNAL_SERVER_ERROR);
jsonError.put("msg", t.getMessage());
jsonError.put("trace", t.getClass().getName());
jsonResponse.put("error", jsonError);
/************************************************/
3.========org.apache.tuscany.sca.binding.jsonrpc.LocalArgResolver.java==========
It is a new file created by me.

I have added some comments into source code, you can aslo see the difference by comparing source code.

Best regards!

Jacky Mao , Software Engineer
IBM GPSG Wuhan Perform Center
Tel : 0086-027-87406266-1212
E-Mail: [EMAIL PROTECTED]
Willis C White/Poughkeepsie/IBM



Reply via email to