Search in sources :

Example 1 with VirtualFile

use of play.vfs.VirtualFile in project play-cookbook by spinscale.

the class Log4jWatcherJob method doJob.

public void doJob() {
    String log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.xml");
    VirtualFile file = VirtualFile.open(log4jPath);
    if (!file.exists()) {
        // try again with the .properties
        log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.properties");
        file = VirtualFile.open(log4jPath);
    }
    if (file.exists()) {
        PropertyConfigurator.configureAndWatch(file.relativePath());
    }
}
Also used : VirtualFile(play.vfs.VirtualFile)

Example 2 with VirtualFile

use of play.vfs.VirtualFile 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 3 with VirtualFile

use of play.vfs.VirtualFile in project Japid by branaway.

the class JapidCommands method reloadChanged.

/**
	 * check the current app and dependencies for changed templates
	 * @return
	 */
public static List<File> reloadChanged() {
    List<File> reloadChanged = reloadChanged(new File(Play.applicationPath, APP).getAbsolutePath());
    Collection<VirtualFile> modules = Play.modules.values();
    for (VirtualFile module : modules) {
        try {
            VirtualFile root = module.child(APP);
            VirtualFile japidViewDir = root.child(DirUtil.JAPIDVIEWS_ROOT);
            File japidFile = japidViewDir.getRealFile();
            if (japidFile.exists()) {
                String absoluteRootPath = root.getRealFile().getAbsolutePath();
                reloadChanged.addAll(reloadChanged(absoluteRootPath));
            }
        } catch (Throwable t) {
        }
    }
    return reloadChanged;
}
Also used : VirtualFile(play.vfs.VirtualFile) VirtualFile(play.vfs.VirtualFile) File(java.io.File)

Example 4 with VirtualFile

use of play.vfs.VirtualFile in project Japid by branaway.

the class JapidCommands method rmOrphanJava.

/**
	 * delete orphaned java artifacts from the japidviews directory of the current app and all the depended modules
	 * 
	 * @return
	 */
public static boolean rmOrphanJava() {
    boolean hasOrphan = false;
    String appAbs = Play.applicationPath.getAbsolutePath() + File.separator + APP;
    hasOrphan = removeOrphanedJavaFrom(appAbs);
    Collection<VirtualFile> modules = Play.modules.values();
    for (VirtualFile module : modules) {
        try {
            VirtualFile root = module.child(APP);
            VirtualFile japidViewDir = root.child(DirUtil.JAPIDVIEWS_ROOT);
            File japidFile = japidViewDir.getRealFile();
            if (japidFile.exists()) {
                String absoluteRootPath = root.getRealFile().getAbsolutePath();
                if (removeOrphanedJavaFrom(absoluteRootPath))
                    hasOrphan = true;
            }
        } catch (Throwable t) {
        }
    }
    return hasOrphan;
}
Also used : VirtualFile(play.vfs.VirtualFile) VirtualFile(play.vfs.VirtualFile) File(java.io.File)

Example 5 with VirtualFile

use of play.vfs.VirtualFile in project Japid by branaway.

the class JapidPlugin method beforeDetectingChanges.

@Override
public void beforeDetectingChanges() {
    // have a delay in change detection.
    if (System.currentTimeMillis() - lastTimeChecked.get() < 1000)
        return;
    try {
        List<File> changed = JapidCommands.reloadChanged();
        if (changed.size() > 0) {
            for (File f : changed) {
            // System.out.println("pre-detect changed: " + f.getName());
            }
        }
    } catch (JapidCompilationException e) {
        // turn japid compilation error to Play's template error to get
        // better error reporting
        JapidPlayTemplate jpt = new JapidPlayTemplate();
        jpt.name = e.getTemplateName();
        jpt.source = e.getTemplateSrc();
        // throw new TemplateExecutionException(jpt, e.getLineNumber(),
        // e.getMessage(), e);
        VirtualFile vf = VirtualFile.fromRelativePath("/app/" + e.getTemplateName());
        throw new CompilationException(vf, "\"" + e.getMessage() + "\"", e.getLineNumber(), 0, 0);
    } catch (RuntimeException e) {
        // e.printStackTrace();
        throw e;
    }
    boolean hasRealOrphan = JapidCommands.rmOrphanJava();
    lastTimeChecked.set(System.currentTimeMillis());
    if (hasRealOrphan) {
        // a little messy here. clean the cache in case bad files are delete
        // remove all the existing ApplicationClass will reload everything.
        // ideally we just need to remove the orphan. But the internal cache
        // is not visible. Need API change to do that.
        Play.classes.clear();
        throw new RuntimeException("found orphan template Java artifacts. reload to be safe.");
    }
}
Also used : VirtualFile(play.vfs.VirtualFile) CompilationException(play.exceptions.CompilationException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) VirtualFile(play.vfs.VirtualFile) File(java.io.File)

Aggregations

VirtualFile (play.vfs.VirtualFile)7 File (java.io.File)4 CompilationException (play.exceptions.CompilationException)3 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)2 JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)1 RendererClass (cn.bran.japid.rendererloader.RendererClass)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Http (play.mvc.Http)1 Request (play.mvc.Http.Request)1