View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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   
9   package it.imolinfo.jbi4ejb.jbi.xfire;
10  
11  import it.imolinfo.jbi4ejb.Logger;
12  import it.imolinfo.jbi4ejb.LoggerFactory;
13  import it.imolinfo.jbi4ejb.jbi.Messages;
14  import it.imolinfo.jbi4ejb.jbi.component.Jbi4EjbLifeCycle;
15  
16  import java.io.OutputStream;
17  
18  import javax.xml.stream.XMLOutputFactory;
19  import javax.xml.stream.XMLStreamException;
20  import javax.xml.stream.XMLStreamWriter;
21  
22  import org.codehaus.xfire.MessageContext;
23  import org.codehaus.xfire.XFireException;
24  import org.codehaus.xfire.exchange.OutMessage;
25  import org.codehaus.xfire.transport.AbstractChannel;
26  import org.codehaus.xfire.transport.Channel;
27  
28  /**
29   * Ejb channel, only support local invocations and backchannel uri.
30   * 
31   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
32   */
33  public class EjbChannel extends AbstractChannel {
34      
35      /** The logger for this class and its instances. */
36      private static final Logger LOG = LoggerFactory.getLogger(EjbChannel.class);
37      private static final Messages MESSAGES = Messages.getMessages(EjbChannel.class);
38      
39      /** The output factory. */
40      private XMLOutputFactory outputFactory;             
41  
42      /**
43       * Instantiates a new ejb channel.
44       * 
45       * @param uri the channel uri
46       * @param transport the EjbTransport
47       */
48      public EjbChannel(String uri, EjbTransport transport) {
49          setTransport(transport);
50          setUri(uri);
51          this.outputFactory = XMLOutputFactory.newInstance();        
52      }
53  
54      
55      /**
56       * Do nothing..
57       * 
58       * @throws Exception if some proble occurs
59       * 
60       * @see org.codehaus.xfire.transport.Channel#open()
61       */
62      public void open() throws Exception {
63          // Do nothing...
64      }
65  
66      /* (non-Javadoc)
67  
68       */
69      
70      
71      /**
72       * Sends the message.
73       * 
74       * @param context The message context
75       * @param message the out message
76       * @see org.codehaus.xfire.transport.Channel#send(org.codehaus.xfire.MessageContext, org.codehaus.xfire.exchange.OutMessage)
77       * @throws XFireException if some problem occurs 
78       */
79      public void send(MessageContext context, OutMessage message) throws XFireException {
80          LOG.debug("Xfire send called");
81          if (Channel.BACKCHANNEL_URI.equals(message.getUri())) {
82              LOG.debug("message.getUri(): " + message.getUri());
83              final OutputStream out = (OutputStream) context.getProperty(Channel.BACKCHANNEL_URI);
84              if (out != null) {
85                  try {
86                      final XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out, message.getEncoding());                                        
87                      message.getSerializer().writeMessage(message, writer, context);                    
88                      writer.close();
89                  } catch (XMLStreamException e) {
90                  	String msg=MESSAGES.getString("EJB000501_EjbChannel", new Object[]{e.getMessage()});
91                      LOG.error(msg,e);
92                      throw new XFireException(msg,e);   
93  
94                  }
95                  return;
96              }
97          } else {
98              // Not implemented.         
99              LOG.debug("NOT a backchannel URI, not implemented");
100         }
101     }
102 
103 }