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

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

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

    
48
import com.hazelcast.client.HazelcastClient;
49

    
50
/**
51
 * The start class of the index.
52
 * @author tao
53
 *
54
 */
55
public class ApplicationController implements Runnable {
56
    
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
    Log log = LogFactory.getLog(ApplicationController.class);
68
    
69
    
70
    /**
71
     * Constructor
72
     */
73
    /*public ApplicationController () throws Exception {
74
        init();
75
    }*/
76
    
77
    /**
78
     * Set the Spring configuration file and metacat.properties file
79
     * @param springConfigFile  the path of the Spring configuration file
80
     * @param metacatPropertyFile  the path of the metacat.properties file
81
     */
82
    public ApplicationController(String springConfigFile, String metacatPropertiesFile) throws Exception {
83
        this.springConfigFile = springConfigFile;
84
        this.metacatPropertiesFile = metacatPropertiesFile;
85
        //init();
86
    }
87
    
88
    /**
89
     * Loads the metacat.prioerties into D1 Settings utility
90
     * this gives us access to all metacat properties as well as 
91
     * overriding any properties as needed. 
92
     * Note: in the junit test, we are using the test.properties rather than this properties file.
93
     * 
94
     * Makes sure shared Hazelcast configuration file location is set
95
     */
96
    private void initializeSharedConfiguration() {
97
        int times = 0;
98
        while(true) {
99
            File metacatProperties = new File(metacatPropertiesFile);
100
            if(metacatProperties.exists()) {
101
                break;
102
            } else {
103
                try {
104
                        Thread.sleep(waitingTime);
105
                } catch (InterruptedException e) {
106
                            // TODO Auto-generated catch block
107
                        e.printStackTrace();
108
                }
109
            }
110
            times++;
111
            if(times >= maxAttempts) {
112
                log.error("ApplicationController.initialzeSharedConfiguration - MetacatIndex wait a while and still can't find the metacat.properties file.");
113
                break;//we still break the while loop and continue. But the properties in the metacat.properties can't be read.
114
            }
115
                
116
        }
117
        
118
        try {
119
            Settings.getConfiguration();
120
            Settings.augmentConfiguration(metacatPropertiesFile);
121
        } catch (ConfigurationException e) {
122
            log.error("Could not initialize shared Metacat properties. " + e.getMessage(), e);
123
        }
124
        
125
        // make sure hazelcast configuration is defined so that
126
        String hzConfigFileName = Settings.getConfiguration().getString("dataone.hazelcast.configFilePath");
127
        if (hzConfigFileName == null) {
128
            // use default metacat hazelcast.xml file in metacat deployment
129
            hzConfigFileName = 
130
                    Settings.getConfiguration().getString("application.deployDir") +
131
                    "/" +
132
                    Settings.getConfiguration().getString("application.context") + 
133
                    "/WEB-INF/hazelcast.xml";
134
            // set it for other parts of the code
135
            Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", hzConfigFileName);
136
            //set data.hazelcast.location.clientconfig. This property will be used in d1_cn_index_processor module.
137
            //if we don't set this property, d1_cn_index_processor will use the default location /etc/dataone/storage.
138
            Settings.getConfiguration().setProperty("dataone.hazelcast.location.clientconfig", hzConfigFileName);
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 FileSystemXmlApplicationContext(springConfigFile);
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.springConfigFile;
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
     */
203
    private void startIndex() {
204
        SolrIndex index = solrIndexes.get(FIRST);
205
        //SystemMetadataEventListener listener = sysmetaListeners.get(FIRST);
206
        IndexGenerator generator = new IndexGenerator(index);
207
        Thread indexThread = new Thread(generator);
208
        indexThread.start();
209
    }
210
    
211
    /**
212
     * Start the system metadata listener. Prior to call this method, we should call
213
     * initialize method first.
214
     */
215
    public void startSysmetaListener() {
216
        if(sysmetaListeners != null) {
217
            //only expects one listener.
218
            for(SystemMetadataEventListener listener : sysmetaListeners) {
219
                if(listener != null) {
220
                    listener.start();
221
                }
222
            }
223
        }
224
    }
225
    
226
    /**
227
     * It will initialize the shared metacat.properties and the SolrIndex, then start system metadata event listener and 
228
     *  generate indexes for those haven't been indexed in another thread. This method is for the MetacatIndexServlet.
229
     *  The reason we put those methods (init, startSysmetaListener, startIndex)in the run (another thread) is that the waiting readiness of the metacat wouldn't 
230
     *  block the start of the servlet container (e.g., tomcat).
231
     */
232
    public void run() {
233
        initializeSharedConfiguration();
234
        try {
235
            boolean isSolrEnabled = true;
236
            try {
237
               isSolrEnabled = EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE);
238
            } catch (Exception e) {
239
                log.error("ApplicationController.run - Metacat-index can't read the enabled query engine list from metacat.properties :"+e.getMessage());
240
            }
241
            //if the solr query engine is disabled, we stop here.
242
            if(!isSolrEnabled) {
243
                return;
244
            }
245
            initialize();
246
            startSysmetaListener();
247
            startIndex();//it will create another thread.
248
        } catch (Exception e) {
249
            log.error("Application.run "+e.getMessage());
250
        }
251
        
252
    }
253
}
(1-1/6)