On Mon, Aug 10, 2009 at 1:41 PM, Sergiu Dumitriu <[email protected]> wrote:
> Anamaria Stoica wrote: > > It looks like you can bind an instance to type in Guice. This is done > either > > with Instance Bindings, > > @Provides Methods or Provider Bindings, depending on the complexity of > the > > class. > > (http://code.google.com/p/google-guice/wiki/Bindings) > > > > All bindings are defined in a class that extends AbstractModule, this > would > > be > > XWSocialModule in my code ( > > > https://svn.xwiki.org/svnroot/xwiki/sandbox/gsoc/opensocial/xwiki-social-opensocial/src/main/java/org/xwiki/opensocial/social/XWSocialModule.java > ). > > > > > > These modules are then passed as arguments to Guice.createInjector(), > which > > builds the injector. > > In my application, the injector is build by GuiceServletContextListener ( > > > http://svn.apache.org/repos/asf/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/servlet/GuiceServletContextListener.java > ) > > This servlet takes the context parameters defined in my web.xml; these > are: > > > > <context-param> > > <param-name>guice-modules</param-name> > > <param-value> > > org.apache.shindig.common.PropertiesModule: > > org.apache.shindig.gadgets.DefaultGuiceModule: > > org.apache.shindig.gadgets.oauth.OAuthModule: > > org.apache.shindig.common.cache.ehcache.EhCacheModule: > > org.xwiki.opensocial.social.XWSocialModule > > </param-value> > > </context-param> > > > > <listener> > > > > > <listener-class>org.xwiki.container.servlet.XWikiServletContextListener</listener-class> > > </listener> > > > > Now, all I need to do is bind the instance of PersonServiceXW after it > has > > been initialized by XWiki's Component Manager. > > The binding will be done in XWSocialModule, using one of the instance > > binding methods (Instance Bindings, > > @Provides Methods or Provider Bindings). > > > > My questions are: > > 1. How do I make sure PersonServiceXW has been initialized already by the > > XWiki CM before binding it for Guice ? > > List GuiceServletContextListener AFTER XWikiServletContextListener in > web.xml. The servlet spec specifies that listeners are called in the > order they are encountered in web.xml, and XWikiServletContextListener > is the one that starts our component manager. I already had listed the Guice listener after the XWiki one in my web.xml. <!-- Initializes XWiki --> <listener> <listener-class>org.xwiki.container.servlet.XWikiServletContextListener</listener-class> </listener> <!-- Shindig Guice listener --> <listener> <listener-class>org.apache.shindig.common.servlet.GuiceServletContextListener</listener-class> </listener> > > > 2. How do I get the initialized PersonServiceXW instance from > XWSocialModule? > > com.xpn.xwiki.web.Utils.getComponent(PersonServiceXW.class) > Here I modified XWSocialModule to look like this: //bind(PersonService.class).to(PersonServiceXW.class); PersonServiceXW psxw = (PersonServiceXW) Utils.getComponent(SocialServiceComponent.class, "PersonServiceXW"); bind(PersonService.class).toInstance(psxw); The PersonServiceXW code is: public Future<Person> getPerson(UserId userId, Set<String> fields, SecurityToken token) throws ProtocolException { Person person = new PersonXW(); person.setId("XWiki.Julia"); if (!documentAccessBridge.exists("XWiki.Julia")) //some code } //some other code } The outcome: I get a NPE when trying to get the context from DefaultExtension, exactly like the one send earlier in a previous mail (stacktrace here http://ana-s-private-labs.pastebin.com/m655cdf70) What could be going wrong here ? Thanks, Anamaria > -- > Sergiu Dumitriu > http://purl.org/net/sergiu/ > _______________________________________________ > devs mailing list > [email protected] > http://lists.xwiki.org/mailman/listinfo/devs > _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs

