Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.isb.publisher; import com.sfs.beans.ObjectTypeBean; import com.sfs.dao.ObjectTypeDAO; import com.sfs.dao.SFSDaoException; import com.sfs.whichdoctor.beans.IsbMessageBean; import com.sfs.whichdoctor.dao.IsbMessageDAO; import com.sfs.whichdoctor.dao.WhichDoctorDaoException; import com.sfs.whichdoctor.isb.publisher.WhichDoctorIsbPublisherException; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.net.URL; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.codehaus.xfire.client.Client; import org.jdom.Document; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /** * The Class IsbMessageSender. */ public class IsbMessageSender { /** The isb logger. */ private static Logger isbLogger = Logger.getLogger(IsbMessageSender.class); /** The source. */ private String source = "whichdoctor"; /** The debug mode. */ private boolean debugMode = false; /** The isb message dao. */ @Resource private IsbMessageDAO isbMessageDAO; /** The object type dao. */ @Resource private ObjectTypeDAO objectTypeDAO; /** * Set the source of the ISB message. * * @param sourceVal the source */ public final void setSource(final String sourceVal) { this.source = sourceVal; } /** * Set whether the ISB message should be published to the bus completely. * * @param debugModeVal the debug mode */ public final void setDebugMode(final String debugModeVal) { this.debugMode = false; if (StringUtils.equalsIgnoreCase(debugModeVal, "true")) { this.debugMode = true; } } /** * Places a message on the ISB Bus - also logs a copy into the database. * * @param target the ISB target to use * @param xmlPayload a String representing the XML payload * * @throws IOException Signals that an I/O exception has occurred. * * @exception IOException */ public final void send(final String target, final String xmlPayload) throws IOException { if (xmlPayload == null) { throw new NullPointerException("The ISB XML payload cannot be null"); } SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); Reader payloadReader = new StringReader(xmlPayload); // Setup the XML formatter XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); Document isbPayloadDocument = null; try { isbLogger.debug("ISB XML: " + xmlPayload); isbPayloadDocument = saxBuilder.build(payloadReader); } catch (Exception e) { isbLogger.error("Error parsing ISB XML into JDOM document: " + e.getMessage()); } // If the XML payload is a valid JDOM document - post onto the ISB bus if (isbPayloadDocument != null) { isbLogger.debug("ISB Message: " + xmlPayload); IsbMessageBean message = new IsbMessageBean(); message.setSource(source); message.setTarget(target); message.getIsbPayload().setXmlPayload(outputter.outputString(isbPayloadDocument)); message.setIsInbound(false); // Send the message to the WhichDoctor ISB module // for publishing on the bus isbLogger.info("ISB XML payload: " + message.getIsbPayload().getXmlPayload()); String wsdlUrl = ""; try { ObjectTypeBean tg = objectTypeDAO.load("ISB Target", "", target); wsdlUrl = tg.getAbbreviation(); } catch (SFSDaoException sde) { isbLogger.error("Error loading ISB Target: " + sde.getMessage()); } if (StringUtils.isBlank(wsdlUrl)) { isbLogger.error("The WSDL URL for the ISB target '" + target + "' is not defined"); message.setProcessed(true); } if (this.debugMode) { isbLogger.warn("ISB message sender in debug mode - message not sent"); message.setProcessed(true); } // Log the message into the processing queue for delivery try { this.isbMessageDAO.create(message); } catch (Exception e) { isbLogger.error("Error logging message to database: " + e.getMessage()); } } } /** * Publish the supplied IsbMessage to the designated ISB target. * * @param isbMessage the isb message */ public final void publish(final IsbMessageBean isbMessage) throws WhichDoctorIsbPublisherException { if (isbMessage != null && isbMessage.getId() > 0) { String target = ""; String xmlPayload = ""; String wsdlUrl = ""; String isbKey = ""; try { target = isbMessage.getTarget(); } catch (NullPointerException npe) { isbLogger.error("The ISB target for this IsbMessage was null"); } try { XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getCompactFormat()); xmlPayload = outputter.outputString(isbMessage.getIsbPayload().getXmlDocument()); } catch (NullPointerException npe) { isbLogger.error("The ISB payload for this IsbMessage (" + isbMessage.getId() + ") was null"); xmlPayload = ""; } catch (WhichDoctorDaoException wde) { isbLogger.error("Error generating ISB XML to send: " + wde.getMessage()); xmlPayload = ""; } try { ObjectTypeBean tg = objectTypeDAO.load("ISB Target", "", target); wsdlUrl = tg.getAbbreviation(); isbKey = tg.getSecurity(); } catch (SFSDaoException sde) { isbLogger.error("Error loading ISB Target: " + sde.getMessage()); } boolean publish = false; boolean processed = false; if (StringUtils.isBlank(xmlPayload)) { // The XML payload is empty so it is pointless sending this message. processed = true; } else { if (StringUtils.isNotBlank(wsdlUrl) && !isbMessage.getProcessed() && !isbMessage.getIsInbound()) { publish = true; } } if (publish) { // An ISB endpoint web service has been defined // Post change to service isbLogger.info("ISB web service end point defined"); isbLogger.info("Publishing ISB XML: "); isbLogger.info(xmlPayload); /* Perform a web service call using XFire libraries */ try { Client client = new Client(new URL(wsdlUrl)); Object[] results = client.invoke("Publish", new Object[] { isbKey, source, target, xmlPayload }); final String processMessage = (String) results[0]; isbLogger.info("ISB change sent: " + processMessage); if (processMessage.startsWith("ERROR")) { // The ISB message was not processed correctly // Throw an exception throw new Exception(processMessage); } isbLogger.info("ISB message sent, update the status"); // The message has been sent - set processed to true processed = true; } catch (Exception e) { isbLogger.error("Error publishing to ISB service: " + e.getMessage()); throw new WhichDoctorIsbPublisherException( "Error publishing to ISB service: " + e.getMessage()); } } if (processed) { // Update the status of the IsbMessage try { this.isbMessageDAO.updateProcessStatus(isbMessage.getId(), processed); } catch (WhichDoctorDaoException wde) { isbLogger.error( "Error updating the process status of " + "the ISB message: " + wde.getMessage()); } } } else { isbLogger.info("No unprocessed ISB message exists in the queue"); } } }