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 7783 tao
import java.io.File;
30 7811 leinfelder
import java.io.FileNotFoundException;
31 7613 tao
import java.util.ArrayList;
32 7542 tao
import java.util.List;
33 7788 tao
import java.util.Timer;
34 7542 tao
35 7783 tao
import org.apache.commons.configuration.ConfigurationException;
36 7542 tao
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38 7589 leinfelder
import org.apache.solr.client.solrj.SolrServer;
39 7783 tao
import org.dataone.configuration.Settings;
40
import org.dataone.service.exceptions.ServiceFailure;
41 7542 tao
import org.springframework.context.ApplicationContext;
42
import org.springframework.context.support.FileSystemXmlApplicationContext;
43
44 7788 tao
import com.ibm.icu.util.Calendar;
45 7783 tao
46 7811 leinfelder
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
47
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
48
49 7542 tao
/**
50
 * The start class of the index.
51
 * @author tao
52
 *
53
 */
54 7776 tao
public class ApplicationController implements Runnable {
55 7542 tao
56 7788 tao
    private static long DEFAULTINTERVAL = 7200000;
57 7542 tao
    private static String SOLRINDEXES = "solrIndexes";
58 7613 tao
    private static short FIRST = 0;
59 7589 leinfelder
60 7542 tao
    private List<SolrIndex> solrIndexes = null;
61 7776 tao
    private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>();
62 7542 tao
    private static ApplicationContext context = null;
63
    private String springConfigFile = "src/main/resources/index-processor-context.xml";
64 7783 tao
    private String metacatPropertiesFile = null;
65
    private static int waitingTime = IndexGenerator.WAITTIME;
66
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
67 7788 tao
    private static long period = DEFAULTINTERVAL;
68 7542 tao
    Log log = LogFactory.getLog(ApplicationController.class);
69
70 7613 tao
71 7542 tao
    /**
72
     * Constructor
73
     */
74 7783 tao
    /*public ApplicationController () throws Exception {
75 7542 tao
        init();
76 7783 tao
    }*/
77 7542 tao
78 7552 tao
    /**
79 7783 tao
     * Set the Spring configuration file and metacat.properties file
80 7552 tao
     * @param springConfigFile  the path of the Spring configuration file
81 7783 tao
     * @param metacatPropertyFile  the path of the metacat.properties file
82 7552 tao
     */
83 7783 tao
    public ApplicationController(String springConfigFile, String metacatPropertiesFile) throws Exception {
84 7552 tao
        this.springConfigFile = springConfigFile;
85 7783 tao
        this.metacatPropertiesFile = metacatPropertiesFile;
86
        //init();
87 7552 tao
    }
88
89 7560 leinfelder
    /**
90 7783 tao
     * Loads the metacat.prioerties into D1 Settings utility
91
     * this gives us access to all metacat properties as well as
92
     * overriding any properties as needed.
93
     * Note: in the junit test, we are using the test.properties rather than this properties file.
94
     *
95
     * Makes sure shared Hazelcast configuration file location is set
96
     */
97
    private void initializeSharedConfiguration() {
98
        int times = 0;
99 7788 tao
        boolean foundProperty = false;
100 7783 tao
        while(true) {
101
            File metacatProperties = new File(metacatPropertiesFile);
102
            if(metacatProperties.exists()) {
103 7788 tao
                foundProperty = true;
104 7783 tao
                break;
105
            } else {
106
                try {
107
                        Thread.sleep(waitingTime);
108
                } catch (InterruptedException e) {
109
                            // TODO Auto-generated catch block
110
                        e.printStackTrace();
111
                }
112
            }
113
            times++;
114
            if(times >= maxAttempts) {
115
                log.error("ApplicationController.initialzeSharedConfiguration - MetacatIndex wait a while and still can't find the metacat.properties file.");
116
                break;//we still break the while loop and continue. But the properties in the metacat.properties can't be read.
117
            }
118
119
        }
120
121
        try {
122
            Settings.getConfiguration();
123
            Settings.augmentConfiguration(metacatPropertiesFile);
124
        } catch (ConfigurationException e) {
125
            log.error("Could not initialize shared Metacat properties. " + e.getMessage(), e);
126
        }
127
128
        // make sure hazelcast configuration is defined so that
129
        String hzConfigFileName = Settings.getConfiguration().getString("dataone.hazelcast.configFilePath");
130
        if (hzConfigFileName == null) {
131
            // use default metacat hazelcast.xml file in metacat deployment
132
            hzConfigFileName =
133
                    Settings.getConfiguration().getString("application.deployDir") +
134
                    "/" +
135
                    Settings.getConfiguration().getString("application.context") +
136
                    "/WEB-INF/hazelcast.xml";
137
            // set it for other parts of the code
138
            Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", hzConfigFileName);
139
            //set data.hazelcast.location.clientconfig. This property will be used in d1_cn_index_processor module.
140
            //if we don't set this property, d1_cn_index_processor will use the default location /etc/dataone/storage.
141
            Settings.getConfiguration().setProperty("dataone.hazelcast.location.clientconfig", hzConfigFileName);
142
        }
143 7788 tao
        if(foundProperty) {
144
            period = Settings.getConfiguration().getLong("index.regenerate.interval");
145
        }
146
147 7783 tao
    }
148
149
    /**
150 7589 leinfelder
     * Initialize the list of the SolrIndex objects from the configuration file.
151
     * Set the SolrServer implementation using the factory.
152 7542 tao
     */
153 7783 tao
    public void initialize() throws Exception {
154 7542 tao
        context = getContext();
155
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
156
157 7591 leinfelder
        // use factory to create the correct impl
158 7589 leinfelder
    	SolrServer solrServer = null;
159 7591 leinfelder
		try {
160
			solrServer = SolrServerFactory.createSolrServer();
161
		} catch (Exception e) {
162
			log.error("Could not create SolrServer form factory", e);
163 7613 tao
			throw e;
164 7591 leinfelder
		}
165 7589 leinfelder
166 7560 leinfelder
        // start the SystemMetadata listener[s] (only expect there to be one)
167
        for (SolrIndex solrIndex: solrIndexes) {
168 7589 leinfelder
        	// set the solr server to use
169
			solrIndex.setSolrServer(solrServer);
170
171
			// start listening for events
172 7560 leinfelder
        	SystemMetadataEventListener smel = new SystemMetadataEventListener();
173
        	smel.setSolrIndex(solrIndex);
174 7776 tao
        	sysmetaListeners.add(smel);
175
        	//smel.start();
176 7560 leinfelder
        }
177
178 7542 tao
    }
179
180 7589 leinfelder
    /**
181 7542 tao
     * Get the ApplicaionContext of Spring.
182
     */
183
    private ApplicationContext getContext() {
184
        if (context == null) {
185
            context = new FileSystemXmlApplicationContext(springConfigFile);
186
        }
187
        return context;
188
    }
189
190
    /**
191
     * Get the path of the Spring configuration file.
192
     * @return the path of the Spring configuration file.
193
     */
194
    public String getSpringConfigFile() {
195
        return this.springConfigFile;
196
    }
197
198
    /**
199
     * Get the list of the solr index.
200
     * @return the list of the solr index.
201
     */
202
    public List<SolrIndex> getSolrIndexes() {
203
        return this.solrIndexes;
204
    }
205 7613 tao
206
207
    /**
208
     * Start to generate indexes for those haven't been indexed in another thread.
209 7788 tao
     * It will create a timer to run this task periodically.
210
     * If the property of "index.regenerate.interval" is less than 0, the thread would NOT run.
211 7613 tao
     */
212 7776 tao
    private void startIndex() {
213 7788 tao
        if(period > 0) {
214
            SolrIndex index = solrIndexes.get(FIRST);
215
            //SystemMetadataEventListener listener = sysmetaListeners.get(FIRST);
216
            IndexGenerator generator = new IndexGenerator(index);
217
            //Thread indexThread = new Thread(generator);
218
            //indexThread.start();
219
            Timer indexTimer = new Timer();
220 7866 tao
            //indexTimer.scheduleAtFixedRate(generator, Calendar.getInstance().getTime(), period);
221
            indexTimer.schedule(generator, Calendar.getInstance().getTime(), period);
222 7788 tao
        }
223
224 7613 tao
    }
225 7776 tao
226
    /**
227 7783 tao
     * Start the system metadata listener. Prior to call this method, we should call
228
     * initialize method first.
229 7811 leinfelder
     * @throws ServiceFailure
230
     * @throws FileNotFoundException
231 7776 tao
     */
232 7811 leinfelder
    public void startSysmetaListener() throws FileNotFoundException, ServiceFailure {
233 7776 tao
        if(sysmetaListeners != null) {
234
            //only expects one listener.
235
            for(SystemMetadataEventListener listener : sysmetaListeners) {
236
                if(listener != null) {
237
                    listener.start();
238
                }
239
            }
240
        }
241
    }
242
243
    /**
244 7783 tao
     * It will initialize the shared metacat.properties and the SolrIndex, then start system metadata event listener and
245
     *  generate indexes for those haven't been indexed in another thread. This method is for the MetacatIndexServlet.
246
     *  The reason we put those methods (init, startSysmetaListener, startIndex)in the run (another thread) is that the waiting readiness of the metacat wouldn't
247
     *  block the start of the servlet container (e.g., tomcat).
248 7776 tao
     */
249
    public void run() {
250 7783 tao
        initializeSharedConfiguration();
251
        try {
252
            boolean isSolrEnabled = true;
253
            try {
254
               isSolrEnabled = EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE);
255
            } catch (Exception e) {
256
                log.error("ApplicationController.run - Metacat-index can't read the enabled query engine list from metacat.properties :"+e.getMessage());
257
            }
258
            //if the solr query engine is disabled, we stop here.
259
            if(!isSolrEnabled) {
260
                return;
261
            }
262
            initialize();
263
            startSysmetaListener();
264
            startIndex();//it will create another thread.
265
        } catch (Exception e) {
266
            log.error("Application.run "+e.getMessage());
267
        }
268
269 7776 tao
    }
270 7542 tao
}