openamf/src/java/org/openamf AdvancedGateway.java,1.6,1.7 ServiceRequest.java,1.3,1.4

[email protected]
Newsgroups gmane.comp.java.openamf.cvs
Message-ID <[email protected]>
Update of /cvsroot/openamf/openamf/src/java/org/openamf
In directory sc8-pr-cvs1:/tmp/cvs-serv11447/src/java/org/openamf

Modified Files:
	AdvancedGateway.java ServiceRequest.java 
Log Message:
added the ability to have the AdvancedGateway translate the service result 
based on an the xml config


The first Translator is BeanListToRecordSet

Index: AdvancedGateway.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/AdvancedGateway.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** AdvancedGateway.java	30 Apr 2003 01:02:07 -0000	1.6
--- AdvancedGateway.java	30 Apr 2003 19:40:29 -0000	1.7
***************
*** 2,5 ****
--- 2,6 ----
  
  import java.io.IOException;
+ import java.util.ArrayList;
  import java.util.Iterator;
  
***************
*** 10,16 ****
--- 11,22 ----
  import org.openamf.config.Config;
  import org.openamf.config.ServiceConfig;
+ import org.openamf.config.ServiceOperationConfig;
+ import org.openamf.config.ServiceOperationParameterConfig;
  import org.openamf.config.StateBeanConfig;
+ import org.openamf.config.TranslatorConfig;
  import org.openamf.invoker.ServiceInvocationException;
  import org.openamf.invoker.ServiceInvoker;
+ import org.openamf.translator.TranslationException;
+ import org.openamf.translator.Translator;
  import org.xml.sax.SAXException;
  
***************
*** 48,53 ****
  				ServiceInvoker.load(
  					serviceConfig.getServiceInvokerConfig().getClassName(),
! 					request, getServletContext());
! 					
  		} catch (Exception e) {
  			throw new ServiceInvocationException(requestBody, e);
--- 54,60 ----
  				ServiceInvoker.load(
  					serviceConfig.getServiceInvokerConfig().getClassName(),
! 					request,
! 					getServletContext());
! 
  		} catch (Exception e) {
  			throw new ServiceInvocationException(requestBody, e);
***************
*** 57,60 ****
--- 64,120 ----
  	}
  
+ 	protected Object postInvokeService(
+ 		HttpServletRequest httpServletRequest,
+ 		ServiceInvoker serviceInvoker,
+ 		Object serviceResult)
+ 		throws ServiceInvocationException {
+ 
+ 		ServiceOperationConfig operationConfig = null;
+ 		try {
+ 			operationConfig = getOperationConfig(serviceInvoker.getRequest());
+ 
+ 			if (operationConfig != null
+ 				&& operationConfig.getResultTranslatorConfig() != null) {
+ 					
+ 				serviceResult =
+ 					translateResult(
+ 						serviceResult,
+ 						operationConfig.getResultTranslatorConfig());
+ 			}
+ 
+ 		} catch (Exception e) {
+ 			throw new ServiceInvocationException(
+ 				serviceInvoker.getRequest(),
+ 				e);
+ 		}
+ 
+ 		return super.postInvokeService(
+ 			httpServletRequest,
+ 			serviceInvoker,
+ 			serviceResult);
+ 	}
+ 
+ 	private Object translateResult(
+ 		Object serviceResult,
+ 		TranslatorConfig translatorConfig)
+ 		throws
+ 			ClassNotFoundException,
+ 			InstantiationException,
+ 			IllegalAccessException,
+ 			TranslationException {
+ 
+ 		log.debug(
+ 			"loading translator class: " + translatorConfig.getClassName());
+ 		Class translatorClass =
+ 			Thread.currentThread().getContextClassLoader().loadClass(
+ 				translatorConfig.getClassName());
+ 
+ 		Translator translator = (Translator) translatorClass.newInstance();
+ 
+ 		log.debug("calling translate");
+ 		return translator.translate(serviceResult, translatorConfig);
+ 
+ 	}
+ 
  	private ServiceConfig getServiceConfig(AMFBody requestBody)
  		throws IOException, SAXException {
***************
*** 66,69 ****
--- 126,198 ----
  
  		return serviceConfig;
+ 	}
+ 
+ 	private ServiceOperationConfig getOperationConfig(ServiceRequest request)
+ 		throws IOException, SAXException, ClassNotFoundException {
+ 
+ 		log.debug("getting OperationConfig");
+ 		log.debug("looking for " + request.getServiceMethodName());
+ 
+ 		ServiceOperationConfig operationConfig = null;
+ 
+ 		ArrayList parameters = request.getParameters();
+ 
+ 		Iterator socs =
+ 			getServiceConfig(request.getRequestBody()).getOperationConfigs();
+ 
+ 		socsLabel : while (socs.hasNext()) {
+ 
+ 			ServiceOperationConfig soc = (ServiceOperationConfig) socs.next();
+ 			log.debug("soc name: " + soc.getName());
+ 			if (soc.getName().equals(request.getServiceMethodName())) {
+ 				log.debug("name matches, now to compare params");
+ 				//compare params
+ 				int paramIndex = -1;
+ 				Iterator sopcs = soc.getParameterConfigs();
+ 
+ 				sopcsLabel : while (sopcs.hasNext()) {
+ 					paramIndex++;
+ 					ServiceOperationParameterConfig sopc =
+ 						(ServiceOperationParameterConfig) sopcs.next();
+ 					if (sopc.getType().equals("*")) {
+ 						log.debug("matches the rest");
+ 						operationConfig = soc;
+ 						break socsLabel;
+ 					} else if (
+ 						sopc.getType().equals("?")
+ 							|| typesMatch(parameters, paramIndex, sopc)) {
+ 						if (paramIndex == parameters.size()) {
+ 							log.debug("all parameters match");
+ 							operationConfig = soc;
+ 							break socsLabel;
+ 						} else {
+ 							log.debug(
+ 								"this parameter matches, contine checking the rest");
+ 							continue sopcsLabel;
+ 						}
+ 					}
+ 				}
+ 			}
+ 		}
+ 
+ 		return operationConfig;
+ 	}
+ 
+ 	private boolean typesMatch(
+ 		ArrayList parameters,
+ 		int paramIndex,
+ 		ServiceOperationParameterConfig sopc)
+ 		throws ClassNotFoundException {
+ 
+ 		boolean typesMatch = false;
+ 
+ 		Class typeClass =
+ 			Thread.currentThread().getContextClassLoader().loadClass(
+ 				sopc.getType());
+ 		if (typeClass.isInstance(parameters.get(paramIndex))) {
+ 			typesMatch = true;
+ 		}
+ 
+ 		return typesMatch;
  	}
  

Index: ServiceRequest.java
===================================================================
RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/ServiceRequest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ServiceRequest.java	18 Apr 2003 23:53:12 -0000	1.3
--- ServiceRequest.java	30 Apr 2003 19:40:30 -0000	1.4
***************
*** 7,14 ****
  public class ServiceRequest {
  
! 	protected String target;
! 	protected String serviceName;
! 	protected String serviceMethodName;
! 	protected ArrayList parameters;
  
  	public ServiceRequest(AMFBody requestBody) {
--- 7,15 ----
  public class ServiceRequest {
  
! 	private AMFBody requestBody;
! 	private String target;
! 	private String serviceName;
! 	private String serviceMethodName;
! 	private ArrayList parameters;
  
  	public ServiceRequest(AMFBody requestBody) {
***************
*** 17,20 ****
--- 18,22 ----
  
  	public ServiceRequest(AMFBody requestBody, ServiceConfig serviceConfig) {
+ 		this.requestBody = requestBody;
  		this.target = requestBody.getTarget();
  		if (serviceConfig == null) {
***************
*** 29,32 ****
--- 31,42 ----
  			this.parameters = (ArrayList) value;
  		}
+ 	}
+ 
+ 	public AMFBody getRequestBody() {
+ 		return requestBody;
+ 	}
+ 
+ 	public void setRequestBody(AMFBody body) {
+ 		requestBody = body;
  	}
  




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.