Project

General

Profile

1
/**
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
import java.io.File;
30
import java.io.FileNotFoundException;
31
import java.util.ArrayList;
32
import java.util.List;
33
import java.util.Timer;
34

    
35
import org.apache.commons.configuration.ConfigurationException;
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38
import org.apache.solr.client.solrj.SolrServer;
39
import org.dataone.configuration.Settings;
40
import org.dataone.service.exceptions.ServiceFailure;
41
import org.springframework.context.ApplicationContext;
42
import org.springframework.context.support.FileSystemXmlApplicationContext;
43

    
44
import com.ibm.icu.util.Calendar;
45

    
46
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
47
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
48

    
49
/**
50
 * The start class of the index.
51
 * @author tao
52
 *
53
 */
54
public class ApplicationController implements Runnable {
55
    
56
    private static long DEFAULTINTERVAL = 7200000;
57
    private static String SOLRINDEXES = "solrIndexes";
58
    private static short FIRST = 0;
59

    
60
    private List<SolrIndex> solrIndexes = null;
61
    private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>();
62
    private static ApplicationContext context = null;
63
    private String springConfigFile = "src/main/resources/index-processor-context.xml";
64
    private String metacatPropertiesFile = null;
65
    private static int waitingTime = IndexGenerator.WAITTIME;
66
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
67
    private static long period = DEFAULTINTERVAL;
68
    Log log = LogFactory.getLog(ApplicationController.class);
69
    
70
    
71
    /**
72
     * Constructor
73
     */
74
    /*public ApplicationController () throws Exception {
75
        init();
76
    }*/
77
    
78
    /**
79
     * Set the Spring configuration file and metacat.properties file
80
     * @param springConfigFile  the path of the Spring configuration file
81
     * @param metacatPropertyFile  the path of the metacat.properties file
82
     */
83
    public ApplicationController(String springConfigFile, String metacatPropertiesFile) throws Exception {
84
        this.springConfigFile = springConfigFile;
85
        this.metacatPropertiesFile = metacatPropertiesFile;
86
        //init();
87
    }
88
    
89
    /**
90
     * 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
        boolean foundProperty = false;
100
        while(true) {
101
            File metacatProperties = new File(metacatPropertiesFile);
102
            if(metacatProperties.exists()) {
103
                foundProperty = true;
104
                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
        if(foundProperty) {
144
            period = Settings.getConfiguration().getLong("index.regenerate.interval");
145
        }
146
        
147
    }
148
    
149
    /**
150
     * Initialize the list of the SolrIndex objects from the configuration file.
151
     * Set the SolrServer implementation using the factory.
152
     */
153
    public void initialize() throws Exception {
154
        context = getContext();
155
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
156
        
157
        // use factory to create the correct impl
158
    	SolrServer solrServer = null;
159
		try {
160
			solrServer = SolrServerFactory.createSolrServer();
161
		} catch (Exception e) {
162
			log.error("Could not create SolrServer form factory", e);
163
			throw e;
164
		}
165

    
166
        // start the SystemMetadata listener[s] (only expect there to be one)
167
        for (SolrIndex solrIndex: solrIndexes) {
168
        	// set the solr server to use
169
			solrIndex.setSolrServer(solrServer);
170
			
171
			// start listening for events
172
        	SystemMetadataEventListener smel = new SystemMetadataEventListener();
173
        	smel.setSolrIndex(solrIndex);
174
        	sysmetaListeners.add(smel);
175
        	//smel.start();
176
        }
177
        
178
    }
179
    
180
    /**
181
     * 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
    
206
    
207
    /**
208
     * Start to generate indexes for those haven't been indexed in another thread.
209
     * 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
     */
212
    private void startIndex() {
213
        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
            //indexTimer.scheduleAtFixedRate(generator, Calendar.getInstance().getTime(), period);
221
            indexTimer.schedule(generator, Calendar.getInstance().getTime(), period);
222
        }
223
        
224
    }
225
    
226
    /**
227
     * Start the system metadata listener. Prior to call this method, we should call
228
     * initialize method first.
229
     * @throws ServiceFailure 
230
     * @throws FileNotFoundException 
231
     */
232
    public void startSysmetaListener() throws FileNotFoundException, ServiceFailure {
233
        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
     * 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
     */
249
    public void run() {
250
        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
    }
270
}
(1-1/6)