KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > audit > lucene > LuceneAuditor


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.lucene;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.jbi.JBIException;
24 import javax.jbi.messaging.ExchangeStatus;
25 import javax.jbi.messaging.MessageExchange;
26 import javax.jbi.messaging.MessagingException;
27 import javax.jbi.messaging.NormalizedMessage;
28
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.document.Field;
31 import org.apache.servicemix.jbi.audit.AbstractAuditor;
32 import org.apache.servicemix.jbi.audit.AuditorException;
33 import org.apache.servicemix.jbi.audit.AuditorMBean;
34 import org.apache.servicemix.jbi.audit.AuditorQueryMBean;
35 import org.apache.servicemix.jbi.event.ExchangeEvent;
36 import org.apache.servicemix.jbi.event.ExchangeListener;
37 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
38
39 /**
40  * Lucene AuditorQuery implementation.
41  * It uses Lucene as the indexing mechanism for searching Exchanges
42  * and needs a delegated AuditorMBean to persist Exchanges.
43  *
44  * The Content of messages are stored as:
45  * - org.apache.servicemix.in.contents
46  * - org.apache.servicemix.out.contents, if exists
47  * - org.apache.servicemix.fault.contents, if exists
48  *
49  * Properties for IN Messages are stored as:
50  * - org.apache.servicemix.in.propertyname
51  * - org.apache.servicemix.out.propertyname, if exists
52  * - org.apache.servicemix.fault.propertyname, if exists
53  *
54  * @author George Gastaldi
55  * @since 2.1
56  * @version $Revision: 426415 $
57  */

58 public class LuceneAuditor extends AbstractAuditor implements AuditorQueryMBean {
59     
60     private AuditorMBean delegatedAuditor;
61     private LuceneIndexer luceneIndexer = new LuceneIndexer();
62
63     protected void doStart() throws JBIException {
64         super.doStart();
65         if (delegatedAuditor == null) {
66             throw new JBIException("A delegated auditor must be provided");
67         }
68         this.delegatedAuditor.start();
69     }
70     
71     protected void doStop() throws JBIException {
72         super.doStop();
73         this.delegatedAuditor.stop();
74     }
75     
76     /**
77      * @return Returns the luceneIndexer.
78      */

79     public LuceneIndexer getLuceneIndexer() {
80         return luceneIndexer;
81     }
82
83     /**
84      * @param luceneIndexer The luceneIndexer to set.
85      */

86     public void setLuceneIndexer(LuceneIndexer luceneIndexer) {
87         this.luceneIndexer = luceneIndexer;
88     }
89
90     /**
91      * @return Returns the delegatedAuditor.
92      */

93     public AuditorMBean getDelegatedAuditor() {
94         return delegatedAuditor;
95     }
96
97     /**
98      * @param delegatedAuditor The delegatedAuditor to set.
99      */

100     public void setDelegatedAuditor(AuditorMBean delegatedAuditor) {
101         this.delegatedAuditor = delegatedAuditor;
102         if (delegatedAuditor instanceof AbstractAuditor) {
103             ((AbstractAuditor)delegatedAuditor).setAsContainerListener(false);
104         }
105     }
106
107     public int getExchangeCount() throws AuditorException {
108         return this.delegatedAuditor.getExchangeCount();
109     }
110
111     public String JavaDoc[] getExchangeIds(int fromIndex, int toIndex) throws AuditorException {
112         return this.delegatedAuditor.getExchangeIds(fromIndex,toIndex);
113     }
114
115     public MessageExchange[] getExchanges(String JavaDoc[] ids) throws AuditorException {
116         return this.delegatedAuditor.getExchanges(ids);
117     }
118
119     public int deleteExchanges(int fromIndex, int toIndex) throws AuditorException {
120         //TODO: Remove ids from Lucene Index
121
return this.delegatedAuditor.deleteExchanges(fromIndex,toIndex);
122     }
123
124     public int deleteExchanges(String JavaDoc[] ids) throws AuditorException {
125         try {
126             this.luceneIndexer.remove(ids);
127         } catch (IOException JavaDoc io) {
128             throw new AuditorException(io);
129         }
130         return this.delegatedAuditor.deleteExchanges(ids);
131     }
132
133     public void exchangeSent(ExchangeEvent event) {
134         MessageExchange exchange = event.getExchange();
135         try {
136             Document doc = createDocument(exchange);
137             this.luceneIndexer.add(doc,exchange.getExchangeId());
138             if (delegatedAuditor instanceof ExchangeListener) {
139                 ((ExchangeListener) delegatedAuditor).exchangeSent(event);
140             }
141         } catch (Exception JavaDoc e) {
142             log.error("Error while adding to lucene", e);
143         }
144     }
145
146     public String JavaDoc getDescription() {
147         return "Lucene Auditor";
148     }
149
150     public String JavaDoc[] findExchangesIDsByStatus(ExchangeStatus status) throws AuditorException {
151         String JavaDoc field = "org.apache.servicemix.exchangestatus";
152         return getExchangeIds(field,String.valueOf(status));
153     }
154
155     public String JavaDoc[] findExchangesIDsByMessageContent(String JavaDoc type, String JavaDoc content) throws AuditorException {
156         String JavaDoc field = "org.apache.servicemix."+type+".contents";
157         return getExchangeIds(field,content);
158     }
159
160     public String JavaDoc[] findExchangesIDsByMessageProperty(String JavaDoc type, String JavaDoc property, String JavaDoc value) throws AuditorException {
161         if (property != null && !property.startsWith("org.apache.servicemix")) {
162             property = "org.apache.servicemix."+type+"."+property;
163         }
164         return getExchangeIds(property,value);
165     }
166     
167     protected Document createDocument(MessageExchange me) throws MessagingException {
168         try {
169             //This could be in a separated class (a LuceneDocumentProvider)
170
SourceTransformer st = new SourceTransformer();
171             Document d = new Document();
172             d.add(Field.Keyword("org.apache.servicemix.exchangeid",me.getExchangeId()));
173             d.add(Field.Keyword("org.apache.servicemix.exchangestatus",String.valueOf(me.getStatus())));
174             
175             String JavaDoc[] types = {"in","out","fault"};
176             for (int i=0;i<types.length;i++) {
177                 String JavaDoc type = types[i];
178                 NormalizedMessage nm = me.getMessage(type);
179                 if (nm != null) {
180                     d.add(Field.UnStored("org.apache.servicemix."+type+".contents",st.contentToString(nm)));
181                     addMessagePropertiesToDocument(nm,d,type);
182                 }
183             }
184             return d;
185         } catch (MessagingException mse) {
186             throw mse;
187         } catch (Exception JavaDoc ex) {
188             throw new MessagingException("Error while creating Lucene Document",ex);
189         }
190     }
191     
192     protected void addMessagePropertiesToDocument(NormalizedMessage nm,
193             Document document, String JavaDoc type) throws MessagingException {
194         Set JavaDoc propertyNames = nm.getPropertyNames();
195         for (Iterator JavaDoc iter = propertyNames.iterator(); iter.hasNext();) {
196             String JavaDoc propertyName = (String JavaDoc) iter.next();
197             Object JavaDoc value = nm.getProperty(propertyName);
198             if (value instanceof String JavaDoc)
199                 //org.apache.servicemix.out.myproperty
200
document.add(Field.Keyword("org.apache.servicemix."+type+"."+propertyName,String.valueOf(value)));
201         }
202     }
203     
204     public String JavaDoc[] getExchangeIds(String JavaDoc queryContent, String JavaDoc field) throws AuditorException {
205         DefaultLuceneCallback dfc = new DefaultLuceneCallback(queryContent,field);
206         try {
207             return (String JavaDoc[])luceneIndexer.search(dfc);
208         } catch (IOException JavaDoc e) {
209             throw new AuditorException("Error while getting Exchange IDs",e);
210         }
211     }
212 }
213
Popular Tags