001    package railo.runtime.listener;
002    
003    import java.io.IOException;
004    
005    import railo.runtime.CFMLFactory;
006    import railo.runtime.PageContext;
007    import railo.runtime.PageSource;
008    import railo.runtime.config.Constants;
009    import railo.runtime.exp.MissingIncludeException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.op.Caster;
012    import railo.runtime.type.UDF;
013    
014    public final class ClassicAppListener extends AppListenerSupport {
015    
016            private int mode=MODE_CURRENT2ROOT;
017    
018            @Override
019            public void onRequest(PageContext pc,PageSource requestedPage, RequestListener rl) throws PageException {
020                    
021                    PageSource application=//pc.isCFCRequest()?null:
022                            AppListenerUtil.getApplicationPageSource(pc,requestedPage,Constants.APP_CFM,mode);
023                    
024                    _onRequest(pc, requestedPage, application,rl);
025            }
026            
027            static void _onRequest(PageContext pc,PageSource requestedPage,PageSource application, RequestListener rl) throws PageException {
028                    
029                    // on requestStart
030                    if(application!=null)pc.doInclude(new PageSource[]{application},false);
031                    
032                    if(rl!=null) {
033                            requestedPage=rl.execute(pc, requestedPage);
034                            if(requestedPage==null) return;
035                    }
036                    
037                    // request
038                    try{
039                            pc.doInclude(new PageSource[]{requestedPage},false);
040                    }
041                    catch(MissingIncludeException mie){
042                            ApplicationContext ac = pc.getApplicationContext();
043                            boolean rethrow=true;
044                            if(ac instanceof ClassicApplicationContext) {
045                                    ClassicApplicationContext cfc=(ClassicApplicationContext) ac;
046                                    UDF udf = cfc.getOnMissingTemplate();
047                                    if(udf!=null) {
048                                            String targetPage=requestedPage.getFullRealpath();
049                                            rethrow=(!Caster.toBooleanValue(udf.call(pc, new Object[]{targetPage}, true),true));
050                                    }
051                            }
052                            if(rethrow)throw mie;
053                    }
054                    
055                    // on Request End
056                    if(application!=null){
057                            PageSource onReqEnd = application.getRealPage("OnRequestEnd.cfm");
058                    if(onReqEnd.exists())pc.doInclude(new PageSource[]{onReqEnd},false);
059                    }
060            }
061    
062            @Override
063            public boolean onApplicationStart(PageContext pc) throws PageException {
064                    // do nothing
065                    return true;
066            }
067    
068            @Override
069            public void onSessionStart(PageContext pc) throws PageException {
070                    // do nothing
071            }
072    
073            @Override
074            public void onApplicationEnd(CFMLFactory factory, String applicationName) throws PageException {
075                    // do nothing   
076            }
077    
078            @Override
079            public void onSessionEnd(CFMLFactory cfmlFactory, String applicationName, String cfid) throws PageException {
080                    // do nothing
081            }
082    
083            @Override
084            public void onDebug(PageContext pc) throws PageException {
085                    try {
086                            pc.getDebugger().writeOut(pc);
087                    } 
088                    catch (IOException e) {
089                            throw Caster.toPageException(e);
090                    }
091            }
092    
093            @Override
094            public void onError(PageContext pc,PageException pe) {
095                    pc.handlePageException(pe);
096            }
097    
098            @Override
099            public void setMode(int mode) {
100                    this.mode=mode;
101            }
102    
103            @Override
104            public int getMode() {
105                    return mode;
106            }
107    
108            @Override
109            public String getType() {
110                    return "classic";
111            }
112    }