~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to clc/modules/axis2-transport/src/edu/ucsb/eucalyptus/transport/Axis2MessageDispatcher.java

  • Committer: graziano obertelli
  • Date: 2009-01-07 03:32:35 UTC
  • Revision ID: graziano@cs.ucsb.edu-20090107033235-oxhuexp18v8hg0pw
Tags: 1.4
from CVS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Software License Agreement (BSD License)
 
3
 *
 
4
 * Copyright (c) 2008, Regents of the University of California
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use of this software in source and binary forms, with or
 
8
 * without modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * * Redistributions of source code must retain the above
 
12
 *   copyright notice, this list of conditions and the
 
13
 *   following disclaimer.
 
14
 *
 
15
 * * Redistributions in binary form must reproduce the above
 
16
 *   copyright notice, this list of conditions and the
 
17
 *   following disclaimer in the documentation and/or other
 
18
 *   materials provided with the distribution.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
24
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
30
 * POSSIBILITY OF SUCH DAMAGE.
 
31
 *
 
32
 * Author: Chris Grzegorczyk grze@cs.ucsb.edu
 
33
 */
 
34
 
 
35
package edu.ucsb.eucalyptus.transport;
 
36
 
 
37
import edu.ucsb.eucalyptus.cloud.EucalyptusCloudException;
 
38
import edu.ucsb.eucalyptus.msgs.*;
 
39
import edu.ucsb.eucalyptus.transport.binding.BindingManager;
 
40
import edu.ucsb.eucalyptus.transport.client.BasicClient;
 
41
import edu.ucsb.eucalyptus.transport.config.*;
 
42
import edu.ucsb.eucalyptus.util.BindingUtil;
 
43
import org.apache.axis2.AxisFault;
 
44
import org.apache.axis2.context.ConfigurationContext;
 
45
import org.apache.log4j.Logger;
 
46
import org.jibx.runtime.JiBXException;
 
47
import org.mule.DefaultMuleMessage;
 
48
import org.mule.api.*;
 
49
import org.mule.api.endpoint.OutboundEndpoint;
 
50
import org.mule.api.transformer.TransformerException;
 
51
import org.mule.message.ExceptionMessage;
 
52
import org.mule.transport.AbstractMessageDispatcher;
 
53
 
 
54
 
 
55
public class Axis2MessageDispatcher extends AbstractMessageDispatcher {
 
56
 
 
57
  private static Logger LOG = Logger.getLogger( Axis2MessageDispatcher.class );
 
58
  private Axis2OutProperties properties;
 
59
  private BasicClient client;
 
60
 
 
61
  @SuppressWarnings("unchecked")
 
62
  public Axis2MessageDispatcher( OutboundEndpoint endpoint ) throws AxisFault, JiBXException
 
63
  {
 
64
    super( endpoint );
 
65
    if ( !endpoint.getProperties().containsKey( Key.WSSEC_POLICY.getKey() ) )
 
66
      endpoint.getProperties().put( Key.WSSEC_POLICY.getKey(), ( ( Axis2Connector ) endpoint.getConnector() ).getDefaultWssecPolicy() );
 
67
    ConfigurationContext ctx = (( Axis2Connector) endpoint.getConnector() ).getAxisConfig();
 
68
    Mep wssecFlow = Mep.valueOf( (String) endpoint.getProperty( Key.WSSEC_FLOW.getKey() ) );
 
69
    if( Mep.IN_ONLY.equals( wssecFlow ) ) endpoint.getProperties().put( Key.WSSEC_FLOW.getKey(), Mep.OUT_ONLY.name() );
 
70
    this.properties = new Axis2OutProperties( endpoint.getProperties(), ctx );
 
71
    System.out.println( "" );
 
72
  }
 
73
 
 
74
  public void sendAsync( Object msg ) throws EucalyptusCloudException
 
75
  {
 
76
    try
 
77
    {
 
78
      this.client.dispatch( (EucalyptusMessage) msg );
 
79
    }
 
80
    catch ( AxisFault e )
 
81
    {
 
82
      LOG.error( e, e );
 
83
      throw new EucalyptusCloudException( "Error dispatching the message", e );
 
84
    }
 
85
  }
 
86
 
 
87
  public void doDispatch( MuleEvent event ) throws EucalyptusCloudException
 
88
  {
 
89
    Object msg = getMessage( event );
 
90
    this.sendAsync( msg );
 
91
  }
 
92
 
 
93
  public MuleMessage doSend( MuleEvent event ) throws EucalyptusCloudException
 
94
  {
 
95
    Object msg = null;
 
96
    try
 
97
    {
 
98
      msg = event.transformMessage();
 
99
      if ( msg instanceof ExceptionMessage )
 
100
        msg = checkException( msg );
 
101
    }
 
102
    catch ( TransformerException e )
 
103
    {
 
104
      LOG.error( e, e );
 
105
      throw new EucalyptusCloudException( "Error handling message preparation for dispatch", e );
 
106
    }
 
107
    Object msgResponse = sendSync( msg );
 
108
    return new DefaultMuleMessage( msgResponse );
 
109
  }
 
110
 
 
111
  public Object sendSync( Object msg ) throws EucalyptusCloudException
 
112
  {
 
113
    Object msgResponse = null;
 
114
    try
 
115
    {
 
116
      msgResponse = this.client.send( ( EucalyptusMessage ) msg );
 
117
    }
 
118
    catch ( Exception e )
 
119
    {
 
120
      LOG.error( e, e );
 
121
      throw new EucalyptusCloudException( "Error dispatching the message", e );
 
122
    }
 
123
    return msgResponse;
 
124
  }
 
125
 
 
126
  private Object checkException( Object msg )
 
127
  {
 
128
    ExceptionMessage em = ( ExceptionMessage ) msg;
 
129
    try
 
130
    {
 
131
      String eucaMsgString = em.getPayloadAsString( "UTF-8" );
 
132
 
 
133
      EucalyptusMessage sourceMessage = ( EucalyptusMessage ) BindingManager.getBinding( BindingUtil.sanitizeNamespace( "http://msgs.eucalyptus.ucsb.edu/" ) ).fromOM( eucaMsgString );
 
134
      int i = 0;
 
135
      Throwable exception = em.getException().getCause();
 
136
      EucalyptusCloudException myEx = null;
 
137
      if ( exception instanceof EucalyptusCloudException )
 
138
        myEx = ( EucalyptusCloudException ) exception;
 
139
      else
 
140
        myEx = new EucalyptusCloudException( exception.getMessage(), exception);
 
141
      msg = new EucalyptusErrorMessageType( em.getEndpoint().getAddress(), sourceMessage, myEx.getMessage() );
 
142
    }
 
143
    catch ( Exception e )
 
144
    {
 
145
      LOG.error( e, e );
 
146
    }
 
147
    return msg;
 
148
  }
 
149
 
 
150
  private Object getMessage( final MuleEvent event ) throws EucalyptusCloudException
 
151
  {
 
152
    Object msg = null;
 
153
    try
 
154
    {
 
155
      msg = event.transformMessage();
 
156
      if ( msg instanceof ExceptionMessage )
 
157
        msg = checkException( msg );
 
158
    }
 
159
    catch ( TransformerException e )
 
160
    {
 
161
      LOG.error( e, e );
 
162
      throw new EucalyptusCloudException( "Error handling message preparation for dispatch", e );
 
163
    }
 
164
    return msg;
 
165
  }
 
166
 
 
167
  public BasicClient getClient()
 
168
  {
 
169
    return client;
 
170
  }
 
171
 
 
172
  public void setClient( final BasicClient client )
 
173
  {
 
174
    this.client = client;
 
175
  }
 
176
 
 
177
  public Axis2OutProperties getProperties()
 
178
  {
 
179
    return properties;
 
180
  }
 
181
 
 
182
  public void doConnect() throws Exception
 
183
  {
 
184
 
 
185
  }
 
186
 
 
187
  public void doDisconnect() throws Exception
 
188
  {
 
189
 
 
190
  }
 
191
 
 
192
  protected void doDispose() {}
 
193
}
 
194