2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server.ejb.indexer;
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.server.configuration.GSSConfigurationFactory;
24 import gr.ebs.gss.server.domain.FileBody;
25 import gr.ebs.gss.server.domain.FileHeader;
26 import gr.ebs.gss.server.domain.FileTag;
27 import gr.ebs.gss.server.ejb.ExternalAPI;
28 import gr.ebs.gss.server.ejb.GSSDAO;
31 import java.io.IOException;
32 import java.io.UnsupportedEncodingException;
33 import java.net.MalformedURLException;
34 import java.util.StringTokenizer;
36 import javax.ejb.ActivationConfigProperty;
38 import javax.ejb.EJBException;
39 import javax.ejb.MessageDriven;
40 import javax.ejb.TransactionAttribute;
41 import javax.ejb.TransactionAttributeType;
42 import javax.jms.JMSException;
43 import javax.jms.MapMessage;
44 import javax.jms.Message;
45 import javax.jms.MessageListener;
47 import org.apache.commons.configuration.Configuration;
48 import org.apache.commons.configuration.ConfigurationException;
49 import org.apache.commons.httpclient.HttpClient;
50 import org.apache.commons.httpclient.HttpException;
51 import org.apache.commons.httpclient.methods.PostMethod;
52 import org.apache.commons.httpclient.methods.StringRequestEntity;
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55 import org.apache.solr.client.solrj.SolrServerException;
56 import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
57 import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
58 import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
59 import org.apache.solr.common.SolrException;
60 import org.apache.solr.common.SolrInputDocument;
61 import org.jboss.ejb3.annotation.ResourceAdapter;
64 * Message driven bean that accepts messages whenever a document is created,
65 * modified or deleted and adds/removes the item from the search index.
67 @MessageDriven(activationConfig={@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
68 @ActivationConfigProperty(propertyName="destination", propertyValue="queue/gss-indexingQueue")})
69 @ResourceAdapter("hornetq-ra.rar")
70 public class IndexerMDBean implements MessageListener {
74 private static final Log logger = LogFactory.getLog(IndexerMDBean.class);
77 * EJB offering access to the JPA entity manager
79 @EJB ExternalAPI service;
82 * Decides to add or drop an item from the index depending on the message
85 * It currently uses the patched solr API for rich documents. This API does not
86 * allow indexing time field boosting. For this reason we have to use the dismax search API (instead of the
87 * standard) that allows for search time field boosting
90 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
93 public void onMessage(Message msg) {
96 MapMessage map = (MapMessage) msg;
97 id = (Long) map.getObject("id");
98 boolean delete = map.getBoolean("delete");
99 Configuration config = GSSConfigurationFactory.getConfiguration();
100 CommonsHttpSolrServer solr = new CommonsHttpSolrServer(getConfiguration().getString("solr.url"));
102 sendDelete(solr, id);
105 service.postFileToSolr(solr, id);
109 catch (JMSException e) {
110 throw new EJBException("Error processing file ID " + id, e);
112 catch (IOException e) {
113 throw new EJBException("Error processing file ID " + id, e);
115 catch (SolrServerException e) {
116 throw new EJBException(e);
122 * Sends a delete command to solr. The id is the Long id of the indexed document
126 * @throws SolrServerException
127 * @throws IOException
129 private void sendDelete(CommonsHttpSolrServer solr, Long id) throws SolrServerException, IOException {
130 solr.deleteById(id.toString());