Changeset 1540 for MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/internal/Cache.java
- Timestamp:
- 09/19/11 10:39:55 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/internal/Cache.java
r1513 r1540 35 35 36 36 import org.apache.commons.lang.StringEscapeUtils; 37 import org.apache.log4j.Logger; 37 38 import org.w3c.dom.Document; 38 39 import org.w3c.dom.Element; … … 50 51 * mapping the request parameters to the indexed files.</p> 51 52 * 52 * This is an internal object, direct user access to cache-data via interface is only possible via <code> AdminAction</code>53 * This is an internal object, direct user access to cache-data via interface is only possible via <code>...Action</code> 53 54 * @author master 54 55 * … … 56 57 57 58 public class Cache { 59 60 private static Logger log = Logger.getLogger("Cache"); 61 58 62 public static String PREFIX = "xc_"; 59 63 … … 72 76 73 77 public Cache () { 74 cachepath = Admin.getConfig().getProperty("cache.path");75 cacheindex_path = cachepath + Admin.getConfig().getProperty("cacheindex.file");78 cachepath = Utils.getConfig().getProperty("cache.path"); 79 cacheindex_path = cachepath + Utils.getConfig().getProperty("cacheindex.file"); 76 80 cachecounter = initCachecounter(); 77 81 … … 125 129 public String putInCache(String key_info, InputStream instream) { 126 130 127 Admin.notifyUser("CACHE.putInCache:"+key_info);131 log.debug("CACHE.putInCache:"+key_info); 128 132 Integer c = getCounter(); 129 133 String xc; 130 134 131 132 135 //update xml_structure 133 136 Element e = cacheindex_doc.createElement("f"); 134 //Attr attr = cacheindex_doc.createAttribute("id");135 //attr.setValue(c.toString());136 //e.setNamedItem(attr);137 137 e.setAttribute("id", c.toString()); 138 138 String[] key_array = key_info.split("//-"); … … 197 197 198 198 // update XML-counter 199 //Admin.writeToFile(formPath(id + xc),instream);200 201 199 updateCachecounter(); 202 200 203 201 xc = PREFIX + key_array[0] + "_" + c.toString(); 204 Admin.notifyUser("CACHE.putInCache.filename:" + xc + ".xml");205 Admin.writeToFile(cachepath + xc + ".xml",instream);202 log.debug("CACHE.putInCache.filename:" + xc + ".xml"); 203 Utils.write2File(cachepath + xc + ".xml",instream); 206 204 207 205 return xc; … … 213 211 214 212 File f = new File (formPath(key_info)); 215 //Admin.notifyUser("CACHE.getFromCache:"+key_info);216 217 213 218 214 if (f.exists()) { // read from file to InputStream; 219 Admin.notifyUser("CACHE.getFromCache.fromfile:"+f.getName() + "[#" + key_info + "]");215 log.debug("CACHE.getFromCache.fromfile:"+f.getName() + "[#" + key_info + "]"); 220 216 InputStream instream; 221 217 try { … … 224 220 return instream; 225 221 } catch (FileNotFoundException e) { 226 // TODO Auto-generated catch block 227 e.printStackTrace(); 222 Utils.errorMessage(e); 228 223 return null; 229 224 } 230 225 231 226 } else { //not cached, my dear 232 Admin.notifyUser("CACHE.getFromCache: NOT CACHED" + "[#" + key_info + "]");227 log.debug("CACHE.getFromCache: NOT CACHED" + "[#" + key_info + "]"); 233 228 return null; 234 229 } … … 245 240 query = new Query("", str,"recordset",""); 246 241 if (query.isStatus(Query.PARSEERROR)) { 247 Admin.notifyUser("Cache.transformQuery.PARSEERROR:" + query.getMsg());242 log.debug("Cache.transformQuery.PARSEERROR:" + query.getMsg()); 248 243 // pass this bad news to the client 249 //setUserMsg(query.getMsg());250 244 transformed = ""; 251 245 } else { 252 246 transformed = Query.getSimpleQueryString(str); 253 247 transformed = StringEscapeUtils.escapeXml(transformed); 254 // Admin.notifyUser("CACHE.transformQuery:"+transformed);248 //log.debug("CACHE.transformQuery:"+transformed); 255 249 } 256 250 return transformed; … … 268 262 269 263 if(key_array.length > 1){ 270 // Admin.notifyUser("formPATH- querystring:" + key_array[1]);264 // log.debug("formPATH- querystring:" + key_array[1]); 271 265 if (key_array[0].equals("recordset")){ 272 266 query_str = this.transformQuery(key_array[1]); 273 // Admin.notifyUser("formPATH- querystring-transformed:" + query_str);267 // log.debug("formPATH- querystring-transformed:" + query_str); 274 268 } 275 269 else { … … 319 313 } 320 314 321 // Admin.notifyUser("formPath:xpath:"+xpath_expr);315 //log.debug("formPath:xpath:"+xpath_expr); 322 316 //creating an XPathFactory: 323 317 XPathFactory factory = XPathFactory.newInstance(); … … 335 329 path = cachepath + PREFIX + key_array[0] + "_"+id + ".xml"; 336 330 } else if (list.getLength() > 1){ 337 Admin.notifyUser("formPath ERROR:!!!:multiple paths:"+key_info);331 log.debug("formPath ERROR:!!!:multiple paths:"+key_info); 338 332 339 333 } 340 334 341 335 } catch (XPathExpressionException e) { 342 // TODO Auto-generated catch block343 Admin.notifyUser("ERROR Cache.formPath: "+xpath_expr);336 log.debug("ERROR Cache.formPath: "+xpath_expr); 337 log.error(Utils.errorMessage(e)); 344 338 } 345 339 … … 353 347 354 348 /** 355 * read counter-integer from file in cache349 * Read counter-integer from file in cache 356 350 * or start that file 357 351 * @return The current maximum cache counter … … 360 354 361 355 Integer counter = start_cache; 362 boolean init=false;363 356 String fname = cacheindex_path; 364 357 File f = new File (fname); 365 358 366 //Admin.notifyUser("initCacheCounter");367 359 if (!f.exists()) { 368 360 // create new counter document … … 378 370 writeCachecounter(counter); 379 371 380 //Admin.notifyUser("new document");381 372 } catch (ParserConfigurationException e) { 382 // TODO Auto-generated catch block 383 e.printStackTrace(); 373 log.error(Utils.errorMessage(e)); 384 374 } 385 386 //} catch (IOException e2) {387 // Admin.notifyUser("ERROR creating cache counter");388 // e2.printStackTrace();389 //}390 375 } 391 376 else { … … 399 384 cacheindex_doc = docBuilder.parse(fname); 400 385 } catch (SAXException e) { 401 // TODO Auto-generated catch block 402 403 e.printStackTrace(); 386 log.error(Utils.errorMessage(e)); 404 387 } 405 388 } catch (ParserConfigurationException e) { 406 // TODO Auto-generated catch block 407 e.printStackTrace(); 389 log.error(Utils.errorMessage(e)); 408 390 } 409 391 410 392 //read counter 411 393 counter = new Integer(cacheindex_doc.getFirstChild().getAttributes().getNamedItem("idcounter").getNodeValue()); 412 init = true; 413 } catch (IOException ex){ 414 //Admin.notifyUser("initCacheCounter:" + ex.toString()); 415 ex.printStackTrace(); 394 } catch (IOException e){ 395 log.error(Utils.errorMessage(e)); 416 396 } 417 397 } 418 398 419 399 return counter; 420 /*421 //use buffering, reading one line at a time422 //FileReader always assumes default encoding is OK!423 BufferedReader input = new BufferedReader(new FileReader(f));424 try {425 String line = null; //not declared within while loop426 /*427 * readLine is a bit quirky :428 * it returns the content of a line MINUS the newline.429 * it returns null only for the END of the stream.430 * it returns an empty String if two newlines appear in a row.431 *432 try {433 if (( line = input.readLine()) != null){434 try {435 counter = new Integer(line);436 } catch (NumberFormatException e) {437 // if not a number, write a number438 init=true;439 }440 } else {441 init=true;442 }443 } catch (IOException e) {444 init=true;445 }446 447 if (init) {448 writeCachecounter( start_cache);449 }450 451 } finally {452 input.close();453 }454 */455 400 } 456 401 457 402 public void updateCachecounter () { 458 //Admin.notifyUser("CACHEcounter:" + cachecounter);459 403 writeCachecounter( cachecounter); 460 404 } … … 464 408 cacheindex_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString()); 465 409 466 //Admin.notifyUser("writeCacheCounter:" + i.toString());467 410 // write xml 468 411 Transformer transformer; … … 476 419 try { 477 420 transformer.transform(source, result); 478 } catch (TransformerException e1) { 479 // TODO Auto-generated catch block 480 e1.printStackTrace(); 421 } catch (TransformerException e) { 422 log.error(Utils.errorMessage(e)); 481 423 } 482 424 483 425 String xmlString = result.getWriter().toString(); 484 //Admin.notifyUser("writeCacheCounter:" + xmlString);485 //Admin.writeToFile("", result);486 487 426 488 427 File f = new File (cacheindex_path); … … 494 433 fw.close(); 495 434 } catch (IOException e) { 496 // TODO Auto-generated catch block 497 e.printStackTrace(); 435 log.error(Utils.errorMessage(e)); 498 436 } 499 437 } catch (IOException e) { 500 // TODO Auto-generated catch block 501 e.printStackTrace(); 502 } 503 } catch (TransformerConfigurationException e2) { 504 // TODO Auto-generated catch block 505 e2.printStackTrace(); 506 } catch (TransformerFactoryConfigurationError e2) { 507 // TODO Auto-generated catch block 508 e2.printStackTrace(); 509 } 510 511 512 513 /* 514 File f = new File (Admin.getConfig().getProperty("cache.path") + "counter.txt"); 515 516 try { 517 Writer output = new BufferedWriter(new FileWriter(f)); 518 //FileWriter always assumes default encoding is OK! 519 try { 520 output.write( i); 521 } finally { 522 output.close(); 523 } 524 } catch (IOException e) { 525 // TODO Auto-generated catch block 526 e.printStackTrace(); 527 } 528 */ 438 log.error(Utils.errorMessage(e)); 439 } 440 } catch (TransformerConfigurationException e) { 441 log.error(Utils.errorMessage(e)); 442 } catch (TransformerFactoryConfigurationError e) { 443 log.error(Utils.errorMessage(e)); 444 } 445 529 446 } 530 447 531 448 public InputStream clear(HashMap<String,String> clear_params) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { 532 449 String xpath_expr = ""; 533 Integer removed = 0;534 450 InputStream is = null; 535 451 … … 604 520 Result outputTarget = new StreamResult(outputStream); 605 521 TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); 606 is = new ByteArrayInputStream(outputStream.toByteArray()); 607 608 removed = list.getLength(); 522 is = new ByteArrayInputStream(outputStream.toByteArray()); 609 523 610 524 } catch (XPathExpressionException e) { 611 // TODO Auto-generated catch block 612 e.printStackTrace(); 525 log.error(Utils.errorMessage(e)); 613 526 } 614 527
Note: See TracChangeset
for help on using the changeset viewer.