Search in sources :

Example 66 with PageContextImpl

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

the class CreateODBCDate method call.

public static DateTime call(PageContext pc, DateTime datetime, TimeZone tz) {
    if (tz == null)
        tz = ((PageContextImpl) pc).getTimeZone();
    Calendar c = Calendar.getInstance(tz);
    c.setTime(datetime);
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return new DateImpl(c.getTime());
}
Also used : Calendar(java.util.Calendar) PageContextImpl(lucee.runtime.PageContextImpl) DateImpl(lucee.runtime.type.dt.DateImpl)

Example 67 with PageContextImpl

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

the class JavaProxyFactory method createProxy.

/*

	public static Object to(Object obj, Class clazz) {
		return obj;
	}*/
/*public static Object createProxy(Config config,Component cfc, String className) throws PageException, IOException {
		return createProxy(cfc, null, ClassUtil.loadClass(config.getClassLoader(), className));
	}*/
public static Object createProxy(PageContext pc, Component cfc, Class extendz, Class... interfaces) throws PageException, IOException {
    PageContextImpl pci = (PageContextImpl) pc;
    // ((ConfigImpl)pci.getConfig()).getClassLoaderEnv()
    ClassLoader[] parents = extractClassLoaders(null, interfaces);
    if (extendz == null)
        extendz = Object.class;
    if (interfaces == null)
        interfaces = new Class[0];
    else {
        for (int i = 0; i < interfaces.length; i++) {
            if (!interfaces[i].isInterface())
                throw new IOException("definition [" + interfaces[i].getName() + "] is a class and not a interface");
        }
    }
    Type typeExtends = Type.getType(extendz);
    Type[] typeInterfaces = ASMUtil.toTypes(interfaces);
    String[] strInterfaces = new String[typeInterfaces.length];
    for (int i = 0; i < strInterfaces.length; i++) {
        strInterfaces[i] = typeInterfaces[i].getInternalName();
    }
    String className = createClassName(extendz, interfaces);
    // Mapping mapping = cfc.getPageSource().getMapping();
    // get ClassLoader
    PhysicalClassLoader pcl = null;
    try {
        // mapping.getConfig().getRPCClassLoader(false)
        pcl = (PhysicalClassLoader) pci.getRPCClassLoader(false, parents);
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    Resource classFile = pcl.getDirectory().getRealResource(className.concat(".class"));
    // check if already exists, if yes return
    if (classFile.exists()) {
        try {
            Object obj = newInstance(pcl, className, pc.getConfig(), cfc);
            if (obj != null)
                return obj;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    /*
		String classNameOriginal=component.getPageSource().getFullClassName();
    	String realOriginal=classNameOriginal.replace('.','/');
		Resource classFileOriginal = mapping.getClassRootDirectory().getRealResource(realOriginal.concat(".class"));
		*/
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, typeExtends.getInternalName(), strInterfaces);
    // BytecodeContext statConstr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.STATIC_CONSTRUCTOR);
    // BytecodeContext constr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.CONSTRUCTOR);
    // field Component
    FieldVisitor _fv = cw.visitField(Opcodes.ACC_PRIVATE, "cfc", COMPONENT_NAME, null, null);
    _fv.visitEnd();
    _fv = cw.visitField(Opcodes.ACC_PRIVATE, "config", CONFIG_WEB_NAME, null, null);
    _fv.visitEnd();
    // Constructor
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);
    Label begin = new Label();
    adapter.visitLabel(begin);
    adapter.loadThis();
    adapter.invokeConstructor(Types.OBJECT, SUPER_CONSTRUCTOR);
    // adapter.putField(JAVA_PROXY, arg1, arg2)
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "config", CONFIG_WEB_NAME);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 2);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "cfc", COMPONENT_NAME);
    adapter.visitInsn(Opcodes.RETURN);
    Label end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("config", CONFIG_WEB_NAME, null, begin, end, 1);
    adapter.visitLocalVariable("cfc", COMPONENT_NAME, null, begin, end, 2);
    // adapter.returnValue();
    adapter.endMethod();
    // create methods
    Set<Class> cDone = new HashSet<Class>();
    Map<String, Class> mDone = new HashMap<String, Class>();
    for (int i = 0; i < interfaces.length; i++) {
        _createProxy(cw, cDone, mDone, cfc, interfaces[i], className);
    }
    cw.visitEnd();
    // create class file
    byte[] barr = cw.toByteArray();
    try {
        ResourceUtil.touch(classFile);
        IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);
        pcl = (PhysicalClassLoader) pci.getRPCClassLoader(true, parents);
        Class<?> clazz = pcl.loadClass(className, barr);
        return newInstance(clazz, pc.getConfig(), cfc);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
}
Also used : HashMap(java.util.HashMap) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) HashSet(java.util.HashSet) Resource(lucee.commons.io.res.Resource) PageContextImpl(lucee.runtime.PageContextImpl) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) Type(org.objectweb.asm.Type) PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter)

Example 68 with PageContextImpl

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

the class VariableInterpreter method _variable.

public static Object _variable(PageContext pc, String str, Object value, Scope scope) throws PageException {
    // define a ohter enviroment for the function
    if (scope != null) {
        // Variables Scope
        Variables var = null;
        if (scope instanceof Variables) {
            var = (Variables) scope;
        } else if (scope instanceof CallerImpl) {
            var = ((CallerImpl) scope).getVariablesScope();
        }
        if (var != null) {
            Variables current = pc.variablesScope();
            pc.setVariablesScope(var);
            try {
                if (value != NULL)
                    return setVariable(pc, str, value);
                return getVariable(pc, str);
            } finally {
                pc.setVariablesScope(current);
            }
        } else // Undefined Scope
        if (scope instanceof Undefined) {
            PageContextImpl pci = (PageContextImpl) pc;
            Undefined undefined = (Undefined) scope;
            boolean check = undefined.getCheckArguments();
            Variables orgVar = pc.variablesScope();
            Argument orgArgs = pc.argumentsScope();
            Local orgLocal = pc.localScope();
            pci.setVariablesScope(undefined.variablesScope());
            if (check)
                pci.setFunctionScopes(undefined.localScope(), undefined.argumentsScope());
            try {
                if (value != NULL)
                    return setVariable(pc, str, value);
                return getVariable(pc, str);
            } finally {
                pc.setVariablesScope(orgVar);
                if (check)
                    pci.setFunctionScopes(orgLocal, orgArgs);
            }
        }
    }
    if (value != NULL)
        return setVariable(pc, str, value);
    return getVariable(pc, str);
}
Also used : Variables(lucee.runtime.type.scope.Variables) Undefined(lucee.runtime.type.scope.Undefined) CallerImpl(lucee.runtime.type.scope.CallerImpl) Argument(lucee.runtime.type.scope.Argument) Local(lucee.runtime.type.scope.Local) PageContextImpl(lucee.runtime.PageContextImpl)

Example 69 with PageContextImpl

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

the class ClassicAppListener method _onRequest.

static void _onRequest(PageContext pc, PageSource requestedPage, PageSource application, RequestListener rl) throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    pci.setAppListenerType(ApplicationListener.TYPE_CLASSIC);
    // on requestStart
    if (application != null)
        pci._doInclude(new PageSource[] { application }, false, null);
    if (rl != null) {
        requestedPage = rl.execute(pc, requestedPage);
        if (requestedPage == null)
            return;
    }
    // request
    try {
        pci._doInclude(new PageSource[] { requestedPage }, false, null);
    } catch (MissingIncludeException mie) {
        ApplicationContext ac = pc.getApplicationContext();
        boolean rethrow = true;
        if (ac instanceof ClassicApplicationContext) {
            ClassicApplicationContext cfc = (ClassicApplicationContext) ac;
            UDF udf = cfc.getOnMissingTemplate();
            if (udf != null) {
                String targetPage = requestedPage.getRealpathWithVirtual();
                rethrow = (!Caster.toBooleanValue(udf.call(pc, new Object[] { targetPage }, true), true));
            }
        }
        if (rethrow)
            throw mie;
    }
    // on Request End
    if (application != null) {
        PageSource onReqEnd = application.getRealPage(Constants.CFML_CLASSIC_APPLICATION_END_EVENT_HANDLER);
        if (onReqEnd.exists())
            pci._doInclude(new PageSource[] { onReqEnd }, false, null);
    }
}
Also used : UDF(lucee.runtime.type.UDF) MissingIncludeException(lucee.runtime.exp.MissingIncludeException) PageContextImpl(lucee.runtime.PageContextImpl) PageSource(lucee.runtime.PageSource)

Example 70 with PageContextImpl

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

the class ModernAppListener method _onRequest.

protected void _onRequest(PageContext pc, PageSource requestedPage, PageSource appPS, RequestListener rl) throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    pci.setAppListenerType(ApplicationListener.TYPE_MODERN);
    if (appPS != null) {
        String callPath = appPS.getComponentName();
        Component app = ComponentLoader.loadComponent(pci, appPS, callPath, false, false);
        // init
        ModernApplicationContext appContext = initApplicationContext(pci, app);
        apps.put(appContext.getName(), app);
        if (!pci.initApplicationContext(this))
            return;
        if (rl != null) {
            requestedPage = rl.execute(pc, requestedPage);
            if (requestedPage == null)
                return;
        }
        String targetPage = requestedPage.getRealpathWithVirtual();
        RefBoolean goon = new RefBooleanImpl(true);
        // onRequestStart
        if (app.contains(pc, ON_REQUEST_START)) {
            try {
                Object rtn = call(app, pci, ON_REQUEST_START, new Object[] { targetPage }, false);
                if (!Caster.toBooleanValue(rtn, true))
                    return;
            } catch (PageException pe) {
                pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
                if (pe != null)
                    throw pe;
            }
        }
        // onRequest
        if (goon.toBooleanValue()) {
            boolean isComp = isComponent(pc, requestedPage);
            Object method;
            if (isComp && app.contains(pc, ON_CFCREQUEST) && (method = pc.urlFormScope().get(KeyConstants._method, null)) != null) {
                Struct url = (Struct) Duplicator.duplicate(pc.urlFormScope(), true);
                url.removeEL(KeyConstants._fieldnames);
                url.removeEL(KeyConstants._method);
                Object args = url.get(KeyConstants._argumentCollection, null);
                // url returnFormat
                Object oReturnFormat = url.removeEL(KeyConstants._returnFormat);
                int urlReturnFormat = -1;
                if (oReturnFormat != null)
                    urlReturnFormat = UDFUtil.toReturnFormat(Caster.toString(oReturnFormat, null), -1);
                // request header accept
                List<MimeType> accept = ReqRspUtil.getAccept(pc);
                int headerReturnFormat = MimeType.toFormat(accept, -1, -1);
                Object queryFormat = url.removeEL(KeyConstants._queryFormat);
                if (args == null) {
                    args = pc.getHttpServletRequest().getAttribute("argumentCollection");
                }
                if (args instanceof String) {
                    args = new JSONExpressionInterpreter().interpret(pc, (String) args);
                }
                if (args != null) {
                    if (Decision.isCastableToStruct(args)) {
                        Struct sct = Caster.toStruct(args, false);
                        // Key[] keys = url.keys();
                        Iterator<Entry<Key, Object>> it = url.entryIterator();
                        Entry<Key, Object> e;
                        while (it.hasNext()) {
                            e = it.next();
                            sct.setEL(e.getKey(), e.getValue());
                        }
                        args = sct;
                    } else if (Decision.isCastableToArray(args)) {
                        args = Caster.toArray(args);
                    } else {
                        Array arr = new ArrayImpl();
                        arr.appendEL(args);
                        args = arr;
                    }
                } else
                    args = url;
                Object rtn = call(app, pci, ON_CFCREQUEST, new Object[] { requestedPage.getComponentName(), method, args }, true);
                if (rtn != null) {
                    if (pc.getHttpServletRequest().getHeader("AMF-Forward") != null) {
                        pc.variablesScope().setEL("AMF-Forward", rtn);
                    // ThreadLocalWDDXResult.set(rtn);
                    } else {
                        try {
                            ComponentPageImpl.writeToResponseStream(pc, app, method.toString(), urlReturnFormat, headerReturnFormat, queryFormat, rtn);
                        } catch (Exception e) {
                            throw Caster.toPageException(e);
                        }
                    }
                }
            } else {
                // TODO impl die nicht so generisch ist
                try {
                    if (!isComp && app.contains(pc, ON_REQUEST))
                        call(app, pci, ON_REQUEST, new Object[] { targetPage }, false);
                    else
                        pci._doInclude(new PageSource[] { requestedPage }, false, null);
                } catch (PageException pe) {
                    pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
                    if (pe != null)
                        throw pe;
                }
            }
        }
        // onRequestEnd
        if (goon.toBooleanValue() && app.contains(pc, ON_REQUEST_END)) {
            try {
                call(app, pci, ON_REQUEST_END, new Object[] { targetPage }, false);
            } catch (PageException pe) {
                pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
                if (pe != null)
                    throw pe;
            }
        }
    } else {
        apps.put(pc.getApplicationContext().getName(), null);
        if (rl != null) {
            requestedPage = rl.execute(pc, requestedPage);
            if (requestedPage == null)
                return;
        }
        pci._doInclude(new PageSource[] { requestedPage }, false, null);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) RefBoolean(lucee.commons.lang.types.RefBoolean) ArrayImpl(lucee.runtime.type.ArrayImpl) PageContextImpl(lucee.runtime.PageContextImpl) MimeType(lucee.commons.lang.mimetype.MimeType) PageException(lucee.runtime.exp.PageException) MissingIncludeException(lucee.runtime.exp.MissingIncludeException) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) PageSource(lucee.runtime.PageSource) Array(lucee.runtime.type.Array) Entry(java.util.Map.Entry) JSONExpressionInterpreter(lucee.runtime.interpreter.JSONExpressionInterpreter) Component(lucee.runtime.Component) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) Key(lucee.runtime.type.Collection.Key)

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