alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (GroovyScriptEngineReloadingTest.groovy)

This example Groovy source code file (GroovyScriptEngineReloadingTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

binding, charset, ioexception, ioexception, mapfileentry, mapfileentry, mapurlconnection, mapurlfactory, mapurlfactory, mapurlhandler, string, string, threading, threads, url_host, urlconnection

The Groovy GroovyScriptEngineReloadingTest.groovy source code

package groovy.util

import java.util.concurrent.ConcurrentHashMap

/**
 *
 * @author Guillaume Laforge
 */
class GroovyScriptEngineReloadingTest extends GroovyTestCase {
    GroovyScriptEngine gse;

    void setUp() {
        MapFileSystem.instance.registerMapFileSystem()
        gse = new GroovyScriptEngine([MapUrlConnection.URL_SCHEME] as String[])
        gse.config.minimumRecompilationInterval = 0
    }

    void testIsSourceNewer() {
        Binding binding = new Binding()
        int val = 0
        binding.setVariable("val", val)
        MapFileSystem.instance.modFile("s_1", "val = 1", new Date().time)
        gse.run("s_1", binding)

        assert binding.getVariable("val") == 1

        sleep 1000

        MapFileSystem.instance.modFile("s_1", "val = 2", new Date().time)
        gse.run("s_1", binding)

        assert binding.getVariable("val") == 2
    }
}

class MapFileEntry {
    String content
    long lutime

    public MapFileEntry(String content, long lutime) {
        this.content = content
        this.lutime = lutime
    }
}

@Singleton
class MapFileSystem {

    public final ConcurrentHashMap<String, MapFileEntry> fileCache = new ConcurrentHashMap()
    private boolean registered = false

    void registerMapFileSystem() {
        if (!registered) {
            try {
                URL.setURLStreamHandlerFactory(new MapUrlFactory())
                registered = true
            } catch (Error e) { }
        }
    }

    void modFile(String name, String content, long lutime) {
        if (fileCache.containsKey(name)) {
            MapFileEntry sce = fileCache.get(name)
            sce.content = content
            sce.lutime = lutime
        } else {
            fileCache.put(name, new MapFileEntry(content, lutime))
        }
    }

    String getFilesrc(String name) {
        return fileCache.get(name).content
    }

    boolean fileExists(String name) {
        return fileCache.containsKey(name)
    }
}

class MapUrlHandler extends URLStreamHandler {

    MapUrlHandler() {
        super()
    }

    protected URLConnection openConnection(URL u) throws IOException {
        return new MapUrlConnection(u)
    }

    protected void parseURL(URL u, String spec, int start, int limit) {
        super.parseURL(u, spec, start, limit)
    }
}

class MapUrlConnection extends URLConnection {
    String getContentEncoding() {
        return CHARSET
    }

    Object getContent() throws IOException {
        return super.content
    }

    public static final String CHARSET = "UTF-8"
    public static final String URL_HOST = "local"
    public static final String URL_SCHEME = "map://" + URL_HOST + "/"

    public static final String PROTOCOL = "map"

    private String name

    InputStream getInputStream() throws IOException {
        // System.out.println(name+"\t"+MapFileSystem.fileCache.get(name).content);
        if (MapFileSystem.instance.fileCache.containsKey(name)) {
            String content = MapFileSystem.instance.fileCache.get(name).content
            return new ByteArrayInputStream(content.getBytes(CHARSET))
        } else {
            throw new IOException("file not found" + name)
        }
    }

    long getLastModified() {
        long lastmodified = 0
        if (MapFileSystem.instance.fileCache.containsKey(name)) {
            lastmodified = MapFileSystem.instance.fileCache.get(name).lutime
        }
        // System.out.println(name+"\t"+lastmodified);
        return lastmodified
    }

    URL getURL() {
        return super.getURL()
    }

    MapUrlConnection(URL url) {
        super(url)
        name = url.getFile()
        if (name.startsWith("/")) {
            name = name.substring(1)
        }
    }

    void connect() throws IOException { }
}

class MapUrlFactory implements URLStreamHandlerFactory {

    MapUrlFactory() {
        super()
    }

    URLStreamHandler createURLStreamHandler(String protocol) {
        if (MapUrlConnection.PROTOCOL.equals(protocol)) {
            return new MapUrlHandler()
        } else {
            return null
        }
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GroovyScriptEngineReloadingTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.