Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DI to instanciate xxxFactory #1785

Closed
jfarcand opened this issue Nov 21, 2014 · 8 comments
Closed

Allow DI to instanciate xxxFactory #1785

jfarcand opened this issue Nov 21, 2014 · 8 comments

Comments

@jfarcand
Copy link
Member

A default constructor instead of the current one will do the work, with class creation delagated to AtmosphereObjectFactory

jfarcand pushed a commit that referenced this issue Nov 21, 2014
@jfarcand jfarcand changed the title Allow DI to instanciate AtmosphereResourceFactory Allow DI to instanciate xxxFactory Nov 21, 2014
jfarcand pushed a commit that referenced this issue Nov 21, 2014
jfarcand pushed a commit that referenced this issue Nov 21, 2014
jfarcand pushed a commit that referenced this issue Nov 21, 2014
@slovdahl
Copy link
Contributor

slovdahl commented Dec 8, 2014

Commit 9371be0 makes it harder (or even impossible?) to inject AtmosphereResourceFactory with Guice. Below are the Guice module provider method I use with Atmosphere 2.2.3:

@Provides @Singleton
AtmosphereResourceFactory provideAtmosphereResourceFactory(AtmosphereGuiceServlet atmosphereGuiceServlet) {
    return atmosphereGuiceServlet.framework().atmosphereFactory();
}

The atmosphere-guice extension is configured to use the same injector as my application, which means that my provider method will be invoked when Atmosphere's GuiceObjectFactory tries to create an AtmosphereResourceFactory instance. And calling atmosphereGuiceServlet.framework().atmosphereFactory() will result in Guice invoking my provider method again and again. Voilá, a StackOverflowError occurs.

Removing the provider method makes Guice create a new AtmosphereResourceFactory instance every time an instance is injected, which obviously isn't desired.

I think there are two ways to work around this problem:

  1. Make AtmosphereResourceFactory abstract and implement it in eg. DefaultAtmosphereResourceFactory. In that case AtmosphereFramework line 2988 could ask for an instance of DefaultAtmosphereResourceFactory. This is how BroadcasterFactory is implemented, and the reason why I'm able to inject BroadcasterFactory with Guice.
  2. Annotate AtmosphereResourceFactory with the javax.inject.Singleton annotation. That way only one instance would ever be created by Guice.

Thoughts on this, @jfarcand?

@jfarcand
Copy link
Member Author

jfarcand commented Dec 8, 2014

DefaultAtmosphereResourceFactory has been added recently, as well as https://github.com/Atmosphere/atmosphere/blob/master/modules/cpr/src/main/java/org/atmosphere/cpr/AtmosphereFramework.java#L3028 . We cannot use Singleton has it will make a strong dependency on that library which I want to avoid.

@slovdahl
Copy link
Contributor

slovdahl commented Dec 8, 2014

Heh, didn't check the master branch. ;) Sorry for that. So can I backport the interface and DefaultAtmosphereResourceFactory in order to resolve my issue? Without the new Async API I guess?

And yes, I agree with you about Singletonas well!

@jfarcand
Copy link
Member Author

jfarcand commented Dec 8, 2014

Yes you can backport except the new API if that fixes Guice. Let me know once done as I'm planning to cut the release today (will test once your changes are in).

@slovdahl
Copy link
Contributor

slovdahl commented Dec 8, 2014

Of course it's not so easy..

  1. Having a AtmosphereResourceFactory interface and a DefaultAtmosphereResourceFactory implementation makes it impossible to keep the AtmosphereResourceFactory.getDefault() method, and hence breaks backward compatibility.
  2. Having an abstract AtmosphereResourceFactory class and a DefaultAtmosphereResourceFactory implementation would force DefaultAtmosphereResourceFactory to update a static field in AtmosphereResourceFactory every time a new DefaultAtmosphereResourceFactory is created.
  3. Revert the changes made for this issue on the atmosphere-2.2.x branch.

I don't like breaking backward compatibility, so I suggest either solution 2 or 3. What do you think?

@jfarcand
Copy link
Member Author

jfarcand commented Dec 8, 2014

True 1 shoudn't be done. Go with 2 IMO as it keep backward compatibility.

@slovdahl
Copy link
Contributor

slovdahl commented Dec 8, 2014

What do you think about slovdahl@e0f8852? It's in my private fork, I'll push it if it looks okay.

@jfarcand
Copy link
Member Author

jfarcand commented Dec 8, 2014

+1

slovdahl added a commit that referenced this issue Dec 8, 2014
Make AtmopshereResourceFactory abstract and implement it in DefaultAtmosphereResourceFactory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants