KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > audit > AbstractAuditor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.audit;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.messaging.MessageExchange;
21 import javax.management.JMException JavaDoc;
22 import javax.management.MBeanAttributeInfo JavaDoc;
23 import javax.management.MBeanOperationInfo JavaDoc;
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 /**
35  * Base class for ServiceMix auditors implementations.
36  *
37  * @author Guillaume Nodet (gnt)
38  * @since 2.1
39  * @version $Revision: 426415 $
40  */

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 JavaDoc getServiceMBean() {
56         return AuditorMBean.class;
57     }
58
59     /* (non-Javadoc)
60      * @see javax.jbi.management.LifeCycleMBean#start()
61      */

62     public void start() throws javax.jbi.JBIException {
63         super.start();
64         doStart();
65         if (isAsContainerListener())
66             this.container.addListener(this);
67     }
68
69     /* (non-Javadoc)
70      * @see javax.jbi.management.LifeCycleMBean#stop()
71      */

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     /* (non-Javadoc)
85      * @see org.apache.servicemix.jbi.management.MBeanInfoProvider#getAttributeInfos()
86      */

87     public MBeanAttributeInfo JavaDoc[] getAttributeInfos() throws JMException JavaDoc {
88         // TODO: this should not be an attribute, as it can require access to database
89
AttributeInfoHelper helper = new AttributeInfoHelper();
90         helper.addAttribute(getObjectToManage(), "exchangeCount", "number of exchanges");
91         return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos());
92     }
93     
94     /* (non-Javadoc)
95      * @see org.apache.servicemix.jbi.management.MBeanInfoProvider#getOperationInfos()
96      */

97     public MBeanOperationInfo JavaDoc[] getOperationInfos() throws JMException JavaDoc {
98         // TODO: add other operations infos
99
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     /* (non-Javadoc)
107      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeCount()
108      */

109     public abstract int getExchangeCount() throws AuditorException;
110     
111     /* (non-Javadoc)
112      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeId(int)
113      */

114     public String JavaDoc getExchangeId(int index) throws AuditorException {
115         if (index < 0) {
116             throw new IllegalArgumentException JavaDoc("index should be greater or equal to zero");
117         }
118         return getExchangeIds(index, index + 1)[0];
119     }
120     
121     /* (non-Javadoc)
122      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeIds()
123      */

124     public String JavaDoc[] getExchangeIds() throws AuditorException {
125         return getExchangeIds(0, getExchangeCount());
126     }
127     
128     /* (non-Javadoc)
129      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchangeIds(int, int)
130      */

131     public abstract String JavaDoc[] getExchangeIds(int fromIndex, int toIndex) throws AuditorException;
132     
133     /* (non-Javadoc)
134      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchange(int)
135      */

136     public MessageExchange getExchange(int index) throws AuditorException {
137         if (index < 0) {
138             throw new IllegalArgumentException JavaDoc("index should be greater or equal to zero");
139         }
140         return getExchanges(index, index + 1)[0];
141     }
142     
143     /* (non-Javadoc)
144      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchange(java.lang.String)
145      */

146     public MessageExchange getExchange(String JavaDoc id) throws AuditorException {
147         if (id == null || id.length() == 0) {
148             throw new IllegalArgumentException JavaDoc("id should be non null and non empty");
149         }
150         return getExchanges(new String JavaDoc[] { id })[0];
151     }
152     
153     /* (non-Javadoc)
154      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges()
155      */

156     public MessageExchange[] getExchanges() throws AuditorException {
157         return getExchanges(0, getExchangeCount());
158     }
159     
160     /* (non-Javadoc)
161      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges(int, int)
162      */

163     public MessageExchange[] getExchanges(int fromIndex, int toIndex) throws AuditorException {
164         return getExchanges(getExchangeIds(fromIndex, toIndex));
165     }
166
167     /* (non-Javadoc)
168      * @see org.apache.servicemix.jbi.audit.AuditorMBean#getExchanges(java.lang.String[])
169      */

170     public abstract MessageExchange[] getExchanges(String JavaDoc[] ids) throws AuditorException;
171
172     /* (non-Javadoc)
173      * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges()
174      */

175     public int deleteExchanges() throws AuditorException {
176         return deleteExchanges(0, getExchangeCount());
177     }
178     
179     /* (non-Javadoc)
180      * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchange(int)
181      */

182     public boolean deleteExchange(int index) throws AuditorException {
183         if (index < 0) {
184             throw new IllegalArgumentException JavaDoc("index should be greater or equal to zero");
185         }
186         return deleteExchanges(index, index + 1) == 1;
187     }
188     
189     /* (non-Javadoc)
190      * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchange(java.lang.String)
191      */

192     public boolean deleteExchange(String JavaDoc id) throws AuditorException {
193         return deleteExchanges(new String JavaDoc[] { id }) == 1;
194     }
195     
196     /* (non-Javadoc)
197      * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges(int, int)
198      */

199     public int deleteExchanges(int fromIndex, int toIndex) throws AuditorException {
200         return deleteExchanges(getExchangeIds(fromIndex, toIndex));
201     }
202     
203     /* (non-Javadoc)
204      * @see org.apache.servicemix.jbi.audit.AuditorMBean#deleteExchanges(java.lang.String[])
205      */

206     public abstract int deleteExchanges(String JavaDoc[] ids) throws AuditorException;
207     
208     /* (non-Javadoc)
209      * @see org.apache.servicemix.jbi.audit.AuditorMBean#resendExchange(javax.jbi.messaging.MessageExchange)
210      */

211     public void resendExchange(MessageExchange exchange) throws JBIException {
212         container.resendExchange(exchange);
213     }
214
215     /**
216      * Test if Auditor should be included as a container listener
217      *
218      * @return Returns the addToContainer.
219      */

220     public boolean isAsContainerListener() {
221         return asContainerListener;
222     }
223
224     /**
225      * Set if Auditor should be included as a container listener.
226      *
227      * @param addToContainer
228      * The addToContainer to set.
229      */

230     public void setAsContainerListener(boolean addToContainer) {
231         this.asContainerListener = addToContainer;
232     }
233 }
234
Popular Tags