Changeset 28872

Show
Ignore:
Timestamp:
05.03.2014 13:46:28 (7 years ago)
Author:
kjdon
Message:

deleted old unused code. added code to save current tokens to a file when server shuts down, and read them back in when server starts up. note if server crashes then tokens won't be saved. expired tokens are deleted.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/OAIResumptionToken.java

    r28851 r28872  
    2525 
    2626import java.io.File; 
     27import java.net.URL; 
    2728import java.util.HashMap; 
     29import java.util.Iterator; 
     30import java.util.Map; 
     31import java.util.Set; 
     32 
    2833 
    2934// import file Logger.java 
     
    3944  public static final String CURRENT_CURSOR = "current_cursor"; 
    4045 
     46  // for token XML 
     47  public static final String TOKEN = "token"; 
     48  public static final String TOKEN_LIST = "tokenList"; 
     49  public static final String NAME = "name"; 
     50  public static final String EXPIRATION = "expiration"; 
     51  //public static final String FROM 
    4152  public static final String FILE_SEPARATOR = File.separator; 
    4253  static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName()); 
     
    5162  private static HashMap<String, Long> expiration_data = new HashMap<String, Long>(); 
    5263 
    53   /** initialize stored resumption tokens */ 
     64  /** initialize stored resumption tokens and clear any expired ones*/ 
    5465  public static void init() { 
    55  
    56   } 
     66    if (!findOrCreateTokenFile()) { 
     67      // can't read in old tokens 
     68      logger.error("Can't find token file, so we can't initialise tokens"); 
     69      return; 
     70    } 
     71 
     72    Document token_doc = converter.getDOM(resumption_token_file, "utf-8"); 
     73    Element token_elem = null; 
     74    if (token_doc != null) { 
     75      token_elem = token_doc.getDocumentElement(); 
     76    } else { 
     77      logger.error("Failed to parse oai resumption token file OAIResumptionToken.xml."); 
     78      resetTokenFile(); 
     79      return; 
     80    } 
     81       
     82    // read in stored tokens 
     83    NodeList tokens = token_elem.getElementsByTagName(TOKEN); 
     84    for(int i=0; i<tokens.getLength(); i++) { 
     85      Element token = (Element)tokens.item(i); 
     86      String set = token.getAttribute(OAIXML.SET); 
     87      if (set.isEmpty()) { 
     88    set = null; 
     89      } 
     90      String meta_prefix = token.getAttribute(OAIXML.METADATA_PREFIX); 
     91      if (meta_prefix.isEmpty()) { 
     92    meta_prefix = null; 
     93      } 
     94      String from = token.getAttribute(OAIXML.FROM); 
     95      if (from.isEmpty()) { 
     96    from = null; 
     97      } 
     98      String until = token.getAttribute(OAIXML.UNTIL); 
     99      if (until.isEmpty()) { 
     100    until = null; 
     101      } 
     102      storeToken(token.getAttribute(NAME), set, meta_prefix, from, until, token.getAttribute(EXPIRATION)); 
     103 
     104    } 
     105  } 
     106  // store the token read in from saved tokens file 
     107  protected static void storeToken(String name, String set, String metadata_prefix, String from, String until, String expiration) { 
     108    HashMap<String, String> data = new HashMap<String, String>(); 
     109    data.put(OAIXML.FROM, from); 
     110    data.put(OAIXML.UNTIL, until); 
     111    data.put(OAIXML.SET, set); 
     112    data.put(OAIXML.METADATA_PREFIX, metadata_prefix); 
     113    stored_tokens.put(name, data); 
     114    expiration_data.put(name, new Long(expiration)); 
     115  } 
     116 
    57117  /** generate the token, and store all the data for it */ 
    58118  public static String createAndStoreResumptionToken(String set, String metadata_prefix, String from, String until, String cursor, String current_set, String current_cursor) { 
     
    61121    String base_token_name = ""+expiration_date; 
    62122    stored_tokens.put(base_token_name, data);    
    63     expiration_date += (OAIXML.token_expiration*1000); 
     123    expiration_date += (OAIXML.getTokenExpiration()); 
    64124    expiration_data.put(base_token_name, new Long(expiration_date)); 
    65125    String token_name = base_token_name+":" + cursor + ":" + current_set +":"+ current_cursor; 
     
    86146    // when we are generating a new token, we update the expiration date to allow the new token to have the full length of time 
    87147    long exp_date = System.currentTimeMillis(); 
    88     exp_date += (OAIXML.token_expiration*1000); 
     148    exp_date += (OAIXML.getTokenExpiration()); 
    89149    expiration_data.put(token, exp_date); 
    90150     
     
    95155  /** check that this token is currently valid */ 
    96156  public static boolean isValidToken(String token) { 
     157    // we clear expired tokens each time we check one. 
     158    clearExpiredTokens(); 
    97159    // take off the set/cursor parts to check for the main key 
    98160    if (token.indexOf(":") != -1) { 
     
    118180      return null; 
    119181    } 
     182    // add in cursor, etc from the token name     
    120183    if (base_name != token) { 
    121184      String[] parts = token.split(":"); 
     
    127190      data.put(CURRENT_CURSOR, parts[3]); 
    128191    } 
    129     // add in cursor, etc form the token name 
    130     //return stored_tokens.get(token); 
    131192    return data; 
    132193 
    133194  } 
    134195 
     196  // read through all stored expiry dates, and delete any tokens that are too 
     197  // old 
    135198  public static void clearExpiredTokens() { 
    136199 
     200    Set<Map.Entry<String,Long>> token_set = expiration_data.entrySet(); 
     201    Iterator<Map.Entry<String,Long>> i = token_set.iterator(); 
     202    int size = expiration_data.size(); 
     203    logger.error("start tokens "+size); 
     204    Long time_now = System.currentTimeMillis(); 
     205    while (i.hasNext()==true) { 
     206      Map.Entry<String,Long> entry = i.next(); 
     207      String key = entry.getKey(); 
     208      Long exp = entry.getValue(); 
     209      if (exp < time_now) { 
     210    logger.error("token "+key+" is expired, "+ OAIXML.getTime(exp)); 
     211    i.remove(); 
     212    // also remove the token from the stored tokens 
     213    stored_tokens.remove(key); 
     214      } 
     215    } 
     216    size = expiration_data.size(); 
     217    logger.error("end tokens "+size); 
     218  } 
     219 
     220  protected static boolean findOrCreateTokenFile() { 
    137221     
    138   } 
    139   // ******************************************************************** 
    140   // ********************************************************************* 
    141   // following is old code 
    142   /** Read in OAIResumptionToken.xml (residing web/WEB-INF/classes/) */  
    143   public static Element getOAIResumptionTokenXML() {      
    144        
    145     // The system environment variable $GSDL3HOME(ends ../web) does not contain the file separator  
    146     resumption_token_file = new File(GlobalProperties.getGSDL3Home() + FILE_SEPARATOR + 
    147                      "WEB-INF" + FILE_SEPARATOR + "classes" +FILE_SEPARATOR + "OAIResumptionToken.xml"); 
    148     if (resumption_token_file.exists()) { 
    149       Document token_doc = converter.getDOM(resumption_token_file, "utf-8"); 
    150       if (token_doc != null) { 
    151     resumption_token_elem = token_doc.getDocumentElement(); 
    152       } else { 
    153     logger.error("Fail to parse resumption token file OAIReceptionToken.xml."); 
    154     return null; 
    155       } 
    156       //remove all expired tokens 
    157       clearExpiredTokens(); 
    158       return resumption_token_elem;   
    159     } 
    160     //if resumption_token_file does not exist         
    161     logger.info("resumption token file: "+ resumption_token_file.getPath()+" not found! create an empty one."); 
     222    try { 
     223      URL token_file_url = Class.forName("org.greenstone.gsdl3.OAIServer").getClassLoader().getResource("OAIResumptionToken.xml"); 
     224      if (token_file_url != null) { 
     225    resumption_token_file = new File(token_file_url.toURI()); 
     226    if (resumption_token_file != null && resumption_token_file.exists()) { 
     227      return true; 
     228    } 
     229    else { 
     230      logger.error("Resumption token file found, "+ token_file_url.toURI()+" but couldn't create a file from it."); 
     231    } 
     232      } 
     233    } catch (Exception e) { 
     234      logger.error("couldn't find or ResumptionToken.xml "+e.getMessage()); 
     235    } 
     236    // if we have got here, have't managed to load file via class loader - 
     237    // it may not exist yet. 
     238    // try and create a new empty one 
     239    if (resetTokenFile()) { 
     240      return true; 
     241    } 
     242    return false; 
     243  } 
     244 
     245  // If there was no file, or something went wrong with the file, use this to 
     246  // create a new one. 
     247  public static boolean resetTokenFile() { 
     248    resumption_token_file = new File(GlobalProperties.getGSDL3Home() +  
     249                     FILE_SEPARATOR + "WEB-INF" +  
     250                     FILE_SEPARATOR + "classes" + 
     251                     FILE_SEPARATOR + "OAIResumptionToken.xml"); 
     252    try { 
     253      if (!resumption_token_file.createNewFile()) { 
     254    logger.error("Couldn't create a new resumption token file "+ resumption_token_file.getPath()); 
     255    resumption_token_file = null; 
     256    return false; 
     257      } 
     258    } catch (Exception e) { 
     259      logger.error("Couldn't create a new resumption token file "+ resumption_token_file.getPath()+", "+e.getMessage()); 
     260      resumption_token_file = null; 
     261      return false; 
     262    } 
    162263    Document doc = converter.newDOM(); 
    163     resumption_token_elem = doc.createElement(/*OAI_RESUMPTION_TOKENS*/"resumptionTokens"); 
    164     saveOAIResumptionTokenXML(resumption_token_elem); 
    165     return resumption_token_elem; 
    166   } 
    167   public static void saveOAIResumptionTokenXML(Element token_elem) {      
    168     if(converter.writeDOM(token_elem, resumption_token_file) == false) { 
    169       logger.error("Fail to save the resumption token file"); 
    170     } 
    171   } 
    172   public static void clearExpiredTokensOld() { 
    173     boolean token_deleted = false; 
    174     NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN); 
    175     for (int i=0; i<tokens.getLength(); i++) { 
    176       Element token_elem = (Element)tokens.item(i); 
    177       String expire_str = token_elem.getAttribute(OAIXML.EXPIRATION_DATE); 
    178       long datestamp = OAIXML.getTime(expire_str); // expire_str is in milliseconds 
    179       if(datestamp < System.currentTimeMillis()) { 
    180     resumption_token_elem.removeChild(token_elem); 
    181     token_elem = null; 
    182     token_deleted = true; 
    183       } 
    184     }  
    185        
    186     if(token_deleted) { 
    187       saveOAIResumptionTokenXML(resumption_token_elem); 
    188     } 
    189   } 
    190   public static boolean containsToken(String token) { 
    191     NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN); 
    192     for (int i=0; i<tokens.getLength(); i++) { 
    193       if(token.equals(GSXML.getNodeText((Element)tokens.item(i)).trim() )) 
    194     return true; 
    195     } 
     264    Element elem = doc.createElement(TOKEN_LIST); 
     265    if (converter.writeDOM(elem, resumption_token_file)) { 
     266      return true; 
     267    } 
     268    resumption_token_file = null; 
    196269    return false; 
    197270  } 
    198   public static void addToken(Element token) { 
    199     Document doc = resumption_token_elem.getOwnerDocument(); 
    200     resumption_token_elem.appendChild(doc.importNode(token, true)); 
    201     saveOAIResumptionTokenXML(resumption_token_elem); 
    202   } 
    203   public static void addToken(String token) { 
    204     Element te = resumption_token_elem.getOwnerDocument().createElement(OAIXML.RESUMPTION_TOKEN); 
    205     //add expiration att 
    206     resumption_token_elem.appendChild(te); 
    207     saveOAIResumptionTokenXML(resumption_token_elem); 
    208   } 
    209   public static boolean removeToken(String token) { 
    210     NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN); 
    211     int num_tokens = tokens.getLength(); 
    212     for (int i=0; i<num_tokens; i++) { 
    213       Element e = (Element)(tokens.item(i)); 
    214       if(token.equals(GSXML.getNodeText(e))) { 
    215     resumption_token_elem.removeChild(e); 
    216     saveOAIResumptionTokenXML(resumption_token_elem); 
    217     return true; 
    218       } 
    219     } 
    220     return false;       
    221   } 
    222  
     271   
     272 
     273  public static boolean saveTokensToFile() { 
     274    clearExpiredTokens(); 
     275    if (resumption_token_file == null) { 
     276      logger.error("no available resumption token file, not storing tokens"); 
     277      return false; 
     278    } 
     279     
     280    // Create an XML representation of the stored tokens 
     281    Document doc = converter.newDOM(); 
     282    Element token_list = doc.createElement(TOKEN_LIST); 
     283 
     284    if (stored_tokens.size() == 0) { 
     285      // nothing to store, store an empty file 
     286      converter.writeDOM(token_list, resumption_token_file); 
     287      return true; 
     288    } 
     289    Set<String> keys = stored_tokens.keySet(); 
     290    Iterator<String> i = keys.iterator(); 
     291    while(i.hasNext()) { 
     292      String token = i.next(); 
     293      HashMap<String, String> data = stored_tokens.get(token); 
     294      Element token_elem = doc.createElement(TOKEN); 
     295      token_elem.setAttribute(NAME, token); 
     296      token_elem.setAttribute(OAIXML.FROM, data.get(OAIXML.FROM)); 
     297      token_elem.setAttribute(OAIXML.UNTIL, data.get(OAIXML.UNTIL)); 
     298      token_elem.setAttribute(OAIXML.SET, data.get(OAIXML.SET)); 
     299      token_elem.setAttribute(OAIXML.METADATA_PREFIX, data.get(OAIXML.METADATA_PREFIX)); 
     300      token_elem.setAttribute(EXPIRATION, ""+expiration_data.get(token)); 
     301      token_list.appendChild(token_elem); 
     302    } 
     303    converter.writeDOM(token_list, resumption_token_file); 
     304    return true; 
     305  } 
    223306 
    224307}