View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   package it.imolinfo.jbi4cics.webservices.runtime;
9   
10  import it.imolinfo.jbi4cics.Logger;
11  import it.imolinfo.jbi4cics.LoggerFactory;
12  import it.imolinfo.jbi4cics.connection.ConnectionManager;
13  import it.imolinfo.jbi4cics.connection.ConnectionManagerFactory;
14  import it.imolinfo.jbi4cics.exception.ConnectionException;
15  import it.imolinfo.jbi4cics.exception.FormatException;
16  import it.imolinfo.jbi4cics.jbi.Messages;
17  import it.imolinfo.jbi4cics.messageformat.MessageFormatter;
18  import it.imolinfo.jbi4cics.messageformat.MessageFormatterFactory;
19  import it.imolinfo.jbi4cics.service.ServiceContext;
20  import it.imolinfo.jbi4cics.webservices.descriptor.ServiceDescriptor;
21  
22  import java.lang.reflect.Method;
23  import java.util.Arrays;
24  
25  import org.codehaus.xfire.MessageContext;
26  import org.codehaus.xfire.fault.XFireFault;
27  import org.codehaus.xfire.service.invoker.Invoker;
28  
29  /**
30   * This class handle invokation of web services and forward in to a legacy service invokation. 
31   * @author raffaele
32   *
33   */
34  
35  public class ServiceInvoker implements Invoker {
36    
37    /**
38     * The logger for this class and its instances.
39     */
40    private static final Logger LOG
41            = LoggerFactory.getLogger(ServiceInvoker.class);
42    
43    /**
44     * The responsible to translate localized messages.
45     */
46    private static final Messages MESSAGES
47            = Messages.getMessages(ServiceInvoker.class);
48      
49    private ServiceDescriptor serviceDescriptor;
50  
51    public ServiceInvoker(ServiceDescriptor serviceDescriptor) {
52      this.serviceDescriptor=serviceDescriptor;
53    }
54  
55    public Object invoke(Method method, Object[] args, MessageContext messageContext) throws XFireFault {
56      //we need to construct a ServiceContext and execute it
57      LOG.debug("method: "+method+" with parameters: "+Arrays.toString(args)+" is being invoked on service: "+serviceDescriptor);
58      ServiceContext serviceContext;
59      serviceContext = new ServiceContext();
60      
61      serviceContext.setInputMappingDescriptor(serviceDescriptor.getInputMappingDescriptor());
62      serviceContext.setOutputMappingDescriptor(serviceDescriptor.getOutputMappingDescriptor());
63      
64      serviceContext.setAccount(serviceDescriptor.getAccount());
65      LOG.debug("Setting ServiceLocation: " + serviceDescriptor.getServiceLocation());
66      serviceContext.setServiceLocation(serviceDescriptor.getServiceLocation());
67      serviceContext.setInteractionDescription(serviceDescriptor.getInteractionDescription());
68      
69      serviceContext.setInputBean(args[0]);
70      
71      MessageFormatter inputMessageFormatter=MessageFormatterFactory.createMessageFormatter(serviceContext,true);
72      try {
73        // eseguo la formattazione in andata
74        long millis1=System.currentTimeMillis();
75        inputMessageFormatter.mapInputBeanToInputMessage(serviceContext);
76        long millis2=System.currentTimeMillis();
77        LOG.debug("input conversion time="+(millis2-millis1)+" millis");
78        LOG.debug("input message: ["+serviceContext.getInputMessage()+"]");
79      }
80      catch (FormatException e){
81        LOG.error("CIC002301_Formatting_input_bean_error", new Object[] {
82            serviceContext.getInputBean(), 
83            serviceContext.getInputMappingDescriptor(), 
84            inputMessageFormatter}, e);
85        throw new XFireFault(MESSAGES.getString(
86            "CIC002301_Formatting_input_bean_error", 
87            new Object[] {serviceContext.getInputBean(), 
88            serviceContext.getInputMappingDescriptor(), 
89            inputMessageFormatter
90            }, e), e, XFireFault.RECEIVER);
91      }
92      
93      ConnectionManager connectionManager=ConnectionManagerFactory.createConnectionManager(serviceContext);
94      
95      try {
96        // eseguo la chiamata
97        long millis1=System.currentTimeMillis();
98        connectionManager.handleCall(serviceContext);
99        long millis2=System.currentTimeMillis();
100       LOG.debug("connection execution time="+(millis2-millis1)+" millis");
101       LOG.debug("output message: ["+serviceContext.getOutputMessage()+"]");
102     } catch (ConnectionException e) {
103       LOG.error("CIC002302_Error_executing_call", new Object[] {
104           serviceContext.getInputMessage(), 
105           serviceContext.getInteractionDescription(), connectionManager}, e);
106       throw new XFireFault(MESSAGES.getString("CIC002302_Error_executing_call", 
107               new Object[] {serviceContext.getInputMessage(), 
108               serviceContext.getInteractionDescription(), 
109               connectionManager}), e, XFireFault.RECEIVER);
110     }
111     
112     MessageFormatter outputMessageFormatter=MessageFormatterFactory.createMessageFormatter(serviceContext,false);
113     try{
114       // eseguo la formattazione all'indietro      
115       long millis1=System.currentTimeMillis();
116       outputMessageFormatter.mapOutputMessageToOutputBean(serviceContext);
117       long millis2=System.currentTimeMillis();
118       LOG.debug("output conversion time1="+(millis2-millis1)+" millis");
119       LOG.debug("output bean : ["+serviceContext.getOutputBean()+"]");
120     } catch (FormatException e) {
121       LOG.error("CIC002303_IO_exception=CIC002303", new Object[] {
122           serviceContext.getOutputMessage(), 
123           serviceContext.getOutputMappingDescriptor(), 
124           outputMessageFormatter}, e);
125       throw new XFireFault(MESSAGES.getString(
126               "CIC002303_IO_exception=CIC002303", new Object[] {
127               serviceContext.getOutputMessage(), 
128               serviceContext.getOutputMappingDescriptor(), 
129               outputMessageFormatter}), e, XFireFault.RECEIVER);
130     }
131     
132     return serviceContext.getOutputBean();
133   }
134 
135 }