source: greenstone3/trunk/src/java/org/greenstone/gsdl3/service/GS2MGRetrieve.java@ 14936

Last change on this file since 14936 was 14936, checked in by shaoqun, 16 years ago

fotgot to setIndex to the mg indexer before retrieving the document

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/*
2 * GS2MGRetrieve.java
3 * Copyright (C) 2005 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.service;
20
21// Greenstone classes
22import org.greenstone.mg.*;
23import org.greenstone.gsdl3.core.GSException;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.GSXML;
26
27// XML classes
28import org.w3c.dom.Element;
29import org.w3c.dom.Text;
30
31// General Java classes
32import java.io.File;
33
34import org.apache.log4j.*;
35
36public class GS2MGRetrieve
37extends AbstractGS2DocumentRetrieve {
38 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2MGRetrieve.class.getName ());
39
40 // Elements used in the config file that are specific to this class
41 private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
42 private static final String INDEX_LIST_ELEM = "indexList";
43 private static final String INDEX_ELEM = "index";
44 private static final String DEFAULT_INDEX_SUBCOLLECTION_ELEM = "defaultIndexSubcollection";
45 private static final String DEFAULT_INDEX_LANGUAGE_ELEM = "defaultIndexLanguage";
46
47 private static MGRetrieveWrapper mg_src = null;
48 private String mg_basedir = null;
49 private String mg_textdir = null;
50 private String default_index = null;
51 private boolean has_default_index = false;
52
53 public GS2MGRetrieve () {
54 if (this.mg_src == null){
55 this.mg_src = new MGRetrieveWrapper ();
56 }
57 }
58
59 public void cleanUp () {
60 super.cleanUp ();
61 this.mg_src.unloadIndexData ();
62 }
63
64 /** configure this service */
65 public boolean configure (Element info, Element extra_info) {
66 if (!super.configure (info, extra_info)){
67 return false;
68 }
69
70 // Do specific configuration
71 logger.info ("Configuring GS2MGRetrieve...");
72
73 // Get the default index out of <defaultIndex> (buildConfig.xml)
74 Element def = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_ELEM);
75 if (def != null) {
76 this.default_index = def.getAttribute (GSXML.SHORTNAME_ATT);
77 }
78 Element defSub = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_SUBCOLLECTION_ELEM);
79 if (defSub != null) {
80 this.default_index += defSub.getAttribute (GSXML.SHORTNAME_ATT);
81 logger.info ("default indexSubcollection is "+defSub.getAttribute (GSXML.SHORTNAME_ATT));
82 } //concate defaultIndex + defaultIndexSubcollection
83
84 //get the default indexLanguage out of <defaultIndexLanguage> (buildConfig.xml)
85 Element defLang = (Element) GSXML.getChildByTagName (info, DEFAULT_INDEX_LANGUAGE_ELEM);
86 if (defLang != null) {
87 this.default_index += defLang.getAttribute (GSXML.SHORTNAME_ATT);
88 logger.info ("default indexLanguage is "+defLang.getAttribute (GSXML.SHORTNAME_ATT));
89 } //concate defaultIndex + defaultIndexSubcollection + defaultIndexLanguage
90
91
92 if (this.default_index == null || this.default_index.equals ("")) {
93 logger.error ("default index is not specified, the content of a document will not be retrieved");
94 has_default_index = false;
95 return true;
96 //}
97 }
98 logger.debug ("Default index: " + this.default_index);
99
100 // The location of the MG index and text files
101 mg_basedir = GSFile.collectionBaseDir (this.site_home, this.cluster_name) + File.separatorChar; // Needed by MG
102 mg_textdir = GSFile.collectionTextPath (this.index_stem);
103 // index is only needed to start up MG, not used so just use the default index
104 String indexpath = GSFile.collectionIndexPath (this.index_stem, this.default_index);
105 this.mg_src.setIndex (indexpath);
106 has_default_index = true;
107 return true;
108 }
109
110 /** returns the content of a node
111 * should return a nodeContent element:
112 * <nodeContent>text content or other elements</nodeContent>
113 */
114 protected Element getNodeContent (String doc_id, String lang) throws GSException {
115 long doc_num = this.gdbm_src.OID2Docnum (doc_id);
116 if (doc_num == -1) {
117 logger.error ("OID "+doc_id +" couldn't be converted to mg num");
118 return null;
119 }
120 Element content_node = this.doc.createElement (GSXML.NODE_CONTENT_ELEM);
121
122 String doc_content = null;
123
124 //means that this.mg_src is up and running
125 if (has_default_index ){
126 synchronized(this.mg_src){
127 String indexpath = GSFile.collectionIndexPath(this.index_stem, this.default_index);
128 this.mg_src.setIndex(indexpath);
129 doc_content = this.mg_src.getDocument (this.mg_basedir,
130 this.mg_textdir, doc_num);
131 }
132 }
133
134 if (doc_content!=null) {
135 // remove any ctrl-c or ctrl-b
136 doc_content = doc_content.replaceAll ("\u0002|\u0003", "");
137 // replace _httpimg_ with the correct address
138 doc_content = resolveTextMacros (doc_content, doc_id, lang);
139 //GSXML.addDocText(this.doc, doc, doc_content);
140 } else {
141 logger.error ("the doc content was null, not getting that section\n");
142 doc_content = "couldn't retrieve content for this section, please check the log file for more detail\n";
143 }
144 Text t = this.doc.createTextNode (doc_content);
145 content_node.appendChild (t);
146 return content_node;
147
148 }
149}
Note: See TracBrowser for help on using the repository browser.