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

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

replace Category class which is deprecated with Logger class

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