Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.6 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package gr.ebs.gss.server.ejb.indexer;
20

    
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;
29

    
30
import java.io.File;
31
import java.io.IOException;
32
import java.io.UnsupportedEncodingException;
33
import java.net.MalformedURLException;
34
import java.util.StringTokenizer;
35

    
36
import javax.ejb.ActivationConfigProperty;
37
import javax.ejb.EJB;
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;
46

    
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;
62

    
63
/**
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.
66
 */
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 {
71
        /**
72
         * The logger
73
         */
74
        private static final Log logger = LogFactory.getLog(IndexerMDBean.class);
75

    
76
        /**
77
         * EJB offering access to the JPA entity manager
78
         */
79
        @EJB ExternalAPI service;
80

    
81
        /**
82
         * Decides to add or drop an item from the index depending on the message
83
         * received
84
         *
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
88
         *
89
         * @param msg
90
         * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
91
         */
92
        @Override
93
        public void onMessage(Message msg) {
94
                Long id = null;
95
                try {
96
                        MapMessage map = (MapMessage) msg;
97
                        id = (Long) map.getObject("id");
98
                        boolean delete = map.getBoolean("delete");
99
                        Configuration config = GSSConfigurationFactory.getConfiguration();
100
                        if (delete) {
101
                                sendDelete(config.getString("solr.url"), id);
102
                        } else {
103
                                service.postFileToSolr(id);
104
                        }        
105
                }
106
                catch (JMSException e) {
107
                        throw new EJBException("Error processing file ID " + id, e);
108
                }
109
                catch (IOException e) {
110
                        throw new EJBException("Error processing file ID " + id, e);
111
                }
112
                catch (SolrServerException e) {
113
                        throw new EJBException(e);
114
                }
115
        }
116

    
117

    
118
        /**
119
         * Sends a delete command to solr. The id is the Long id of the indexed document
120
         * 
121
         * @param solrUrl
122
         * @param id
123
         * @throws SolrServerException
124
         * @throws IOException
125
         */
126
        private void sendDelete(String solrUrl, Long id)        throws SolrServerException, IOException {
127
                CommonsHttpSolrServer solr = new CommonsHttpSolrServer(solrUrl);
128
                solr.deleteById(id.toString());
129
                solr.commit();
130
        }
131

    
132

    
133
}