- Timestamp:
- 08/25/10 20:51:06 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/Cache.java
r651 r655 23 23 import javax.xml.transform.*; 24 24 import javax.xml.transform.sax.*; 25 import javax.xml.transform.stream.*; 26 import javax.xml.transform.dom.*; 25 import javax.xml.transform.stream.*;import javax.xml.transform.dom.*; 27 26 import javax.xml.xpath.*; 28 27 … … 52 51 private static Cache singleton; 53 52 private String cachepath; 53 private String cacheindex_path; 54 54 private static Integer cachecounter; 55 55 private static final Integer start_cache = 1; 56 56 57 private static Document document_counter;57 private static Document cacheindex_doc; 58 58 59 59 public Cache () { 60 60 cachepath = Admin.getConfig().getProperty("cache.path"); 61 cacheindex_path = cachepath + Admin.getConfig().getProperty("cacheindex.file"); 61 62 cachecounter = initCachecounter(); 62 63 } … … 97 98 98 99 //update xml_structure 99 Element e = document_counter.createElement("f");100 //Attr attr = document_counter.createAttribute("id");100 Element e = cacheindex_doc.createElement("f"); 101 //Attr attr = cacheindex_doc.createAttribute("id"); 101 102 //attr.setValue(c.toString()); 102 103 //e.setNamedItem(attr); … … 124 125 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:hh-mm-ss"); 125 126 e.setAttribute("date", dateFormat.format(new Date()).toString()); 126 document_counter.getFirstChild().appendChild(e);127 cacheindex_doc.getFirstChild().appendChild(e); 127 128 128 129 // update XML-counter … … 132 133 133 134 xc = PREFIX + key_array[0] + "_" + c.toString(); 135 Admin.notifyUser("CACHE.putInCache.filename:" + xc + ".xml"); 134 136 Admin.writeToFile(cachepath + xc + ".xml",instream); 135 137 … … 139 141 140 142 public InputStream getFromCache(String key_info) { 143 144 145 File f = new File (formPath(key_info)); 141 146 Admin.notifyUser("CACHE.getFromCache:"+key_info); 142 143 File f = new File (formPath(key_info)); 144 147 145 148 if (f.exists()) { // read from file to InputStream; 149 Admin.notifyUser("CACHE.getFromCache.fromfile:"+f.getName()); 146 150 InputStream instream; 147 151 try { … … 156 160 157 161 } else { //not cached, my dear 162 Admin.notifyUser("CACHE.getFromCache: NOT CACHED"); 158 163 return null; 159 164 } … … 222 227 expr = xpath.compile(xpath_expr); 223 228 //expression is evaluated with respect to a certain context node which is doc. 224 Object result = expr.evaluate( document_counter, XPathConstants.NODESET);229 Object result = expr.evaluate(cacheindex_doc, XPathConstants.NODESET); 225 230 NodeList list = (NodeList) result; 226 231 if (list.getLength() > 0) { … … 254 259 Integer counter = start_cache; 255 260 boolean init=false; 256 String fname = cache path + "cache_counter.xml";261 String fname = cacheindex_path; 257 262 File f = new File (fname); 258 263 … … 264 269 try { 265 270 docBuilder = docFactory.newDocumentBuilder(); 266 document_counter= docBuilder.newDocument();271 cacheindex_doc = docBuilder.newDocument(); 267 272 // append root tag <index > 268 Element root = (Element) document_counter.createElement("index");273 Element root = (Element) cacheindex_doc.createElement("index"); 269 274 root.setAttribute("idcounter", "1"); 270 document_counter.appendChild(root);275 cacheindex_doc.appendChild(root); 271 276 writeCachecounter(counter); 272 277 … … 290 295 docBuilder = docFactory.newDocumentBuilder(); 291 296 try { 292 document_counter= docBuilder.parse(fname);297 cacheindex_doc = docBuilder.parse(fname); 293 298 } catch (SAXException e) { 294 299 // TODO Auto-generated catch block … … 302 307 303 308 //read counter 304 counter = new Integer( document_counter.getFirstChild().getAttributes().getNamedItem("idcounter").getNodeValue());309 counter = new Integer(cacheindex_doc.getFirstChild().getAttributes().getNamedItem("idcounter").getNodeValue()); 305 310 init = true; 306 311 } catch (IOException ex){ … … 351 356 writeCachecounter( cachecounter); 352 357 } 353 public void writeCachecounter (Integer i) { 354 358 public void writeCachecounter (Integer i) { 355 359 356 360 // first update <index idcounter> 357 document_counter.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString());361 cacheindex_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString()); 358 362 359 363 //Admin.notifyUser("writeCacheCounter:" + i.toString()); … … 366 370 //initialize StreamResult with File object to save to file 367 371 StreamResult result = new StreamResult(new StringWriter()); 368 DOMSource source = new DOMSource( document_counter);372 DOMSource source = new DOMSource(cacheindex_doc); 369 373 try { 370 374 transformer.transform(source, result); … … 379 383 380 384 381 File f = new File (cache path + "counter.xml");385 File f = new File (cacheindex_path); 382 386 FileWriter fw; 383 387 try {
Note: See TracChangeset
for help on using the changeset viewer.