Search in sources :

Example 6 with PhysicalClassLoader

use of lucee.commons.lang.PhysicalClassLoader 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 7 with PhysicalClassLoader

use of lucee.commons.lang.PhysicalClassLoader in project Lucee by lucee.

the class EnvClassLoader method load.

private synchronized Object load(String name, short type, boolean doLog) {
    Object obj = null;
    // first we check the callers classpath
    Caller caller = SystemUtil.getCallerClass();
    if (!caller.isEmpty()) {
        // if the request comes from classpath
        Class clazz = caller.fromClasspath();
        if (clazz != null) {
            if (clazz.getClassLoader() != null) {
                obj = _load(clazz.getClassLoader(), name, type);
                if (obj == null && clazz.getClassLoader() instanceof PhysicalClassLoader && clazz != caller.fromBootDelegation && caller.fromBootDelegation != null & caller.fromBootDelegation.getClassLoader() != null) {
                    obj = _load(caller.fromBootDelegation.getClassLoader(), name, type);
                }
            }
        }
        if (obj == null && caller.fromBundle != null) {
            if (caller.fromBundle.getClassLoader() != null)
                obj = _load(caller.fromBundle.getClassLoader(), name, type);
        }
        if (obj != null)
            return obj;
    }
    // now we check in the core  for the class (this includes all jars loaded by the core)
    if ((caller.isEmpty() || caller.fromBundle != null) && caller.fromBundle.getClassLoader() != getParent()) {
        obj = _load(getParent(), name, type);
        if (obj != null) {
            return obj;
        }
    }
    // now we check extension bundles
    if (caller.isEmpty() || /*PATCH LDEV-1312*/
    (ThreadLocalPageContext.get() == null) || /* if we are in a child threads*/
    caller.fromBundle != null) {
        Bundle[] bundles = ConfigWebUtil.getEngine(config).getBundleContext().getBundles();
        Bundle b = null;
        for (int i = 0; i < bundles.length; i++) {
            b = bundles[i];
            if (b != null && !OSGiUtil.isFrameworkBundle(b)) {
                try {
                    if (type == CLASS)
                        obj = b.loadClass(name);
                    else if (type == URL)
                        obj = b.getResource(name);
                    else {
                        java.net.URL url = b.getResource(name);
                        if (url != null)
                            obj = url.openStream();
                    }
                    if (obj != null)
                        break;
                } catch (Exception e) {
                    obj = null;
                }
            }
        }
        if (obj != null) {
            return obj;
        }
    }
    if (caller.fromClasspath() != null) {
        ClassLoader loader = CFMLEngineFactory.class.getClassLoader();
        obj = _load(loader, name, type);
        if (obj != null) {
            // print.e("found in classpath:"+name+"->");
            return obj;
        }
    }
    return obj;
}
Also used : PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) Caller(lucee.commons.io.SystemUtil.Caller) Bundle(org.osgi.framework.Bundle) PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) IOException(java.io.IOException)

Example 8 with PhysicalClassLoader

use of lucee.commons.lang.PhysicalClassLoader in project Lucee by lucee.

the class ConfigImpl method getRPCClassLoader.

public ClassLoader getRPCClassLoader(boolean reload, ClassLoader[] parents) throws IOException {
    if (rpcClassLoader != null && !reload)
        return rpcClassLoader;
    Resource dir = getClassDirectory().getRealResource("RPC");
    if (!dir.exists())
        dir.createDirectory(true);
    rpcClassLoader = new PhysicalClassLoader(this, dir, parents, false);
    return rpcClassLoader;
}
Also used : PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) Resource(lucee.commons.io.res.Resource) CompressResource(lucee.commons.io.res.type.compress.CompressResource)

Example 9 with PhysicalClassLoader

use of lucee.commons.lang.PhysicalClassLoader 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)

Aggregations

PhysicalClassLoader (lucee.commons.lang.PhysicalClassLoader)9 Resource (lucee.commons.io.res.Resource)6 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 PageContextImpl (lucee.runtime.PageContextImpl)3 LitString (lucee.transformer.expression.literal.LitString)3 ArrayList (java.util.ArrayList)2 CompressResource (lucee.commons.io.res.type.compress.CompressResource)2 Mapping (lucee.runtime.Mapping)2 Key (lucee.runtime.type.Collection.Key)2 ClassWriter (org.objectweb.asm.ClassWriter)2 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)2 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 Caller (lucee.commons.io.SystemUtil.Caller)1 ResourceClassLoader (lucee.commons.io.res.util.ResourceClassLoader)1