Ignore:
Timestamp:
09/19/11 10:39:55 (13 years ago)
Author:
gaba
Message:

Utils.java in place of Helpers and Admin
Diagnostics changes in GenericAction?
code arrangement, comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/internal/Cache.java

    r1513 r1540  
    3535
    3636import org.apache.commons.lang.StringEscapeUtils;
     37import org.apache.log4j.Logger;
    3738import org.w3c.dom.Document;
    3839import org.w3c.dom.Element;
     
    5051 * mapping the request parameters to the indexed files.</p>
    5152 *
    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>
    5354 * @author master
    5455 *
     
    5657
    5758public class Cache {
     59       
     60        private static Logger log = Logger.getLogger("Cache");
     61       
    5862        public static String PREFIX = "xc_";
    5963       
     
    7276       
    7377        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");
    7680                cachecounter = initCachecounter();
    7781               
     
    125129        public String putInCache(String key_info, InputStream instream) {
    126130
    127                 Admin.notifyUser("CACHE.putInCache:"+key_info);
     131                log.debug("CACHE.putInCache:"+key_info);
    128132                Integer c = getCounter();
    129133                String xc;
    130134               
    131                
    132135                //update xml_structure
    133136                Element e = cacheindex_doc.createElement("f");
    134                 //Attr attr = cacheindex_doc.createAttribute("id");
    135                 //attr.setValue(c.toString());
    136                 //e.setNamedItem(attr);
    137137                e.setAttribute("id", c.toString());
    138138                String[] key_array = key_info.split("//-");
     
    197197               
    198198                // update XML-counter
    199             //Admin.writeToFile(formPath(id + xc),instream);
    200                
    201199                updateCachecounter();
    202200               
    203201                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);
    206204               
    207205            return xc;
     
    213211               
    214212                   File f = new File (formPath(key_info));
    215                    //Admin.notifyUser("CACHE.getFromCache:"+key_info);
    216                    
    217213                   
    218214                    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 + "]");
    220216                        InputStream instream;
    221217                                try {
     
    224220                                        return instream;
    225221                                } catch (FileNotFoundException e) {
    226                                         // TODO Auto-generated catch block
    227                                         e.printStackTrace();
     222                                        Utils.errorMessage(e);
    228223                                        return null;
    229224                                }
    230225                       
    231226                    } else { //not cached, my dear
    232                         Admin.notifyUser("CACHE.getFromCache: NOT CACHED" + "[#" + key_info + "]");
     227                        log.debug("CACHE.getFromCache: NOT CACHED" + "[#" + key_info + "]");
    233228                        return null;
    234229                    }       
     
    245240                query = new Query("", str,"recordset","");
    246241                if (query.isStatus(Query.PARSEERROR)) {
    247                         Admin.notifyUser("Cache.transformQuery.PARSEERROR:" + query.getMsg());
     242                        log.debug("Cache.transformQuery.PARSEERROR:" + query.getMsg());
    248243                        // pass this bad news to the client
    249                         //setUserMsg(query.getMsg());
    250244                        transformed = "";
    251245                } else {
    252246                        transformed =  Query.getSimpleQueryString(str);
    253247                        transformed = StringEscapeUtils.escapeXml(transformed);
    254                         //Admin.notifyUser("CACHE.transformQuery:"+transformed);
     248                        //log.debug("CACHE.transformQuery:"+transformed);
    255249                }
    256250                return transformed;
     
    268262               
    269263                if(key_array.length > 1){
    270                 //      Admin.notifyUser("formPATH- querystring:" + key_array[1]);
     264                //      log.debug("formPATH- querystring:" + key_array[1]);
    271265                        if (key_array[0].equals("recordset")){
    272266                                query_str = this.transformQuery(key_array[1]); 
    273                         //      Admin.notifyUser("formPATH- querystring-transformed:" + query_str);
     267                        //      log.debug("formPATH- querystring-transformed:" + query_str);
    274268                        }
    275269                        else {
     
    319313                }
    320314               
    321                 //Admin.notifyUser("formPath:xpath:"+xpath_expr);
     315                //log.debug("formPath:xpath:"+xpath_expr);
    322316                //creating an XPathFactory:
    323317        XPathFactory factory = XPathFactory.newInstance();
     
    335329                        path = cachepath + PREFIX + key_array[0] + "_"+id + ".xml";
    336330                }  else if (list.getLength() > 1){
    337                         Admin.notifyUser("formPath ERROR:!!!:multiple paths:"+key_info);
     331                        log.debug("formPath ERROR:!!!:multiple paths:"+key_info);
    338332                       
    339333                }
    340334               
    341335                } catch (XPathExpressionException e) {
    342                         // TODO Auto-generated catch block
    343                         Admin.notifyUser("ERROR Cache.formPath: "+xpath_expr);
     336                        log.debug("ERROR Cache.formPath: "+xpath_expr);
     337                        log.error(Utils.errorMessage(e));
    344338                }
    345339               
     
    353347       
    354348        /**
    355          * read counter-integer from file in cache
     349         * Read counter-integer from file in cache
    356350         * or start that file
    357351         * @return The current maximum cache counter
     
    360354               
    361355                Integer counter = start_cache;
    362                 boolean init=false;
    363356                String fname = cacheindex_path;
    364357                File f = new File (fname);
    365358               
    366                 //Admin.notifyUser("initCacheCounter");
    367359                if (!f.exists()) {
    368360                                // create new  counter document
     
    378370                                        writeCachecounter(counter);
    379371                                       
    380                                         //Admin.notifyUser("new document");
    381372                                } catch (ParserConfigurationException e) {
    382                                         // TODO Auto-generated catch block
    383                                         e.printStackTrace();
     373                                        log.error(Utils.errorMessage(e));
    384374                                }
    385                                
    386                         //} catch (IOException e2) {
    387                         //      Admin.notifyUser("ERROR creating cache counter");
    388                         //      e2.printStackTrace();
    389                         //}
    390375            }
    391376                else {
     
    399384                                                        cacheindex_doc = docBuilder.parse(fname);
    400385                                        } catch (SAXException e) {
    401                                                         // TODO Auto-generated catch block
    402                                                        
    403                                                         e.printStackTrace();
     386                                                log.error(Utils.errorMessage(e));
    404387                                        }
    405388                                } catch (ParserConfigurationException e) {
    406                                                 // TODO Auto-generated catch block
    407                                                 e.printStackTrace();
     389                                        log.error(Utils.errorMessage(e));
    408390                                }
    409391                               
    410392                         //read counter
    411393                         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));
    416396                    }
    417397                }
    418398               
    419399            return counter;
    420             /*
    421               //use buffering, reading one line at a time
    422               //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 loop
    426                         /*
    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 number
    438                                                         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              */         
    455400        }
    456401
    457402        public void updateCachecounter () {
    458                 //Admin.notifyUser("CACHEcounter:" + cachecounter);
    459403            writeCachecounter( cachecounter);
    460404        }
     
    464408                cacheindex_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString());
    465409               
    466                 //Admin.notifyUser("writeCacheCounter:" + i.toString());
    467410                // write xml
    468411                Transformer transformer;
     
    476419                        try {
    477420                                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));
    481423                        }
    482424
    483425                        String xmlString = result.getWriter().toString();
    484                         //Admin.notifyUser("writeCacheCounter:" + xmlString);
    485                         //Admin.writeToFile("", result);
    486 
    487426
    488427                        File f = new File (cacheindex_path);
     
    494433                                        fw.close();
    495434                                } catch (IOException e) {
    496                                         // TODO Auto-generated catch block
    497                                         e.printStackTrace();
     435                                        log.error(Utils.errorMessage(e));
    498436                                }       
    499437                        } 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               
    529446        }
    530447       
    531448        public InputStream clear(HashMap<String,String> clear_params) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
    532449                String xpath_expr = "";
    533                 Integer removed = 0;
    534450                InputStream is = null;
    535451               
     
    604520                        Result outputTarget = new StreamResult(outputStream);
    605521                        TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    606                         is = new ByteArrayInputStream(outputStream.toByteArray());
    607 
    608                         removed = list.getLength();
     522                        is = new ByteArrayInputStream(outputStream.toByteArray());
    609523               
    610524                } catch (XPathExpressionException e) {
    611                         // TODO Auto-generated catch block
    612                         e.printStackTrace();
     525                        log.error(Utils.errorMessage(e));
    613526                }
    614527               
Note: See TracChangeset for help on using the changeset viewer.