Search in sources :

Example 11 with PageContextImpl

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

the class ThreadTag method register.

public void register(Page currentPage, int threadIndex) throws PageException {
    if (ACTION_RUN != action)
        return;
    Key name = name(true);
    try {
        // pc.getThreadScope(name);
        Threads ts = ThreadTag.getThreadScope(pc, name, ThreadTag.LEVEL_ALL);
        if (type == TYPE_DAEMON) {
            if (ts != null)
                throw new ApplicationException("could not create a thread with the name [" + name.getString() + "]. name must be unique within a request");
            ChildThreadImpl ct = new ChildThreadImpl((PageContextImpl) pc, currentPage, name.getString(), threadIndex, attrs, false);
            pc.setThreadScope(name, new ThreadsImpl(ct));
            ct.setPriority(priority);
            ct.setDaemon(false);
            ct.start();
        } else {
            ChildThreadImpl ct = new ChildThreadImpl((PageContextImpl) pc, currentPage, name.getString(), threadIndex, attrs, true);
            ct.setPriority(priority);
            ((ConfigImpl) pc.getConfig()).getSpoolerEngine().add(new ChildSpoolerTask(ct, plans));
        }
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    } finally {
        // this method is not called from template when type is run, a call from template is to early,
        ((PageContextImpl) pc).reuse(this);
    }
}
Also used : Threads(lucee.runtime.type.scope.Threads) ThreadsImpl(lucee.runtime.thread.ThreadsImpl) ChildThreadImpl(lucee.runtime.thread.ChildThreadImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ChildSpoolerTask(lucee.runtime.thread.ChildSpoolerTask) PageContextImpl(lucee.runtime.PageContextImpl) Key(lucee.runtime.type.Collection.Key)

Example 12 with PageContextImpl

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

the class ChildThreadImpl method execute.

public PageException execute(Config config) {
    PageContext oldPc = ThreadLocalPageContext.get();
    Page p = page;
    PageContextImpl pc = null;
    try {
        // deamon
        if (this.pc != null) {
            pc = this.pc;
            ThreadLocalPageContext.register(pc);
        } else // task
        {
            ConfigWebImpl cwi;
            try {
                cwi = (ConfigWebImpl) config;
                DevNullOutputStream os = DevNullOutputStream.DEV_NULL_OUTPUT_STREAM;
                pc = ThreadUtil.createPageContext(cwi, os, serverName, requestURI, queryString, SerializableCookie.toCookies(cookies), headers, null, parameters, attributes, true, -1);
                pc.setRequestTimeout(requestTimeout);
                p = PageSourceImpl.loadPage(pc, cwi.getPageSources(oldPc == null ? pc : oldPc, null, template, false, false, true));
            // p=cwi.getPageSources(oldPc,null, template, false,false,true).loadPage(cwi);
            } catch (PageException e) {
                return e;
            }
            pc.addPageSource(p.getPageSource(), true);
        }
        threadScope = pc.getThreadScope(KeyConstants._cfthread, null);
        pc.setCurrentThreadScope(new ThreadsImpl(this));
        pc.setThread(Thread.currentThread());
        // String encodings = pc.getHttpServletRequest().getHeader("Accept-Encoding");
        Undefined undefined = pc.us();
        Argument newArgs = new ArgumentThreadImpl((Struct) Duplicator.duplicate(attrs, false));
        LocalImpl newLocal = pc.getScopeFactory().getLocalInstance();
        // Key[] keys = attrs.keys();
        Iterator<Entry<Key, Object>> it = attrs.entryIterator();
        Entry<Key, Object> e;
        while (it.hasNext()) {
            e = it.next();
            newArgs.setEL(e.getKey(), e.getValue());
        }
        newLocal.setEL(KEY_ATTRIBUTES, newArgs);
        Argument oldArgs = pc.argumentsScope();
        Local oldLocal = pc.localScope();
        int oldMode = undefined.setMode(Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS);
        pc.setFunctionScopes(newLocal, newArgs);
        try {
            p.threadCall(pc, threadIndex);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            if (!Abort.isSilentAbort(t)) {
                ConfigWeb c = pc.getConfig();
                if (c instanceof ConfigImpl) {
                    ConfigImpl ci = (ConfigImpl) c;
                    Log log = ci.getLog("thread");
                    if (log != null)
                        LogUtil.log(log, Log.LEVEL_ERROR, this.getName(), t);
                }
                PageException pe = Caster.toPageException(t);
                if (!serializable)
                    catchBlock = pe.getCatchBlock(pc.getConfig());
                return pe;
            }
        } finally {
            completed = true;
            pc.setFunctionScopes(oldLocal, oldArgs);
            undefined.setMode(oldMode);
            // pc.getScopeFactory().recycle(newArgs);
            pc.getScopeFactory().recycle(pc, newLocal);
            if (pc.getHttpServletResponse() instanceof HttpServletResponseDummy) {
                HttpServletResponseDummy rsp = (HttpServletResponseDummy) pc.getHttpServletResponse();
                pc.flush();
                contentType = rsp.getContentType();
                Pair<String, Object>[] _headers = rsp.getHeaders();
                if (_headers != null)
                    for (int i = 0; i < _headers.length; i++) {
                        if (_headers[i].getName().equalsIgnoreCase("Content-Encoding"))
                            contentEncoding = Caster.toString(_headers[i].getValue(), null);
                    }
            }
        }
    } finally {
        pc.getConfig().getFactory().releaseLuceePageContext(pc, true);
        pc = null;
        if (oldPc != null)
            ThreadLocalPageContext.register(oldPc);
    }
    return null;
}
Also used : Argument(lucee.runtime.type.scope.Argument) Page(lucee.runtime.Page) Entry(java.util.Map.Entry) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) LocalImpl(lucee.runtime.type.scope.LocalImpl) Pair(lucee.commons.lang.Pair) PageException(lucee.runtime.exp.PageException) Undefined(lucee.runtime.type.scope.Undefined) Log(lucee.commons.io.log.Log) Local(lucee.runtime.type.scope.Local) PageContextImpl(lucee.runtime.PageContextImpl) ConfigWeb(lucee.runtime.config.ConfigWeb) DevNullOutputStream(lucee.commons.io.DevNullOutputStream) ArgumentThreadImpl(lucee.runtime.type.scope.ArgumentThreadImpl) ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) Key(lucee.runtime.type.Collection.Key) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 13 with PageContextImpl

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

the class ThreadUtil method createPageContext.

/**
 * @param factory
 * @param rootDirectory
 * @param os
 * @param serverName
 * @param requestURI
 * @param queryString
 * @param cookies
 * @param headers
 * @param parameters
 * @param attributes
 * @param register
 * @param timeout in ms, if the value is smaller than 1 it is ignored and the value comming from the context is used
 * @return
 */
public static PageContextImpl createPageContext(CFMLFactory factory, Resource rootDirectory, OutputStream os, String serverName, String requestURI, String queryString, Cookie[] cookies, Pair[] headers, Pair[] parameters, Struct attributes, boolean register, long timeout) {
    HttpServletRequest req = createHttpServletRequest(rootDirectory, serverName, requestURI, queryString, cookies, headers, parameters, attributes, null);
    HttpServletResponse rsp = createHttpServletResponse(os);
    return (PageContextImpl) factory.getLuceePageContext(factory.getServlet(), req, rsp, null, false, -1, false, register, timeout, false, false);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PageContextImpl(lucee.runtime.PageContextImpl)

Example 14 with PageContextImpl

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

the class ThreadUtil method clonePageContext.

public static PageContextImpl clonePageContext(PageContext pc, OutputStream os, boolean stateless, boolean register2Thread, boolean register2RunningThreads) {
    // TODO stateless
    CFMLFactoryImpl factory = (CFMLFactoryImpl) pc.getConfig().getFactory();
    HttpServletRequest req = new HTTPServletRequestWrap(cloneHttpServletRequest(pc));
    HttpServletResponse rsp = createHttpServletResponse(os);
    // copy state
    PageContextImpl pci = (PageContextImpl) pc;
    PageContextImpl dest = factory.getPageContextImpl(factory.getServlet(), req, rsp, null, false, -1, false, register2Thread, true, pc.getRequestTimeout(), register2RunningThreads, false, false);
    pci.copyStateTo(dest);
    return dest;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) HTTPServletRequestWrap(lucee.runtime.net.http.HTTPServletRequestWrap) HttpServletResponse(javax.servlet.http.HttpServletResponse) PageContextImpl(lucee.runtime.PageContextImpl)

Example 15 with PageContextImpl

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

the class SMTPClient method send.

public void send(PageContext pc, long sendTime) throws MailException {
    if (plainText == null && htmlText == null)
        throw new MailException("you must define plaintext or htmltext");
    Server[] servers = ((PageContextImpl) pc).getMailServers();
    ConfigWeb config = pc.getConfig();
    if (ArrayUtil.isEmpty(servers) && ArrayUtil.isEmpty(host))
        throw new MailException("no SMTP Server defined");
    if (spool == SPOOL_YES || (spool == SPOOL_UNDEFINED && config.isMailSpoolEnable())) {
        MailSpoolerTask mst = new MailSpoolerTask(this, servers, sendTime);
        if (listener instanceof Component)
            mst.setListener(new ComponentSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (Component) listener));
        else if (listener instanceof UDF)
            mst.setListener(new UDFSpoolerTaskListener(SystemUtil.getCurrentContext(), mst, (UDF) listener));
        config.getSpoolerEngine().add(mst);
    } else
        _send(config, servers);
}
Also used : Server(lucee.runtime.net.mail.Server) UDFSpoolerTaskListener(lucee.runtime.spooler.UDFSpoolerTaskListener) UDF(lucee.runtime.type.UDF) MailException(lucee.runtime.net.mail.MailException) PageContextImpl(lucee.runtime.PageContextImpl) MailSpoolerTask(lucee.runtime.spooler.mail.MailSpoolerTask) Component(lucee.runtime.Component) ConfigWeb(lucee.runtime.config.ConfigWeb) ComponentSpoolerTaskListener(lucee.runtime.spooler.ComponentSpoolerTaskListener)

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