source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

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