in rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java [395:627]
public synchronized void addServant(URL url, JettyHTTPHandler handler) {
//CHECKSTYLE:ON
if (shouldCheckUrl(handler.getBus())) {
checkRegistedContext(url);
}
initializeContexts();
SecurityHandler securityHandler = null;
if (server == null) {
DefaultHandler defaultHandler = null;
// create a new jetty server instance if there is no server there
server = createServer();
addServerMBean();
if (connector == null) {
connector = createConnector(getHost(), getPort(), handler.getBus());
if (LOG.isLoggable(Level.FINER)) {
logConnector((ServerConnector)connector);
}
}
server.addConnector(connector);
setupThreadPool();
/*
* The server may have no handler, it might have a collection handler,
* it might have a one-shot. We need to add one or more of ours.
*
*/
int numberOfHandlers = 1;
if (handlers != null) {
numberOfHandlers += handlers.size();
}
Handler existingHandler = server.getHandler();
Handler.Collection handlerCollection = null;
boolean existingHandlerCollection = existingHandler instanceof Handler.Collection;
if (existingHandlerCollection) {
handlerCollection = (Handler.Collection) existingHandler;
}
if (!existingHandlerCollection
&&
(existingHandler != null || numberOfHandlers > 1)) {
handlerCollection = new Handler.Sequence(new ArrayList<Handler>());
if (existingHandler != null) {
handlerCollection.addHandler(existingHandler);
}
server.setHandler(handlerCollection);
}
/*
* At this point, the server's handler is a collection. It was either
* one to start, or it is now one containing only the single handler
* that was there to begin with.
*/
if (handlers != null && !handlers.isEmpty()) {
for (Handler h : handlers) {
// Filtering out the jetty default handler
// which should not be added at this point.
if (h instanceof DefaultHandler) {
defaultHandler = (DefaultHandler) h;
} else {
if (h instanceof SecurityHandler
&& ((SecurityHandler)h).getHandler() == null) {
//if h is SecurityHandler(such as ConstraintSecurityHandler)
//then it need be on top of JettyHTTPHandler
//set JettyHTTPHandler as inner handler if
//inner handler is null
securityHandler = (SecurityHandler)h;
} else {
if (!(h instanceof JettyHTTPHandler)) {
//JettyHTTPHandler is a ServletHandler
//and must be added with ServletContext Handler later
handlerCollection.addHandler(h);
} else {
String contextName = HttpUriMapper.getContextName(url.getPath());
ServletContextHandler context = null;
context = ((JettyHTTPHandler)h).createContextHandler();
context.setContextPath(contextName);
context.setHandler(h);
contexts.addHandler(context);
}
}
}
}
}
/*
* handlerCollection may be null here if is only one handler to deal with.
* Which in turn implies that there can't be a 'defaultHander' to deal with.
*/
if (handlerCollection != null) {
if (defaultHandler != null) {
handlerCollection.addHandler(defaultHandler);
}
} else {
server.setHandler(contexts);
}
try {
server.start();
} catch (Exception e) {
LOG.log(Level.SEVERE, "START_UP_SERVER_FAILED_MSG", new Object[] {e.getMessage(), port});
//problem starting server
try {
server.stop();
server.destroy();
} catch (Exception ex) {
//ignore - probably wasn't fully started anyway
}
server = null;
throw new Fault(new Message("START_UP_SERVER_FAILED_MSG", LOG, e.getMessage(), port), e);
}
}
String contextName = HttpUriMapper.getContextName(url.getPath());
if (contextName.length() == 0) {
contextName = "/"; //ensure it is mapped as root context,
}
String path = HttpUriMapper.getResourceBase(url.getPath());
if (!path.equals("/")) {
//add /* to enable wild match
path = path + "/*";
}
if (contextName.endsWith("/*")) {
//This is how Jetty ServletContextHandler handle the contextName
//without suffix "/", the "*" suffix will be removed
contextName = contextName + "/";
}
ServletContextHandler context = null;
if (this.contextHandlerMap.containsKey(contextName)
&& this.contextHandlerMap.get(contextName).getServletHandler().getMatchedServlet(path) != null) {
context = this.contextHandlerMap.get(contextName);
ServletHandler servletHandler = context.getServletHandler();
MatchedResource<MappedServlet> mappedServlet = servletHandler.getMatchedServlet(path);
ServletHolder servletHolder = mappedServlet.getResource().getServletHolder();
Servlet servlet = null;
try {
servlet = servletHolder.getServlet();
} catch (ServletException ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] {
ex.getMessage()
});
}
if (servlet != null && servlet instanceof JettyHTTPHandler && servletHolder.isStarted()) {
try {
// the servlet exist with the same path
// just update the servlet
context.stop();
servletHolder.setServlet(handler);
servletHolder.stop();
servletHolder.start();
servletHolder.initialize();
context.start();
} catch (Exception ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] {
ex.getMessage()
});
}
} else {
try {
context.addServlet(handler, path);
} catch (Exception ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] {
ex.getMessage()
});
}
}
} else {
context = handler.createContextHandler();
context.setContextPath(contextName);
context.addServlet(handler, path);
contexts.addHandler(context);
this.contextHandlerMap.put(contextName, context);
// bind the jetty http handler with the context handler
if (isSessionSupport) {
SessionHandler sh = configureSession();
if (securityHandler != null) {
//use the securityHander which already wrap the jetty http handler
sh.setHandler(securityHandler);
}
context.setSessionHandler(sh);
} else {
// otherwise, just the one.
if (securityHandler != null) {
//use the securityHander which already wrap the jetty http handler
context.setSecurityHandler(securityHandler);
}
}
}
if (server.getHandler() != contexts) {
for (Handler h : contexts.getHandlers()) {
((Handler.Collection)server.getHandler()).addHandler(h);
try {
h.start();
} catch (Exception ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] {ex.getMessage()});
}
}
}
ServletContext sc = context.getServletContext();
handler.setServletContext(sc);
String smap = getHandlerName(url, context);
handler.setName(smap);
if (contexts.isStarted() && context != null && !context.isStarted()) {
try {
context.start();
} catch (Exception ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] {ex.getMessage()});
}
}
registedPaths.add(url.getPath());
++servantCount;
}