Project

General

Profile

« Previous | Next » 

Revision 7783

Added by Jing Tao over 11 years ago

Add code to check if the metacat.properties is available.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/ApplicationController.java
27 27
package edu.ucsb.nceas.metacat.index;
28 28

  
29 29
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
30
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
31

  
32
import java.io.File;
30 33
import java.util.ArrayList;
31 34
import java.util.List;
32 35

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

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

  
48
import com.hazelcast.client.HazelcastClient;
49

  
39 50
/**
40 51
 * The start class of the index.
41 52
 * @author tao
......
50 61
    private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>();
51 62
    private static ApplicationContext context = null;
52 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;
53 67
    Log log = LogFactory.getLog(ApplicationController.class);
54 68
    
55 69
    
56 70
    /**
57 71
     * Constructor
58 72
     */
59
    public ApplicationController () throws Exception {
73
    /*public ApplicationController () throws Exception {
60 74
        init();
61
    }
75
    }*/
62 76
    
63 77
    /**
64
     * Set the Spring configuration file.
78
     * Set the Spring configuration file and metacat.properties file
65 79
     * @param springConfigFile  the path of the Spring configuration file
80
     * @param metacatPropertyFile  the path of the metacat.properties file
66 81
     */
67
    public ApplicationController(String springConfigFile) throws Exception {
82
    public ApplicationController(String springConfigFile, String metacatPropertiesFile) throws Exception {
68 83
        this.springConfigFile = springConfigFile;
69
        init();
84
        this.metacatPropertiesFile = metacatPropertiesFile;
85
        //init();
70 86
    }
71 87
    
72 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
    /**
73 143
     * Initialize the list of the SolrIndex objects from the configuration file.
74 144
     * Set the SolrServer implementation using the factory.
75
     * Start listening for events on Hazelcast
76 145
     */
77
    private void init() throws Exception {
146
    public void initialize() throws Exception {
78 147
        context = getContext();
79 148
        solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES);
80 149
        
......
140 209
    }
141 210
    
142 211
    /**
143
     * Start the system metadata listener
212
     * Start the system metadata listener. Prior to call this method, we should call
213
     * initialize method first.
144 214
     */
145 215
    public void startSysmetaListener() {
146 216
        if(sysmetaListeners != null) {
......
154 224
    }
155 225
    
156 226
    /**
157
     * It will start system metadata event listener first. Then to generate indexes for those haven't been indexed in another thread.
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).
158 231
     */
159 232
    public void run() {
160
        startSysmetaListener();
161
        startIndex();//it will create another thread.
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
        
162 252
    }
163 253
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/MetacatIndexServlet.java
65 65
     */
66 66
    public void init(ServletConfig config) throws ServletException {
67 67
        //System.out.println("++++++++++++++++++++++++------------------- start the servlet");
68
    	
69
    	initializeSharedConfiguration(config);
70
    	boolean isSolrEnabled = true;
71
    	try {
72
    	   isSolrEnabled = EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE);
73
    	} catch (Exception e) {
74
    	    throw new ServletException("MetacatIndexServlet.init - Metacat-index can't read the enabled query engine list from metacat.properties :"+e.getMessage());
75
    	}
76
    	//if the solr query engine is disabled, we stop here.
77
    	if(!isSolrEnabled) {
78
    	    return;
79
    	}
68
    	//initializeSharedConfiguration(config);
80 69
    	// initialize the application using the configured application-context
81 70
        URL url = getClass().getResource("/index-processor-context.xml");
71
        //find the sibling metacat.properties file
72
        String metacatPropertiesFilePath = config.getServletContext().getInitParameter("metacat.properties.path");
73
        File contextDeploymentDir = new File(config.getServletContext().getRealPath("/"));
74
        String fullMetacatPropertiesFilePath = contextDeploymentDir.getParent()  + metacatPropertiesFilePath;
82 75
        //System.out.println("the url is "+url);
83 76
        //System.out.println("the path is "+url.getPath());
84 77
        //System.out.println("the file is "+url.getPath());
85
        ApplicationController controller = null;
78
        //ApplicationController controller = null;
86 79
        try {
87
             controller = new ApplicationController(FILEPREFIX + url.getFile());
88
             //Start SystemmetadataEventListener and to generate indexes for those haven't been indexed in another thread
89
             Thread sysListenerAndindexThread = new Thread(controller);
90
             sysListenerAndindexThread.start();
80
             ApplicationController controller = new ApplicationController(FILEPREFIX + url.getFile(), fullMetacatPropertiesFilePath);
81
             //Start the controller in other thread - SystemmetadataEventListener and to generate indexes for those haven't been indexed in another thread
82
             Thread controllerThread = new Thread(controller);
83
             controllerThread.start();
91 84
        } catch (Exception e) {
92 85
            throw new ServletException(e.getMessage());
93 86
        }
......
106 99
     * 
107 100
     * @param config the servlet config
108 101
     */
109
    private void initializeSharedConfiguration(ServletConfig config) {
102
    /*private void initializeSharedConfiguration(ServletConfig config) {
110 103
    	
111 104
		try {
112 105
			// find the sibling metacat.properties file
......
133 126
			//if we don't set this property, d1_cn_index_processor will use the default location /etc/dataone/storage.
134 127
			Settings.getConfiguration().setProperty("dataone.hazelcast.location.clientconfig", hzConfigFileName);
135 128
		}
136
    }
129
    }*/
137 130
    
138 131
    /**
139 132
     *Actions needed to be done before close the servlet

Also available in: Unified diff