Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] embedding jetty

Marc,

Try just setting the resource base and the context path.  Don't set the War
on the context.

cheers



marc2112 wrote:
> Hi,
> 
> I'm trying to embed Jetty in my own process.  I created a jar file that
> has all of my webapp resources under a directory in the jar called
> webapp (/webapp).  I want the context root to my webapplication to just
> be "/".  This is how it works when I run from Eclipse (so no jar file,
> everything in eclipse's classloader).  When I run from the jar, the
> context root is /webapp (the name of the directory).
> 
> Any thoughts on what I might be doing wrong?
> 
> My code is pasted below.
> 
> Thanks,
> ---Marc
> 
> public class ReverseProxy implements Runnable, InitializingBean {
>        private static final Logger log = Logger.getLogger(
> ReverseProxy.class );
> 
>        @Autowired
>        private Config config;
> 
>        @Autowired
>        private Server jetty;
> 
>        /**
>         * This is just a directory within your source tree, and
>         * can be exported as part of your normal .jar
>         */
>        private String webappDir = "webapp";
> 
>        private String contextPath = "/";
> 
>        private String resourceBase = webappDir;
>        private boolean resourceBaseWasChanged = false;
> 
>        /**
>         * This is the name of the class to use to load the webapp directory
>         */
>        private String classNameForResourceLoading = getClass().getName();
> 
>        private boolean enableJmx = true;
> 
>        @Override
>        public void run() {
>                Server jettyServer = getJetty();
> 
>                if ( enableJmx ) {
>                        // -- add jmx
>                        MBeanServer platformMBeanServer =
> ManagementFactory.getPlatformMBeanServer();
>                        MBeanContainer jmxContainer = new
> MBeanContainer(platformMBeanServer);
>                      
>  jettyServer.getContainer().addEventListener(jmxContainer);
>                        jettyServer.addBean(jmxContainer); //Add to the
> Server as a lifecycle
>                        jmxContainer.addBean( Log.getLog() );
>                }
> 
>                // -- add webapp files from jar
>                Class loaderClass;
>                try {
>                        loaderClass = Class.forName(
> getClassNameForResourceLoading() );
>                } catch (ClassNotFoundException e) {
>                        log.fatal( "Unable to load class " +
> getClassNameForResourceLoading(), e );
>                        return;
>                }
>                final URL warUrl =
> loaderClass.getClassLoader().getResource( webappDir );
> 
>                if ( warUrl == null ) {
>                        log.fatal( "Unable to load resource " + webappDir );
>                        return;
>                }
> 
>                final String warUrlString = warUrl.toExternalForm();
>                WebAppContext webAppContext = new WebAppContext( );
>                webAppContext.setWar( warUrlString );
>                webAppContext.setContextPath( contextPath );
> //              webAppContext.setResourceBase( resourceBase );
>                webAppContext.setLogUrlOnStart(true);
>                jettyServer.setHandler( webAppContext );
>                webAppContext.setClassLoader( getClass().getClassLoader() );
> 
> 
>                // -- set default web.xml (register JSP handler, etc)
>                String defaultWebXml = webappDir + "/WEB-INF/webdefault.xml";
>                webAppContext.setDefaultsDescriptor(defaultWebXml);
> 
>                HashMap<String, String> ctxInitParams = new
> HashMap<String, String>();
>              
>  ctxInitParams.put("org.eclipse.jetty.servlet.SessionDomain",
> config.getProperty("portal.cookieDomain", "gcotdwx0178.nam.nsroot.net
> <http://gcotdwx0178.nam.nsroot.net>"));
>              
>  ctxInitParams.put("org.eclipse.jetty.servlet.SessionCookie",
> config.getProperty("portal.jettySessionIdCookieName", "XSESSIONID"));
>                webAppContext.setInitParams(ctxInitParams);
> 
>                // -- expose bean factory to servlet context for Servlet
> and Filter Initialization
>                ApplicationContext beanFactory = config.getBeanFactory();
> 
>                final WebApplicationContext springWebAppContext;
>                if ( beanFactory instanceof WebApplicationContext ) {
>                        springWebAppContext = (WebApplicationContext)
> beanFactory;
>                }
>                else {
>                        springWebAppContext = new
> GenericWebApplicationContext();
>                        ( (GenericWebApplicationContext)
> springWebAppContext ).setServletContext(
> webAppContext.getServletContext() );
>                        ( (GenericWebApplicationContext)
> springWebAppContext ).setParent(beanFactory);
>                        ( (GenericWebApplicationContext)
> springWebAppContext ).refresh();
>                }
>                webAppContext.setAttribute(
> WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
> springWebAppContext );
>                log.info <http://log.info>( "************ " +
> springWebAppContext.getBeansOfType(ContentProviderManager.class).size() );
>                log.info <http://log.info>( "************ " +
> beanFactory.getBeansOfType(ContentProviderManager.class).size() );
> 
>                try {
>                        jettyServer.start();
>                } catch (Exception e) {
>                        System.err.println( "Error starting jetty server:
> " + e.getMessage() );
>                        e.printStackTrace();
>                }
>                try {
>                        jettyServer.join();
>                } catch (InterruptedException e) {
>                        System.out.println( "Jetty server thread
> interrupted: " + e.getMessage() );
>                }
>                System.out.println( "EXITING" );
>        }
> 
> 
>        public void setJetty(Server jetty) {
>                this.jetty = jetty;
>        }
> 
> 
>        public Server getJetty() {
>                return jetty;
>        }
> 
> 
>        public void setClassNameForResourceLoading(
>                        String classNameForResourceLoading) {
>                this.classNameForResourceLoading =
> classNameForResourceLoading;
>        }
> 
> 
>        public String getClassNameForResourceLoading() {
>                return classNameForResourceLoading;
>        }
> 
> 
>        public String getWebappDir() {
>                return webappDir;
>        }
> 
> 
>        public void setWebappDir(String webappDir) {
>                this.webappDir = webappDir;
>        }
> 
> 
>        public String getContextPath() {
>                return contextPath;
>        }
> 
> 
>        public void setContextPath(String contextPath) {
>                this.contextPath = contextPath;
>        }
> 
> 
>        public void setResourceBase(String resourceBase) {
>                this.resourceBase = resourceBase;
>                resourceBaseWasChanged = true;
>        }
> 
> 
>        public String getResourceBase() {
>                return resourceBase;
>        }
> 
>        @Override
>        public void afterPropertiesSet() throws Exception {
>                if ( !resourceBaseWasChanged ) {
>                        resourceBase = webappDir;
>                }
>        }
> 
> 
>        public void setEnableJmx(boolean enableJmx) {
>                this.enableJmx = enableJmx;
>        }
> 
> 
>        public boolean isEnableJmx() {
>                return enableJmx;
>        }
> }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> jetty-dev mailing list
> jetty-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/jetty-dev



Back to the top