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