Search in sources :

Example 56 with PageContextImpl

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

the class ComponentUtil method _getStructPropertiesClass.

private static Class _getStructPropertiesClass(PageContext pc, Struct sct, PhysicalClassLoader cl) throws PageException, IOException, ClassNotFoundException {
    // create hash based on the keys of the struct
    String hash = StructUtil.keyHash(sct);
    char c = hash.charAt(0);
    if (c >= '0' && c <= '9')
        hash = "a" + hash;
    // create class name (struct class name + hash)
    String className = sct.getClass().getName() + "." + hash;
    // create physcal location for the file
    String real = className.replace('.', '/');
    Resource classFile = cl.getDirectory().getRealResource(real.concat(".class"));
    // load existing class
    if (classFile.exists()) {
        try {
            Class clazz = cl.loadClass(className);
            if (clazz != null)
                return clazz;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    // Properties
    List<ASMProperty> props = new ArrayList<ASMProperty>();
    Iterator<Entry<Key, Object>> it = sct.entryIterator();
    Entry<Key, Object> e;
    while (it.hasNext()) {
        e = it.next();
        props.add(new ASMPropertyImpl(ASMUtil.toType(e.getValue() == null ? Object.class : Object.class, /*e.getValue().getClass()*/
        true), e.getKey().getString()));
    }
    // create file
    byte[] barr = ASMUtil.createPojo(real, props.toArray(new ASMProperty[props.size()]), Object.class, new Class[] { Pojo.class }, null);
    // create class file from bytecode
    ResourceUtil.touch(classFile);
    IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);
    cl = (PhysicalClassLoader) ((PageContextImpl) pc).getRPCClassLoader(true);
    return cl.loadClass(className);
}
Also used : Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) LitString(lucee.transformer.expression.literal.LitString) PageContextImpl(lucee.runtime.PageContextImpl) ASMProperty(lucee.transformer.bytecode.util.ASMProperty) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) ASMPropertyImpl(lucee.transformer.bytecode.util.ASMPropertyImpl) Key(lucee.runtime.type.Collection.Key)

Example 57 with PageContextImpl

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

the class ComponentUtil method getComponentJavaAccess.

// private static final Method INVOKE_PROPERTY = new Method("invoke",Types.OBJECT,new Type[]{Types.STRING,Types.OBJECT_ARRAY});
/**
 * generate a ComponentJavaAccess (CJA) class from a component
 * a CJA is a dynamic genarted java class that has all method defined inside a component as java methods.
 *
 * This is used to generated server side Webservices.
 * @param component
 * @param isNew
 * @return
 * @throws PageException
 */
public static Class getComponentJavaAccess(PageContext pc, Component component, RefBoolean isNew, boolean create, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
    isNew.setValue(false);
    String classNameOriginal = component.getPageSource().getClassName();
    String className = getClassname(component, null).concat("_wrap");
    String real = className.replace('.', '/');
    String realOriginal = classNameOriginal.replace('.', '/');
    Mapping mapping = component.getPageSource().getMapping();
    PhysicalClassLoader cl = null;
    try {
        cl = (PhysicalClassLoader) ((PageContextImpl) pc).getRPCClassLoader(false);
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    Resource classFile = cl.getDirectory().getRealResource(real.concat(".class"));
    Resource classFileOriginal = mapping.getClassRootDirectory().getRealResource(realOriginal.concat(".class"));
    // check last Mod
    if (classFile.lastModified() >= classFileOriginal.lastModified()) {
        try {
            Class clazz = cl.loadClass(className);
            if (clazz != null && !hasChangesOfChildren(classFile.lastModified(), clazz))
                return registerTypeMapping(clazz);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    if (!create)
        return null;
    isNew.setValue(true);
    // print.out("new");
    // CREATE CLASS
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, real, null, "java/lang/Object", null);
    // GeneratorAdapter ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC,Page.STATIC_CONSTRUCTOR,null,null,cw);
    // StaticConstrBytecodeContext statConstr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.STATIC_CONSTRUCTOR);
    // /ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC,Page.CONSTRUCTOR,null,null,cw);
    // new BytecodeContext(null,null,null,cw,real,ga,Page.CONSTRUCTOR);
    ConstrBytecodeContext constr = null;
    // field component
    // FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "c", "Llucee/runtime/ComponentImpl;", null, null);
    // fv.visitEnd();
    java.util.List<LitString> _keys = new ArrayList<LitString>();
    // remote methods
    Collection.Key[] keys = component.keys(Component.ACCESS_REMOTE);
    int max;
    for (int i = 0; i < keys.length; i++) {
        max = -1;
        while ((max = createMethod(constr, _keys, cw, real, component.get(keys[i]), max, writeLog, suppressWSbeforeArg, output, returnValue)) != -1) {
            // for overload remove this
            break;
        }
    }
    // Constructor
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_OBJECT, null, null, cw);
    adapter.loadThis();
    adapter.invokeConstructor(Types.OBJECT, CONSTRUCTOR_OBJECT);
    lucee.transformer.bytecode.Page.registerFields(new BytecodeContext(null, constr, getPage(constr), _keys, cw, real, adapter, CONSTRUCTOR_OBJECT, writeLog, suppressWSbeforeArg, output, returnValue), _keys);
    adapter.returnValue();
    adapter.endMethod();
    cw.visitEnd();
    byte[] barr = cw.toByteArray();
    try {
        ResourceUtil.touch(classFile);
        IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);
        cl = (PhysicalClassLoader) ((PageContextImpl) pc).getRPCClassLoader(true);
        return registerTypeMapping(cl.loadClass(className, barr));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
}
Also used : Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) Mapping(lucee.runtime.Mapping) LitString(lucee.transformer.expression.literal.LitString) PageContextImpl(lucee.runtime.PageContextImpl) IOException(java.io.IOException) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext) ClassWriter(org.objectweb.asm.ClassWriter) LitString(lucee.transformer.expression.literal.LitString) PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Key(lucee.runtime.type.Collection.Key) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext)

Example 58 with PageContextImpl

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

the class ThreadUtil method createPageContext.

/**
 * @param config
 * @param os
 * @param serverName
 * @param requestURI
 * @param queryString
 * @param cookies
 * @param headers
 * @param parameters
 * @param attributes
 * @param register
 * @param timeout 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(ConfigWeb config, OutputStream os, String serverName, String requestURI, String queryString, Cookie[] cookies, Pair[] headers, byte[] body, Pair[] parameters, Struct attributes, boolean register, long timeout) {
    CFMLFactory factory = config.getFactory();
    HttpServletRequest req = new HttpServletRequestDummy(config.getRootDirectory(), serverName, requestURI, queryString, cookies, headers, parameters, attributes, null, body);
    req = new HTTPServletRequestWrap(req);
    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) HTTPServletRequestWrap(lucee.runtime.net.http.HTTPServletRequestWrap) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) HttpServletResponse(javax.servlet.http.HttpServletResponse) CFMLFactory(lucee.runtime.CFMLFactory) PageContextImpl(lucee.runtime.PageContextImpl)

Example 59 with PageContextImpl

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

the class Admin method terminateRunningThread.

private static boolean terminateRunningThread(ConfigWeb configWeb, int id) {
    Map<Integer, PageContextImpl> pcs = ((CFMLFactoryImpl) configWeb.getFactory()).getActivePageContexts();
    Iterator<PageContextImpl> it = pcs.values().iterator();
    PageContextImpl pc;
    Collection.Key key;
    while (it.hasNext()) {
        pc = it.next();
        if (pc.getId() == id) {
            CFMLFactoryImpl.terminate(pc, true);
            return true;
        }
    }
    return false;
}
Also used : Key(lucee.runtime.type.Collection.Key) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) BundleCollection(lucee.loader.osgi.BundleCollection) Collection(lucee.runtime.type.Collection) PageContextImpl(lucee.runtime.PageContextImpl)

Example 60 with PageContextImpl

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

the class Admin method fillGetRunningThreads.

private static void fillGetRunningThreads(lucee.runtime.type.Query qry, ConfigWeb configWeb) throws PageException {
    CFMLFactoryImpl factory = ((CFMLFactoryImpl) configWeb.getFactory());
    Map<Integer, PageContextImpl> pcs = factory.getActivePageContexts();
    Iterator<PageContextImpl> it = pcs.values().iterator();
    PageContextImpl pc;
    Collection.Key key;
    int row = 0;
    while (it.hasNext()) {
        pc = it.next();
        qry.addRow();
        row++;
        StackTraceElement[] st = pc.getThread().getStackTrace();
        configWeb.getConfigDir();
        configWeb.getIdentification().getId();
        configWeb.getConfigDir();
        qry.setAt("Id", row, new Double(pc.getId()));
        qry.setAt("Start", row, new DateTimeImpl(pc.getStartTime(), false));
        qry.setAt("Timeout", row, new Double(pc.getRequestTimeout() / 1000));
        qry.setAt("ThreadType", row, pc.getParentPageContext() == null ? "main" : "child");
        qry.setAt("StackTrace", row, toString(st));
        qry.setAt("TagContext", row, PageExceptionImpl.getTagContext(pc.getConfig(), st));
        qry.setAt("label", row, factory.getLabel());
        qry.setAt("RootPath", row, ReqRspUtil.getRootPath(((ConfigWebImpl) configWeb).getServletContext()));
        qry.setAt("ConfigFile", row, configWeb.getConfigFile().getAbsolutePath());
        if (factory.getURL() != null)
            qry.setAt("url", row, factory.getURL().toExternalForm());
    }
}
Also used : PageContextImpl(lucee.runtime.PageContextImpl) Key(lucee.runtime.type.Collection.Key) ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) BundleCollection(lucee.loader.osgi.BundleCollection) Collection(lucee.runtime.type.Collection) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl)

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