1 package org.objectweb.petals.engine.clock; 2 3 import java.text.DateFormat ; 4 import java.text.SimpleDateFormat ; 5 import java.util.Date ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 9 import javax.jbi.messaging.DeliveryChannel; 10 import javax.jbi.messaging.ExchangeStatus; 11 import javax.jbi.messaging.MessageExchange; 12 import javax.jbi.messaging.MessagingException; 13 import javax.jbi.messaging.NormalizedMessage; 14 import javax.xml.namespace.QName ; 15 16 import org.objectweb.petals.shared.xmlmanipulation.SourceHelper; 17 18 public class ClockListener implements Runnable { 19 20 private DeliveryChannel channel; 21 22 private MessageExchange messageExchange; 23 24 private boolean running; 25 26 private Logger logger; 27 28 public ClockListener(DeliveryChannel channel, Logger logger) { 29 super(); 30 this.channel = channel; 31 this.logger = logger; 32 } 33 34 public void run() { 35 36 running=true; 37 38 mainLoop: while(running){ 39 try 40 { 41 messageExchange = channel.accept(); 42 43 process(messageExchange); 44 45 } 46 catch (MessagingException e) { 47 logger.log(Level.SEVERE,e.getMessage(),e); 48 continue mainLoop; 49 } 50 } 51 52 } 53 54 private void process(MessageExchange msg) 55 { 56 logger.log(Level.INFO,""); 57 try 58 { 59 if(new QName ("time").equals(msg.getOperation())) 60 { 61 logger.log(Level.INFO,"The Clock service is called to return current time"); 62 63 DateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss"); 64 String en = formatter.format(new Date (System.currentTimeMillis())); 65 66 67 logger.log(Level.INFO,"The Clock answer : "+en); 68 69 NormalizedMessage nm = msg.createMessage(); 70 71 nm.setContent(SourceHelper.createSource(en)); 72 msg.setMessage(nm,"OUT"); 73 msg.setStatus(ExchangeStatus.DONE); 74 } 75 else 76 { 77 Exception e = new Exception ("only 'time' operation is recognized"); 78 msg.setError(e); 79 msg.setStatus(ExchangeStatus.ERROR); 80 logger.log(Level.WARNING,e.getMessage(),e); 81 } 82 channel.send(msg); 84 } 85 catch (Throwable e) 86 { 87 logger.log(Level.SEVERE,e.getMessage(),e); 88 } 89 } 90 91 public void stopProcessing(){ 92 this.running = false; 93 } 94 95 } 96 | Popular Tags |