mcardle     2005/11/04 17:53:37 CET

  Modified files:
    src/org/jahia/esi/invalidation/services Invalidator.java 
  Log:
  * rewrote invalidation mechanism to a more extendible and faster one which 
now supports multiple OTHER tags
  
  Revision  Changes    Path
  1.2       +86 -40    
esi_server/src/org/jahia/esi/invalidation/services/Invalidator.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/esi_server/src/org/jahia/esi/invalidation/services/Invalidator.java.diff?r1=1.1&r2=1.2&f=h
  
  
  
  Index: Invalidator.java
  ===================================================================
  RCS file: 
/home/cvs/repository/esi_server/src/org/jahia/esi/invalidation/services/Invalidator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Invalidator.java  2 Nov 2005 15:50:14 -0000       1.1
  +++ Invalidator.java  4 Nov 2005 16:53:37 -0000       1.2
  @@ -12,6 +12,7 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.regex.Pattern;
  +import java.util.regex.Matcher;
   
   import org.apache.commons.codec.binary.Base64;
   import org.apache.commons.digester.Digester;
  @@ -362,89 +363,134 @@
           }
       }
   
  +    //type of regex matching
  +    static final Integer MATCHES = new Integer(0);
  +    static final Integer FIND = new Integer(1);
  +    static final Integer LOOKINGAT = new Integer(2);
  +
       private String processInvalidationObjects(Vector objects) {
   
           FragmentCache cache = FragmentCache.getInstance();
           HashMap urlCache = cache.getUrlCache();
   
  -        Enumeration enumVec = objects.elements();
  -
           StringBuffer respMsg = new StringBuffer("");
   
  -        while (enumVec.hasMoreElements()) {
  +        Enumeration enumObj = objects.elements();
  +        while (enumObj.hasMoreElements()) {
               org.jahia.esi.invalidation.markup.Object obj =
  -                    (org.jahia.esi.invalidation.markup.Object) 
enumVec.nextElement();
  +                    (org.jahia.esi.invalidation.markup.Object) 
enumObj.nextElement();
   
               log.debug("Invalidation Object :" +obj);
  -
  -            //TODO: obj.getREMOVALTTL();
  +            
  +            Vector matchers = new Vector();  //holds all URL matchers
  +            int numInv = 0; //number of succesfully invalidated objects
   
               Enumeration enumAdvSel = obj.getAdvancedSelectors().elements();
               while (enumAdvSel.hasMoreElements()) {
                   AdvancedSelector advSel = (AdvancedSelector) 
enumAdvSel.nextElement();
   
  -                Iterator Itid = urlCache.keySet().iterator();
  -
  -                //TODO: support COOKIE and HEADER
  -                Pattern otherpUriExp = null;
  -                if (advSel.getOthers().size()>0)   {
  -                    Other other = (Other) advSel.getOthers().firstElement(); 
//TODO: support more than one OTHER tag
  +                //Deal with OTHER tags
  +                //TODO: support COOKIE and HEADER tags
  +                Enumeration enumOther = advSel.getOthers().elements();
  +                while (enumOther.hasMoreElements()) {
  +                    Other other = (Other) enumOther.nextElement();
  +                    //TODO: support OTHER 
NAME="URI|BODY|QUERYSTRING_PARAMETER|SEARCHKEY"  and TYPE="SUBSTRING|REGEX"
                       if ("URI".equals(other.getNAME()) &&
                               "REGEX".equals(other.getTYPE()) &&
                               other.getVALUE() != null) {
   
  -                        //convert it from the Oracle format
  -                        if (other.getVALUE().indexOf("__esi_fragment=") !=-1)
  -                            otherpUriExp = 
Pattern.compile(".*"+other.getVALUE()+".*");
  -                        else
  -                            otherpUriExp = 
Pattern.compile(".*"+other.getVALUE());
  +                        Matcher mOtherpUriExp = 
getReformatedMatcher(other.getVALUE());
  +                        matchers.addElement(new 
java.lang.Object[]{mOtherpUriExp, MATCHES});
                       }
                   }
   
  -                Pattern pUriExp = null;
  +                //Deal with ADVANCEDSELECTOR attributes
                   if (advSel.getURIEXP()!=null) {
  -                    //convert it from the Oracle format
  -                    if (advSel.getURIEXP().indexOf("__esi_fragment=") !=-1)
  -                        pUriExp = 
Pattern.compile(".*"+advSel.getURIEXP()+".*");
  -                    else
  -                        pUriExp = Pattern.compile(".*"+advSel.getURIEXP());
  +                    Matcher mUriExp = 
getReformatedMatcher(advSel.getURIEXP());
  +                    matchers.addElement(new Object[]{mUriExp, MATCHES});
  +                }
  +                if (advSel.getHOST()!=null) {
  +                    matchers.addElement(
  +                            new 
java.lang.Object[]{Pattern.compile(advSel.getHOST()).matcher(""), FIND}
  +                    );
  +                }
  +                if (advSel.getURIPREFIX()!=null) {
  +                    matchers.addElement(
  +                            new 
java.lang.Object[]{Pattern.compile(advSel.getURIPREFIX()).matcher(""), FIND}
  +                    );
                   }
   
  -                Pattern pHost = 
(advSel.getHOST()==null)?null:Pattern.compile(advSel.getHOST());
  -                Pattern pMethod = 
(advSel.getMETHOD()==null)?null:Pattern.compile(advSel.getMETHOD()); //TODO:use 
it
  -                Pattern pUriPrefix = 
(advSel.getURIPREFIX()==null)?null:Pattern.compile(advSel.getURIPREFIX());
  +                //TODO: add METHOD support :
  +                // Pattern pMethod = 
(advSel.getMETHOD()==null)?null:Pattern.compile(advSel.getMETHOD());
   
  +                //Search cache for any matches
                   Vector urlKeysToRemove = new Vector();
  -
  -                while (Itid.hasNext()) {
  -                    String urlKey = (String) Itid.next();
  +                Iterator urlItid = urlCache.keySet().iterator();
  +                while (urlItid.hasNext()) {
  +                    String urlKey = (String) urlItid.next();
                       UrlCacheObject urlObj = (UrlCacheObject) 
urlCache.get(urlKey);
   
  -                    if( otherpUriExp==null || 
otherpUriExp.matcher(urlObj.getUrl()).matches() ) {
  -                        if( pUriExp==null || 
pUriExp.matcher(urlObj.getUrl()).matches() ) {
  -                            if (pHost==null || 
pHost.matcher(urlObj.getUrl()).find() ) {
  -                                if (pUriPrefix==null || 
pUriPrefix.matcher(urlObj.getUrl()).find()  ) {//TODO: check if we want matches 
here
  -                                    //TODO: return a proper invalidation XML 
document
  -                                    //cannot remove from within a collection 
iteration, so just flagging it for later removal
  -                                    urlKeysToRemove.addElement(urlKey);
  -                                }
  -                            }
  +                    boolean validMatch = true;
  +
  +                    Enumeration enumM = matchers.elements();
  +                    while (enumM.hasMoreElements()) {
  +                        Object[] ot = (java.lang.Object[]) 
enumM.nextElement();
  +                        Matcher matcher = (Matcher)ot[0];
  +                        Integer type = (Integer)ot[1];
  +
  +                        matcher.reset(urlObj.getUrl());
  +
  +                        boolean success = false;
  +                        if (type==MATCHES)
  +                            success = matcher.matches();
  +                        else if (type==FIND)
  +                            success = matcher.find();
  +                        else if (type==LOOKINGAT)
  +                            success = matcher.lookingAt();
  +                        else
  +                            log.error("This is impossible");
  +
  +                        if (success) {
  +                            validMatch = false;
  +                            break;
                           }
                       }
  +                    if (validMatch) {
  +                        //TODO: return a proper invalidation XML document
  +                        //cannot remove from within a collection iteration, 
so just flagging it for later removal
  +                        urlKeysToRemove.addElement(urlKey);
  +                    }
                   }
   
  +                //Delete matching entries from cache
                   if (urlKeysToRemove.size()>0) {
                       Enumeration enumK = urlKeysToRemove.elements();
                       while (enumK.hasMoreElements()) {
                           String urlKey = (String) enumK.nextElement();
                           cache.remove(urlKey);
  +                        //TODO: support obj.getREMOVALTTL();
                           respMsg.append("Removed ["+urlKey+"] from Cache with 
INFO: ["+obj.getINFO()+"]\n");
  +                        numInv++;
                       }
                   }
  -            }
  -        }
   
  +            } //end advancedselector
  +
  +            respMsg.append("\nNumber of objects Invalidated : "+numInv+"\n");
  +
  +        }//end Object
           return respMsg.toString();
  +
  +    }
  +
  +    private Matcher getReformatedMatcher(String pat) {
  +        //converts Regex Pattern from the Oracle-compatible format to a 
usable format
  +        Matcher mOtherpUriExp;
  +        if (pat.indexOf("__esi_fragment=") !=-1)
  +            mOtherpUriExp = Pattern.compile(".*"+pat+".*").matcher("");
  +        else
  +            mOtherpUriExp = Pattern.compile(".*"+pat).matcher("");
  +        return mOtherpUriExp;
       }
   
   
  

Reply via email to