Changeset 457:06e0a5b6aedc
- Timestamp:
- 09/21/09 12:21:56 (4 years ago)
- Branch:
- default
- convert_revision:
- svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@458
- Files:
-
- 1 modified
-
src/org/w3c/unicorn/UnicornCall.java (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/org/w3c/unicorn/UnicornCall.java
r447 r457 1 // $Id: UnicornCall.java,v 1. 19 2009-09-21 09:38:22tgambet Exp $1 // $Id: UnicornCall.java,v 1.20 2009-09-21 12:21:56 tgambet Exp $ 2 2 // Author: Jean-Guilhem Rouel 3 3 // (c) COPYRIGHT MIT, ERCIM and Keio, 2006. … … 12 12 13 13 import 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;20 14 21 15 import org.apache.commons.logging.Log; 22 16 import org.apache.commons.logging.LogFactory; 23 import org.w3c.dom.Document;24 17 import org.w3c.unicorn.contract.CallParameter; 25 18 import org.w3c.unicorn.contract.EnumInputMethod; … … 36 29 import org.w3c.unicorn.tasklist.parameters.Parameter; 37 30 import org.w3c.unicorn.tasklist.parameters.Value; 38 import org.w3c.unicorn.tasklisttree.EnumCondType;39 31 import org.w3c.unicorn.tasklisttree.TLTCond; 40 32 import org.w3c.unicorn.tasklisttree.TLTExec; … … 44 36 import org.w3c.unicorn.util.Property; 45 37 46 import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl;47 48 38 /** 49 39 * UnicornCall Created: Jun 29, 2006 2:44:12 PM … … 75 65 76 66 private LinkedHashMap<String, Response> observationMap; 67 68 private ArrayList<Message> messages; 77 69 78 70 /** … … 83 75 mapOfStringParameter = new LinkedHashMap<String, String[]>(); 84 76 mapOfResponse = new LinkedHashMap<String, Response>(); 77 messages = new ArrayList<Message>(); 85 78 } 86 79 … … 148 141 boolean conditionOK = false; 149 142 for (TLTCond cond : ifs.getCondArray()) { 150 if (c heckCond(cond)) {143 if (cond.check(this)) { 151 144 conditionOK = true; 152 145 } … … 155 148 } 156 149 157 /**158 * Giving a TLTCond, checks in the map of response if the condition passes159 * or fails and consequently returns a boolean.160 *161 * @param cond162 * The condition to check163 * @return true if there is a matching response and if the condition passes164 * else false165 */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 map181 // and if it is passed182 if (res != null) {183 String xmlStr = res.getXml().toString();184 185 DocumentBuilderFactory xmlFact = DocumentBuilderFactory186 .newInstance();187 188 // namespace awareness is escaped since we don't use it189 // for the moment190 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 }223 150 224 151 /** … … 430 357 * Input/Output error 431 358 */ 432 private void doRequests(RequestList requestList) throws IOException{359 private void doRequests(RequestList requestList) { 433 360 logger.trace("doRequests"); 434 361 … … 439 366 for (final String obsID : requests.keySet()) { 440 367 // send request to observer 441 //threadsList.add(new RequestThread(mapOfResponse, requests.get(obsID), obsID, this));442 368 threadsList.add(new RequestThread(requests.get(obsID), obsID, this.getLang())); 443 369 logger.debug("Request " + requests.get(obsID) + " added to threadsList"); 444 370 } 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) { 451 376 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"); 455 382 } catch (InterruptedException e) { 456 383 e.printStackTrace(); … … 577 504 } 578 505 506 public InputParameter getInputParameter() { 507 return inputParameter; 508 } 509 579 510 /** 580 511 * Set the task to perform … … 619 550 this.inputParameter = inputParameter; 620 551 } 552 553 public ArrayList<Message> getMessages() { 554 return messages; 555 } 556 557 public void setMessages(ArrayList<Message> messages) { 558 this.messages = messages; 559 } 621 560 }
