Search in sources :

Example 1 with RendererClass

use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.

the class JapidRenderer method getFunctionalRendererClass.

/**
 * Get a newly loaded class for the template renderer
 *
 * @param name
 * @return the renderer class wrapper
 */
public static RendererClass getFunctionalRendererClass(String name) {
    refreshClasses();
    RendererClass rc = classes.get(name);
    if (rc == null)
        throw new RuntimeException("renderer class not found: " + name);
    else {
        Class<? extends JapidTemplateBaseWithoutPlay> cls = rc.getClz();
        if (cls == null) {
            loadRendererClass(rc);
        }
    }
    return rc;
}
Also used : RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 2 with RendererClass

use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.

the class JapidRenderer method refreshClasses.

static synchronized void refreshClasses() {
    if (!timeToRefresh())
        return;
    try {
        // find out all removed classes
        List<String> allHtml = DirUtil.getAllTemplateHtmlFiles(new File(templateRoot));
        Set<String> currentClassesOnDir = createNameSet(allHtml);
        Set<String> tmp = new HashSet<String>(currentClassesOnDir);
        Set<String> keySet = classes.keySet();
        // added html
        tmp.removeAll(keySet);
        removeRemoved(currentClassesOnDir, keySet);
        for (String c : tmp) {
            RendererClass rc = newRendererClass(c);
            classes.put(c, rc);
        }
        // now all the class set size is up to date
        // now update any Java source code
        List<File> gen = gen(templateRoot);
        // this would include both new and updated java
        Set<String> updatedClasses = new HashSet<String>();
        if (gen.size() > 0) {
            // int i = 0;
            for (File f : gen) {
                String className = getClassName(f);
                updatedClasses.add(className);
                RendererClass rendererClass = classes.get(className);
                if (rendererClass == null) {
                    // this should not happen, since
                    throw new RuntimeException("any new key should have been in the classes container: " + className);
                // rendererClass = newRendererClass(className);
                // classes.put(className, rendererClass);
                }
                setSources(rendererClass, f);
                removeInnerClasses(className);
                cleanClassHolder(rendererClass);
            }
        }
        // find all render class without bytecode
        for (Iterator<String> i = classes.keySet().iterator(); i.hasNext(); ) {
            String k = i.next();
            RendererClass rc = classes.get(k);
            if (rc.getSourceCode() == null) {
                if (!rc.getClassName().contains("$")) {
                    setSources(rc, getJavaSrcFile(k));
                    cleanClassHolder(rc);
                    updatedClasses.add(k);
                } else {
                    rc.setLastUpdated(0);
                }
            } else {
                if (rc.getBytecode() == null) {
                    cleanClassHolder(rc);
                    updatedClasses.add(k);
                }
            }
        }
        // compile all
        if (updatedClasses.size() > 0) {
            String[] names = new String[updatedClasses.size()];
            int i = 0;
            for (String s : updatedClasses) {
                names[i++] = s;
            }
            long t = System.currentTimeMillis();
            compiler.compile(names);
            // shall I load them right away?
            // let's try
            ClassLoader cl = _parentClassLoader == null ? JapidRenderer.class.getClassLoader() : _parentClassLoader;
            TemplateClassLoader classReloader = new TemplateClassLoader(cl);
            for (RendererClass rc : classes.values()) {
                try {
                    if (isDevMode())
                        // to enable JIT loading in dev mode
                        rc.setClz(null);
                    else
                        rc.setClz((Class<JapidTemplateBaseWithoutPlay>) classReloader.loadClass(rc.getClassName()));
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            howlong("compile/load time for " + names.length + " classes", t);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) IOException(java.io.IOException) TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) RendererClass(cn.bran.japid.rendererloader.RendererClass) RendererClass(cn.bran.japid.rendererloader.RendererClass) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with RendererClass

use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.

the class JapidController2 method template.

public static String template() {
    // the super.template() class uses current request object to determine
    // the caller and method to find the matching template
    // this won't work if the current method is called from another action.
    // let's fall back to use the stack trace to deduce the template.
    // String caller2 = StackTraceUtils.getCaller2();
    final StackTraceElement[] stes = new Throwable().getStackTrace();
    // let's iterate back in the stacktrace to find the recent action calls.
    for (StackTraceElement st : stes) {
        String controller = st.getClassName();
        String action = st.getMethodName();
        ApplicationClass conAppClass = Play.classes.getApplicationClass(controller);
        if (conAppClass != null) {
            Class controllerClass = conAppClass.javaClass;
            if (JapidController2.class.isAssignableFrom(controllerClass)) {
                Method actionMethod = /* Java. */
                findActionMethod(action, controllerClass);
                if (actionMethod != null) {
                    String expr = controller + "." + action;
                    // content negotiation
                    String format = Request.current().format;
                    if ("html".equals(format)) {
                        return expr;
                    } else {
                        String expr_format = expr + "_" + format;
                        if (expr_format.startsWith("controllers.")) {
                            expr_format = "japidviews" + expr_format.substring(expr_format.indexOf('.'));
                        }
                        RendererClass rc = JapidPlayRenderer.japidClasses.get(expr_format);
                        if (rc != null)
                            return expr_format;
                        else {
                            // fall back
                            return expr;
                        }
                    }
                }
            }
        }
    }
    throw new RuntimeException("The calling stack does not contain a valid controller. Should not have happended...");
}
Also used : ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) RendererClass(cn.bran.japid.rendererloader.RendererClass) ApplicationClass(play.classloading.ApplicationClasses.ApplicationClass) Method(java.lang.reflect.Method) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 4 with RendererClass

use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.

the class JapidPlayRenderer method refreshClasses.

static synchronized void refreshClasses() {
    if (classesInited) {
        if (!timeToRefresh())
            return;
    } else {
        JapidFlags.info("Japid scripts not loaded yet. Initializing them...");
    }
    try {
        // PlayDirUtil.mkdir(templateRoot);
        // find out all removed classes
        List<String> allTemps = isDevMode() ? DirUtil.getAllTemplateFiles(new File(defaultTemplateRoot)) : DirUtil.getAllTemplateFilesJavaFiles(new File(defaultTemplateRoot));
        Set<String> currentClassesOnDir = createNameSet(allTemps);
        Set<String> allNames = new HashSet<String>(currentClassesOnDir);
        Set<String> keySet = japidClasses.keySet();
        // got new templates
        allNames.removeAll(keySet);
        removeRemoved(currentClassesOnDir, keySet);
        for (String c : allNames) {
            RendererClass rc = newRendererClass(c);
            japidClasses.put(c, rc);
        }
        // now all the class set size is up to date
        // now update any Java source code
        List<File> gen = gen(defaultTemplateRoot);
        // this would include both new and updated java
        Set<String> updatedClasses = new HashSet<String>();
        if (gen.size() > 0) {
            for (File f : gen) {
                String className = getClassName(f);
                updatedClasses.add(className);
                RendererClass rendererClass = japidClasses.get(className);
                if (rendererClass == null) {
                    // this should not happen, since
                    throw new RuntimeException("any new class names should have been in the classes container: " + className);
                // rendererClass = newRendererClass(className);
                // classes.put(className, rendererClass);
                }
                JapidRenderer.setSources(rendererClass, f);
                removeInnerClasses(className);
                cleanClassHolder(rendererClass);
            }
        }
        // find all render class without bytecode
        for (Iterator<String> i = japidClasses.keySet().iterator(); i.hasNext(); ) {
            String k = i.next();
            RendererClass rc = japidClasses.get(k);
            if (rc.getSourceCode() == null) {
                if (!rc.getClassName().contains("$")) {
                    String pathname = defaultTemplateRoot + sep + k.replace(".", sep);
                    File f = new File(pathname + ".java");
                    JapidRenderer.setSources(rc, f);
                    cleanClassHolder(rc);
                    updatedClasses.add(k);
                } else {
                    rc.setLastUpdated(0);
                }
            } else {
                if (rc.getBytecode() == null) {
                    cleanClassHolder(rc);
                    updatedClasses.add(k);
                }
            }
        }
        // compile all
        if (updatedClasses.size() > 0) {
            String[] names = new String[updatedClasses.size()];
            int i = 0;
            for (String s : updatedClasses) {
                names[i++] = s;
            }
            long t = System.currentTimeMillis();
            // newly compiled class bytecode bodies are set in the global
            // classes set ready for defining
            compiler.compile(names);
            initJapidClassLoader();
            for (RendererClass rc : japidClasses.values()) {
                try {
                    if (isDevMode())
                        // to enable JIT loading in DEV mode
                        rc.setClz(null);
                    else
                        rc.setClz((Class<JapidTemplateBaseWithoutPlay>) japidClassLoader.loadClass(rc.getClassName()));
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
            howlong("compile/load time for " + names.length + " classe(s)", t);
            classesInited = true;
        }
    } catch (JapidCompilationException e) {
        if (presentErrorInHtml)
            throw e;
        String tempName = e.getTemplateName();
        if (tempName.startsWith(defaultTemplateRoot)) {
        } else {
            tempName = defaultTemplateRoot + File.separator + tempName;
        }
        VirtualFile vf = VirtualFile.fromRelativePath(tempName);
        CompilationException ce = new CompilationException(vf, "\"" + e.getMessage() + "\"", e.getLineNumber(), 0, 0);
        throw ce;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : VirtualFile(play.vfs.VirtualFile) CompilationException(play.exceptions.CompilationException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) CompilationException(play.exceptions.CompilationException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) RendererClass(cn.bran.japid.rendererloader.RendererClass) RendererClass(cn.bran.japid.rendererloader.RendererClass) VirtualFile(play.vfs.VirtualFile) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with RendererClass

use of cn.bran.japid.rendererloader.RendererClass in project Japid by branaway.

the class JapidPlayRenderer method handleException.

public static RenderResult handleException(Throwable e) throws RuntimeException {
    if (!presentErrorInHtml)
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException(e);
    // if (Play.mode == Mode.PROD)
    // throw new RuntimeException(e);
    // 
    Class<? extends JapidTemplateBaseWithoutPlay> rendererClass = getErrorRendererClass();
    if (e instanceof JapidTemplateException) {
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, (JapidTemplateException) e);
        return (rr);
    }
    if (e instanceof RuntimeException && e.getCause() != null)
        e = e.getCause();
    if (e instanceof JapidCompilationException) {
        JapidCompilationException jce = (JapidCompilationException) e;
        JapidTemplateException te = JapidTemplateException.from(jce);
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
        return (rr);
    }
    e.printStackTrace();
    // find the latest japidviews exception or the controller that caused
    // the exception
    StackTraceElement[] stackTrace = e.getStackTrace();
    for (StackTraceElement ele : stackTrace) {
        String className = ele.getClassName();
        if (className.startsWith("japidviews")) {
            int lineNumber = ele.getLineNumber();
            RendererClass applicationClass = japidClasses.get(className);
            if (applicationClass != null) {
                // let's get the line of problem
                int oriLineNumber = applicationClass.mapJavaLineToJapidScriptLine(lineNumber);
                if (oriLineNumber > 0) {
                    if (rendererClass != null) {
                        String path = applicationClass.getOriSourceCode();
                        JapidTemplateException te = new JapidTemplateException("Japid Error", path + "(" + oriLineNumber + "): " + e.getClass().getName() + ": " + e.getMessage(), oriLineNumber, path, applicationClass.getOriSourceCode());
                        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
                        return (rr);
                    }
                }
            }
        } else if (className.startsWith("controllers.")) {
            if (e instanceof RuntimeException)
                throw (RuntimeException) e;
            else
                throw new RuntimeException(e);
        }
    }
    JapidTemplateException te = new JapidTemplateException(e);
    RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
    return rr;
// if (e instanceof RuntimeException)
// throw (RuntimeException) e;
// else
// throw new RuntimeException(e);
}
Also used : JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RenderResult(cn.bran.japid.template.RenderResult) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Aggregations

RendererClass (cn.bran.japid.rendererloader.RendererClass)19 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)9 JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)9 IOException (java.io.IOException)9 File (java.io.File)6 TemplateClassLoader (cn.bran.japid.rendererloader.TemplateClassLoader)5 HashSet (java.util.HashSet)5 JarFile (java.util.jar.JarFile)4 JapidTemplateBaseWithoutPlay (cn.bran.japid.template.JapidTemplateBaseWithoutPlay)1 RenderResult (cn.bran.japid.template.RenderResult)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 ObjectInputStream (java.io.ObjectInputStream)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 ApplicationClass (play.classloading.ApplicationClasses.ApplicationClass)1