Search in sources :

Example 76 with PageContextImpl

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

the class GatewayEngineImpl method call.

public Object call(String cfcPath, String id, String functionName, Struct arguments, boolean cfcPeristent, Object defaultValue) throws PageException {
    String requestURI = toRequestURI(cfcPath);
    PageContext oldPC = ThreadLocalPageContext.get();
    PageContextImpl pc = null;
    try {
        pc = createPageContext(requestURI, id, functionName, arguments, cfcPeristent, true);
        String ext = ResourceUtil.getExtension(cfcPath, null);
        ConfigWeb config = (ConfigWeb) ThreadLocalPageContext.getConfig();
        int dialect = ext == null ? CFMLEngine.DIALECT_CFML : config.getFactory().toDialect(ext);
        // ThreadLocalPageContext.register(pc);
        Component cfc = getCFC(pc, requestURI);
        if (cfc.containsKey(functionName)) {
            if (dialect == CFMLEngine.DIALECT_LUCEE)
                pc.execute(requestURI, true, false);
            else
                pc.executeCFML(requestURI, true, false);
            // Result
            return pc.variablesScope().get(AMF_FORWARD, null);
        }
    } finally {
        CFMLFactory f = config.getFactory();
        f.releaseLuceePageContext(pc, true);
        ThreadLocalPageContext.register(oldPC);
    }
    return defaultValue;
}
Also used : ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) CFMLFactory(lucee.runtime.CFMLFactory) PageContextImpl(lucee.runtime.PageContextImpl) Component(lucee.runtime.Component) ConfigWeb(lucee.runtime.config.ConfigWeb)

Example 77 with PageContextImpl

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

the class GatewayEngineImpl method getComponent.

public Object getComponent(String cfcPath, String id) throws PageException {
    String requestURI = toRequestURI(cfcPath);
    PageContext oldPC = ThreadLocalPageContext.get();
    PageContextImpl pc = null;
    try {
        pc = createPageContext(requestURI, id, "init", null, false, true);
        // ThreadLocalPageContext.register(pc);
        return getCFC(pc, requestURI);
    } finally {
        CFMLFactory f = config.getFactory();
        f.releaseLuceePageContext(pc, true);
        ThreadLocalPageContext.register(oldPC);
    }
}
Also used : ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) CFMLFactory(lucee.runtime.CFMLFactory) PageContextImpl(lucee.runtime.PageContextImpl)

Example 78 with PageContextImpl

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

the class Caster method toString.

/**
 * cast a Object to a String
 * @param o Object to cast
 * @return casted String
 * @throws PageException
 */
public static String toString(Object o) throws PageException {
    if (o instanceof String)
        return (String) o;
    else if (o instanceof Number)
        return toString(((Number) o));
    else if (o instanceof Boolean)
        return toString(((Boolean) o).booleanValue());
    else if (o instanceof Castable)
        return ((Castable) o).castToString();
    else if (o instanceof Date) {
        if (o instanceof DateTime)
            return ((DateTime) o).castToString();
        return new DateTimeImpl((Date) o).castToString();
    } else if (o instanceof Clob)
        return toString((Clob) o);
    else if (o instanceof Locale)
        return toString((Locale) o);
    else if (o instanceof TimeZone)
        return toString((TimeZone) o);
    else if (o instanceof Node)
        return XMLCaster.toString((Node) o);
    else if (o instanceof Reader) {
        Reader r = null;
        try {
            return IOUtil.toString(r = (Reader) o);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        } finally {
            IOUtil.closeEL(r);
        }
    } else if (o instanceof InputStream) {
        PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
        InputStream r = null;
        try {
            return IOUtil.toString(r = (InputStream) o, pc.getWebCharset());
        } catch (IOException e) {
            throw Caster.toPageException(e);
        } finally {
            IOUtil.closeEL(r);
        }
    } else if (o instanceof byte[]) {
        PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
        try {
            return new String((byte[]) o, pc.getWebCharset());
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            return new String((byte[]) o);
        }
    } else if (o instanceof char[])
        return new String((char[]) o);
    else if (o instanceof ObjectWrap)
        return toString(((ObjectWrap) o).getEmbededObject());
    else if (o instanceof Calendar)
        return toString(((Calendar) o).getTime());
    else if (o == null)
        return "";
    // INFO Collection is new of type Castable
    if (o instanceof Map || o instanceof List || o instanceof Function)
        throw new CasterException(o, "string");
    return o.toString();
}
Also used : Locale(java.util.Locale) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Calendar(java.util.Calendar) Reader(java.io.Reader) IOException(java.io.IOException) PageContextImpl(lucee.runtime.PageContextImpl) Date(java.util.Date) DateTime(lucee.runtime.type.dt.DateTime) Function(lucee.runtime.ext.function.Function) TimeZone(java.util.TimeZone) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) Clob(java.sql.Clob) Map(java.util.Map)

Example 79 with PageContextImpl

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

the class Dump method call.

public static String call(PageContext pc, Object object, String label, boolean expand, double maxLevel, String show, String hide, String output, String format, double keys, boolean metainfo, boolean showUDFs) throws PageException {
    if (show != null && "all".equalsIgnoreCase(show.trim()))
        show = null;
    if (hide != null && "all".equalsIgnoreCase(hide.trim()))
        hide = null;
    // PageContext pcc = pc;
    try {
        // output
        int defType = DumpWriter.DEFAULT_RICH;
        int outputType = OUTPUT_TYPE_NONE;
        Resource outputRes = null;
        if (!StringUtil.isEmpty(output, true)) {
            output = output.trim();
            if ("browser".equalsIgnoreCase(output)) {
                outputType = OUTPUT_TYPE_BROWSER;
                defType = DumpWriter.DEFAULT_RICH;
            } else if ("console".equalsIgnoreCase(output)) {
                outputType = OUTPUT_TYPE_CONSOLE;
                defType = DumpWriter.DEFAULT_PLAIN;
            } else {
                outputType = OUTPUT_TYPE_RESOURCE;
                defType = DumpWriter.DEFAULT_RICH;
                outputRes = ResourceUtil.toResourceNotExisting(pc, output);
            }
        }
        // format
        DumpWriter writer = pc.getConfig().getDumpWriter(format, defType);
        Set<String> setShow = (show != null) ? ListUtil.listToSet(show.toLowerCase(), ",", true) : null;
        Set<String> setHide = (hide != null) ? ListUtil.listToSet(hide.toLowerCase(), ",", true) : null;
        DumpProperties properties = new DumpProperties((int) maxLevel, setShow, setHide, (int) keys, metainfo, showUDFs);
        DumpData dd = DumpUtil.toDumpData(object, pc, (int) maxLevel, properties);
        if (!StringUtil.isEmpty(label)) {
            DumpTable table = new DumpTable("#ffffff", "#cccccc", "#000000");
            table.appendRow(1, new SimpleDumpData(label));
            // table.appendRow(1,new SimpleDumpData(getContext()));
            table.appendRow(0, dd);
            dd = table;
        }
        // formatType==FORMAT_TYPE_TEXT
        boolean isText = "text".equalsIgnoreCase(format);
        if (OUTPUT_TYPE_BROWSER == outputType || outputType == OUTPUT_TYPE_NONE) {
            if (isText)
                pc.forceWrite("<pre>");
            pc.forceWrite(writer.toString(pc, dd, expand));
            if (isText)
                pc.forceWrite("</pre>");
        } else if (OUTPUT_TYPE_CONSOLE == outputType)
            System.out.println(writer.toString(pc, dd, expand));
        else if (OUTPUT_TYPE_RESOURCE == outputType)
            IOUtil.write(outputRes, writer.toString(pc, dd, expand) + "\n************************************************************************************\n", ((PageContextImpl) pc).getResourceCharset(), true);
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    return "";
}
Also used : DumpTable(lucee.runtime.dump.DumpTable) DumpWriter(lucee.runtime.dump.DumpWriter) SimpleDumpData(lucee.runtime.dump.SimpleDumpData) Resource(lucee.commons.io.res.Resource) DumpProperties(lucee.runtime.dump.DumpProperties) PageContextImpl(lucee.runtime.PageContextImpl) IOException(java.io.IOException) DumpData(lucee.runtime.dump.DumpData) SimpleDumpData(lucee.runtime.dump.SimpleDumpData)

Example 80 with PageContextImpl

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

the class JavaProxy method loadClassByPath.

private static Class<?> loadClassByPath(PageContext pc, String className, String[] paths) throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    java.util.List<Resource> resources = new ArrayList<Resource>();
    if (paths != null && paths.length > 0) {
        // load resources
        for (int i = 0; i < paths.length; i++) {
            Resource res = ResourceUtil.toResourceExisting(pc, paths[i]);
            if (res.isDirectory()) {
                // a directory was passed, add all of the jar files from it
                FileResource dir = (FileResource) res;
                Resource[] jars = dir.listResources((ResourceNameFilter) new WildCardFilter("*.jar"));
                for (Resource jar : jars) {
                    resources.add(jar);
                }
            } else {
                resources.add(res);
            }
        }
    // throw new FunctionException(pc, "JavaProxy", 2, "path", "argument path has to be a array of strings or a single string, where every string is defining a path");
    }
    // load class
    try {
        ClassLoader cl = resources.isEmpty() ? pci.getClassLoader() : pci.getClassLoader(resources.toArray(new Resource[resources.size()]));
        Class clazz = null;
        try {
            clazz = ClassUtil.loadClass(cl, className);
        } catch (ClassException ce) {
            // try java.lang if no package definition
            if (className.indexOf('.') == -1) {
                try {
                    clazz = ClassUtil.loadClass(cl, "java.lang." + className);
                } catch (ClassException e) {
                    throw ce;
                }
            } else
                throw ce;
        }
        return clazz;
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
}
Also used : Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) ArrayList(java.util.ArrayList) FileResource(lucee.commons.io.res.type.file.FileResource) ClassException(lucee.commons.lang.ClassException) PageContextImpl(lucee.runtime.PageContextImpl) ClassException(lucee.commons.lang.ClassException) SecurityException(lucee.runtime.exp.SecurityException) PageException(lucee.runtime.exp.PageException) WildCardFilter(lucee.commons.io.res.util.WildCardFilter)

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