FileTopicHandler.java |
1 /* 2 * Copyright (C) 2003 robert rowntree 3 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 14 * ref. http://www.gnu.org/copyleft/gpl.html 15*/ 16 17/** CLIENT-SIDE - decoupled pattern for "data producer" & "data consumer" 18 * is being used. Producer and consumer are located on different host 19 * machines (possibly at diffent companies ). This implemnents Publish. 20 * ALL Subscribe activity is totally de-coupled... 21 * ( separate thread-pool used to distribute data to subscribers ). 22 * * * * * * * * * * 23 * * * * * * * * * * 24 * Semantic Details: 25 * 1. Data has been produced on the client machine. 26 * 2. Client has generated an "I-JUST-CREATED-DATA" event. 27 * 3. The event "knows" what files or data it has to distribute. 28 * 3. The event "knows" what "Topic" governs the data distribution. 29 * 4. A "handler" or implementation for the event must exist 30 * 5. That handler will trigger the actual PUBLISH activity 31 * 6. Event and Data will be "sent" to a POST-OFFICE on a remote machine 32 * 8. Subscriber maintenance and Publishing both occur on a Remote VM. 33 * 34 * This class implements Publish for data just created 35 * This implementation uses following API's to PUSH data to Remote: 36 * HTTP, SOAP, Apache AXIS, JAX-RPC, SAAJ or SwA. 37 * 38 * In a JAXRPC implementation, the SOAP MEP is "sychronous". 39 * This implementation is much more appropriate for lower volume data 40 * interchange activity where data in files needs to be passed to 41 * other applications. 42 * 43 * Although you could do it with this implementation, it would be LESS 44 * appropriate for hi-volume interchange of small snips of XML 45 * between applications. 46 * 47 * For hi-volume messages on small XML snips, a non-blocking, 48 * non-JAXRPC implementation would be recommended. 49 * * * * * * * * * * 50 * * * * * * * * * * 51 * <p> Class Responsibilities: 52 * Handler for client action event where event contains properties: 53 * (Topic, TopicAction, FileSet ). 54 * The Fileset was just created by a DataProducer and may need to distributed 55 * to DataConsumer(s). 56 * </p><p> 57 * Semantic is that this collection of file(s) has just been created and that 58 * the FileSet belongs to a Publishable Topic. Therefor, on the event, all that 59 * is necessary is to get a Connection and Transport protocal between the 60 * nodes where: 61 * data-is-created 62 * data-is-distributed 63 * </p><p> 64 * RPC and SOAP Body - Spec requires that the Server-side method Name 65 * in the Java impl be the same string as "soap.rpc.methodname.attachment" 66 * in the code below. 67 * So, over on the server, THERE MUST be a method in the class which provides 68 * imple of the service. This class is pointed to by "wsdl.service.addr.port.name". 69 * @param Event to signal "move a fileSet up to the PO from client" ... 70 * @return there is not really a return arg BUT: 71 * look at the "response" message after the call on the SoapConnection 72 * the SOAP Response msg is dumped to SysOUT... 73 * So, when you run the client, SysOUT receives the SOAP response 74 * message as a chunk of xml for an <exchangeSession> Node. Wrapped 75 * by the response are the "commands" to exhange data that will 76 * process in another thread and that will transfer the data to subscribers. 77 * * * * * * * * * * 78 * * * * * * * * * * 79 * <A HREF="http://aWebAppContext/po_1_2/xsd/ftp.xsd">SOAP Message Response Details</A> 80 * 81 */ 82 83/** Java class "FileTopicHandler.java" generated from Poseidon for UML. 84 * Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>. 85 * Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine. 86 */ 87package com.borneo.po.event; 88 89import java.awt.event.ActionEvent; 90import java.awt.event.ActionListener; 91import java.io.IOException; 92import java.util.Enumeration; 93import java.util.Iterator; 94import java.util.ListIterator; 95 96import javax.xml.soap.SOAPException; 97import javax.xml.soap.SOAPMessage; 98 99import org.apache.axis.AxisFault; 00import org.apache.axis.Message; 01import org.apache.axis.attachments.AttachmentPart; 02import org.apache.axis.MessageContext; 03import org.apache.axis.client.Service; 04import org.apache.axis.message.RPCElement; 05import org.apache.axis.message.SOAPEnvelope; 06import org.apache.axis.soap.SOAPConnectionFactoryImpl; 07 08import org.apache.log4j.Logger; 09import org.apache.log4j.PropertyConfigurator; 10 11import com.borneo.net.soap.SAAJCallMgr; 12import com.borneo.util.POProperty; 13 14/** 15 * <p> 16 * 17 * </p> 18 */ 19public class FileTopicHandler implements ActionListener { 20 protected static Logger log; 21 /////////////////////////////////////// 22 // operations 23 24 private Exception _failure = null; 25 26 public void actionPerformed(ActionEvent event) { 27 //TODO the collaboration belo with class=SAAJCalMgr needs to be UNCOUPLED. 28 // Put it behind an Interface like PubDataCreate 29 // Then, this would be a SOAP implementation for the client... 30 SAAJCallMgr mgr = new SAAJCallMgr(); 31 mgr.setMessageContext(new MessageContext(new Service().getEngine())); 32 // get new SOAPenvelope that will hold all attachments 33 mgr.setRequestEnvelope( 34 new SOAPEnvelope( 35 mgr.getMessageContext().getSOAPConstants(), 36 mgr.getMessageContext().getSchemaVersion())); 37 // Event hasA: FileSet. I "just-created-this-data" asCollection of Files 38 // get new RPCelement with FileSet to DataHandler[] to RPCParam[] 39 // rev - add Str topicnm as first param. 40 // see api for RPCElement in Axis implementation 41 RPCElement myRpcEl = new RPCElement( 42 POProperty.get("wsdl.service.addr.port.name") 43 , POProperty.get("soap.rpc.methodname.attachment") 44 , mgr.setParams( ( 45 (FileTopic) event.getSource()).getTopic() 46 , mgr.setDH(((FileTopic) event.getSource()) 47 .getFiles() 48 ) 49 ) 50 ); 51 //log.debug("using encode style : " + mgr.getMessageContext().getSOAPConstants().getEncodingURI()); 52 try { 53 myRpcEl.setEncodingStyle( 54 mgr.getMessageContext().getSOAPConstants().getEncodingURI()); 55 // envelope hasA bodyElement and a ref to a Context 56 mgr.getRequestEnvelope().addBodyElement(myRpcEl); 57 mgr.getRequestEnvelope().setMessageType(Message.REQUEST); 58 mgr.getMessageContext().setTargetService( 59 mgr.getRequestEnvelope().getFirstBody().getNamespaceURI()); 60 // get the required SOAPconnection 61 mgr.setConnection( 62 SOAPConnectionFactoryImpl.newInstance().createConnection()); 63 } catch (UnsupportedOperationException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } catch (SOAPException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } catch (AxisFault e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } 73 74 // call on the SOAPconnection with parm of new message 75 mgr.setReqMessage(new Message(mgr.getRequestEnvelope())); 76 mgr.getReqMessage().setMessageContext(mgr.getMessageContext()); 77 Message response = (Message) mgr.call(mgr.getReqMessage()); 78 try { 79 // response.writeTo(System.out); 80 log.debug("attachment cnt : " + new Integer(response.countAttachments()).toString()); 81 response.getAttachmentsImpl().writeContentToStream(System.out); 82// use below to get a sax source on the response 83// for (Iterator i = response.getAttachments(); i.hasNext();) { 84// AttachmentPart mm = (AttachmentPart)i.next() ; 85// log.debug("DS fm part : " + mm.getDataHandler().getDataSource().getInputStream().toString()); 86// } 87// 88 89 } catch (IOException e1) { 90 // TODO Auto-generated catch block 91 _failure = e1; 92 e1.printStackTrace(); 93 } finally { 94 mgr.close(); 95 log.debug("SOAP Connection closed"); 96 } 97 98 } // end actionPerformed 99 00 //TODO 01 static { 02 log = Logger.getLogger((FileTopicHandler.class).getName()); 03 ClassLoader cl = FileTopicHandler.class.getClassLoader(); 04 PropertyConfigurator.configure( 05 cl.getResource("config/log4j.properties")); 06 } 07 /** 08 * 09 * @return 10 */ 11 public Exception getFailure() { 12 return _failure; 13 } 14 15} // end FileTopicHandler 16