source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java@ 30550

Last change on this file since 30550 was 30550, checked in by Georgiy Litvinov, 8 years ago

At collection initialization stage added checks for activated Solr cores. In case Solr cores not loaded force Solr to load collection cores.

  • Property svn:keywords set to Author Date Id Revision
File size: 30.5 KB
Line 
1/*
2 * Collection.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.collection;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.util.ArrayList;
30import java.util.HashMap;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.apache.solr.client.solrj.SolrServerException;
35import org.apache.solr.client.solrj.impl.HttpSolrServer;
36import org.apache.solr.client.solrj.impl.HttpSolrServer.RemoteSolrException;
37import org.apache.solr.client.solrj.request.CoreAdminRequest;
38import org.apache.solr.client.solrj.response.CoreAdminResponse;
39import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;
40import org.apache.solr.common.util.NamedList;
41import org.greenstone.gsdl3.core.ModuleInterface;
42import org.greenstone.gsdl3.util.GSFile;
43import org.greenstone.gsdl3.util.GSXML;
44import org.greenstone.gsdl3.util.GSXSLT;
45import org.greenstone.gsdl3.util.OAIXML;
46import org.greenstone.gsdl3.util.SimpleMacroResolver;
47import org.greenstone.gsdl3.util.UserContext;
48import org.greenstone.gsdl3.util.XMLConverter;
49import org.greenstone.gsdl3.util.XMLTransformer;
50import org.w3c.dom.Document;
51import org.w3c.dom.Element;
52import org.w3c.dom.Node;
53import org.w3c.dom.NodeList;
54
55/**
56 * Represents a collection in Greenstone. A collection is an extension of a
57 * ServiceCluster - it has local data that the services use.
58 *
59 * @author Katherine Don
60 * @see ModuleInterface
61 */
62public class Collection extends ServiceCluster
63{
64
65 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
66
67 /** is this collection being tidied and therefore can support realistic book view? */
68 protected boolean useBook = false;
69 /**
70 * is this collection public or private - public collections will
71 * appear on the home page, whereas private collections won't
72 */
73 protected boolean is_public = true;
74 /** collection type : mg, mgpp or lucene */
75 protected String col_type = "";
76 /** database type : gdbm, jdbm or sqlite */
77 protected String db_type = "";
78 /** time when this collection was built Used by RSS */
79 protected long lastmodified = 0;
80 /** earliestDatestamp of this collection. Used by OAI/RSS */
81 protected long earliestDatestamp = 0;
82
83 /** Stores the default accessibility of guest users */
84 protected boolean _publicAccess = true;
85 /** Stores the scope of any security rules (either collection or document) */
86 protected boolean _securityScopeCollection = true;
87
88 protected HashMap<String, ArrayList<Element>> _documentSets = new HashMap<String, ArrayList<Element>>();
89 protected ArrayList<HashMap<String, ArrayList<String>>> _securityExceptions = new ArrayList<HashMap<String, ArrayList<String>>>();
90
91 protected XMLTransformer transformer = null;
92
93 /** same as setClusterName */
94 public void setCollectionName(String name)
95 {
96 setClusterName(name);
97 }
98
99 public Collection()
100 {
101 super();
102 this.description = this.desc_doc.createElement(GSXML.COLLECTION_ELEM);
103 }
104
105 /**
106 * Configures the collection.
107 *
108 * gsdlHome and collectionName must be set before configure is called.
109 *
110 * the file buildcfg.xml is located in gsdlHome/collect/collectionName
111 * collection metadata is obtained, and services loaded.
112 *
113 * @return true/false on success/fail
114 */
115 public boolean configure()
116 {
117 if (this.site_home == null || this.cluster_name == null)
118 {
119 logger.error("Collection: site_home and collection_name must be set before configure called!");
120 return false;
121 }
122
123 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
124
125 Element coll_config_xml = loadCollConfigFile();
126 if (coll_config_xml == null) {
127 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load collection config file");
128
129 return false;
130 }
131 Element build_config_xml = loadBuildConfigFile();
132
133 if (build_config_xml == null)
134 {
135 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
136
137 return false;
138 }
139
140 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
141 // get the collection type attribute
142 Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
143 if (search != null)
144 {
145 col_type = search.getAttribute(GSXML.TYPE_ATT);
146 //If use Solr check if cores loaded
147 if (col_type.equals("solr"))
148 {
149 if (!loadSolrCores()) {
150 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't activate Solr cores");
151 return false;
152 }
153 }
154 }
155
156 Element browse = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.INFODB_ELEM);
157 if (browse != null)
158 {
159 db_type = browse.getAttribute(GSXML.TYPE_ATT);
160 }
161 else
162 {
163 db_type = "gdbm"; //Default database type
164 }
165
166 this.description.setAttribute(GSXML.TYPE_ATT, col_type);
167 this.description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
168
169 _globalFormat = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.FORMAT_ELEM);
170 // process the metadata and display items and default library params
171 super.configureLocalData(coll_config_xml);
172 super.configureLocalData(build_config_xml);
173 // get extra collection specific stuff
174 findAndLoadInfo(coll_config_xml, build_config_xml);
175
176 loadSecurityInformation(coll_config_xml);
177
178 // now do the services
179 configureServiceRacks(coll_config_xml, build_config_xml);
180
181 return true;
182
183 }
184
185 public boolean useBook()
186 {
187 return useBook;
188 }
189
190 public boolean isPublic()
191 {
192 return is_public;
193 }
194
195 // Used by OAI Receptionist and RSSRetrieve
196 public long getLastmodified()
197 {
198 return lastmodified;
199 }
200
201 // used by the OAIReceptionist and RSSRetrieve
202 public long getEarliestDatestamp()
203 {
204 return earliestDatestamp;
205 }
206
207 /**
208 * load in the collection config file into a DOM Element
209 */
210 protected Element loadCollConfigFile()
211 {
212
213 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
214
215 if (!coll_config_file.exists())
216 {
217 return null;
218 }
219 // get the xml
220 Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
221 Element coll_config_elem = null;
222 if (coll_config_doc != null)
223 {
224 coll_config_elem = coll_config_doc.getDocumentElement();
225 }
226 return coll_config_elem;
227
228 }
229
230 /**
231 * load in the collection build config file into a DOM Element
232 */
233 protected Element loadBuildConfigFile()
234 {
235 File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
236 if (!build_config_file.exists())
237 {
238 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + build_config_file + " does not exist");
239 return null;
240 }
241 Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
242 Element build_config_elem = null;
243 if (build_config_doc != null)
244 {
245 build_config_elem = build_config_doc.getDocumentElement();
246 }
247
248 lastmodified = build_config_file.lastModified();
249
250 return build_config_elem;
251 }
252
253 /**
254 * find the metadata and display elems from the two config files and add it
255 * to the appropriate lists
256 */
257 protected boolean findAndLoadInfo(Element coll_config_xml, Element build_config_xml)
258 {
259 addMetadata("httpPath", this.site_http_address + "/collect/" + this.cluster_name);
260
261
262 //check whether the html are tidy or not
263 Element import_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
264 if (import_list != null)
265 {
266 Element plugin_list = (Element) GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
267 //addPlugins(plugin_list);
268 if (plugin_list != null)
269 {
270 Element plugin_elem = (Element) GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlugin");
271 if (plugin_elem != null)
272 {
273 //get the option
274 Element option_elem = (Element) GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-use_realistic_book");
275 if (option_elem != null)
276 {
277 useBook = true;
278 }
279 }
280 }
281 }
282 String tidy = (useBook == true ? "tidy" : "untidy");
283 addMetadata("tidyoption", tidy);
284
285
286 if (this.metadata_list != null)
287 {
288 // check whether we are public or not
289 Element meta_elem = (Element) GSXML.getNamedElement(this.metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
290 if (meta_elem != null)
291 {
292 String value = GSXML.getValue(meta_elem).toLowerCase().trim();
293 if (value.equals("false"))
294 {
295 is_public = false;
296 }
297 }
298 // earliest datestamp is the time the collection was created.
299 meta_elem = (Element) GSXML.getNamedElement(this.metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, OAIXML.EARLIEST_DATESTAMP);
300 if (meta_elem != null) {
301 String earliestDatestampStr = GSXML.getValue(meta_elem);
302 if (!earliestDatestampStr.equals("")) {
303 earliestDatestamp = Long.parseLong(earliestDatestampStr) * 1000; // stored in seconds, convert to milliseconds
304 }
305 }
306
307 }
308 return true;
309 }
310
311 protected void loadSecurityInformation(Element coll_config_xml)
312 {
313 Element securityBlock = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SECURITY_ELEM);
314
315 if (securityBlock == null)
316 {
317 return;
318 }
319
320 String disabled = securityBlock.getAttribute(GSXML.DISABLED_ATT);
321 if (!disabled.equals("")) {
322 // security block has been disabled.
323 logger.warn("Security block has been disabled. Not implementing any security for collection "+this.cluster_name);
324 return;
325 }
326 String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
327 String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
328
329 if (defaultAccess.toLowerCase().equals("public"))
330 {
331 _publicAccess = true;
332 }
333 else if (defaultAccess.toLowerCase().equals("private"))
334 {
335 _publicAccess = false;
336 }
337 else
338 {
339 logger.warn("Default access for collection " + this.cluster_name + " is neither public or private, assuming public");
340 }
341
342 if (scope.toLowerCase().equals("collection"))
343 {
344 _securityScopeCollection = true;
345 }
346 else if (scope.toLowerCase().equals("documents") || scope.toLowerCase().equals("document"))
347 {
348 _securityScopeCollection = false;
349 }
350 else
351 {
352 logger.warn("Security scope is neither collection or document, assuming collection");
353 }
354
355 NodeList exceptions = GSXML.getChildrenByTagName(securityBlock, GSXML.EXCEPTION_ELEM);
356
357 if (exceptions.getLength() > 0)
358 {
359 if (!_securityScopeCollection)
360 {
361 NodeList documentSetElems = GSXML.getChildrenByTagName(securityBlock, GSXML.DOCUMENT_SET_ELEM);
362 for (int i = 0; i < documentSetElems.getLength(); i++)
363 {
364 Element documentSet = (Element) documentSetElems.item(i);
365 String setName = documentSet.getAttribute(GSXML.NAME_ATT);
366 NodeList matchStatements = GSXML.getChildrenByTagName(documentSet, GSXML.MATCH_ELEM);
367 ArrayList<Element> matchStatementList = new ArrayList<Element>();
368 for (int j = 0; j < matchStatements.getLength(); j++)
369 {
370 matchStatementList.add((Element) matchStatements.item(j));
371 }
372 _documentSets.put(setName, matchStatementList);
373 }
374 }
375
376 for (int i = 0; i < exceptions.getLength(); i++)
377 {
378 HashMap<String, ArrayList<String>> securityException = new HashMap<String, ArrayList<String>>();
379 ArrayList<String> exceptionGroups = new ArrayList<String>();
380 ArrayList<String> exceptionSets = new ArrayList<String>();
381
382 Element exception = (Element) exceptions.item(i);
383 NodeList groups = GSXML.getChildrenByTagName(exception, GSXML.GROUP_ELEM);
384 for (int j = 0; j < groups.getLength(); j++)
385 {
386 Element group = (Element) groups.item(j);
387 String groupName = group.getAttribute(GSXML.NAME_ATT);
388 exceptionGroups.add(groupName);
389 }
390 NodeList docSets = GSXML.getChildrenByTagName(exception, GSXML.DOCUMENT_SET_ELEM);
391 for (int j = 0; j < docSets.getLength(); j++)
392 {
393 Element docSet = (Element) docSets.item(j);
394 String docSetName = docSet.getAttribute(GSXML.NAME_ATT);
395 exceptionSets.add(docSetName);
396 }
397 if (_securityScopeCollection) {
398 // we don't add in any exceptions that have document sets
399 if (!exceptionSets.isEmpty()) {
400 continue;
401 }
402 }
403 securityException.put("groups", exceptionGroups);
404 securityException.put("sets", exceptionSets);
405 _securityExceptions.add(securityException);
406 }
407 }
408 }
409
410 protected boolean configureServiceRacks(Element coll_config_xml, Element build_config_xml)
411 {
412 clearServices();
413 Element service_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
414 if (service_list != null)
415 {
416 configureServiceRackList(service_list, coll_config_xml);
417 }
418 // collection Config may also contain manually added service racks
419 service_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
420 if (service_list != null)
421 {
422 configureServiceRackList(service_list, build_config_xml);
423 }
424 return true;
425 }
426
427 /**
428 * do a configure on only part of the collection
429 */
430 protected boolean configureSubset(String subset)
431 {
432
433 // need the coll config files
434 Element coll_config_elem = loadCollConfigFile();
435 Element build_config_elem = loadBuildConfigFile();
436 if (coll_config_elem == null || build_config_elem == null)
437 {
438 // wont be able to do any of the requests
439 return false;
440 }
441
442 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
443 {
444 return configureServiceRacks(coll_config_elem, build_config_elem);
445 }
446
447 if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.LIBRARY_PARAM_ELEM+GSXML.LIST_MODIFIER))
448 {
449 configureLocalData(coll_config_elem);
450 configureLocalData(build_config_elem);
451 return findAndLoadInfo(coll_config_elem, build_config_elem);
452
453 }
454
455 logger.error("Collection: cant process system request, configure " + subset);
456 return false;
457 }
458
459 /**
460 * handles requests made to the ServiceCluster itself
461 *
462 * @param req
463 * - the request Element- <request>
464 * @return the result Element - should be <response>
465 */
466 protected Element processMessage(Document result_doc, Element request)
467 {
468 String type = request.getAttribute(GSXML.TYPE_ATT);
469 if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
470 {
471 return processFormatStringRequest(result_doc, request);
472 }
473 else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
474 {
475 return processSecurityRequest(result_doc, request);
476 }
477 else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
478 {
479
480 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
481 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
482 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
483 if (_globalFormat != null)
484 {
485 response.appendChild(result_doc.importNode(_globalFormat, true));
486 }
487 return response;
488 }
489 // unknown type
490 return super.processMessage(result_doc, request);
491
492 }
493
494 protected Element processSecurityRequest(Document result_doc, Element request)
495 {
496 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
497 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
498 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
499
500 String oid = request.getAttribute("oid");
501 if (oid.contains("."))
502 {
503 oid = oid.substring(0, oid.indexOf("."));
504 }
505
506 ArrayList<String> groups = getPermittedGroups(oid);
507
508 Element groupList = result_doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
509 response.appendChild(groupList);
510
511 for (String groupName : groups)
512 {
513 Element group = result_doc.createElement(GSXML.GROUP_ELEM);
514 groupList.appendChild(group);
515 group.setAttribute(GSXML.NAME_ATT, groupName);
516 }
517 return response;
518 }
519
520 protected ArrayList<String> getPermittedGroups(String oid)
521 {
522 ArrayList<String> groups = new ArrayList<String>();
523
524 if (_securityScopeCollection)
525 {
526 if (_publicAccess)
527 {
528 groups.add("");
529 }
530 else
531 {
532 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
533 {
534 for (String group : exception.get("groups"))
535 {
536 groups.add(group);
537 }
538 }
539 }
540 }
541 else
542 {
543 if (oid != null && !oid.equals(""))
544 {
545 boolean inSet = false;
546 for (HashMap<String, ArrayList<String>> exception : _securityExceptions) {
547
548 ArrayList<String> exceptionSets = exception.get("sets");
549 if (exceptionSets.size() == 0) {
550 inSet = true;
551 for (String group : exception.get("groups"))
552 {
553 groups.add(group);
554 }
555 }
556 else {
557 for (String setName : exception.get("sets"))
558 {
559 if (documentIsInSet(oid, setName))
560 {
561 inSet = true;
562 for (String group : exception.get("groups"))
563 {
564 groups.add(group);
565 }
566 break;
567 }
568 }
569 }
570 }
571
572
573
574 if (!inSet && _publicAccess)
575 {// our doc was not part of any exception, so it must be public
576 groups.add("");
577 }
578 }
579 else // if we are not doing a request with an oid, then free to access
580 {
581 groups.add("");
582 }
583 }
584
585 return groups;
586 }
587
588 protected boolean documentIsInSet(String oid, String setName)
589 {
590 ArrayList<Element> matchStatements = _documentSets.get(setName);
591 if (matchStatements == null || matchStatements.size() == 0)
592 {
593 return false;
594 }
595
596 for (Element currentMatchStatement : matchStatements)
597 {
598 String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
599 if (fieldName == null || fieldName.equals(""))
600 {
601 fieldName = "oid";
602 }
603
604 String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
605 if (type == null || type.equals(""))
606 {
607 type = "match";
608 }
609
610 String fieldValue = "";
611 if (!fieldName.equals("oid"))
612 {
613 fieldValue = getFieldValue(oid, fieldName);
614 if (fieldValue == null)
615 {
616 return false;
617 }
618 }
619 else
620 {
621 fieldValue = oid;
622 }
623
624 String matchValue = GSXML.getNodeText(currentMatchStatement);
625 if (type.equals("match"))
626 {
627 if (matchValue.equals(fieldValue))
628 {
629 return true;
630 }
631 }
632 else if (type.equals("regex"))
633 {
634 if (fieldValue.matches(matchValue))
635 {
636 return true;
637 }
638 }
639 else
640 {
641 logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
642 }
643 }
644
645 return false;
646 }
647
648 protected String getFieldValue(String oid, String fieldName)
649 {
650 Document msg_doc = XMLConverter.newDOM();
651 Element metadataMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
652 Element metadataRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
653 metadataMessage.appendChild(metadataRequest);
654
655 Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
656 metadataRequest.appendChild(paramList);
657
658 Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
659 paramList.appendChild(param);
660
661 param.setAttribute(GSXML.NAME_ATT, "metadata");
662 param.setAttribute(GSXML.VALUE_ATT, fieldName);
663
664 Element docList = msg_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
665 metadataRequest.appendChild(docList);
666
667 Element doc = msg_doc.createElement(GSXML.DOC_NODE_ELEM);
668 docList.appendChild(doc);
669
670 doc.setAttribute(GSXML.NODE_ID_ATT, oid);
671
672 Element response = (Element) this.router.process(metadataMessage);
673 NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
674
675 if (metadataElems.getLength() > 0)
676 {
677 Element metadata = (Element) metadataElems.item(0);
678 return GSXML.getNodeText(metadata);
679 }
680
681 return null;
682 }
683
684 protected Element processFormatStringRequest(Document result_doc, Element request)
685 {
686 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
687 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
688 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
689
690 String subaction = request.getAttribute("subaction");
691 String service = request.getAttribute("service");
692
693 String classifier = null;
694 if (service.equals("ClassifierBrowse"))
695 {
696 classifier = request.getAttribute("classifier");
697 }
698
699 // check for version file
700 String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
701
702 String version_filename = "";
703 if (service.equals("ClassifierBrowse"))
704 version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
705 else
706 version_filename = directory + "query_format_statement_version.txt";
707
708 File version_file = new File(version_filename);
709
710 if (subaction.equals("update"))
711 {
712 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
713 //String format_string = GSXML.getNodeText(format_element);
714 Element format_statement = (Element) format_element.getFirstChild();
715
716 String version_number = "1";
717 BufferedWriter writer;
718
719 try
720 {
721
722 if (version_file.exists())
723 {
724 // Read version
725 BufferedReader reader = new BufferedReader(new FileReader(version_filename));
726 version_number = reader.readLine();
727 int aInt = Integer.parseInt(version_number) + 1;
728 version_number = Integer.toString(aInt);
729 reader.close();
730 }
731 else
732 {
733 // Create
734 version_file.createNewFile();
735 writer = new BufferedWriter(new FileWriter(version_filename));
736 writer.write(version_number);
737 writer.close();
738 }
739
740 // Write version file
741 String format_statement_filename = "";
742
743 if (service.equals("ClassifierBrowse"))
744 format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
745 else
746 format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
747
748 // Write format statement
749 String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
750 writer = new BufferedWriter(new FileWriter(format_statement_filename));
751 writer.write(format_string);
752 writer.close();
753
754 // Update version number
755 writer = new BufferedWriter(new FileWriter(version_filename));
756 writer.write(version_number);
757 writer.close();
758
759 }
760 catch (IOException e)
761 {
762 logger.error("IO Exception " + e);
763 }
764 }
765
766 if (subaction.equals("saveDocument"))
767 {
768 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
769 //String format_string = GSXML.getNodeText(format_element);
770 // Get display tag
771 Element display_format = (Element) format_element.getFirstChild();
772
773 String collection_config = directory + "collectionConfig.xml";
774 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
775
776 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
777
778 // Get display child
779 if (GSXML.getChildByTagName(current_node, "display") == null)
780 {
781 // well then create a format tag
782 Element display_tag = config.createElement("display");
783 current_node = (Node) current_node.appendChild(display_tag);
784 }
785 else
786 {
787 current_node = GSXML.getChildByTagName(current_node, "display");
788 }
789
790 if (GSXML.getChildByTagName(current_node, "format") == null)
791 {
792 // well then create a format tag
793 Element format_tag = config.createElement("format");
794 current_node.appendChild(format_tag);
795 }
796
797 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
798
799 String new_config = this.converter.getString(config);
800
801 new_config = StringUtils.replace(new_config, "&lt;", "<");
802 new_config = StringUtils.replace(new_config, "&gt;", ">");
803 new_config = StringUtils.replace(new_config, "&quot;", "\"");
804
805 try
806 {
807 // Write to file (not original! for now)
808 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
809 writer.write(new_config);
810 writer.close();
811 }
812 catch (IOException e)
813 {
814 logger.error("IO Exception " + e);
815 }
816 }
817
818 if (subaction.equals("save"))
819 {
820 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
821 Element format_statement = (Element) format_element.getFirstChild();
822
823 try
824 {
825 // open collectionConfig.xml and read in to w3 Document
826 String collection_config = directory + "collectionConfig.xml";
827 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
828
829 //String tag_name = "";
830 int k;
831 int index;
832 Element elem;
833 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
834 NodeList current_node_list;
835
836 if (service.equals("ClassifierBrowse"))
837 {
838 //tag_name = "browse";
839 // if CLX then need to look in <classifier> X then <format>
840 // default is <browse><format>
841
842 current_node = GSXML.getChildByTagName(current_node, "browse");
843
844 // find CLX
845 if (classifier != null)
846 {
847 current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
848 index = Integer.parseInt(classifier.substring(2)) - 1;
849
850 // index should be given by X-1
851 current_node = current_node_list.item(index);
852 // what if classifier does not have a format tag?
853 if (GSXML.getChildByTagName(current_node, "format") == null)
854 {
855 // well then create a format tag
856 Element format_tag = config.createElement("format");
857 current_node.appendChild(format_tag);
858 }
859 }
860 else
861 {
862 // To support all classifiers, set classifier to null? There is the chance here that the format tag does not exist
863 if (GSXML.getChildByTagName(current_node, "format") == null)
864 {
865 // well then create a format tag
866 Element format_tag = config.createElement("format");
867 current_node.appendChild(format_tag);
868 }
869 }
870 }
871 else if (service.equals("AllClassifierBrowse"))
872 {
873 current_node = GSXML.getChildByTagName(current_node, "browse");
874 if (GSXML.getChildByTagName(current_node, "format") == null)
875 {
876 // well then create a format tag
877 Element format_tag = config.createElement("format");
878 current_node.appendChild(format_tag);
879 }
880 }
881 else
882 {
883 // look in <format> with no attributes
884 current_node_list = GSXML.getChildrenByTagName(current_node, "search");
885 for (k = 0; k < current_node_list.getLength(); k++)
886 {
887 current_node = current_node_list.item(k);
888 // if current_node has no attributes then break
889 elem = (Element) current_node;
890 if (elem.hasAttribute("name") == false)
891 break;
892 }
893 }
894
895 current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
896
897 // Now convert config document to string for writing to file
898 String new_config = this.converter.getString(config);
899
900 new_config = StringUtils.replace(new_config, "&lt;", "<");
901 new_config = StringUtils.replace(new_config, "&gt;", ">");
902 new_config = StringUtils.replace(new_config, "&quot;", "\"");
903
904 // Write to file (not original! for now)
905 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
906 writer.write(new_config);
907 writer.close();
908
909 }
910 catch (Exception ex)
911 {
912 logger.error("There was an exception " + ex);
913
914 StringWriter sw = new StringWriter();
915 PrintWriter pw = new PrintWriter(sw, true);
916 ex.printStackTrace(pw);
917 pw.flush();
918 sw.flush();
919 logger.error(sw.toString());
920 }
921
922 }
923
924 return response;
925 }
926
927 private boolean loadSolrCores() {
928
929 // Solr servlet url
930 String solrUrl = "http://localhost:8383/solr/";
931 HttpSolrServer solrServer = new HttpSolrServer(solrUrl);
932 // Max retries
933 solrServer.setMaxRetries(1);
934 // Connection Timeout
935 solrServer.setConnectionTimeout(3000);
936 //Cores
937 String coreSecName = this.router.getSiteName() + "-" + cluster_name + "-sidx";
938 String coreDocName = this.router.getSiteName() + "-" + cluster_name + "-didx";
939
940 if (!checkSolrCore(coreSecName, solrServer)){
941 if (!activateSolrCore(coreSecName, solrServer)){
942 logger.error("Couldn't activate Solr core " + coreSecName + " for collection " + cluster_name);
943 return false;
944 }
945 }
946 if (!checkSolrCore(coreDocName, solrServer)){
947 if (!activateSolrCore(coreDocName, solrServer)){
948 logger.error("Couldn't activate Solr core " + coreDocName + " for collection " + cluster_name);
949 return false;
950 }
951 }
952 return true;
953 }
954
955 private boolean checkSolrCore(String coreName, HttpSolrServer solrServer) {
956
957 CoreAdminRequest adminRequest = new CoreAdminRequest();
958 adminRequest.setAction(CoreAdminAction.STATUS);
959 adminRequest.setCoreName(coreName);
960
961 try {
962 CoreAdminResponse adminResponse = adminRequest.process(solrServer);
963 NamedList<NamedList<Object>> coreStatus = adminResponse.getCoreStatus();
964 NamedList<Object> coreList = coreStatus.getVal(0);
965 if (coreList != null) {
966 if (coreList.get("name") == null) {
967 logger.warn("Solr core " + coreName + " for collection " + cluster_name + " not exists.");
968 return false;
969 }
970 }
971
972 } catch (SolrServerException e) {
973 e.printStackTrace();
974 return false;
975 } catch (IOException e) {
976 e.printStackTrace();
977 return false;
978 } catch (RemoteSolrException e1){
979 e1.printStackTrace();
980 return false;
981 }
982 return true;
983 }
984
985 private boolean activateSolrCore(String coreName, HttpSolrServer solrServer) {
986
987 String dataDir = GSFile.collectionIndexDir(site_home, cluster_name) + File.separator + coreName.substring(coreName.length() - 4);
988 String instanceDir = GSFile.collectionEtcDir(site_home, cluster_name);
989
990 try {
991 CoreAdminRequest.createCore(coreName, instanceDir, solrServer, "", "", dataDir, "");
992 logger.warn("Solr core " + coreName + " for collection " + cluster_name + " activated.");
993 } catch (SolrServerException e1) {
994 e1.printStackTrace();
995 return false;
996 } catch (IOException e1) {
997 e1.printStackTrace();
998 return false;
999 } catch (RemoteSolrException e1){
1000 logger.error("Activation solr core " + coreName + " for collection " + cluster_name + "failed.");
1001 e1.printStackTrace();
1002 return false;
1003 }
1004
1005 return true;
1006 }
1007
1008}
1009
Note: See TracBrowser for help on using the repository browser.