Thanks for the reply, now I know I have more work to do.On Thu, Feb 6, 2014 at 12:33 AM, Stuart Douglas <sdouglas@redhat.com> wrote:
From: "Mondain" <mondain@gmail.com>
To: undertow-dev@lists.jboss.org
Sent: Thursday, 6 February, 2014 9:36:34 AM
Subject: [undertow-dev] Embedding undertow into Red5I'm a core dev on the Red5 project and I would like to offer undertow as an alternative to our current embedded tomcat server. I have read through the examples, unit tests, and documentation regarding undertow. I have coded and run my "plugin" implementation, but all I am getting are "Not Found" responses in the browser. My suspicion is that the web.xml is not being read in and / or parsed, but I'm not sure since I'm new with this code base. Below you will see how I'm setting up and starting things; let me know what I'm doing incorrectly.Hi Mondain,You are correct in your suspicion that Undertow is not parsing the web.xml. Undertow provides an embedded Servlet implementation but it does not implement the discovery/parsing parts, as this is left up to the embedding application.Basically instead of using web.xml you need to add servlets, filters etc to the DeploymentInfo structure programatically. The main reason for this is basically because Undertow is designed to integrate well into other applications (especially Wildfly), and application servers generally provide their own implementation of parsers, annotation processors etc.With that said though at some point we will probably do a standalone parser/annotation processor to make this work out of the box (probably by just copying the relevant bits from wildfly and packaging them in their own jar). This is a fairly low priority item at the moment though.Stuart_______________________________________________//get a reference to the current threads classloaderfinal ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();// root location for servlet containerString serverRoot = System.getProperty("red5.root");String confRoot = System.getProperty("red5.config_root");System.setProperty("UNDERTOW_HOME", serverRoot);if (webappFolder == null) {// Use default webapps directorywebappFolder = FileUtil.formatPath(System.getProperty("red5.root"), "/webapps");}System.setProperty("red5.webapp.root", webappFolder);// scan the sub directories to determine our context pathsbuildContextPathList(webappFolder);try {// create our servlet containercontainer = ServletContainer.Factory.newInstance();// create a root path handlerfinal PathHandler rootHandler = new PathHandler();// create one server and use it everywhereUndertow.Builder builder = Undertow.builder();// loop through connectors adding listeners to the builderfor (UndertowConnector undertowConnector : connectors) {InetSocketAddress addr = undertowConnector.getSocketAddress();builder.addListener(addr.getPort(), addr.getHostName(), (undertowConnector.isSecure() ? ListenerType.HTTPS : ListenerType.HTTP));}// loop the other contextsfor (String contextPath : contextPaths) {// create a class loader for the contextClassLoader classLoader = buildWebClassLoader(originalClassLoader, webappFolder, contextPath);// create deployment infoDeploymentInfo info = deployment().setClassLoader(classLoader).setContextPath(contextPath.equalsIgnoreCase("/ROOT") ? "/" : contextPath).setDefaultEncoding("UTF-8").setDeploymentName(contextPath.substring(1) + ".war").setUrlEncoding("UTF-8");// add the new deployment to the servlet containerDeploymentManager manager = container.addDeployment(info);// deploymanager.deploy();// add pathrootHandler.addPrefixPath(info.getContextPath(), manager.start());// get deploymentDeployment deployment = manager.getDeployment();// get servlet contextfinal ServletContext servletContext = deployment.getServletContext();log.debug("Context initialized: {}", servletContext.getContextPath());try {log.debug("Context: {}", servletContext);final ClassLoader webClassLoader = info.getClassLoader();log.debug("Webapp classloader: {}", webClassLoader);// get the (spring) config file pathfinal String contextConfigLocation = servletContext.getInitParameter(org.springframework.web.context.ContextLoader.CONFIG_LOCATION_PARAM) == null ? defaultSpringConfigLocation: servletContext.getInitParameter(org.springframework.web.context.ContextLoader.CONFIG_LOCATION_PARAM);log.debug("Spring context config location: {}", contextConfigLocation);// get the (spring) parent context keyfinal String parentContextKey = servletContext.getInitParameter(org.springframework.web.context.ContextLoader.LOCATOR_FACTORY_KEY_PARAM) == null ? defaultParentContextKey: servletContext.getInitParameter(org.springframework.web.context.ContextLoader.LOCATOR_FACTORY_KEY_PARAM);// set current threads classloader to the webapp classloaderThread.currentThread().setContextClassLoader(webClassLoader);// create a thread to speed-up application loadingThread thread = new Thread("Launcher:" + servletContext.getContextPath()) {public void run() {//set thread context classloader to web classloaderThread.currentThread().setContextClassLoader(webClassLoader);//get the web app's parent contextApplicationContext parentContext = null;if (applicationContext.containsBean(parentContextKey)) {parentContext = (ApplicationContext) applicationContext.getBean(parentContextKey);} else {log.warn("Parent context was not found: {}", parentContextKey);}// create a spring web application contextfinal String contextClass = servletContext.getInitParameter(org.springframework.web.context.ContextLoader.CONTEXT_CLASS_PARAM) == null ? XmlWebApplicationContext.class.getName() : servletContext.getInitParameter(org.springframework.web.context.ContextLoader.CONTEXT_CLASS_PARAM);// web app context (spring)ConfigurableWebApplicationContext appctx = null;try {Class<?> clazz = Class.forName(contextClass, true, webClassLoader);appctx = (ConfigurableWebApplicationContext) clazz.newInstance();// set the root webapp ctx attr on the each servlet context so spring can find it laterservletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appctx);appctx.setConfigLocations(new String[] { contextConfigLocation });appctx.setServletContext(servletContext);// set parent context or use current app contextif (parentContext != null) {appctx.setParent(parentContext);} else {appctx.setParent(applicationContext);}// refresh the factoryappctx.refresh();appctx.start();} catch (Throwable e) {throw new RuntimeException("Failed to load webapplication context class", e);}}};thread.setDaemon(true);thread.start();} catch (Throwable t) {t.printStackTrace();} finally {//reset the classloaderThread.currentThread().setContextClassLoader(originalClassLoader);}}// Dump deployments listif (log.isDebugEnabled()) {for (String deployment : container.listDeployments()) {log.debug("Container deployment: {}", deployment);}}// add a root handlerbuilder.setHandler(rootHandler);// build the server instanceserver = builder.build();log.info("Starting Undertow");server.start();} catch (Exception e) {if (e instanceof BindException || e.getMessage().indexOf("BindException") != -1) {log.error("Error loading undertow, unable to bind connector. You may not have permission to use the selected port", e);} else {log.error("Error loading undertow", e);}}Regards,
undertow-dev mailing list
undertow-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/undertow-dev--
http://gregoire.org/
http://code.google.com/p/red5/