Example usage for org.apache.commons.jci ReloadingClassLoader handleNotification

List of usage examples for org.apache.commons.jci ReloadingClassLoader handleNotification

Introduction

In this page you can find the example usage for org.apache.commons.jci ReloadingClassLoader handleNotification.

Prototype

public void handleNotification() 

Source Link

Usage

From source file:framework.ReloadingServer.java

public ReloadingServer() throws InterruptedException, IOException {
    URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
    ReloadingClassLoader pParent = new ReloadingClassLoader(urlClassLoader);
    final ReloadingClassLoader loader = new ReloadingClassLoader(pParent);
    for (URL url : urlClassLoader.getURLs()) {
        String file = url.getFile();
        if (!file.contains("commons-jci")) {
            if (file.endsWith(".jar") || file.endsWith("/web.xml") || file.endsWith(".properties")) {
                pParent.addResourceStore(new JarResourceStore(file));
            } else {
                pParent.addResourceStore(new FileResourceStore(new File(file)));
            }//from  w ww .  j  a  va 2  s. c o  m
        }
    }
    Thread.currentThread().setContextClassLoader(loader);

    String classesDir = new File("target/classes").getAbsolutePath();
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
    boolean watchSubtree = true;
    try {
        int watchID = JNotify.addWatch(classesDir, mask, watchSubtree, new JNotifyAdapter() {
            @Override
            public void fileModified(int wd, String rootPath, String name) {
                if (name.endsWith(".class")) {
                    modified = true;
                }
            }
        });
    } catch (Exception e) {
        Loggers.SERVER.error(e.getMessage(), e);
    }
    startJetty();
    while (true) {
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            break;
        }
        synchronized (ReloadingServer.class) {
            if (modified) {
                stopJetty();
                ReloadingClassLoader contextClassLoader = (ReloadingClassLoader) Thread.currentThread()
                        .getContextClassLoader();
                contextClassLoader.handleNotification();
                ReloadingClassLoader parent = (ReloadingClassLoader) contextClassLoader.getParent();
                parent.handleNotification();
                Thread.currentThread().setContextClassLoader(new ReloadingClassLoader(parent));
                startJetty();
                modified = false;
            }
        }
    }
}