source: greenstone3/branches/customizingGreenstone3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 14713

Last change on this file since 14713 was 14515, checked in by shaoqun, 17 years ago

add gsf namespace

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15import java.util.ArrayList;
16
17import org.apache.log4j.*;
18
19/** the most basic Receptionist, used for interface generation.
20 * Receives requests consisting
21 * of an xml representation of cgi args, and returns the page of data. The requests are processed by the appropriate action class
22 *
23 * @see Action
24 */
25public class Receptionist implements ModuleInterface {
26
27 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.Receptionist.class.getName());
28
29 /** the set up variables */
30 protected HashMap config_params = null;
31 /** container Document to create XML Nodes */
32 protected Document doc=null;
33
34 /** a converter class to parse XML and create Docs */
35 protected XMLConverter converter=null;
36
37 /** the message router that the Receptionist and Actions will talk to */
38 protected ModuleInterface mr=null;
39
40 /** the list of actions */
41 protected HashMap action_map=null;
42
43 /** the list of params */
44 protected GSParams params=null;
45 protected Element language_list = null;
46
47 /** the list of interfaces this is based on */
48 protected ArrayList base_interfaces = null;
49
50 public Receptionist() {
51 this.converter = new XMLConverter();
52 this.doc = this.converter.newDOM();
53 this.action_map= new HashMap();
54 }
55
56 public void cleanUp() {}
57 public void setParams(GSParams params) {
58 this.params = params;
59 }
60 public void setConfigParams(HashMap params) {
61 this.config_params = params;
62 }
63 public HashMap getConfigParams() {
64 return this.config_params;
65 }
66 /** sets the message router - it should already be created and
67 * configured before being passed to the receptionist*/
68 public void setMessageRouter(ModuleInterface m) {
69 this.mr = m;
70 }
71
72 /** configures the receptionist */
73 public boolean configure() {
74
75 if (this.config_params==null) {
76 logger.error(" config variables must be set before calling configure");
77 return false;
78 }
79 if (this.mr==null) {
80 logger.error(" message router must be set before calling configure");
81 return false;
82 }
83
84 // find the config file containing a list of actions
85 File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
86 if (!interface_config_file.exists()) {
87 logger.error(" interface config file: "+interface_config_file.getPath()+" not found!");
88 return false;
89 }
90
91 Document config_doc = this.converter.getDOM(interface_config_file);
92 if (config_doc == null) {
93 logger.error(" could not parse interface config file: "+interface_config_file.getPath());
94 return false;
95 }
96 Element config_elem = config_doc.getDocumentElement();
97 String base_interface = config_elem.getAttribute("baseInterface");
98 setUpBaseInterface(base_interface);
99 setUpInterfaceOptions(config_elem);
100
101 // load up the actions
102 Element action_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
103 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
104
105 for (int i=0; i<actions.getLength(); i++) {
106 Element action = (Element) actions.item(i);
107 String class_name = action.getAttribute("class");
108 String action_name = action.getAttribute("name");
109 Action ac = null;
110 try {
111 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
112 } catch (Exception e) {
113 logger.error(" couldn't load in action "+class_name);
114 e.printStackTrace();
115 continue;
116 }
117 ac.setConfigParams(this.config_params);
118 ac.setMessageRouter(this.mr);
119 ac.configure();
120 ac.getActionParameters(this.params);
121 this.action_map.put(action_name, ac);
122 }
123
124 this.language_list = (Element)GSXML.getChildByTagName(config_elem, "languageList");
125 if (language_list == null) {
126 logger.error(" didn't find a language list in the config file!!");
127 }
128
129 return true;
130 }
131 /** process using strings - just calls process using Elements */
132 public String process(String xml_in) {
133
134 Element message = this.converter.getDOM(xml_in).getDocumentElement();
135 Element page = process(message);
136 return this.converter.getString(page);
137 }
138
139 /** process - produce a page of data in response to a request
140 * if something goes wrong, it returns null -
141 * TODO: return a suitable message to the user */
142 public Element process(Element message) {
143
144 // get the request out of the message - assume that there is only one
145 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
146 if (request == null) {
147 logger.error(" message had no request!");
148 return null;
149 }
150 // check the request type
151 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
152 if (!type.equals(GSXML.REQUEST_TYPE_PAGE)) {
153 // now Receptionist forwards non-page requests straight to the MR, and returns the responses
154 logger.error(" request type is not '"+GSXML.REQUEST_TYPE_PAGE+"', but it is '"+type+"', so forwarding the message to the MR!");
155 // process the whole message - mr needs <message> tags, and
156 // in this case, there may be more than one request in the message
157 return this.mr.process(message);
158 }
159 // work out which action to pass to
160 String action = request.getAttribute(GSXML.ACTION_ATT);
161 if (action.equals("")) {
162 logger.error(" no action specified in the request!");
163 return null;
164 }
165
166 // find the appropriate action
167 Action a = (Action)this.action_map.get(action);
168
169 String action_name=null;
170 if (a==null) { // not in the map yet
171 // try to load a new action
172 try {
173 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
174 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
175 ac.setConfigParams(this.config_params);
176 ac.setMessageRouter(this.mr);
177 ac.configure();
178 ac.getActionParameters(this.params);
179 this.action_map.put(action, ac);
180 a = ac;
181 } catch (Exception e) {
182
183 logger.error(" a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
184 return null;
185 }
186 }
187
188 // transform the request in some way -- does nothing!
189 preProcessRequest(request);
190 // set up the page
191 Element page = this.doc.createElement(GSXML.PAGE_ELEM);
192 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
193 page.setAttribute("xmlns:gsf","http://www.greenstone.org/greenstone3/schema/ConfigFormat");
194
195 //logger.info(a+" mesa=" + this.converter.getPrettyString(message));
196 // get the page data from the action
197 Element action_response = a.process(message);
198
199 boolean response_only=false;
200 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
201 if (param_list != null) {
202 Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro");
203 if (param != null) {
204 String value = param.getAttribute("value");
205 if (value.equals("1")) {
206 response_only = true;
207 }
208 }
209 }
210 if (response_only) {
211 // only the response from the action is sent back
212 return action_response;
213 }
214
215 // the request is part of the page
216 page.appendChild(GSXML.duplicateWithNewName(this.doc, request, GSXML.PAGE_REQUEST_ELEM, true));
217 // add the response too
218 Element page_response = GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true);
219 page.appendChild(page_response);
220
221 //logger.info(" raw page="+this.converter.getString(page));
222 // transform the result in some way
223 Element resulting_page = postProcessPage(page);
224 logger.debug("receptionist returned response");
225 logger.debug(this.converter.getString(resulting_page));
226// logger.info("receptionist returned response");
227// logger.info(this.converter.getString(resulting_page));
228
229 return resulting_page;
230
231 }
232
233 protected boolean setUpBaseInterface(String base_interface) {
234 if (base_interface== null || base_interface.equals("")) {
235 // there was no base interface, the list remains null
236 return true;
237 }
238 // foreach base interface
239 while (!base_interface.equals("")) {
240 // find the base interface config file
241 File base_interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), base_interface)));
242 if (!base_interface_config_file.exists()) {
243 logger.error(" base interface config file: "+base_interface_config_file.getPath()+" not found!");
244 return false;
245 }
246 // the interface name is valid, add it to the list
247 if (base_interfaces == null) {
248 base_interfaces = new ArrayList();
249 }
250 base_interfaces.add(base_interface);
251 // now see if this has a base interface
252 Document config_doc = this.converter.getDOM(base_interface_config_file);
253 if (config_doc == null) {
254 logger.error(" could not parse base interface config file: "+base_interface_config_file.getPath());
255 return false;
256 }
257 Element config_elem = config_doc.getDocumentElement();
258 base_interface = config_elem.getAttribute("baseInterface");
259 }
260 return true;
261 }
262
263 protected boolean setUpInterfaceOptions(Element config_elem) {
264 Element option_list = (Element)GSXML.getChildByTagName(config_elem, "optionList");
265 if (option_list != null) {
266 logger.error("found an option list");
267 // we set any options in the config params
268 NodeList options = option_list.getElementsByTagName("option");
269 for (int i=0; i<options.getLength(); i++) {
270 Element option = (Element)options.item(i);
271 String name = option.getAttribute(GSXML.NAME_ATT);
272 String value = option.getAttribute(GSXML.VALUE_ATT);
273 logger.error("option: "+name+", "+value);
274 if (!name.equals("") && !value.equals("")) {
275 this.config_params.put(name, value);
276 }
277 }
278 }
279
280 return true;
281 }
282
283 protected void preProcessRequest(Element request) {
284 return;
285 }
286
287 protected Element postProcessPage(Element page) {
288 return page;
289 }
290
291
292}
Note: See TracBrowser for help on using the repository browser.