Project

General

Profile

1 7542 tao
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.index;
28
29 7631 tao
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
30 7613 tao
import java.util.ArrayList;
31 7542 tao
import java.util.List;
32
33
import org.apache.commons.logging.Log;
34
import org.apache.commons.logging.LogFactory;
35 7589 leinfelder
import org.apache.solr.client.solrj.SolrServer;
36 7542 tao
import org.springframework.context.ApplicationContext;
37
import org.springframework.context.support.FileSystemXmlApplicationContext;
38
39
/**
40
 * The start class of the index.
41
 * @author tao
42
 *
43
 */
44
public class ApplicationController {
45
46
    private static String SOLRINDEXES = "solrIndexes";
47 7613 tao
    private static short FIRST = 0;
48 7589 leinfelder
49 7542 tao
    private List<SolrIndex> solrIndexes = null;
50 7691 tao
    //private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>();
51 7542 tao
    private static ApplicationContext context = null;
52
    private String springConfigFile = "src/main/resources/index-processor-context.xml";
53
    Log log = LogFactory.getLog(ApplicationController.class);
54
55 7613 tao
56 7542 tao
    /**
57
     * Constructor
58
     */
59 7613 tao
    public ApplicationController () throws Exception {
60 7542 tao
        init();
61
    }
62
63 7552 tao
    /**
64
     * Set the Spring configuration file.
65
     * @param springConfigFile  the path of the Spring configuration file
66
     */
67 7613 tao
    public ApplicationController(String springConfigFile) throws Exception {
68 7552 tao
        this.springConfigFile = springConfigFile;
69
        init();
70
    }
71
72 7560 leinfelder
    /**
73 7589 leinfelder
     * Initialize the list of the SolrIndex objects from the configuration file.
74
     * Set the SolrServer implementation using the factory.
75
     * Start listening for events on Hazelcast
76 7542 tao
     */
77 7613 tao
    private void init() throws Exception {
78 7542 tao
        context = getContext();
79
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
80
81 7591 leinfelder
        // use factory to create the correct impl
82 7589 leinfelder
    	SolrServer solrServer = null;
83 7591 leinfelder
		try {
84
			solrServer = SolrServerFactory.createSolrServer();
85
		} catch (Exception e) {
86
			log.error("Could not create SolrServer form factory", e);
87 7613 tao
			throw e;
88 7591 leinfelder
		}
89 7589 leinfelder
90 7560 leinfelder
        // start the SystemMetadata listener[s] (only expect there to be one)
91
        for (SolrIndex solrIndex: solrIndexes) {
92 7589 leinfelder
        	// set the solr server to use
93
			solrIndex.setSolrServer(solrServer);
94
95
			// start listening for events
96 7560 leinfelder
        	SystemMetadataEventListener smel = new SystemMetadataEventListener();
97
        	smel.setSolrIndex(solrIndex);
98
        	smel.start();
99 7691 tao
        	//sysmetaListeners.add(smel);
100 7560 leinfelder
        }
101
102 7542 tao
    }
103
104 7589 leinfelder
    /**
105 7542 tao
     * Get the ApplicaionContext of Spring.
106
     */
107
    private ApplicationContext getContext() {
108
        if (context == null) {
109
            context = new FileSystemXmlApplicationContext(springConfigFile);
110
        }
111
        return context;
112
    }
113
114
    /**
115
     * Get the path of the Spring configuration file.
116
     * @return the path of the Spring configuration file.
117
     */
118
    public String getSpringConfigFile() {
119
        return this.springConfigFile;
120
    }
121
122
    /**
123
     * Get the list of the solr index.
124
     * @return the list of the solr index.
125
     */
126
    public List<SolrIndex> getSolrIndexes() {
127
        return this.solrIndexes;
128
    }
129 7613 tao
130
131
    /**
132
     * Start to generate indexes for those haven't been indexed in another thread.
133
     */
134
    public void startIndex() {
135
        SolrIndex index = solrIndexes.get(FIRST);
136 7691 tao
        //SystemMetadataEventListener listener = sysmetaListeners.get(FIRST);
137
        IndexGenerator generator = new IndexGenerator(index);
138 7613 tao
        Thread indexThread = new Thread(generator);
139
        indexThread.start();
140
    }
141 7542 tao
}