Created
December 17, 2008 01:31
-
-
Save fabiokung/36906 to your computer and use it in GitHub Desktop.
WebAppDeployer for jetty, which supports different war folders for different virtual hosts
This file contains 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
// WebAppDeployer for jetty, which supports different war folders for different virtual hosts | |
package br.com.caelum.jetty; | |
import java.util.ArrayList; | |
import org.mortbay.component.AbstractLifeCycle; | |
import org.mortbay.jetty.Handler; | |
import org.mortbay.jetty.HandlerContainer; | |
import org.mortbay.jetty.deployer.WebAppDeployer; | |
import org.mortbay.jetty.handler.ContextHandler; | |
import org.mortbay.jetty.handler.ContextHandlerCollection; | |
import org.mortbay.jetty.webapp.WebAppContext; | |
import org.mortbay.resource.Resource; | |
import org.mortbay.util.URIUtil; | |
public class CaelumWebAppDeployer extends WebAppDeployer { | |
private ArrayList _deployed; | |
private String[] virtualHosts; | |
public String[] getVirtualHosts() { | |
return this.virtualHosts; | |
} | |
public void setVirtualHosts(String[] virtualHosts) { | |
this.virtualHosts = virtualHosts; | |
} | |
/* ------------------------------------------------------------ */ | |
/** | |
* @throws Exception | |
*/ | |
public void doStart() throws Exception | |
{ | |
_deployed=new ArrayList(); | |
scan(); | |
} | |
public void scan() throws Exception | |
{ | |
if (getContexts()==null) | |
throw new IllegalArgumentException("No HandlerContainer"); | |
Resource r=Resource.newResource(getWebAppDir()); | |
if (!r.exists()) | |
throw new IllegalArgumentException("No such webapps resource "+r); | |
if (!r.isDirectory()) | |
throw new IllegalArgumentException("Not directory webapps resource "+r); | |
String[] files=r.list(); | |
files: for (int f=0; files!=null&&f<files.length; f++) | |
{ | |
String context=files[f]; | |
if (context.equalsIgnoreCase("CVS/")||context.equalsIgnoreCase("CVS")||context.startsWith(".")) | |
continue; | |
Resource app=r.addPath(r.encode(context)); | |
if (context.toLowerCase().endsWith(".war")||context.toLowerCase().endsWith(".jar")) | |
{ | |
context=context.substring(0,context.length()-4); | |
Resource unpacked=r.addPath(context); | |
if (unpacked!=null&&unpacked.exists()&&unpacked.isDirectory()) | |
continue; | |
} | |
else if (!app.isDirectory()) | |
continue; | |
if (context.equalsIgnoreCase("root")||context.equalsIgnoreCase("root/")) | |
context=URIUtil.SLASH; | |
else | |
context="/"+context; | |
if (context.endsWith("/")&&context.length()>0) | |
context=context.substring(0,context.length()-1); | |
// Check the context path has not already been added or the webapp itself is not already deployed | |
if (!getAllowDuplicates()) | |
{ | |
Handler[] installed=getContexts().getChildHandlersByClass(ContextHandler.class); | |
for (int i=0; i<installed.length; i++) | |
{ | |
ContextHandler c=(ContextHandler)installed[i]; | |
if (context.equals(c.getContextPath())) | |
continue files; | |
String path; | |
if (c instanceof WebAppContext) | |
path = ((WebAppContext)c).getWar(); | |
else | |
path = (c.getBaseResource()==null?"":c.getBaseResource().getFile().getAbsolutePath()); | |
if (path.equals(app.getFile().getAbsolutePath())) | |
continue files; | |
} | |
} | |
// create a webapp | |
WebAppContext wah=null; | |
if (getContexts() instanceof ContextHandlerCollection && | |
WebAppContext.class.isAssignableFrom(((ContextHandlerCollection)getContexts()).getContextClass())) | |
{ | |
try | |
{ | |
wah=(WebAppContext)((ContextHandlerCollection)getContexts()).getContextClass().newInstance(); | |
} | |
catch (Exception e) | |
{ | |
throw new Error(e); | |
} | |
} | |
else | |
{ | |
wah=new WebAppContext(); | |
} | |
// configure it | |
wah.setContextPath(context); | |
if (getConfigurationClasses()!=null) | |
wah.setConfigurationClasses(getConfigurationClasses()); | |
if (getDefaultsDescriptor()!=null) | |
wah.setDefaultsDescriptor(getDefaultsDescriptor()); | |
wah.setExtractWAR(isExtract()); | |
wah.setWar(app.toString()); | |
wah.setParentLoaderPriority(isParentLoaderPriority()); | |
wah.setVirtualHosts(this.virtualHosts); | |
// add it | |
getContexts().addHandler(wah); | |
_deployed.add(wah); | |
if (getContexts().isStarted()) | |
getContexts().start(); // TODO Multi exception | |
} | |
} | |
public void doStop() throws Exception | |
{ | |
for (int i=_deployed.size();i-->0;) | |
{ | |
ContextHandler wac = (ContextHandler)_deployed.get(i); | |
wac.stop();// TODO Multi exception | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment