Ignore:
Timestamp:
2013-10-10T17:21:30+13:00 (11 years ago)
Author:
davidb
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/GS2BrowseAction.java

    r26508 r28382  
    1212import org.greenstone.gsdl3.util.OID;
    1313import org.greenstone.gsdl3.util.UserContext;
     14import org.w3c.dom.Document;
    1415import org.w3c.dom.Element;
    1516import org.w3c.dom.Node;
     
    2930    public Node process(Node message_node)
    3031    {
    31 
    3232        Element message = this.converter.nodeToElement(message_node);
    33 
     33        Document doc = message.getOwnerDocument();
     34       
    3435        // get the request - assume only one
    3536        Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
    3637
    3738        // the result
    38         Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
     39        Element result = doc.createElement(GSXML.MESSAGE_ELEM);
    3940        Element response = classifierBrowse(request);
    4041        result.appendChild(response);
     
    4445    protected Element classifierBrowse(Element request)
    4546    {
    46         Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     47        Document doc = request.getOwnerDocument();
     48        Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
    4749
    4850        // extract the params from the cgi-request, and check that we have a coll specified
     
    7274        // this should be cached somehow later on.
    7375
    74         Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
    75         Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
     76        Element info_message = doc.createElement(GSXML.MESSAGE_ELEM);
     77        Element info_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
    7678        info_message.appendChild(info_request);
    7779
    7880        // also get the format stuff now if there is some
    79         Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
     81        Element format_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
    8082        info_message.appendChild(format_request);
    8183        // process the requests
     
    8991
    9092        Element service_description = (Element) GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
    91         page_response.appendChild(this.doc.importNode(service_description, true));
     93        page_response.appendChild(doc.importNode(service_description, true));
    9294
    9395        //append site metadata
     
    139141                }
    140142
    141                 Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
     143                Element new_format = GSXML.duplicateWithNewName(doc, this_format, GSXML.FORMAT_ELEM, false);
    142144                extractMetadataNames(new_format, doc_meta_names, class_meta_names);
    143145
     
    164166
    165167        // get the browse structure for the selected node
    166         Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
    167         Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
     168        Element classify_message = doc.createElement(GSXML.MESSAGE_ELEM);
     169        Element classify_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
    168170        classify_message.appendChild(classify_request);
    169171
    170172        //Create a parameter list to specify the required structure information
    171173        // for now, always get ancestors and children
    172         Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     174        Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    173175        classify_request.appendChild(param_list);
    174         Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     176        Element param = doc.createElement(GSXML.PARAM_ELEM);
    175177        param_list.appendChild(param);
    176178        param.setAttribute(GSXML.NAME_ATT, "structure");
    177179        param.setAttribute(GSXML.VALUE_ATT, "ancestors");
    178         param = this.doc.createElement(GSXML.PARAM_ELEM);
     180        param = doc.createElement(GSXML.PARAM_ELEM);
    179181        param_list.appendChild(param);
    180182        param.setAttribute(GSXML.NAME_ATT, "structure");
     
    183185        if (get_descendants)
    184186        {
    185             param = this.doc.createElement(GSXML.PARAM_ELEM);
     187            param = doc.createElement(GSXML.PARAM_ELEM);
    186188            param_list.appendChild(param);
    187189            param.setAttribute(GSXML.NAME_ATT, "structure");
     
    191193
    192194        // put the classifier node into a classifier node list
    193         Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
    194         Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
     195        Element classifier_list = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
     196        Element classifier = doc.createElement(GSXML.CLASS_NODE_ELEM);
    195197        classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
    196198        classifier_list.appendChild(classifier);
     
    233235                if (firstChildIsClassifierNode)
    234236                {
    235                     Element childStructure = getClassifierStructureFromID(classifier_node + ".1", request, collection, service_name);
     237                    Element childStructure = getClassifierStructureFromID(doc, classifier_node + ".1", request, collection, service_name);
    236238
    237239                    Element replacementElem = null;
     
    266268            else
    267269            {
    268                 Element childStructure = getClassifierStructureFromID(OID.getTop(classifier_node), request, collection, service_name);
     270                Element childStructure = getClassifierStructureFromID(doc, OID.getTop(classifier_node), request, collection, service_name);
    269271
    270272                String[] idParts = classifier_node.split("\\.");
     
    305307        Element page_classifier = null;
    306308        // add the single classifier node as the page classifier
    307         page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
     309        page_classifier = GSXML.duplicateWithNewName(doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
    308310        page_response.appendChild(page_classifier);
    309311        page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
     
    312314        // then for each document node
    313315
    314         Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     316        Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
    315317
    316318        boolean did_classifier = false;
     
    325327        {
    326328            did_classifier = true;
    327             Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
     329            Element cl_meta_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
    328330            metadata_message.appendChild(cl_meta_request);
    329331
    330             Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
     332            Element new_cl_nodes_list = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
    331333            cl_meta_request.appendChild(new_cl_nodes_list);
    332334
     
    334336            {
    335337
    336                 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
     338                Element cl = doc.createElement(GSXML.CLASS_NODE_ELEM);
    337339                cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
    338340                new_cl_nodes_list.appendChild(cl);
     
    342344            // should be based on info sent in from the recept, and the
    343345            // format stuff
    344             Element cl_param_list = createMetadataParamList(class_meta_names);
     346            Element cl_param_list = createMetadataParamList(doc,class_meta_names);
    345347            cl_meta_request.appendChild(cl_param_list);
    346348
     
    354356        {
    355357            did_documents = true;
    356             Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
     358            Element doc_meta_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
    357359            metadata_message.appendChild(doc_meta_request);
    358360
    359             Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
     361            Element doc_list = doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
    360362            doc_meta_request.appendChild(doc_list);
    361363
     
    363365            {
    364366
    365                 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
     367                Element d = doc.createElement(GSXML.DOC_NODE_ELEM);
    366368                d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
    367369                doc_list.appendChild(d);
     
    369371
    370372            // create and add in the param list - add all for now
    371             Element doc_param_list = createMetadataParamList(doc_meta_names);
     373            Element doc_param_list = createMetadataParamList(doc,doc_meta_names);
    372374            doc_meta_request.appendChild(doc_param_list);
    373375
     
    431433    }
    432434
    433     private Element getClassifierStructureFromID(String id, Element request, String collection, String service_name)
     435    private Element getClassifierStructureFromID(Document doc, String id, Element request, String collection, String service_name)
    434436    {
    435437        UserContext userContext = new UserContext(request);
    436438        String to = GSPath.appendLink(collection, service_name);
    437439
    438         Element firstClassifierNodeChildrenMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
    439         Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
     440        Element firstClassifierNodeChildrenMessage = doc.createElement(GSXML.MESSAGE_ELEM);
     441        Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
    440442        firstClassifierNodeChildrenMessage.appendChild(firstClassifierNodeChildrenRequest);
    441443
    442         Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     444        Element paramList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    443445        firstClassifierNodeChildrenRequest.appendChild(paramList);
    444446
    445         Element ancestorParam = this.doc.createElement(GSXML.PARAM_ELEM);
     447        Element ancestorParam = doc.createElement(GSXML.PARAM_ELEM);
    446448        paramList.appendChild(ancestorParam);
    447449        ancestorParam.setAttribute(GSXML.NAME_ATT, "structure");
    448450        ancestorParam.setAttribute(GSXML.VALUE_ATT, "ancestors");
    449451
    450         Element childrenParam = this.doc.createElement(GSXML.PARAM_ELEM);
     452        Element childrenParam = doc.createElement(GSXML.PARAM_ELEM);
    451453        paramList.appendChild(childrenParam);
    452454        childrenParam.setAttribute(GSXML.NAME_ATT, "structure");
    453455        childrenParam.setAttribute(GSXML.VALUE_ATT, "children");
    454456
    455         Element classifierToGetList = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
    456         Element classifierToGet = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
     457        Element classifierToGetList = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
     458        Element classifierToGet = doc.createElement(GSXML.CLASS_NODE_ELEM);
    457459        classifierToGet.setAttribute(GSXML.NODE_ID_ATT, id);
    458460        classifierToGetList.appendChild(classifierToGet);
Note: See TracChangeset for help on using the changeset viewer.