Revision 7613
Added by Jing Tao over 11 years ago
metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/ApplicationControllerIT.java | ||
---|---|---|
14 | 14 |
* Test lookup from default properties file |
15 | 15 |
*/ |
16 | 16 |
@Test |
17 |
public void testConstructor() { |
|
17 |
public void testConstructor() throws Exception {
|
|
18 | 18 |
ApplicationController controller = new ApplicationController(); |
19 | 19 |
List<SolrIndex> list = controller.getSolrIndexes(); |
20 | 20 |
assertTrue(list.size() == 1); |
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/ApplicationController.java | ||
---|---|---|
26 | 26 |
*/ |
27 | 27 |
package edu.ucsb.nceas.metacat.index; |
28 | 28 |
|
29 |
import java.util.ArrayList; |
|
29 | 30 |
import java.util.List; |
30 | 31 |
|
31 | 32 |
import org.apache.commons.logging.Log; |
... | ... | |
42 | 43 |
public class ApplicationController { |
43 | 44 |
|
44 | 45 |
private static String SOLRINDEXES = "solrIndexes"; |
46 |
private static short FIRST = 0; |
|
45 | 47 |
|
46 | 48 |
private List<SolrIndex> solrIndexes = null; |
49 |
private List<SystemMetadataEventListener> sysmetaListeners = new ArrayList<SystemMetadataEventListener>(); |
|
47 | 50 |
private static ApplicationContext context = null; |
48 | 51 |
private String springConfigFile = "src/main/resources/index-processor-context.xml"; |
49 | 52 |
Log log = LogFactory.getLog(ApplicationController.class); |
50 | 53 |
|
54 |
|
|
51 | 55 |
/** |
52 | 56 |
* Constructor |
53 | 57 |
*/ |
54 |
public ApplicationController () { |
|
58 |
public ApplicationController () throws Exception {
|
|
55 | 59 |
init(); |
56 | 60 |
} |
57 | 61 |
|
... | ... | |
59 | 63 |
* Set the Spring configuration file. |
60 | 64 |
* @param springConfigFile the path of the Spring configuration file |
61 | 65 |
*/ |
62 |
public ApplicationController(String springConfigFile) { |
|
66 |
public ApplicationController(String springConfigFile) throws Exception {
|
|
63 | 67 |
this.springConfigFile = springConfigFile; |
64 | 68 |
init(); |
65 | 69 |
} |
... | ... | |
69 | 73 |
* Set the SolrServer implementation using the factory. |
70 | 74 |
* Start listening for events on Hazelcast |
71 | 75 |
*/ |
72 |
private void init() { |
|
76 |
private void init() throws Exception {
|
|
73 | 77 |
context = getContext(); |
74 | 78 |
solrIndexes = (List<SolrIndex>) context.getBean(SOLRINDEXES); |
75 | 79 |
|
... | ... | |
79 | 83 |
solrServer = SolrServerFactory.createSolrServer(); |
80 | 84 |
} catch (Exception e) { |
81 | 85 |
log.error("Could not create SolrServer form factory", e); |
86 |
throw e; |
|
82 | 87 |
} |
83 | 88 |
|
84 | 89 |
// start the SystemMetadata listener[s] (only expect there to be one) |
... | ... | |
90 | 95 |
SystemMetadataEventListener smel = new SystemMetadataEventListener(); |
91 | 96 |
smel.setSolrIndex(solrIndex); |
92 | 97 |
smel.start(); |
98 |
sysmetaListeners.add(smel); |
|
93 | 99 |
} |
94 | 100 |
|
95 | 101 |
} |
... | ... | |
119 | 125 |
public List<SolrIndex> getSolrIndexes() { |
120 | 126 |
return this.solrIndexes; |
121 | 127 |
} |
128 |
|
|
129 |
|
|
130 |
/** |
|
131 |
* Start to generate indexes for those haven't been indexed in another thread. |
|
132 |
*/ |
|
133 |
public void startIndex() { |
|
134 |
SolrIndex index = solrIndexes.get(FIRST); |
|
135 |
SystemMetadataEventListener listener = sysmetaListeners.get(FIRST); |
|
136 |
IndexGenerator generator = new IndexGenerator(index, listener); |
|
137 |
Thread indexThread = new Thread(generator); |
|
138 |
indexThread.start(); |
|
139 |
} |
|
122 | 140 |
} |
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/MetacatIndexServlet.java | ||
---|---|---|
71 | 71 |
//System.out.println("the url is "+url); |
72 | 72 |
//System.out.println("the path is "+url.getPath()); |
73 | 73 |
//System.out.println("the file is "+url.getPath()); |
74 |
ApplicationController controller = new ApplicationController(FILEPREFIX + url.getFile()); |
|
75 |
List<SolrIndex> list = controller.getSolrIndexes(); |
|
74 |
ApplicationController controller = null; |
|
75 |
try { |
|
76 |
controller = new ApplicationController(FILEPREFIX + url.getFile()); |
|
77 |
} catch (Exception e) { |
|
78 |
throw new ServletException(e.getMessage()); |
|
79 |
} |
|
80 |
|
|
81 |
controller.startIndex();//Start to generate indexes for those haven't been indexed in another thread |
|
82 |
//List<SolrIndex> list = controller.getSolrIndexes(); |
|
76 | 83 |
//System.out.println("++++++++++++++++++++++++------------------- the size is "+list.size()); |
77 | 84 |
} |
78 | 85 |
|
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/IndexGenerator.java | ||
---|---|---|
57 | 57 |
* @author tao |
58 | 58 |
* |
59 | 59 |
*/ |
60 |
public class IndexGenerator { |
|
60 |
public class IndexGenerator implements Runnable {
|
|
61 | 61 |
|
62 | 62 |
private static final String HTTP = "http://"; |
63 | 63 |
private static final String MNAPPENDIX = "/d1/mn"; |
... | ... | |
170 | 170 |
} |
171 | 171 |
} |
172 | 172 |
|
173 |
public void run() { |
|
174 |
try { |
|
175 |
indexAll(); |
|
176 |
} catch (InvalidRequest e) { |
|
177 |
// TODO Auto-generated catch block |
|
178 |
//e.printStackTrace(); |
|
179 |
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage()); |
|
180 |
} catch (InvalidToken e) { |
|
181 |
// TODO Auto-generated catch block |
|
182 |
//e.printStackTrace(); |
|
183 |
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage()); |
|
184 |
} catch (NotAuthorized e) { |
|
185 |
// TODO Auto-generated catch block |
|
186 |
//e.printStackTrace(); |
|
187 |
} catch (NotImplemented e) { |
|
188 |
// TODO Auto-generated catch block |
|
189 |
//e.printStackTrace(); |
|
190 |
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage()); |
|
191 |
} catch (ServiceFailure e) { |
|
192 |
// TODO Auto-generated catch block |
|
193 |
//e.printStackTrace(); |
|
194 |
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage()); |
|
195 |
} catch (SolrServerException e) { |
|
196 |
// TODO Auto-generated catch block |
|
197 |
//e.printStackTrace(); |
|
198 |
log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage()); |
|
199 |
} |
|
200 |
} |
|
201 |
|
|
173 | 202 |
/* |
174 | 203 |
* Get the indexed ids list from the solr server. |
175 | 204 |
* An empty list will be returned if there is no ids. |
Also available in: Unified diff
Add code to generate index during the servlet initialization.