Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.index;
20

    
21
import java.io.File;
22
import java.io.FileNotFoundException;
23
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.Timer;
26

    
27
import org.apache.commons.configuration.ConfigurationException;
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.apache.solr.client.solrj.SolrServer;
31
import org.dataone.configuration.Settings;
32
import org.dataone.service.exceptions.ServiceFailure;
33
import org.springframework.context.ApplicationContext;
34
import org.springframework.context.support.ClassPathXmlApplicationContext;
35
import org.springframework.context.support.FileSystemXmlApplicationContext;
36

    
37
import com.ibm.icu.util.Calendar;
38

    
39
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
40
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
41

    
42
/**
43
 * The start class of the index.
44
 * @author tao
45
 *
46
 */
47
public class ApplicationController implements Runnable {
48
    
49
    private static long DEFAULTINTERVAL = 7200000;
50
    private static String SOLRINDEXES = "solrIndexes";
51
    private static short FIRST = 0;
52

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

    
159
        // start the SystemMetadata listener[s] (only expect there to be one)
160
        for (SolrIndex solrIndex: solrIndexes) {
161
        	// set the solr server to use
162
			solrIndex.setSolrServer(solrServer);
163
			
164
			// start listening for events
165
        	SystemMetadataEventListener smel = new SystemMetadataEventListener();
166
        	smel.setSolrIndex(solrIndex);
167
        	sysmetaListeners.add(smel);
168
        	//smel.start();
169
        }
170
        
171
    }
172
    
173
    /**
174
     * Get the ApplicaionContext of Spring.
175
     */
176
    private ApplicationContext getContext() {
177
        if (context == null) {
178
            context = new ClassPathXmlApplicationContext(springConfigFileURL);
179
        }
180
        return context;
181
    }
182

    
183
    /**
184
     * Get the path of the Spring configuration file.
185
     * @return the path of the Spring configuration file.
186
     */
187
    public String getSpringConfigFile() {
188
        return this.springConfigFileURL;
189
    }
190
    
191
    /**
192
     * Get the list of the solr index.
193
     * @return the list of the solr index.
194
     */
195
    public List<SolrIndex> getSolrIndexes() {
196
        return this.solrIndexes;
197
    }
198
    
199
    
200
    /**
201
     * Start to generate indexes for those haven't been indexed in another thread.
202
     * It will create a timer to run this task periodically. 
203
     * If the property of "index.regenerate.interval" is less than 0, the thread would NOT run.
204
     */
205
    private void startIndexGenerator() {
206
        if(period > 0) {
207
            SolrIndex index = solrIndexes.get(FIRST);
208
            //SystemMetadataEventListener listener = sysmetaListeners.get(FIRST);
209
            IndexGenerator generator = new IndexGenerator(index);
210
            //Thread indexThread = new Thread(generator);
211
            //indexThread.start();
212
            Timer indexTimer = new Timer();
213
            //indexTimer.scheduleAtFixedRate(generator, Calendar.getInstance().getTime(), period);
214
            indexTimer.schedule(generator, 60000, period);
215
        }
216
        
217
    }
218
    
219
    /**
220
     * Start the system metadata listener. Prior to call this method, we should call
221
     * initialize method first.
222
     * @throws ServiceFailure 
223
     * @throws FileNotFoundException 
224
     */
225
    private void startSysmetaListener() throws FileNotFoundException, ServiceFailure {
226
        if(sysmetaListeners != null) {
227
            //only expects one listener.
228
            for(SystemMetadataEventListener listener : sysmetaListeners) {
229
                if(listener != null) {
230
                    listener.start();
231
                }
232
            }
233
        }
234
    }
235
    
236
    /**
237
     * It will initialize the shared metacat.properties and the SolrIndex, then start system metadata event listener and 
238
     *  generate indexes for those haven't been indexed in another thread. This method is for the MetacatIndexServlet.
239
     *  The reason we put those methods (init, startSysmetaListener, startIndex)in the run (another thread) is that the waiting readiness of the metacat wouldn't 
240
     *  block the start of the servlet container (e.g., tomcat).
241
     */
242
    public void run() {
243
        initializeSharedConfiguration();
244
        try {
245
            boolean isSolrEnabled = true;
246
            try {
247
               isSolrEnabled = EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE);
248
            } catch (Exception e) {
249
                log.error("ApplicationController.run - Metacat-index can't read the enabled query engine list from metacat.properties :"+e.getMessage());
250
            }
251
            //if the solr query engine is disabled, we stop here.
252
            if(!isSolrEnabled) {
253
                return;
254
            }
255
            initialize();
256
            startSysmetaListener();
257
            startIndexGenerator();//it will create another thread.
258
        } catch (Exception e) {
259
            log.error("Application.run "+e.getMessage());
260
        }
261
        
262
    }
263
}
(1-1/6)