001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.audit;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.management.JMException;
022    import javax.management.MBeanAttributeInfo;
023    import javax.management.MBeanOperationInfo;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.servicemix.jbi.container.JBIContainer;
028    import org.apache.servicemix.jbi.event.ExchangeEvent;
029    import org.apache.servicemix.jbi.event.ExchangeListener;
030    import org.apache.servicemix.jbi.management.AttributeInfoHelper;
031    import org.apache.servicemix.jbi.management.BaseSystemService;
032    import org.apache.servicemix.jbi.management.OperationInfoHelper;
033    import org.apache.servicemix.jbi.management.ParameterHelper;
034    
035    /**
036     * Base class for ServiceMix auditors implementations.
037     * 
038     * @author Guillaume Nodet (gnt)
039     * @since 2.1
040     * @version $Revision: 2153 $
041     */
042    public abstract class AbstractAuditor extends BaseSystemService implements AuditorMBean, ExchangeListener {
043    
044        protected final Log log = LogFactory.getLog(getClass());
045        
046        private boolean asContainerListener = true;
047    
048        public JBIContainer getContainer() {
049            return container;
050        }
051    
052        public void setContainer(JBIContainer container) {
053            this.container = container;
054        }
055        
056        protected Class getServiceMBean() {
057            return AuditorMBean.class;
058        }
059    
060        /* (non-Javadoc)
061         * @see javax.jbi.management.LifeCycleMBean#start()
062         */
063        public void start() throws javax.jbi.JBIException {
064            super.start();
065            doStart();
066            if (isAsContainerListener()) {
067                this.container.addListener(this);
068            }
069        }
070    
071        /* (non-Javadoc)
072         * @see javax.jbi.management.LifeCycleMBean#stop()
073         */
074        public void stop() throws javax.jbi.JBIException {
075            this.container.removeListener(this);
076            doStop();
077            super.stop();
078        }
079        
080        protected void doStart() throws JBIException {
081        }
082    
083        protected void doStop() throws JBIException {
084        }
085    
086        /* (non-Javadoc)
087         * @see org.apache.servicemix.jbi.management.MBeanInfoProvider#getAttributeInfos()
088         */
089        public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
090            // TODO: this should not be an attribute, as it can require access to database
091            AttributeInfoHelper helper = new AttributeInfoHelper();
092            helper.addAttribute(getObjectToManage(), "exchangeCount", "number of exchanges");
093            return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos());
094        }
095        
096        /* (non-Javadoc)
097         * @see org.apache.servicemix.jbi.management.MBeanInfoProvider#getOperationInfos()
098         */
099        public MBeanOperationInfo[] getOperationInfos() throws JMException {
100            // TODO: add other operations infos
101            OperationInfoHelper helper = new OperationInfoHelper();
102            ParameterHelper ph = helper.addOperation(getObjectToManage(), "getExchangesByRange", 2, "retrieve a bunch messages");
103            ph.setDescription(0, "fromIndex", "lower index of message (start from 0)");
104            ph.setDescription(1, "toIndex", "upper index of message (exclusive, > fromIndex)");
105            ph = helper.addOperation(getObjectToManage(), "getExchangeById", 1, "retrieve an exchange given its id");
106            ph.setDescription(0, "id", "id of the exchange to retrieve");
107            return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
108        }
109        
110        /* (non-Javadoc)
111         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeCount()
112         */
113        public abstract int getExchangeCount() throws AuditorException;
114        
115        /* (non-Javadoc)
116         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeId(int)
117         */
118        public String getExchangeIdByIndex(int index) throws AuditorException {
119            if (index < 0) {
120                throw new IllegalArgumentException("index should be greater or equal to zero");
121            }
122            return getExchangeIdsByRange(index, index + 1)[0];
123        }
124        
125        /* (non-Javadoc)
126         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeIds()
127         */
128        public String[] getAllExchangeIds() throws AuditorException {
129            return getExchangeIdsByRange(0, getExchangeCount());
130        }
131        
132        /* (non-Javadoc)
133         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeIds(int, int)
134         */
135        public abstract String[] getExchangeIdsByRange(int fromIndex, int toIndex)  throws AuditorException;
136        
137        /* (non-Javadoc)
138         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchange(int)
139         */
140        public MessageExchange getExchangeByIndex(int index) throws AuditorException {
141            if (index < 0) {
142                throw new IllegalArgumentException("index should be greater or equal to zero");
143            }
144            return getExchangesByRange(index, index + 1)[0];
145        }
146        
147        /* (non-Javadoc)
148         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchange(java.lang.String)
149         */
150        public MessageExchange getExchangeById(String id) throws AuditorException {
151            if (id == null || id.length() == 0) {
152                throw new IllegalArgumentException("id should be non null and non empty");
153            }
154            return getExchangesByIds(new String[] {id })[0];
155        }
156        
157        /* (non-Javadoc)
158         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges()
159         */
160        public MessageExchange[] getAllExchanges() throws AuditorException {
161            return getExchangesByRange(0, getExchangeCount());
162        }
163        
164        /* (non-Javadoc)
165         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges(int, int)
166         */
167        public MessageExchange[] getExchangesByRange(int fromIndex, int toIndex) throws AuditorException {
168            return getExchangesByIds(getExchangeIdsByRange(fromIndex, toIndex));
169        }
170    
171        /* (non-Javadoc)
172         * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges(java.lang.String[])
173         */
174        public abstract MessageExchange[] getExchangesByIds(String[] ids) throws AuditorException;
175    
176        /* (non-Javadoc)
177         * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges()
178         */
179        public int deleteAllExchanges() throws AuditorException {
180            return deleteExchangesByRange(0, getExchangeCount());
181        }
182        
183        /* (non-Javadoc)
184         * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchange(int)
185         */
186        public boolean deleteExchangeByIndex(int index) throws AuditorException {
187            if (index < 0) {
188                throw new IllegalArgumentException("index should be greater or equal to zero");
189            }
190            return deleteExchangesByRange(index, index + 1) == 1;
191        }
192        
193        /* (non-Javadoc)
194         * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchange(java.lang.String)
195         */
196        public boolean deleteExchangeById(String id) throws AuditorException {
197            return deleteExchangesByIds(new String[] {id }) == 1;
198        }
199        
200        /* (non-Javadoc)
201         * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges(int, int)
202         */
203        public int deleteExchangesByRange(int fromIndex, int toIndex) throws AuditorException {
204            return deleteExchangesByIds(getExchangeIdsByRange(fromIndex, toIndex));
205        }
206        
207        /* (non-Javadoc)
208         * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges(java.lang.String[])
209         */
210        public abstract int deleteExchangesByIds(String[] ids) throws AuditorException;
211        
212        /* (non-Javadoc)
213         * @see org.apache.servicemix.jbi.audit.AuditorMBean#resendExchange(javax.jbi.messaging.MessageExchange)
214         */
215        public void resendExchange(MessageExchange exchange) throws JBIException {
216            container.resendExchange(exchange);
217        }
218    
219        /**
220         * Test if Auditor should be included as a container listener
221         * 
222         * @return Returns the addToContainer.
223         */
224        public boolean isAsContainerListener() {
225            return asContainerListener;
226        }
227    
228        /**
229         * Set if Auditor should be included as a container listener.
230         * 
231         * @param addToContainer
232         *            The addToContainer to set.
233         */
234        public void setAsContainerListener(boolean addToContainer) {
235            this.asContainerListener = addToContainer;
236        }
237    
238        public void exchangeAccepted(ExchangeEvent event) {
239        }
240    
241    }