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 edu.ucsb.nceas.metacat.common.SolrServerFactory;
30
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
31

    
32
import java.io.File;
33
import java.util.ArrayList;
34
import java.util.List;
35
import java.util.Timer;
36

    
37
import javax.servlet.ServletConfig;
38
import javax.servlet.ServletException;
39

    
40
import org.apache.commons.configuration.ConfigurationException;
41
import org.apache.commons.logging.Log;
42
import org.apache.commons.logging.LogFactory;
43
import org.apache.solr.client.solrj.SolrServer;
44
import org.dataone.configuration.Settings;
45
import org.dataone.service.exceptions.ServiceFailure;
46
import org.springframework.context.ApplicationContext;
47
import org.springframework.context.support.FileSystemXmlApplicationContext;
48

    
49
import com.hazelcast.client.HazelcastClient;
50
import com.ibm.icu.util.Calendar;
51

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

    
63
    private List<SolrIndex> solrIndexes = null;
64
    private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>();
65
    private static ApplicationContext context = null;
66
    private String springConfigFile = "src/main/resources/index-processor-context.xml";
67
    private String metacatPropertiesFile = null;
68
    private static int waitingTime = IndexGenerator.WAITTIME;
69
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
70
    private static long period = DEFAULTINTERVAL;
71
    Log log = LogFactory.getLog(ApplicationController.class);
72
    
73
    
74
    /**
75
     * Constructor
76
     */
77
    /*public ApplicationController () throws Exception {
78
        init();
79
    }*/
80
    
81
    /**
82
     * Set the Spring configuration file and metacat.properties file
83
     * @param springConfigFile  the path of the Spring configuration file
84
     * @param metacatPropertyFile  the path of the metacat.properties file
85
     */
86
    public ApplicationController(String springConfigFile, String metacatPropertiesFile) throws Exception {
87
        this.springConfigFile = springConfigFile;
88
        this.metacatPropertiesFile = metacatPropertiesFile;
89
        //init();
90
    }
91
    
92
    /**
93
     * Loads the metacat.prioerties into D1 Settings utility
94
     * this gives us access to all metacat properties as well as 
95
     * overriding any properties as needed. 
96
     * Note: in the junit test, we are using the test.properties rather than this properties file.
97
     * 
98
     * Makes sure shared Hazelcast configuration file location is set
99
     */
100
    private void initializeSharedConfiguration() {
101
        int times = 0;
102
        boolean foundProperty = false;
103
        while(true) {
104
            File metacatProperties = new File(metacatPropertiesFile);
105
            if(metacatProperties.exists()) {
106
                foundProperty = true;
107
                break;
108
            } else {
109
                try {
110
                        Thread.sleep(waitingTime);
111
                } catch (InterruptedException e) {
112
                            // TODO Auto-generated catch block
113
                        e.printStackTrace();
114
                }
115
            }
116
            times++;
117
            if(times >= maxAttempts) {
118
                log.error("ApplicationController.initialzeSharedConfiguration - MetacatIndex wait a while and still can't find the metacat.properties file.");
119
                break;//we still break the while loop and continue. But the properties in the metacat.properties can't be read.
120
            }
121
                
122
        }
123
        
124
        try {
125
            Settings.getConfiguration();
126
            Settings.augmentConfiguration(metacatPropertiesFile);
127
        } catch (ConfigurationException e) {
128
            log.error("Could not initialize shared Metacat properties. " + e.getMessage(), e);
129
        }
130
        
131
        // make sure hazelcast configuration is defined so that
132
        String hzConfigFileName = Settings.getConfiguration().getString("dataone.hazelcast.configFilePath");
133
        if (hzConfigFileName == null) {
134
            // use default metacat hazelcast.xml file in metacat deployment
135
            hzConfigFileName = 
136
                    Settings.getConfiguration().getString("application.deployDir") +
137
                    "/" +
138
                    Settings.getConfiguration().getString("application.context") + 
139
                    "/WEB-INF/hazelcast.xml";
140
            // set it for other parts of the code
141
            Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", hzConfigFileName);
142
            //set data.hazelcast.location.clientconfig. This property will be used in d1_cn_index_processor module.
143
            //if we don't set this property, d1_cn_index_processor will use the default location /etc/dataone/storage.
144
            Settings.getConfiguration().setProperty("dataone.hazelcast.location.clientconfig", hzConfigFileName);
145
        }
146
        if(foundProperty) {
147
            period = Settings.getConfiguration().getLong("index.regenerate.interval");
148
        }
149
        
150
    }
151
    
152
    /**
153
     * Initialize the list of the SolrIndex objects from the configuration file.
154
     * Set the SolrServer implementation using the factory.
155
     */
156
    public void initialize() throws Exception {
157
        context = getContext();
158
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
159
        
160
        // use factory to create the correct impl
161
    	SolrServer solrServer = null;
162
		try {
163
			solrServer = SolrServerFactory.createSolrServer();
164
		} catch (Exception e) {
165
			log.error("Could not create SolrServer form factory", e);
166
			throw e;
167
		}
168

    
169
        // start the SystemMetadata listener[s] (only expect there to be one)
170
        for (SolrIndex solrIndex: solrIndexes) {
171
        	// set the solr server to use
172
			solrIndex.setSolrServer(solrServer);
173
			
174
			// start listening for events
175
        	SystemMetadataEventListener smel = new SystemMetadataEventListener();
176
        	smel.setSolrIndex(solrIndex);
177
        	sysmetaListeners.add(smel);
178
        	//smel.start();
179
        }
180
        
181
    }
182
    
183
    /**
184
     * Get the ApplicaionContext of Spring.
185
     */
186
    private ApplicationContext getContext() {
187
        if (context == null) {
188
            context = new FileSystemXmlApplicationContext(springConfigFile);
189
        }
190
        return context;
191
    }
192

    
193
    /**
194
     * Get the path of the Spring configuration file.
195
     * @return the path of the Spring configuration file.
196
     */
197
    public String getSpringConfigFile() {
198
        return this.springConfigFile;
199
    }
200
    
201
    /**
202
     * Get the list of the solr index.
203
     * @return the list of the solr index.
204
     */
205
    public List<SolrIndex> getSolrIndexes() {
206
        return this.solrIndexes;
207
    }
208
    
209
    
210
    /**
211
     * Start to generate indexes for those haven't been indexed in another thread.
212
     * It will create a timer to run this task periodically. 
213
     * If the property of "index.regenerate.interval" is less than 0, the thread would NOT run.
214
     */
215
    private void startIndex() {
216
        if(period > 0) {
217
            SolrIndex index = solrIndexes.get(FIRST);
218
            //SystemMetadataEventListener listener = sysmetaListeners.get(FIRST);
219
            IndexGenerator generator = new IndexGenerator(index);
220
            //Thread indexThread = new Thread(generator);
221
            //indexThread.start();
222
            Timer indexTimer = new Timer();
223
            indexTimer.scheduleAtFixedRate(generator, Calendar.getInstance().getTime(), period);
224
        }
225
        
226
    }
227
    
228
    /**
229
     * Start the system metadata listener. Prior to call this method, we should call
230
     * initialize method first.
231
     */
232
    public void startSysmetaListener() {
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)