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

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

Introduction

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

Prototype

@CallerSensitive
public final ClassLoader getParent() 

Source Link

Document

Returns the parent class loader for delegation.

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   www .j a  v a 2s  . 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;
            }
        }
    }
}