Search in sources :

Example 6 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 7 with RendererClass

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

the class JapidRenderer method newRendererClass.

//
// static String getSourceCode(String k) {
// String pathname = templateRoots + SEP + k;
// pathname = pathname.replace(".", SEP);
// File f = new File(pathname + ".java");
// return readSource(f);
// }
/**
	 * @param c
	 * @return
	 */
static RendererClass newRendererClass(String c) {
    RendererClass rc = new RendererClass();
    rc.setClassName(c);
    // the source code of the Java file might not be available yet
    // rc.setSourceCode(getSouceCode(c));
    rc.setLastUpdated(0);
    return rc;
}
Also used : RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 8 with RendererClass

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

the class JapidRenderer method refreshClassesInMemory.

/**
	 * all artifacts in memory
	 * 
	 * @author Bing Ran (bing.ran@gmail.com)
	 */
static synchronized void refreshClassesInMemory() {
    if (templateRoots == null)
        return;
    try {
        Set<File> allTemplates = DirUtil.getAllTemplateFiles(templateRoots);
        Set<File> toBeUpdated = new HashSet<File>();
        // find out all the classes that need to be updated
        for (File tf : allTemplates) {
            String cname = getClassName(tf);
            RendererClass rc = japidClasses.get(cname);
            if (rc == null) {
                toBeUpdated.add(tf);
            } else if (rc.getScriptTimestamp() < tf.lastModified()) {
                toBeUpdated.add(tf);
            } else if (rc.getJavaSourceCode() == null || rc.getJavaSourceCode().length() == 0) {
                toBeUpdated.add(tf);
            } else if (rc.getBytecode() == null || rc.getBytecode().length == 0) {
                toBeUpdated.add(tf);
            }
        }
        Set<String> currentClassesOnDir = createClassNameSet(allTemplates);
        Set<String> currentClassNames = japidClasses.keySet();
        if (!currentClassNames.equals(currentClassesOnDir)) {
            Set<String> classNamesRegistered = new HashSet<String>(currentClassNames);
            Set<String> classNamesDir = new HashSet<String>(currentClassesOnDir);
            if (classNamesRegistered.containsAll(classNamesDir)) {
                classNamesRegistered.removeAll(classNamesDir);
                if (!classNamesRegistered.isEmpty()) {
                    for (String n : classNamesRegistered) {
                        if (!n.contains("$")) {
                            if (!specialClasses.contains(n)) {
                                touch();
                                break;
                            }
                        }
                    }
                }
            } else {
                touch();
            }
        } else {
        // no name changes
        }
        // allClassNamesOnDir.removeAll(currentClassNames); // got new
        // templates
        removeRemoved(currentClassesOnDir, currentClassNames);
        for (File tb : toBeUpdated) {
            String scriptSrc = DirUtil.readFileAsString(tb);
            String javaCode = JapidTemplateTransformer.generateInMemory(scriptSrc, cleanPath(tb), usePlay);
            JapidFlags.log("converted: " + tb.getPath());
            String className = getClassName(tb);
            RendererClass rc = newRendererClass(className);
            rc.setScriptFile(tb);
            rc.setJapidSourceCode(scriptSrc);
            rc.setJavaSourceCode(javaCode);
            removeInnerClasses(className);
            cleanByteCode(rc);
            japidClasses.put(className, rc);
        }
        setupImports();
        // compile all
        if (toBeUpdated.size() > 0) {
            // XXX why clear the dynamics?
            dynamicClasses.clear();
            Set<String> names = createClassNameSet(toBeUpdated);
            long t = System.currentTimeMillis();
            compiler.compile(names.toArray(new String[] {}));
            howlong("compile time for " + names.size() + " classes", t);
            TemplateClassLoader loader = getClassLoader();
            for (String cname : names) {
                loader.loadClass(cname);
            }
        }
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 9 with RendererClass

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

the class JapidRenderer method compileJapidResources.

// scan classpath for japid scripts
private static void compileJapidResources() {
    if (japidResourceCompiled == true)
        return;
    try {
        Enumeration<URL> resources;
        resources = JapidRenderer.class.getClassLoader().getResources("japidviews/");
        // find out all the jars that contain japidviews
        List<String> scriptNames = new ArrayList<String>();
        while (resources.hasMoreElements()) {
            URL u = resources.nextElement();
            String protocol = u.getProtocol();
            String path = u.getPath();
            if (protocol.equals("jar")) {
                if (path.startsWith("file:")) {
                    path = path.substring("file:".length());
                }
                path = path.substring(0, path.lastIndexOf('!'));
                // test if already in cache
                if (cachedAlready(path))
                    continue;
                JarFile jarFile = new JarFile(path);
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (shouldIgnore(name))
                        continue;
                    if (name.startsWith("japidviews/") && !name.endsWith("/")) {
                        RendererClass rc = process((name), jarFile.getInputStream(entry));
                        rc.setContributor(u.toString());
                        JapidFlags.debug("converted contributed script: " + u + ":" + name);
                        String cname = DirUtil.deriveClassName(name);
                        scriptNames.add(cname);
                        specialClasses.add(cname);
                    }
                }
                resourceJars.add(new File(path));
            }
        }
        compiler.compile(scriptNames);
        // TemplateClassLoader loader = getClassLoader();
        // loader.loadClass(key);
        japidResourceCompiled = true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 10 with RendererClass

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

the class JapidRenderer method recoverClasses.

/**
	 * @author Bing Ran (bing.ran@gmail.com)
	 */
@SuppressWarnings("unchecked")
private static void recoverClasses() {
    String templateRoot = getClassCacheRoot();
    FileInputStream fos;
    File file = new File(new File(templateRoot), JAPID_CLASSES_CACHE);
    try {
        if (file.exists()) {
            // discard it if the file is too old
            long t = System.currentTimeMillis();
            if (t - file.lastModified() > 10000) {
                // too old
                JapidFlags.debug("the japid cache was too old. discarded.");
                file.delete();
            } else {
                fos = new FileInputStream(file);
                BufferedInputStream bos = new BufferedInputStream(fos);
                ObjectInputStream ois = new ObjectInputStream(bos);
                String version = (String) ois.readObject();
                JapidFlags.debug("Japid version: " + VERSION + ". JapidCache version: " + version);
                if (!version.equals(VERSION)) {
                    JapidFlags.debug("Japid classes mismatch. Discard cache.");
                } else {
                    japidClasses = (Map<String, RendererClass>) ois.readObject();
                    resourceJars = (HashSet<File>) ois.readObject();
                    HashSet<File> versionCheckedDirs = (HashSet<File>) ois.readObject();
                    JapidFlags.setVersionCheckedDirs(versionCheckedDirs);
                    JapidFlags.debug("recovered Japid classes from cache");
                }
                ois.close();
            }
        }
    } catch (Exception e) {
        JapidFlags.info("error in recovering class cache. Ignored: " + e);
    // e.printStackTrace();
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) HashSet(java.util.HashSet)

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