Yes, it's possible.  There's probably a number of ways to do this
(like reading the bean directly out of the session map), but I do it
using a modified version of the code found at this url:

http://www.thoughtsabout.net/blog/archives/000033.html

The original code doesn't properly release a created FacesConfig.

    //  You need an inner class to be able to call
    // FacesContext.setCurrentInstance
    //  since it's a protected method
    private abstract static class InnerFacesContext extends FacesContext
    {
        protected static void setFacesContextAsCurrentInstance(
                FacesContext facesContext)
        {
            FacesContext.setCurrentInstance(facesContext);
        }
    }

    private FacesContext createFacesContext(ServletRequest request,
            ServletResponse response)
    {
        FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
        LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
        Lifecycle lifecycle = lifecycleFactory
                .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

        // Either set a private member servletContext =
        // filterConfig.getServletContext();
        // in you filter init() method or set it here like this:
        ServletContext servletContext =
((HttpServletRequest)request).getSession().getServletContext();
        // Note that the above line would fail if you are using any other
        // protocol than http

        // Doesn't set this instance as the current instance of
        // FacesContext.getCurrentInstance
        FacesContext facesContext =
contextFactory.getFacesContext(servletContext, request,
                response, lifecycle);

        // Set using our inner class
        InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

        // set a new viewRoot, otherwise context.getViewRoot returns null
        UIViewRoot view = facesContext.getApplication().getViewHandler()
                .createView(facesContext, "yourOwnID");
        facesContext.setViewRoot(view);

        return facesContext;
    }

//////////// Code used in doFilter:

            // Try to get it first
            boolean createdFacesContext = false;
            FacesContext facesContext = FacesContext.getCurrentInstance();
            if (facesContext == null)
            {
                facesContext = createFacesContext(request, response);
                createdFacesContext = true;
            }

            AdminDbDataStore adminDbDataStore;
            try
            {
                ValueBinding binding =
facesContext.getApplication().createValueBinding("#{adminDbDataStore}");
                adminDbDataStore =
(AdminDbDataStore)binding.getValue(facesContext);
            }
            finally
            {
                if (createdFacesContext)
                {
                    facesContext.release();
                }
            }


On 11/21/05, Peter Maas <[EMAIL PROTECTED]> wrote:
> It helped me in going over the sources to find other locations where the
> bean might be used.... and I found a generic servlet which also uses the
> bean... it is however jsf unaware... is it possible to retrieve to bean from
> the jsf context from a generic servlet in the same webapp?
>
>
>
>
> On 11/21/05, Bruno Aranda <[EMAIL PROTECTED]> wrote:
> > Peter, I've just tried with a simple application to reproduce your
> > problem and it works ok to me, so there must be something wrong
> > there... this is my code:
> >
> > [APP_BEAN]
> >
> > public class AppBacking
> > {
> >
> >     private String time;
> >
> >     public AppBacking() {
> >         System.out.println("APP backing initializing");
> >         time = new Date().toString();
> >     }
> >
> >     public String getTime()
> >     {
> >         return time;
> >     }
> >
> >     public void setTime(String time)
> >     {
> >         this.time = time;
> >     }
> > }
> >
> > [REQUEST_BEAN]
> >
> > public class ReqBacking
> > {
> >
> >     private AppBacking appBacking;
> >
> >     public ReqBacking() {
> >         System.out.println("REQUEST initializing");
> >     }
> >
> >     public AppBacking getAppBacking()
> >     {
> >         return appBacking;
> >     }
> >
> >     public void setAppBacking(AppBacking appBacking)
> >     {
> >         this.appBacking = appBacking;
> >     }
> >
> > }
> >
> > [/REQ_BEAN]
> >
> > [JSP]
> >
> > <h:form id="form">
> >   <h:panelGrid id="grid" columns="2">
> >     <h:outputText value="#{ reqBack.appBacking.time}"/>
> >     <h:commandButton value="test" action="none"/>
> >   </h:panelGrid>
> > </h:form>
> >
> > [/JSP]
> >
> > [CONFIG]
> >
> > <managed-bean>
> >         <managed-bean-name>appBack</managed-bean-name>
> >         <managed-bean-class>
> >             org.apache.myfaces.blank.AppBacking
> >         </managed-bean-class>
> >
> <managed-bean-scope>application</managed-bean-scope>
> >     </managed-bean>
> >
> >     <managed-bean>
> >         <managed-bean-name>reqBack</managed-bean-name>
> >         <managed-bean-class>
> > org.apache.myfaces.blank.ReqBacking</managed-bean-class>
> >         <managed-bean-scope>request</managed-bean-scope>
> >         <managed-property>
> >             <property-name>appBacking</property-name>
> >             <value>#{appBack}</value>
> >         </managed-property>
> >     </managed-bean>
> >
> > [/CONFIG]
> >
> > And the output I am getting, while clicking the button:
> >
> > REQUEST initializing
> > APP backing initializing
> > REQUEST initializing
> > REQUEST initializing
> > REQUEST initializing
> > ...
> >
> > So you can see that everything is working as expected. It is not the
> > case, but if you were setting a bean with shorter lifespan to another
> > bean (a request managed bean into an application managed bean, for
> > instance) an exception would be thrown.
> >
> > Hope that helps,
> >
> > Bruno
> >
> > 2005/11/21, Peter Maas <[EMAIL PROTECTED]>:
> > > The bean should acts as a central service locator for my jsf beans, and
> > > apart from the per request initializations is works as a shine.
> > >  Logging info written in the beans' constructor shows that the bean is
> > > instantiated every time over. The constructors are public, so the bean
> is
> > > not a singleton; should it be?
> > >
> > >  the bean is injected into other managed beans like this:
> > >
> > >      <managed-bean>
> > >          <description>
> > >              Service locator of the business services
> > >          </description>
> > >
> > >
> <managed-bean-name>serviceLocatorBean</managed-bean-name>
> > >          <managed-bean-class>
> > >              com.dp.jsf.ServiceLocatorBean
> > >          </managed-bean-class>
> > >
> > > <managed-bean-scope>application</managed-bean-scope>
> > >      </managed-bean>
> > >
> > >      <managed-bean>
> > >
> > > <managed-bean-name>CardsListBean</managed-bean-name>
> > >
> <managed-bean-class>com.dp.jsf.CardListBean</managed-bean-class>
> > >
> <managed-bean-scope>request</managed-bean-scope>
> > >          <managed-property>
> > >
> <property-name>serviceLocator</property-name>
> > >              <value>#{serviceLocatorBean}</value>
> > >          </managed-property>
> > >      </managed-bean>
> > >
> > >
> > >  Every time the CardsListBean is accessed, I see logging info from the
> > > service locator constructor. Is the way I inject the locator into the
> target
> > > bean correct or is this what causes the strange behaviour?
> > >
> > >  kind regards,
> > >
> > >  Peter
> > >
> > >
> > >
> > > On 11/21/05, Bruno Aranda <[EMAIL PROTECTED]> wrote:
> > > > Something wrong may be happening. A bean in application scope should
> > > > only initialize one time and you can use it as long as your
> > > > application is alive without being initialized again... How are you
> > > > checking the initialization of that bean?
> > > >
> > > > Bruno
> > > >
> > > > 2005/11/21, Peter Maas < [EMAIL PROTECTED]>:
> > > > > Hi all,
> > > > >
> > > > >  I tried to figure out if it is possible to share bean instances
> between
> > > > > different request. Currently the beans I have configured to be in
> > > > > application scope are instantiated for each request... is this the
> way
> > > it
> > > > > should work or am I doing something wrong?
> > > > >
> > > > >  regards,
> > > > >
> > > > >  Peter
> > > > >
> > > >
> > >
> > >
> >
>
>

Reply via email to