Created
August 20, 2013 13:01
-
-
Save rzymek/6281044 to your computer and use it in GitHub Desktop.
JBoss 7 redeploy on refresh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jboss7; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.annotation.WebFilter; | |
import javax.servlet.http.HttpServletRequest; | |
@WebFilter(urlPatterns = "/*") | |
public class HotRedeployer implements Filter { | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
HttpServletRequest req= (HttpServletRequest) request; | |
String host = req.getLocalName(); | |
if("localhost".equals(host)){ | |
File war = new File(request.getServletContext().getRealPath("/")); | |
File deployments = war.getParentFile(); | |
File classes = new File(war, "WEB-INF/classes"); | |
File dodeploy = new File(deployments, war.getName()+".dodeploy"); | |
long lastDeployed = new File(deployments, war.getName()+".deployed").lastModified(); | |
if(isNewer(classes, lastDeployed)) { | |
dodeploy.createNewFile(); | |
} | |
if(dodeploy.exists()){ | |
response.setContentType("text/html"); | |
String uri = req.getRequestURL().toString(); | |
response.getWriter().write("<html><head><meta http-equiv='refresh' content='1; url="+uri+"'></meta></head><body>Redeploying...</body></html>"); | |
return; | |
} | |
} | |
chain.doFilter(request, response); | |
} | |
private boolean isNewer(File entry, long lastDeployed) { | |
if(entry.isDirectory()) { | |
File[] files = entry.listFiles(); | |
for (File file : files) { | |
if(isNewer(file, lastDeployed)) { | |
return true; | |
} | |
} | |
return false; | |
}else{ | |
return entry.lastModified() > lastDeployed; | |
} | |
} | |
@Override | |
public void init(FilterConfig cfg) throws ServletException { | |
} | |
@Override | |
public void destroy() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment