Announcement Announcement Module
Collapse
No announcement yet.
Embedded web server (jetty) in a spring application how to make beans communicate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embedded web server (jetty) in a spring application how to make beans communicate?

    Hi all
    I'm trying to make an application that has an embedded webserver for remote monitoring/interaction.
    Following some tutorials and posts I've found on this forum I was able to start a jetty webserver with spring security and a stub controller.

    This is the context that launch the jetty server
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	  xmlns:context="http://www.springframework.org/schema/context"
    	  xmlns:p="http://www.springframework.org/schema/p"
    	  xmlns:mvc="http://www.springframework.org/schema/mvc"
    	  xmlns:jee="http://www.springframework.org/schema/jee"
    	  xmlns:aop="http://www.springframework.org/schema/aop"
    	  xmlns:task="http://www.springframework.org/schema/task"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    			 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
    >
    
        <!-- 
            This bean will be available for injection in the child context 
            that the Web Service is in. 
        -->
    
        <!--<context:property-placeholder location="org/springbyexample/ws/embedded/ws.properties"/>-->
        
        <!-- Manually start server after setting parent context. (init-method="start") -->
    	<bean id="jettyServer"
    		class="org.mortbay.jetty.Server"
    		destroy-method="stop">
    		<property name="threadPool">
    			<bean id="ThreadPool" class="org.mortbay.thread.QueuedThreadPool">
    				<property name="minThreads" value="2"/>
    				<property name="maxThreads" value="10"/>
    			</bean>		   
    		</property>
    		<property name="connectors">
    			<list>
    				<bean id="Connector"
    				  class="org.mortbay.jetty.nio.SelectChannelConnector"
    				  p:maxIdleTime="30000"
    				  p:acceptors="2"
    				  p:confidentialPort="8443">
    					  <property name="port">
    						  <bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean"
    							p:targetBeanName="config" p:propertyPath="port"/>								  
    					  </property>
    				</bean>
    			</list>
    		</property>
    		<property name="handlers">
    			<list>
    				<bean name="webapp" class="org.mortbay.jetty.webapp.WebAppContext">
    					<property name="contextPath" value="/"/>
    					<property name="server" ref="jettyServer"/>
    					<property name="resourceBase">
    						<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean"
    							p:targetBeanName="config" p:propertyPath="webappLocation"/>
    					</property>
    					</bean>
    			</list>
    		</property>
    	</bean>
    
    </beans>
    web.xml
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <display-name>CluKep</display-name>
        <description>Cluster Keeper</description>
    	
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>clukepwebapp</param-value>
        </context-param>	
    	
    	<!--
    		Log4j have to be the first to log all the other listeners.
    	-->
    <!--    <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.xml</param-value>
        </context-param>-->
    	
    	
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    	
    	<!--
    		Spring Security Context
    	-->
    	
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    
        <servlet>
            <servlet-name>clukep</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <servlet-mapping>
            <servlet-name>clukep</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>	
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/clukep-servlet.xml
                /WEB-INF/clukep-security.xml
            </param-value>
        </context-param>
            
        <distributable />
    
    </web-app>
    Static main
    Code:
    	public static void main(String[] args) {
    		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    
    		// Define a bean with the command line parameters and register it
    		BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Arrays.class, "asList").addConstructorArgValue(args).getBeanDefinition();
    		beanFactory.registerBeanDefinition("cmdArgs", beanDefinition);
    		GenericApplicationContext cmdArgCxt = new GenericApplicationContext(beanFactory);
    		// Must call refresh to initialize context 
    		cmdArgCxt.refresh();
    
    		ApplicationContext ac = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS, cmdArgCxt);
    		Server server = (Server) ac.getBean("jettyServer");
    
    		try {
    			server.start();
    		} catch (Exception ex) {
    			java.util.logging.Logger.getLogger(ClukepServerMain.class.getName()).log(Level.SEVERE, null, ex);
    		}
    
    		ServerEngine se = (ServerEngine) ac.getBean("serverEngine");
    		se.run();
    	}

    Now how can I make the beans of the context of the web xml see the beans on the server context?
    Thank You

  • #2
    Following an indication of another post I generated this code:

    Code:
    	private void init() {
    
    
    		this.webServer = new Server();
    
    		// main WWW context - "/"
    		WebAppContext webApp = new WebAppContext(this.webServer, ConfigurationLoader.getWebappLocation(), "/");
    		QueuedThreadPool threadPool = new QueuedThreadPool(10);
    		threadPool.setMinThreads(2);
    		this.webServer.setThreadPool(threadPool);
    		SelectChannelConnector connector = new SelectChannelConnector();
    		connector.setPort(port);
    		webServer.setConnectors(new Connector[]{connector});
    
    		logger.info("creating web application context");
    		this.webContext = new GenericWebApplicationContext();
    		this.webContext.setServletContext(webApp.getServletContext());
    		this.webContext.setParent(this.applicationContext);
    
    		webApp.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webContext);
    
    		logger.info("refresing web application context");
    		this.webContext.refresh();
    
    		// Creating the only servlet
    		DispatcherServlet mainServlet = new DispatcherServlet();
    		StringBuffer servletConfigLocations = new StringBuffer();
    		for (String location : configsLocations) {
    			servletConfigLocations.append(location);
    			servletConfigLocations.append("\n");
    		}
    
    		mainServlet.setContextConfigLocation(servletConfigLocations.toString());
    
    
    		ServletHolder servletHolder = new ServletHolder(mainServlet);
    		servletHolder.setName("Clukep");
    
    
    		webApp.addServlet(servletHolder, "/*");
    
    
    	}
    The servlet works as expected and it is able to see all the beans of the main context, but I was unable to make spring security work.
    I've tried to add the DelegateFilterProxy manually with this code:

    Code:
    webApp.addFilter(DelegatingFilterProxy.class, "/*", Handler.ALL);
    but i get

    Code:
     Failed startup of context org.mortbay.jetty.webapp.WebAppContext@730efd7c{/,file:/home/kirys/Developing/.store/clukep/ClukepServer/target/classes/}
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.web.filter.DelegatingFilterProxy-1026920195' is defined
    I've also tried
    Code:
    		servletHolder.setDisplayName("Cluster keeper");
    		DelegatingFilterProxy dfp= new DelegatingFilterProxy();
    		dfp.setTargetBeanName("springSecurityFilterChain");
    		dfp.setServletContext(mainServlet.getServletContext());
    		FilterHolder fh= new FilterHolder(dfp);
    but without any luck
    Can someone give me some hint?
    Thank You
    K.

    Comment

    Working...
    X