source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/SystemAction.java@ 28382

Last change on this file since 28382 was 28382, checked in by davidb, 11 years ago

Elimination of the 'this.doc' field from the Action baseclass and the subclasses that rely on it. For Greenstone3 purposes it is unsafe to create this object in the constructor to the action and then store it for other methods to access. This is because the Greenstone 3 (and in particular calls to 'process' operate in a multi-threaded context, that is managed by the Servlet server (e.g. Tomcat by default). Calls to DOM methods are not guaranteed to be thread safe, this became apparent when we started looking in to an exception that was being thrown, and centred around use of the DOM method 'item(i)'. The change this commit makes is to remove 'this.doc' being stored as a field. A document is now created in the top level of a call to 'process()' and when a DOM reference is needed in a subsequent method an Element variable (typically passed in as a parameter to the method) is used (through 'Document doc = element.getOwnerDocument()') to gain access to the DOM

  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.util.*;
4
5// XML classes
6import org.w3c.dom.Document;
7import org.w3c.dom.Node;
8import org.w3c.dom.Element;
9
10// other java stuff
11import java.util.*;
12
13import java.io.Serializable;
14
15import java.io.PrintWriter;
16import java.io.Serializable;
17import java.io.StringWriter;
18
19import org.apache.log4j.*;
20
21public class SystemAction extends Action
22{
23
24 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.SystemAction.class.getName());
25
26 String tempVal = "";
27
28 /** process a request */
29 public Node process(Node message_node)
30 {
31 Element message = this.converter.nodeToElement(message_node);
32 Document doc = message.getOwnerDocument();
33
34 // assume only one request
35 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
36
37 String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
38 UserContext userContext = new UserContext(request);
39 // get the param list
40 Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
41 HashMap<String, Serializable> params = GSXML.extractParams(cgi_param_list, false);
42
43 Element result = doc.createElement(GSXML.MESSAGE_ELEM);
44
45 String coll = (String) params.get(GSParams.SYSTEM_CLUSTER);
46
47 String to = "";
48 if (coll != null && !coll.equals(""))
49 {
50 to = coll;
51 }
52
53 Element mr_request_message = doc.createElement(GSXML.MESSAGE_ELEM);
54 Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SYSTEM, to, userContext);
55 mr_request_message.appendChild(mr_request);
56
57 Element system = doc.createElement(GSXML.SYSTEM_ELEM);
58 mr_request.appendChild(system);
59
60 // will need to change the following if can do more than one system request at once
61 if (subaction.equals("c"))
62 { // configure
63 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
64 String info = (String) params.get(GSParams.SYSTEM_SUBSET);
65 system.setAttribute(GSXML.SYSTEM_SUBSET_ATT, info);
66 }
67 else if (subaction.equals("ping")) { // can ping the server or a collection
68 String name = (String) params.get(GSParams.SYSTEM_MODULE_NAME);
69
70 if(name != null && !name.equals("")) {
71 // Pinging a collection (or module) with ?a=s&sa=ping&st=collection&sn=<colName>
72 // is a collection-level (servicecluster/module level) ping
73
74 String type = (String) params.get(GSParams.SYSTEM_MODULE_TYPE);
75 if(type == null || type.equals("")) {
76 type = "collection"; // if the st=collection was omitted, assume collection
77 }
78 // ping action set to moduleType=Collection and moduleName=colName
79 system.setAttribute(GSXML.SYSTEM_MODULE_NAME_ATT, name);
80 system.setAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT, type);
81 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_PING);
82 } // else SYSTEM_MODULE_NAME given by the "sn" GSParam is null or empty
83 // meaning server-level ping: ?a=s&sa=ping
84
85 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_PING);
86 }
87 //else if (subaction.equals("is-persistent")){
88 // system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_ISPERSISTENT);
89 //}
90 else
91 {
92 String name = (String) params.get(GSParams.SYSTEM_MODULE_NAME);
93 String type = (String) params.get(GSParams.SYSTEM_MODULE_TYPE);
94
95 system.setAttribute(GSXML.SYSTEM_MODULE_NAME_ATT, name);
96 system.setAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT, type);
97
98 if (subaction.equals("d"))
99 { // delete
100 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_DEACTIVATE);
101
102 }
103 else if (subaction.equals("a"))
104 { // add
105 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_ACTIVATE);
106 }
107 else
108 {
109 // create the default response
110 // for now just have an error
111 logger.error("bad subaction type");
112 Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
113 result.appendChild(page_response);
114
115 return result;
116 }
117 }
118
119 Node response_message = this.mr.process(mr_request_message);
120
121 Element response = GSXML.duplicateWithNewName(doc, (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true);
122 addSiteMetadata(response, userContext);
123 addInterfaceOptions(response);
124
125 result.appendChild(response);
126 return result;
127
128 }
129
130}
Note: See TracBrowser for help on using the repository browser.