1 17 package org.apache.servicemix.jbi.audit; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.messaging.MessageExchange; 21 import javax.management.JMException ; 22 import javax.management.MBeanAttributeInfo ; 23 import javax.management.MBeanOperationInfo ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.servicemix.jbi.container.JBIContainer; 28 import org.apache.servicemix.jbi.event.ExchangeListener; 29 import org.apache.servicemix.jbi.management.AttributeInfoHelper; 30 import org.apache.servicemix.jbi.management.BaseSystemService; 31 import org.apache.servicemix.jbi.management.OperationInfoHelper; 32 import org.apache.servicemix.jbi.management.ParameterHelper; 33 34 41 public abstract class AbstractAuditor extends BaseSystemService implements AuditorMBean, ExchangeListener { 42 43 protected final Log log = LogFactory.getLog(getClass()); 44 45 private boolean asContainerListener = true; 46 47 public JBIContainer getContainer() { 48 return container; 49 } 50 51 public void setContainer(JBIContainer container) { 52 this.container = container; 53 } 54 55 protected Class getServiceMBean() { 56 return AuditorMBean.class; 57 } 58 59 62 public void start() throws javax.jbi.JBIException { 63 super.start(); 64 doStart(); 65 if (isAsContainerListener()) 66 this.container.addListener(this); 67 } 68 69 72 public void stop() throws javax.jbi.JBIException { 73 this.container.removeListener(this); 74 doStop(); 75 super.stop(); 76 } 77 78 protected void doStart() throws JBIException { 79 } 80 81 protected void doStop() throws JBIException { 82 } 83 84 87 public MBeanAttributeInfo [] getAttributeInfos() throws JMException { 88 AttributeInfoHelper helper = new AttributeInfoHelper(); 90 helper.addAttribute(getObjectToManage(), "exchangeCount", "number of exchanges"); 91 return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos()); 92 } 93 94 97 public MBeanOperationInfo [] getOperationInfos() throws JMException { 98 OperationInfoHelper helper = new OperationInfoHelper(); 100 ParameterHelper ph = helper.addOperation(getObjectToManage(), "getExchanges", 2, "retrieve a bunch messages"); 101 ph.setDescription(0, "fromIndex", "lower index of message (start from 0)"); 102 ph.setDescription(1, "toIndex", "upper index of message (exclusive, > fromIndex)"); 103 return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos()); 104 } 105 106 109 public abstract int getExchangeCount() throws AuditorException; 110 111 114 public String getExchangeId(int index) throws AuditorException { 115 if (index < 0) { 116 throw new IllegalArgumentException ("index should be greater or equal to zero"); 117 } 118 return getExchangeIds(index, index + 1)[0]; 119 } 120 121 124 public String [] getExchangeIds() throws AuditorException { 125 return getExchangeIds(0, getExchangeCount()); 126 } 127 128 131 public abstract String [] getExchangeIds(int fromIndex, int toIndex) throws AuditorException; 132 133 136 public MessageExchange getExchange(int index) throws AuditorException { 137 if (index < 0) { 138 throw new IllegalArgumentException ("index should be greater or equal to zero"); 139 } 140 return getExchanges(index, index + 1)[0]; 141 } 142 143 146 public MessageExchange getExchange(String id) throws AuditorException { 147 if (id == null || id.length() == 0) { 148 throw new IllegalArgumentException ("id should be non null and non empty"); 149 } 150 return getExchanges(new String [] { id })[0]; 151 } 152 153 156 public MessageExchange[] getExchanges() throws AuditorException { 157 return getExchanges(0, getExchangeCount()); 158 } 159 160 163 public MessageExchange[] getExchanges(int fromIndex, int toIndex) throws AuditorException { 164 return getExchanges(getExchangeIds(fromIndex, toIndex)); 165 } 166 167 170 public abstract MessageExchange[] getExchanges(String [] ids) throws AuditorException; 171 172 175 public int deleteExchanges() throws AuditorException { 176 return deleteExchanges(0, getExchangeCount()); 177 } 178 179 182 public boolean deleteExchange(int index) throws AuditorException { 183 if (index < 0) { 184 throw new IllegalArgumentException ("index should be greater or equal to zero"); 185 } 186 return deleteExchanges(index, index + 1) == 1; 187 } 188 189 192 public boolean deleteExchange(String id) throws AuditorException { 193 return deleteExchanges(new String [] { id }) == 1; 194 } 195 196 199 public int deleteExchanges(int fromIndex, int toIndex) throws AuditorException { 200 return deleteExchanges(getExchangeIds(fromIndex, toIndex)); 201 } 202 203 206 public abstract int deleteExchanges(String [] ids) throws AuditorException; 207 208 211 public void resendExchange(MessageExchange exchange) throws JBIException { 212 container.resendExchange(exchange); 213 } 214 215 220 public boolean isAsContainerListener() { 221 return asContainerListener; 222 } 223 224 230 public void setAsContainerListener(boolean addToContainer) { 231 this.asContainerListener = addToContainer; 232 } 233 } 234 | Popular Tags |