package mycomp.web.server;

import java.io.File;
import java.security.ProtectionDomain;

import org.eclipse.jetty.deploy.providers.ContextProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class EclipseDeveloppementServer extends EmbeddedJettyServer {
    private String currentDir;
    private String eclipseWebAppPath;
    
    public static void main(String[] args) throws Exception {   
        EclipseDeveloppementServer sc = new EclipseDeveloppementServer();
        sc.parseAndRun(args);
    }
    
    public EclipseDeveloppementServer() {
        ProtectionDomain domain = EclipseDeveloppementServer.class.getProtectionDomain();
        currentDir = new File(domain.getCodeSource().getLocation().getPath()).getParent();
        eclipseWebAppPath = new File(currentDir).getParent();
    }
    
    public void prepareWithHotDeployment(Server server, WebAppContext context) {
        context.setResourceBase(eclipseWebAppPath);
        context.setDescriptor(getWebAppDescriptor());
        
        ContextProvider wap = new ContextProvider();
        wap.setMonitoredDirName(eclipseWebAppPath);
        server.addBean(wap);
        wap.setScanInterval(1);
    }
    
    @Override
    public String getWarPath() {
        return eclipseWebAppPath;
    }
    
    @Override
    public String getWebAppDescriptor() {
        return eclipseWebAppPath + "/WEB-INF/web.xml";
    }
}
