Search in sources :

Example 71 with PageContextImpl

use of lucee.runtime.PageContextImpl in project Lucee by lucee.

the class ModernAppListener method onApplicationEnd.

@Override
public void onApplicationEnd(CFMLFactory factory, String applicationName) throws PageException {
    Component app = apps.get(applicationName);
    if (app == null || !app.containsKey(ON_APPLICATION_END))
        return;
    PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
    boolean createPc = pc == null;
    try {
        if (createPc)
            pc = createPageContext(factory, app, applicationName, null, ON_APPLICATION_END, true, -1);
        call(app, pc, ON_APPLICATION_END, new Object[] { pc.applicationScope() }, true);
    } finally {
        if (createPc && pc != null) {
            factory.releaseLuceePageContext(pc, createPc);
        }
    }
}
Also used : PageContextImpl(lucee.runtime.PageContextImpl) Component(lucee.runtime.Component)

Example 72 with PageContextImpl

use of lucee.runtime.PageContextImpl in project Lucee by lucee.

the class ModernAppListener method createPageContext.

private PageContextImpl createPageContext(CFMLFactory factory, Component app, String applicationName, String cfid, Collection.Key methodName, boolean register, long timeout) throws PageException {
    Resource root = factory.getConfig().getRootDirectory();
    String path = app.getPageSource().getRealpathWithVirtual();
    // Request
    HttpServletRequestDummy req = new HttpServletRequestDummy(root, "localhost", path, "", null, null, null, null, null, null);
    if (!StringUtil.isEmpty(cfid))
        req.setCookies(new Cookie[] { new Cookie("cfid", cfid), new Cookie("cftoken", "0") });
    // Response
    OutputStream os = DevNullOutputStream.DEV_NULL_OUTPUT_STREAM;
    // File based output stream
    /*try {
			Resource out = factory.getConfig().getConfigDir().getRealResource("output/"+methodName.getString()+".out");
			out.getParentResource().mkdirs();
			os = out.getOutputStream(false);
		} 
		catch (IOException e) {}*/
    HttpServletResponseDummy rsp = new HttpServletResponseDummy(os);
    // PageContext
    PageContextImpl pc = (PageContextImpl) factory.getLuceePageContext(factory.getServlet(), req, rsp, null, false, -1, false, register, timeout, true, false);
    // ApplicationContext
    ClassicApplicationContext ap = new ClassicApplicationContext(factory.getConfig(), applicationName, false, app == null ? null : ResourceUtil.getResource(pc, app.getPageSource(), null));
    initApplicationContext(pc, app);
    ap.setName(applicationName);
    ap.setSetSessionManagement(true);
    // if(!ap.hasName())ap.setName("Controler")
    // Base
    pc.setBase(app.getPageSource());
    return pc;
}
Also used : Cookie(javax.servlet.http.Cookie) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) DevNullOutputStream(lucee.commons.io.DevNullOutputStream) OutputStream(java.io.OutputStream) Resource(lucee.commons.io.res.Resource) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) PageContextImpl(lucee.runtime.PageContextImpl)

Example 73 with PageContextImpl

use of lucee.runtime.PageContextImpl in project Lucee by lucee.

the class CallStackGet method _getTagContext.

public static void _getTagContext(PageContext pc, Array tagContext, Throwable t, Collection.Key lineNumberName) {
    // Throwable root = t.getRootCause();
    Throwable cause = t.getCause();
    if (cause != null)
        _getTagContext(pc, tagContext, cause, lineNumberName);
    StackTraceElement[] traces = t.getStackTrace();
    UDF[] udfs = ((PageContextImpl) pc).getUDFs();
    int line = 0;
    String template;
    Struct item;
    StackTraceElement trace = null;
    String functionName, methodName;
    int index = udfs.length - 1;
    for (int i = 0; i < traces.length; i++) {
        trace = traces[i];
        template = trace.getFileName();
        if (trace.getLineNumber() <= 0 || template == null || ResourceUtil.getExtension(template, "").equals("java"))
            continue;
        methodName = trace.getMethodName();
        if (methodName != null && methodName.startsWith("udfCall") && index > -1)
            functionName = udfs[index--].getFunctionName();
        else
            functionName = "";
        item = new StructImpl();
        line = trace.getLineNumber();
        item.setEL(KeyConstants._function, functionName);
        /* template is now a absolute path
			try {
				template=ExpandPath.call(pc, template);
			}
			catch (PageException e) {}*/
        item.setEL(KeyConstants._template, abs((PageContextImpl) pc, template));
        item.setEL(lineNumberName, new Double(line));
        tagContext.appendEL(item);
    }
}
Also used : StructImpl(lucee.runtime.type.StructImpl) UDF(lucee.runtime.type.UDF) PageContextImpl(lucee.runtime.PageContextImpl) Struct(lucee.runtime.type.Struct)

Example 74 with PageContextImpl

use of lucee.runtime.PageContextImpl in project Lucee by lucee.

the class GetUsageData method call.

public static Struct call(PageContext pc) throws PageException {
    ConfigWeb cw = pc.getConfig();
    ConfigServer cs = cw.getConfigServer("server");
    ConfigWeb[] webs = cs.getConfigWebs();
    CFMLEngineFactory.getInstance();
    CFMLEngineImpl engine = (CFMLEngineImpl) cs.getCFMLEngine();
    Struct sct = new StructImpl();
    // Locks
    /*LockManager manager = pc.getConfig().getLockManager();
        String[] locks = manager.getOpenLockNames();
        for(int i=0;i<locks.length;i++){
        	locks[i].
        }
        if(!ArrayUtil.isEmpty(locks)) 
        	strLocks=" open locks at this time ("+List.arrayToList(locks, ", ")+").";
		*/
    // Requests
    Query req = new QueryImpl(new Collection.Key[] { KeyConstants._web, KeyConstants._uri, START_TIME, KeyConstants._timeout }, 0, "requests");
    sct.setEL(KeyConstants._requests, req);
    // Template Cache
    Query tc = new QueryImpl(new Collection.Key[] { KeyConstants._web, ELEMENTS, KeyConstants._size }, 0, "templateCache");
    sct.setEL(KeyImpl.init("templateCache"), tc);
    // Scopes
    Struct scopes = new StructImpl();
    sct.setEL(KeyConstants._scopes, scopes);
    Query app = new QueryImpl(new Collection.Key[] { KeyConstants._web, KeyConstants._application, ELEMENTS, KeyConstants._size }, 0, "templateCache");
    scopes.setEL(KeyConstants._application, app);
    Query sess = new QueryImpl(new Collection.Key[] { KeyConstants._web, KeyConstants._application, USERS, ELEMENTS, KeyConstants._size }, 0, "templateCache");
    scopes.setEL(KeyConstants._session, sess);
    // Query
    Query qry = new QueryImpl(new Collection.Key[] { KeyConstants._web, KeyConstants._application, START_TIME, KeyConstants._sql }, 0, "requests");
    sct.setEL(QUERIES, qry);
    // Locks
    Query lck = new QueryImpl(new Collection.Key[] { KeyConstants._web, KeyConstants._application, KeyConstants._name, START_TIME, KeyConstants._timeout, KeyConstants._type }, 0, "requests");
    sct.setEL(LOCKS, lck);
    // Loop webs
    ConfigWebImpl web;
    Map<Integer, PageContextImpl> pcs;
    PageContextImpl _pc;
    int row, openConnections = 0;
    CFMLFactoryImpl factory;
    ActiveQuery[] queries;
    ActiveQuery aq;
    ActiveLock[] locks;
    ActiveLock al;
    for (int i = 0; i < webs.length; i++) {
        // Loop requests
        web = (ConfigWebImpl) webs[i];
        factory = (CFMLFactoryImpl) web.getFactory();
        pcs = factory.getActivePageContexts();
        Iterator<PageContextImpl> it = pcs.values().iterator();
        while (it.hasNext()) {
            _pc = it.next();
            if (_pc.isGatewayContext())
                continue;
            // Request
            row = req.addRow();
            req.setAt(KeyConstants._web, row, web.getLabel());
            req.setAt(KeyConstants._uri, row, getPath(_pc.getHttpServletRequest()));
            req.setAt(START_TIME, row, new DateTimeImpl(pc.getStartTime(), false));
            req.setAt(KeyConstants._timeout, row, new Double(pc.getRequestTimeout()));
            // Query
            queries = _pc.getActiveQueries();
            if (queries != null) {
                for (int y = 0; y < queries.length; y++) {
                    aq = queries[y];
                    row = qry.addRow();
                    qry.setAt(KeyConstants._web, row, web.getLabel());
                    qry.setAt(KeyConstants._application, row, _pc.getApplicationContext().getName());
                    qry.setAt(START_TIME, row, new DateTimeImpl(web, aq.startTime, true));
                    qry.setAt(KeyConstants._sql, row, aq.sql);
                }
            }
            // Lock
            locks = _pc.getActiveLocks();
            if (locks != null) {
                for (int y = 0; y < locks.length; y++) {
                    al = locks[y];
                    row = lck.addRow();
                    lck.setAt(KeyConstants._web, row, web.getLabel());
                    lck.setAt(KeyConstants._application, row, _pc.getApplicationContext().getName());
                    lck.setAt(KeyConstants._name, row, al.name);
                    lck.setAt(START_TIME, row, new DateTimeImpl(web, al.startTime, true));
                    lck.setAt(KeyConstants._timeout, row, Caster.toDouble(al.timeoutInMillis / 1000));
                    lck.setAt(KeyConstants._type, row, al.type == LockManager.TYPE_EXCLUSIVE ? "exclusive" : "readonly");
                }
            }
        }
        Iterator<Integer> _it = web.getDatasourceConnectionPool().openConnections().values().iterator();
        while (_it.hasNext()) {
            openConnections += _it.next().intValue();
        }
        // Template Cache
        Mapping[] mappings = ConfigWebUtil.getAllMappings(web);
        long[] tce = templateCacheElements(mappings);
        row = tc.addRow();
        tc.setAt(KeyConstants._web, row, web.getLabel());
        tc.setAt(KeyConstants._size, row, new Double(tce[1]));
        tc.setAt(ELEMENTS, row, new Double(tce[0]));
        // Scope Application
        getAllApplicationScopes(web, factory.getScopeContext(), app);
        getAllCFSessionScopes(web, factory.getScopeContext(), sess);
    }
    // Datasource
    Struct ds = new StructImpl();
    sct.setEL(KeyConstants._datasources, ds);
    // there is only one cache for all contexts
    ds.setEL(CACHED_QUERIES, Caster.toDouble(pc.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_QUERY, null).size(pc)));
    // ds.setEL(CACHED_QUERIES, Caster.toDouble(pc.getQueryCache().size(pc))); // there is only one cache for all contexts
    ds.setEL(OPEN_CONNECTIONS, Caster.toDouble(openConnections));
    // Memory
    Struct mem = new StructImpl();
    sct.setEL(KeyConstants._memory, mem);
    mem.setEL("heap", SystemUtil.getMemoryUsageAsStruct(SystemUtil.MEMORY_TYPE_HEAP));
    mem.setEL("nonheap", SystemUtil.getMemoryUsageAsStruct(SystemUtil.MEMORY_TYPE_NON_HEAP));
    // uptime
    sct.set("uptime", new DateTimeImpl(engine.uptime(), true));
    // now
    sct.set("now", new DateTimeImpl(pc));
    return sct;
}
Also used : ActiveQuery(lucee.runtime.debug.ActiveQuery) Query(lucee.runtime.type.Query) Mapping(lucee.runtime.Mapping) Struct(lucee.runtime.type.Struct) QueryImpl(lucee.runtime.type.QueryImpl) ActiveLock(lucee.runtime.debug.ActiveLock) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) ConfigServer(lucee.runtime.config.ConfigServer) PageContextImpl(lucee.runtime.PageContextImpl) ConfigWeb(lucee.runtime.config.ConfigWeb) CFMLEngineImpl(lucee.runtime.engine.CFMLEngineImpl) ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) StructImpl(lucee.runtime.type.StructImpl) ActiveQuery(lucee.runtime.debug.ActiveQuery) Collection(lucee.runtime.type.Collection)

Example 75 with PageContextImpl

use of lucee.runtime.PageContextImpl in project Lucee by lucee.

the class InternalRequest method call.

public static Struct call(final PageContext pc, String template, String method, Struct urls, Struct forms, Struct cookies, Struct headers, Object body, String strCharset, boolean addToken) throws PageException {
    // add token
    if (addToken) {
        // if(true) throw new ApplicationException("addtoken==true");
        if (cookies == null)
            cookies = new StructImpl();
        cookies.set(KeyConstants._cfid, pc.getCFID());
        cookies.set(KeyConstants._cftoken, pc.getCFToken());
        String jsessionid = pc.getJSessionId();
        if (jsessionid != null)
            cookies.set("jsessionid", jsessionid);
    }
    // charset
    Charset reqCharset = StringUtil.isEmpty(strCharset) ? pc.getWebCharset() : CharsetUtil.toCharset(strCharset);
    String ext = ResourceUtil.getExtension(template, null);
    // welcome files
    if (StringUtil.isEmpty(ext)) {
        throw new FunctionException(pc, "Invoke", 1, "url", "welcome file listing not supported, please define the template name.");
    }
    // dialect
    int dialect = ((CFMLFactoryImpl) pc.getConfig().getFactory()).toDialect(ext, -1);
    if (dialect == -1)
        dialect = pc.getCurrentTemplateDialect();
    // CFMLEngine.DIALECT_LUCEE
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] _barr = null;
    if (Decision.isBinary(body))
        _barr = Caster.toBinary(body);
    else if (body != null) {
        Charset cs = null;
        // get charset
        if (headers != null) {
            String strCT = Caster.toString(headers.get(CONTENT_TYPE), null);
            if (strCT != null) {
                ContentType ct = HTTPUtil.toContentType(strCT, null);
                if (ct != null) {
                    String strCS = ct.getCharset();
                    if (!StringUtil.isEmpty(strCS))
                        cs = CharsetUtil.toCharSet(strCS, CharSet.UTF8).toCharset();
                }
            }
        }
        if (cs == null)
            cs = CharsetUtil.UTF8;
        String str = Caster.toString(body);
        _barr = str.getBytes(cs);
    }
    PageContextImpl _pc = createPageContext(pc, template, urls, cookies, headers, _barr, reqCharset, baos);
    fillForm(_pc, forms);
    Collection cookie, request, session = null;
    int status;
    long exeTime;
    boolean isText = false;
    Charset _charset = null;
    try {
        if (CFMLEngine.DIALECT_LUCEE == dialect)
            _pc.execute(template, true, false);
        else
            _pc.executeCFML(template, true, false);
    } finally {
        _pc.flush();
        cookie = _pc.cookieScope().duplicate(false);
        request = _pc.requestScope().duplicate(false);
        session = sessionEnabled(_pc) ? _pc.sessionScope().duplicate(false) : null;
        exeTime = System.currentTimeMillis() - pc.getStartTime();
        // debugging=_pc.getDebugger().getDebuggingData(_pc).duplicate(false);
        HttpServletResponseDummy rsp = (HttpServletResponseDummy) _pc.getHttpServletResponse();
        // headers
        Collection.Key name;
        headers = new StructImpl();
        Iterator<String> it = rsp.getHeaderNames().iterator();
        java.util.Collection<String> values;
        while (it.hasNext()) {
            name = KeyImpl.init(it.next());
            values = rsp.getHeaders(name.getString());
            if (values == null || values.size() == 0)
                continue;
            if (values.size() > 1)
                headers.set(name, Caster.toArray(values));
            else
                headers.set(name, values.iterator().next());
        }
        // status
        status = rsp.getStatus();
        ContentType ct = HTTPUtil.toContentType(rsp.getContentType(), null);
        if (ct != null) {
            isText = HTTPUtil.isTextMimeType(ct.getMimeType());
            if (ct.getCharset() != null)
                _charset = CharsetUtil.toCharset(ct.getCharset(), null);
        }
        releasePageContext(_pc, pc);
    }
    Struct rst = new StructImpl();
    byte[] barr = baos.toByteArray();
    if (isText)
        rst.set(KeyConstants._filecontent, new String(barr, _charset == null ? reqCharset : _charset));
    else
        rst.set(FILECONTENT_BYNARY, barr);
    rst.set(KeyConstants._cookies, cookie);
    rst.set(KeyConstants._request, request);
    if (session != null)
        rst.set(KeyConstants._session, session);
    rst.set(KeyConstants._headers, headers);
    // rst.put(KeyConstants._debugging, debugging);
    rst.set(KeyConstants._executionTime, new Double(exeTime));
    rst.set(KeyConstants._status, new Double(status));
    rst.set(STATUS_CODE, new Double(status));
    return rst;
}
Also used : ContentType(lucee.commons.lang.mimetype.ContentType) FunctionException(lucee.runtime.exp.FunctionException) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PageContextImpl(lucee.runtime.PageContextImpl) Struct(lucee.runtime.type.Struct) Key(lucee.runtime.type.Collection.Key) StructImpl(lucee.runtime.type.StructImpl) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) Collection(lucee.runtime.type.Collection) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy)

Aggregations

PageContextImpl (lucee.runtime.PageContextImpl)84 PageSource (lucee.runtime.PageSource)19 Resource (lucee.commons.io.res.Resource)17 Key (lucee.runtime.type.Collection.Key)15 Struct (lucee.runtime.type.Struct)15 StructImpl (lucee.runtime.type.StructImpl)14 IOException (java.io.IOException)12 ApplicationException (lucee.runtime.exp.ApplicationException)10 PageException (lucee.runtime.exp.PageException)10 Component (lucee.runtime.Component)9 ConfigWeb (lucee.runtime.config.ConfigWeb)9 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)9 CFMLFactoryImpl (lucee.runtime.CFMLFactoryImpl)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)6 Mapping (lucee.runtime.Mapping)6 PageContext (lucee.runtime.PageContext)6 ConfigImpl (lucee.runtime.config.ConfigImpl)6 ExpressionException (lucee.runtime.exp.ExpressionException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5