1 19 20 package org.apache.cayenne.remote; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.cayenne.CayenneRuntimeException; 25 26 33 public abstract class BaseConnection implements ClientConnection { 34 35 protected Log logger; 36 protected long messageId; 37 38 41 protected BaseConnection() { 42 this.logger = LogFactory.getLog(getClass()); 43 } 44 45 50 public Object sendMessage(ClientMessage message) throws CayenneRuntimeException { 51 if (message == null) { 52 throw new NullPointerException ("Null message"); 53 } 54 55 beforeSendMessage(message); 56 57 long t0 = 0; 59 String messageLabel = ""; 60 61 long messageId = this.messageId++; 64 65 if (logger.isInfoEnabled()) { 66 t0 = System.currentTimeMillis(); 67 messageLabel = message.toString(); 68 logger.info("--- Message " + messageId + ": " + messageLabel); 69 } 70 71 Object response; 72 try { 73 response = doSendMessage(message); 74 } 75 catch (CayenneRuntimeException e) { 76 77 if (logger.isInfoEnabled()) { 79 long time = System.currentTimeMillis() - t0; 80 logger.info("*** Message error for " 81 + messageId 82 + ": " 83 + messageLabel 84 + " - took " 85 + time 86 + " ms."); 87 } 88 89 throw e; 90 } 91 92 if (logger.isInfoEnabled()) { 94 long time = System.currentTimeMillis() - t0; 95 logger.info("=== Message " 96 + messageId 97 + ": " 98 + messageLabel 99 + " done - took " 100 + time 101 + " ms."); 102 } 103 104 return response; 105 } 106 107 111 public long getProcessedMessagesCount() { 112 return messageId + 1; 113 } 114 115 118 protected abstract void beforeSendMessage(ClientMessage message) 119 throws CayenneRuntimeException; 120 121 124 protected abstract Object doSendMessage(ClientMessage message) 125 throws CayenneRuntimeException; 126 } 127 | Popular Tags |