Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF)
icon5.gif   Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1098877] Sat, 31 August 2013 21:03 Go to next message
Jana Weschenfelder is currently offline Jana WeschenfelderFriend
Messages: 6
Registered: August 2013
Junior Member
Dear Ladies and Gentlemen,

I have a problem and maybe someone can help me here:
The problem is described in the forum of Spring. I will copy the whole content in
my next response to this post, since Eclipse does not allow links for new people.
It cost me 3 days of my time already and I am really stuck now...

I am working with a JAR file and not with a WAR file.

I would like to use the ServletContextAware interface to get the
ServletContext, but it would only work with the ContextLoaderListener as
far as I have found out. But ContextLoaderListener is from Spring (like
ServletContextAware), as the classpath
"org.springframework.web.context.ContextLoaderListener" says and the
ServletContextHandler is from Eclipse as the classpath
"org.eclipse.jetty.servlet.ServletContextHandler" tells, while the
CXFServlet uses Apache CXF classes
("org.apache.cxf.transport.servlet.CXFServlet"). I think that might be the
problem, because 3 institutions are involved here. I do not know how
much compatible all three are, since I have not found a documentation for
such a case. Every institutions only describes its part in the main, but
not a combination of all three together.

I am sorry, if it is posted below the Spring forum and here, but I am not a
professional here and I do not really know who to ask here since 3 institutions
are involved here.

Have you an idea how I can pass an attribute from Spring over Jetty to Apache CXF?
Any help is highly appreciated. I tried already many, many ways, but the
ServletContext is null then. If this forum feels responsible for this problem, I
would delete my other post in the other forum again.

Many thanks,
Jana
Re: Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1099068 is a reply to message #1098877] Sun, 01 September 2013 04:30 Go to previous messageGo to next message
Jana Weschenfelder is currently offline Jana WeschenfelderFriend
Messages: 6
Registered: August 2013
Junior Member
Okay, now the more detailed information, it would have been here earlier if it wouldn't need an approval of the Eclipse admin every time:

From the Spring Forum:
"Hello,

I am new to Spring and I am working on a Maven multi-module project where Spring is used.

My problem is that I try to inject an attribute or bean into a Apache CXF servlet. On the other side, I want to extract the ServletContext and that attribute there again. The problem sounds simple.

Since the parent project has a web module already, it is wanted that my module is not a web application project.
So I have a following construct: An existing web application (based on Spring, with applicationContext.xml and web.xml) which starts an Apache CXF server with JAX-RS support) is embedded in my non-web application module (Maven project) as dependency/as JAR file via my POM.xml. The existing web application is the official CDMI Reference Implementation from SNIA.ORG in this case. But I think that information is less important.

My project has an own XML-based configuration file that starts the CDMI RI (which is a JAR dependency in my project) as follows:
<bean id="server" class="org.eclipse.jetty.server.Server"
    init-method="start" destroy-method="stop">
    <description>Jetty server hosting the web application</description>
    <property name="gracefulShutdown" value="${cdmi.limits.jetty.graceful-shutdown}"/>
    <property name="threadPool" ref="thread-pool"/>
    <property name="connectors">
        <list>
            <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <property name="port" value="8542" />
                <property name="host" value="#{ T(com.google.common.base.Strings).emptyToNull( '${cdmi.net.listen}' ) }"/>
            </bean>
        </list>
    </property>
    <property name="handler">
        <bean class="org.eclipse.jetty.server.handler.HandlerList">
            <property name="handlers">
                <list>
                    <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
                    <!-- <bean class="org.springframework.web.context.ContextLoaderListener"> -->
                    <bean class="org.eclipse.jetty.servlet.ServletContextHandler">
                        <property name="contextPath" value="/cdmi-server" />  <!-- here -->
                        <property name="servletHandler">
                            <bean class="org.eclipse.jetty.servlet.ServletHandler">
                                <property name="servlets">
                                    <list>
                                        <bean class="org.eclipse.jetty.servlet.ServletHolder">
                                            <property name="name" value="cdmi-server" />
                                            <property name="servlet">
                                                <bean class="org.apache.cxf.transport.servlet.CXFServlet" />
                                            </property>
                                            <property name="initParameters">
                                                <map>
                                                    <entry key="contextConfigLocation" value="classpath:cxf-jsr-config.xml" />
                                                </map>
                                            </property>
                                            <property name="attributes">
                                                <bean class="org.eclipse.jetty.util.AttributesMap">
                                                    <constructor-arg>
                                                        <map>
                                                            <entry key="org.dcache.cdmi.container" value-ref="container"/>
                                                        </map>
                                                    </constructor-arg>
                                                </bean>
                                            </property>
                                        </bean>
                                    </list>
                                </property>
                                <property name="servletMappings">
                                    <list>
                                        <bean class="org.eclipse.jetty.servlet.ServletMapping">
                                            <property name="servletName" value="cdmi-server" />
                                            <property name="pathSpec" value="/*" />
                                        </bean>
                                    </list>
                                </property>
                            </bean>
                        </property>
                    </bean>
                    <bean class="org.eclipse.jetty.server.handler.DefaultHandler"/>
                </list>
            </property>
        </bean>
    </property>
</bean>


The cxf-jsr-config.xml is the former applicationContext.xml of the CDMI RI and includes the Apache CFX configuration and JAX-RS resources. It is included in the XML file of my project, too. The servlet gets started, I can see it in my web browser, it works fine so far. That means, the code of the cxf-jsr-config.xml looks like this plus some Spring beans:
<context:annotation-config/>

    <!-- Register autowired components -->

    <context:component-scan base-package="org.snia.cdmiserver"/>

    <!-- Apache CXF Core Configuration -->

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxrs:server id="cdmiService" address="/" beanNames="objectIdResource pathResource">
    ...
    </jaxrs:server>

    ...


My problem are those lines of my XML-based configuration file of my project (see first code snippet):
<property name="attributes">
    <bean class="org.eclipse.jetty.util.AttributesMap">
        <constructor-arg>
            <map>
                <entry key="org.dcache.cdmi.container" value-ref="container"/>
            </map>
        </constructor-arg>
    </bean>
</property>


"container" is a Spring bean which is in my XML file as well:
<bean id="container" class="org.dcache.cdmi.dao.ContainerDaoImpl"
    init-method="start" destroy-method="stop">
    <property name="pnfsStub" ref="pnfs-stub"/>
    <property name="pnfsHandler" ref="pnfs-handler"/>
    <property name="listDirectoryHandler" ref="list-handler"/>
</bean>


Within the ContainerDaoImpl.java which implements an interface of the CDMI RI (which is the CXFServlet), I have implemented the org.springframework.web.context.ServletContextAwar e interface. And I try to extract the ServletContext and the attribute with Object attribute = servletContext.getAttribute("org.dcache.cdmi.conta iner") there again. Before that I try to get the servletContext with the function "@Override public void setServletContext(ServletContext ctx) { this.servletContext = ctx; }". But this function never gets called and so the servletContext is null.

I have searched for a solution here many, many hours already, but I don't find an example or tutorial which solves the problem in a similar way or I am just entering the wrong keywords in the Google search.

Can someone please point me the right direction? What am I doing wrong?

I have tried this already, but it doesn't work, too (It seems that I would have to implement a HTTPServlet here which I have already with the CDMI RI):
<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="org.dcache.cdmi.container">
                <ref bean="container" />
            </entry>
        </map>
    </property>
</bean>


Many thanks in advance (I am sorry for some mistakes in my post, I am not a native English speaker),
Jana"

I think that might explain my problem better now. An answer would be highly appreciated. Many thanks!!!
Jana
Re: Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1099225 is a reply to message #1099068] Sun, 01 September 2013 10:27 Go to previous messageGo to next message
Jana Weschenfelder is currently offline Jana WeschenfelderFriend
Messages: 6
Registered: August 2013
Junior Member
After looking into the source code of org.eclipse.jetty.deploy.DeploymentManager, I have seen that this class is able to set and remove attributes as well.
I'm not sure if it helps me, but I will take a deeper look into it.

http://wiki.eclipse.org/Jetty/Feature/ContextDeployer#Configuring_Other_Things
Re: Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1099264 is a reply to message #1099225] Sun, 01 September 2013 11:56 Go to previous messageGo to next message
Jana Weschenfelder is currently offline Jana WeschenfelderFriend
Messages: 6
Registered: August 2013
Junior Member
Since it seems to be a restriction of the JettyServer (they speak about WAR in the documentation, not about JAR), I would try to extend the JettyServer now.

    <!-- <bean id="server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop"> --> <!-- old -->
    <bean name="webServer" class="path to ServerConfigurer"  init-method="start" destroy-method="stop"> <!-- new -->  
        <property name="connectors">
        ...


With ServerConfigurer.java:
import javax.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.springframework.web.context.ServletContextAware;
...

public class ServerConfigurer extends Server implements ServletContextAware
{
    private ServletHandler servletHandler = null;
    private ServletContext servletContext = null;
    ...
    //Implementation...


I also believe meanwhile that it isn't possible with the usual XML configuration anymore, it will need hard-coding as well. I will try to replace the Jetty server by an own Jetty server implementation then. I just will have to use ServletContextAware instead of ApplicationContextAware, or both.

The problem is described in the abcquick blogspot with the title "Combine Jetty and Spring Application Context" (Google search).
Re: Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1100762 is a reply to message #1099264] Tue, 03 September 2013 15:01 Go to previous messageGo to next message
Denis Roy is currently offline Denis RoyFriend
Messages: 483
Registered: October 2004
Location: Ottawa, Ontario, Canada
Senior Member

I highly doubt this is Newcomer material. In any case, such a lengthy posting has may go unanswered since simply reading it requires a substantial time investment.

I suggest you try to narrow your question as much as possible to better your chances at a response.


Denis Roy
Eclipse Webmaster -- webmaster@eclipse.org
Re: Inject attribute into Apache CXF servlet (from Spring, over Jetty, to Apache CXF) [message #1100810 is a reply to message #1100762] Tue, 03 September 2013 16:18 Go to previous message
Jana Weschenfelder is currently offline Jana WeschenfelderFriend
Messages: 6
Registered: August 2013
Junior Member
I agree about the fact that it might be advanced. And I work with it since 3 weeks for now. I haven't worked with Spring before. I am a student, but they await that from me.

Well, I could solve the problem now, you can find it here:
http://forum.springsource.org/showthread.php?141358-Inject-attribute-into-Apache-CXF-servlet
(I won't write the answer here anymore, since it would have the same length like former posts.)

Case is closed. Please don't search for an answer anymore!

Many thanks!

[Updated on: Tue, 03 September 2013 16:19]

Report message to a moderator

Previous Topic:Headless update from updatesite in Kepler
Next Topic:How to setup Synchronized Project with eGit
Goto Forum:
  


Current Time: Thu Apr 18 16:30:38 GMT 2024

Powered by FUDForum. Page generated in 0.03534 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top