001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.component;
020
021import lucee.runtime.ComponentImpl;
022import lucee.runtime.InterfaceImpl;
023import lucee.runtime.Page;
024import lucee.runtime.PageContext;
025import lucee.runtime.PageSource;
026import lucee.runtime.PageSourceImpl;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.type.util.ArrayUtil;
029
030public class MetadataUtil {
031
032    public static Page getPageWhenMetaDataStillValid(PageContext pc,ComponentImpl comp, boolean ignoreCache) throws PageException {
033        Page page = getPage(pc,comp._getPageSource());
034        if(ignoreCache) return page;
035
036        if(page.metaData!=null && page.metaData.get()!=null) {
037                if(hasChanged(pc,((MetaDataSoftReference)page.metaData).creationTime,comp)) {
038                        page.metaData=null;
039                }
040        }
041        return page;
042        }
043    public static Page getPageWhenMetaDataStillValid(PageContext pc,InterfaceImpl interf, boolean ignoreCache) throws PageException {
044        Page page = getPage(pc,interf.getPageSource());
045        if(ignoreCache) return page;
046        
047        if(page.metaData!=null && page.metaData.get()!=null) {
048                if(hasChanged(pc,((MetaDataSoftReference)page.metaData).creationTime,interf))
049                        page.metaData=null;
050        }
051        return page;
052        }
053    
054        private static boolean hasChanged(PageContext pc,long lastMetaCreation, ComponentImpl cfc) throws PageException {
055                if(cfc==null) return false;
056                
057                // check the component
058                Page p = getPage(pc, cfc._getPageSource());
059                if(hasChanged(p.getCompileTime(),lastMetaCreation)) return true;
060                
061                // check interfaces
062                InterfaceCollection ic = cfc._interfaceCollection();
063        if(ic!=null){
064                if(hasChanged(pc,lastMetaCreation,ic.getInterfaces())) return true;
065        }
066        
067        // check base
068                return hasChanged(pc, lastMetaCreation, (ComponentImpl)cfc.getBaseComponent());
069        }
070        
071        private static boolean hasChanged(PageContext pc,long lastMetaCreation,InterfaceImpl[] interfaces) throws PageException {
072                
073                if(!ArrayUtil.isEmpty(interfaces)){
074            for(int i=0;i<interfaces.length;i++){
075                if(hasChanged(pc,lastMetaCreation,interfaces[i])) return true;
076            }
077        }
078                return false;
079        }
080
081        private static boolean hasChanged(PageContext pc,long lastMetaCreation, InterfaceImpl inter) throws PageException {
082                Page p = getPage(pc, inter.getPageSource());
083                if(hasChanged(p.getCompileTime(),lastMetaCreation)) return true;
084        return hasChanged(pc,lastMetaCreation,inter.getExtends());
085        }
086        
087
088        private static boolean hasChanged(long compileTime, long lastMetaCreation) {
089                return compileTime>lastMetaCreation;
090        }
091        
092        private static Page getPage(PageContext pc,PageSource ps) throws PageException {
093                Page page = ((PageSourceImpl)ps).getPage();
094        if(page==null) {
095                page = ps.loadPage(pc.getConfig());
096        }
097                return page;
098        }
099}