Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / ejb / indexer / IndexerMDBean.java @ 0fcbf8bd

History | View | Annotate | Download (4.6 kB)

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