001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.jms; 005 006 import java.io.StringReader; 007 import java.io.StringWriter; 008 import java.net.URL; 009 010 import javax.jms.Destination; 011 import javax.jms.Message; 012 import javax.jms.MessageProducer; 013 import javax.jms.Session; 014 import javax.jms.TextMessage; 015 import javax.xml.transform.Result; 016 import javax.xml.transform.Templates; 017 import javax.xml.transform.Transformer; 018 import javax.xml.transform.TransformerFactory; 019 import javax.xml.transform.stream.StreamResult; 020 import javax.xml.transform.stream.StreamSource; 021 022 import biz.hammurapi.config.ConfigurationException; 023 024 /** 025 * 026 * @author Tatyana Konukova 027 * 028 */ 029 public class StylingXmlMessageProcessor extends MessageProcessor { 030 031 private static final String RESOURCE_PREFIX = "resource:"; 032 private String replyDestinationName; 033 private Destination replyDestination; 034 private String stylesheet; 035 036 /** 037 * Creates result to receive transformed input. 038 * @return Result instance. 039 */ 040 protected Result createResult() { 041 return new StreamResult(new StringWriter()); 042 } 043 044 /** 045 * Processes result. If this method return value is not null then it is sent to reply destination. 046 * @param result 047 * @return 048 */ 049 protected String processResult(Result result) { 050 return ((StringWriter) ((StreamResult) result).getWriter()).toString(); 051 } 052 053 /** 054 * Parses request text, transforms and 055 */ 056 protected void processMessage(Message request, Session session) { 057 if (request instanceof TextMessage) { 058 try { 059 Result result = createResult(); 060 Transformer transformer = templates==null ? transformerFactory.newTransformer() : templates.newTransformer(); 061 transformer.transform(new StreamSource(new StringReader(((TextMessage) request).getText())), result); 062 String replyText = processResult(result); 063 if (replyText!=null) { 064 Session replySession = session==null ? borrowSession() : session; 065 try { 066 TextMessage msg = replySession.createTextMessage(replyText); 067 msg.setJMSCorrelationID(request.getJMSMessageID()); 068 MessageProducer producer = replySession.createProducer(request.getJMSReplyTo()==null ? replyDestination : request.getJMSReplyTo()); 069 try { 070 producer.send(msg); 071 } finally { 072 producer.close(); 073 } 074 } finally { 075 if (session==null) { 076 releaseSession(replySession); 077 } 078 } 079 } 080 } catch (Exception e) { 081 logger.error(e, e.toString()); 082 } 083 } else { 084 logger.warn(request, "Not a text message"); 085 } 086 } 087 088 /** 089 * Front-end reply destination (queue or topic) name. 090 * 091 * @param destinationName 092 */ 093 public void setReplyDestination(String destinationName) { 094 this.replyDestinationName = destinationName; 095 } 096 097 /** 098 * Stylesheet URL. If it starts with <code>resource:</code> then stylesheet gets loaded from 099 * classloader resource. 100 * @param stylesheet 101 */ 102 public void setStylesheet(String stylesheet) { 103 this.stylesheet = stylesheet; 104 } 105 106 private Templates templates; 107 protected TransformerFactory transformerFactory; 108 109 public void start() throws ConfigurationException { 110 super.start(); 111 try { 112 if (replyDestinationName!=null) { 113 replyDestination = (Destination) initialContext.lookup(replyDestinationName); 114 } 115 116 transformerFactory = TransformerFactory.newInstance(); 117 if (stylesheet!=null) { 118 if (stylesheet.startsWith(RESOURCE_PREFIX)) { 119 templates = transformerFactory.newTemplates( 120 new StreamSource( 121 getClass() 122 .getClassLoader() 123 .getResourceAsStream( 124 stylesheet.substring(RESOURCE_PREFIX.length())))); 125 } else { 126 templates = transformerFactory.newTemplates(new StreamSource(new URL(stylesheet).openStream())); 127 } 128 } 129 } catch (Exception e) { 130 throw new ConfigurationException("Startup failed: "+e, e); 131 } 132 } 133 134 }