Changeset 457:06e0a5b6aedc

Show
Ignore:
Timestamp:
09/21/09 12:21:56 (4 years ago)
Author:
tgambet
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@458
Message:

refactored checkCond(TLTCond) function
+ added an ArrayList? of Messages
+ allowed messages to be added by a RequestThread? object

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/org/w3c/unicorn/UnicornCall.java

    r447 r457  
    1 // $Id: UnicornCall.java,v 1.19 2009-09-21 09:38:22 tgambet Exp $ 
     1// $Id: UnicornCall.java,v 1.20 2009-09-21 12:21:56 tgambet Exp $ 
    22// Author: Jean-Guilhem Rouel 
    33// (c) COPYRIGHT MIT, ERCIM and Keio, 2006. 
     
    1212 
    1313import javax.activation.MimeType; 
    14 import javax.xml.parsers.DocumentBuilder; 
    15 import javax.xml.parsers.DocumentBuilderFactory; 
    16 import javax.xml.xpath.XPath; 
    17 import javax.xml.xpath.XPathConstants; 
    18 import javax.xml.xpath.XPathExpression; 
    19 import javax.xml.xpath.XPathFactory; 
    2014 
    2115import org.apache.commons.logging.Log; 
    2216import org.apache.commons.logging.LogFactory; 
    23 import org.w3c.dom.Document; 
    2417import org.w3c.unicorn.contract.CallParameter; 
    2518import org.w3c.unicorn.contract.EnumInputMethod; 
     
    3629import org.w3c.unicorn.tasklist.parameters.Parameter; 
    3730import org.w3c.unicorn.tasklist.parameters.Value; 
    38 import org.w3c.unicorn.tasklisttree.EnumCondType; 
    3931import org.w3c.unicorn.tasklisttree.TLTCond; 
    4032import org.w3c.unicorn.tasklisttree.TLTExec; 
     
    4436import org.w3c.unicorn.util.Property; 
    4537 
    46 import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl; 
    47  
    4838/** 
    4939 * UnicornCall Created: Jun 29, 2006 2:44:12 PM 
     
    7565         
    7666        private LinkedHashMap<String, Response> observationMap; 
     67         
     68        private ArrayList<Message> messages; 
    7769 
    7870        /** 
     
    8375                mapOfStringParameter = new LinkedHashMap<String, String[]>(); 
    8476                mapOfResponse = new LinkedHashMap<String, Response>(); 
     77                messages = new ArrayList<Message>(); 
    8578        } 
    8679 
     
    148141                boolean conditionOK = false; 
    149142                for (TLTCond cond : ifs.getCondArray()) { 
    150                         if (checkCond(cond)) { 
     143                        if (cond.check(this)) { 
    151144                                conditionOK = true; 
    152145                        } 
     
    155148        } 
    156149         
    157         /** 
    158          * Giving a TLTCond, checks in the map of response if the condition passes 
    159          * or fails and consequently returns a boolean. 
    160          *  
    161          * @param cond 
    162          *            The condition to check 
    163          * @return true if there is a matching response and if the condition passes 
    164          *         else false 
    165          */ 
    166         private boolean checkCond(TLTCond cond) throws Exception { 
    167                 logger.trace("checkCond : "); 
    168                 logger.trace(cond); 
    169                 logger.trace("condId : " + cond.getId()); 
    170                 logger.trace("condType : " + cond.getType()); 
    171                 logger.trace("condValue : " + cond.getValue()); 
    172  
    173                 boolean passed = false; 
    174  
    175                 if (cond.getType().equals(EnumCondType.MIMETYPE)) { 
    176                         passed = cond.getValue().equals(inputParameter.getMimeType().toString()); 
    177                 } else if (cond.getType().equals(EnumCondType.XPATH)) { 
    178                         logger.trace("condObserver : " + cond.getObserver().getID()); 
    179                         Response res = mapOfResponse.get(cond.getObserver().getID()); 
    180                         // Testing if there is a matching response in the map 
    181                         // and if it is passed 
    182                         if (res != null) { 
    183                                 String xmlStr = res.getXml().toString(); 
    184  
    185                                 DocumentBuilderFactory xmlFact = DocumentBuilderFactory 
    186                                                 .newInstance(); 
    187  
    188                                 // namespace awareness is escaped since we don't use it 
    189                                 // for the moment 
    190                                 xmlFact.setNamespaceAware(false); 
    191  
    192                                 DocumentBuilder builder = xmlFact.newDocumentBuilder(); 
    193  
    194                                 Document doc = builder.parse(new java.io.ByteArrayInputStream( 
    195                                                 xmlStr.getBytes("UTF-8"))); 
    196  
    197                                 String xpathStr = cond.getValue(); 
    198  
    199                                 XPathFactory xpathFact = new XPathFactoryImpl(); 
    200  
    201                                 XPath xpath = xpathFact.newXPath(); 
    202                                 XPathExpression xpe = xpath.compile(xpathStr); 
    203                                 passed = (Boolean) xpe.evaluate(doc, XPathConstants.BOOLEAN); 
    204                         } 
    205                 } else if (cond.getType().equals(EnumCondType.PARAMETER)) { 
    206                         passed = false; 
    207                         if (!mapOfStringParameter.containsKey(cond.getParameter())) { 
    208                                 cond.setResult(passed); 
    209                                 return passed; 
    210                         } 
    211                          
    212                         String[] parameterValues = mapOfStringParameter.get(cond.getParameter()); 
    213                         for (int i=0; i<parameterValues.length; i++) 
    214                                 if (parameterValues[i].equals(cond.getValue())) 
    215                                         passed = true; 
    216                          
    217                 } 
    218  
    219                 cond.setResult(passed); 
    220                 logger.trace("cond result : " + passed); 
    221                 return passed; 
    222         } 
    223150         
    224151        /** 
     
    430357         *             Input/Output error 
    431358         */ 
    432         private void doRequests(RequestList requestList) throws IOException { 
     359        private void doRequests(RequestList requestList) { 
    433360                logger.trace("doRequests"); 
    434361 
     
    439366                for (final String obsID : requests.keySet()) { 
    440367                        // send request to observer 
    441                         //threadsList.add(new RequestThread(mapOfResponse, requests.get(obsID), obsID, this)); 
    442368                        threadsList.add(new RequestThread(requests.get(obsID), obsID, this.getLang())); 
    443369                        logger.debug("Request " + requests.get(obsID) + " added to threadsList"); 
    444370                } 
    445                 for (int i = 0; i < threadsList.size(); i++) { 
    446                         threadsList.get(i).start(); 
    447                         logger.debug("Request " + ((RequestThread)threadsList.get(i)).getObsID() + " started"); 
    448                 } 
    449  
    450                 for (int i = 0; i < threadsList.size(); i++) { 
     371                for (RequestThread thread : threadsList) { 
     372                        thread.start(); 
     373                        logger.debug("Request " + thread.getObsID() + " started"); 
     374                } 
     375                for (RequestThread thread : threadsList) { 
    451376                        try { 
    452                                 threadsList.get(i).join(); 
    453                                 mapOfResponse.put(threadsList.get(i).getObsID(), threadsList.get(i).getResponse()); 
    454                                 logger.debug("Request " + ((RequestThread)threadsList.get(i)).getObsID() + " terminated"); 
     377                                thread.join(); 
     378                                messages.addAll(thread.getMessages()); 
     379                                if (thread.getResponse() != null) 
     380                                        mapOfResponse.put(thread.getObsID(), thread.getResponse()); 
     381                                logger.debug("Request " + thread.getObsID() + " terminated"); 
    455382                        } catch (InterruptedException e) { 
    456383                                e.printStackTrace(); 
     
    577504        } 
    578505         
     506        public InputParameter getInputParameter() { 
     507                return inputParameter; 
     508        } 
     509 
    579510        /** 
    580511         * Set the task to perform 
     
    619550                this.inputParameter = inputParameter; 
    620551        } 
     552 
     553        public ArrayList<Message> getMessages() { 
     554                return messages; 
     555        } 
     556 
     557        public void setMessages(ArrayList<Message> messages) { 
     558                this.messages = messages; 
     559        } 
    621560}