tags:

views:

183

answers:

2
  • How can I access a jsf managed bean (say, icefaces) from a blazeds client?
  • Will it be possible to share the same session information also ? (for eg, if I have a page with a jsf/icefaces component and a swf client - can they use the same session?)
A: 

hey. first of all you have to implement the FlexFactory in your own Factory:
http://livedocs.adobe.com/blazeds/1/blazeds_devguide/factory_2.html

I change the SpringFactory a little bit so you can access to the current instance of the given bean:

public class BeanFactory implements FlexFactory {

private static final String SOURCE = "source";

public void initialize(String id, ConfigMap configMap) {}

public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
    BeanFactoryInstance instance = new BeanFactoryInstance(this, id, properties);
    instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
    return instance;
}

public Object lookup(FactoryInstance inst) {
    BeanFactoryInstance factoryInstance = (BeanFactoryInstance) inst;
    return factoryInstance.lookup();
} 

static class BeanFactoryInstance extends FactoryInstance {
    BeanFactoryInstance(BeanFactory factory, String id, ConfigMap properties) {
        super(factory, id, properties);
    }

    public String toString() {
        return "BeanFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
    }

    public Object lookup() {
        HttpServletRequest hsr = FlexContext.getHttpRequest();
        String beanName = getSource();

        try
        {
            Object o = hsr.getSession().getAttribute(beanName);
            return o;
        }
        catch (Exception e)
        {
            ServiceException se = new ServiceException();
            String msg = "Java Bean '" + beanName + "' does not exist.";
            se.setMessage(msg);
            se.setRootCause(e);
            se.setDetails(msg);
            se.setCode("Server.Processing");
            throw se;
        }
    }
}}

in the service-config.xml (WEB-INF/flex folder) you have to register this factory:

<factories>
    <factory id="beanFactory" class="packageName.BeanFactory"/>
</factories>

then you have to register the factory in your destination in the remoting-config.xml like this:

<destination id="remoteService">
        <properties>
            <factory>beanFactory</factory>
            <source>beanName</source>
            <scope>session</scope>
        </properties>
 </destination>

so what is this BeanFactory doing:
when you want to access per remote from flex to java or to the jee-application, you declare a remoteobject in flex with the destination "remoteService" which is configured in the remoting-config.xml. the moment you access from flex to java by calling a server-sided-method, the beanfactory looks after the current instance of the bean you declare in the remoting-config.xml by getting the Request over the FlexContext:
HttpServletRequest hsr = FlexContext.getHttpRequest();

now you get the session and with the beanName the instance by calling
hsr.getSession().getAttribute(beanName)

this is only working with application and session beans and only if jsf instantiated the bean before the BeanFactory want to access the bean...

when you want to integrate a swf-file with icefaces you should take the ice:outputMedia-Tag an set the player-attribute to "flash"

if you work with eclipse to develop your jee-application and you integrate the tomcat in eclipse you can set the server root folder in the properties of your flex-project to the tomcat folder (flex builder):
alt text (Sorry not time for making this looks good ;) )

now you can start the tomcat server directly in eclipse and you can debug on flex and java too :)

hope this helps!

David
A: 

Hi David,

Thanks very much for the direction. I had the same problem and it works for me!!!

Neo

Neo