Re: "Current connection" injection for a FTP client

41 views
Skip to first unread message

jordi

unread,
Jul 6, 2012, 5:21:12 AM7/6/12
to google...@googlegroups.com
I think that you're looking for Assisted Inject: http://code.google.com/p/google-guice/wiki/AssistedInject

On Thu, Jul 5, 2012 at 9:14 PM, cheez <u.int...@gmail.com> wrote:
I have a FTP client which has multiple servers configured by the user at runtime:

class FTPConnection
{
  FTPConnection(ServerInfo server, UserInfo user)...
}

I also have a client class which does something with the connection:

class DoSomethingWithFTP
{
  DoSomethingWithFTP(FTPConnection conn)...
}

For example, one type of DoSomethingWithFTP might be a DownloadAllTheThings process. What I'd like to do is have a notion of a "current connection" which is analogous to the "current user". Nevermind that I probably wouldn't write code like this, all I want to do is figure out how I would inject the "current" FTP connection!

So as you can see, a FTP connection requires some server info and user info. This is where I get stuck. The "current connection" is tied to these two values and they need to be injected into the FTPConnection.

If it was a web app, I might scope these values to the session but what if I wanted to process all of them at once, in multiple threads?

The pseudo-code I'd like to write is this:

foreach( pair<UserInfo,ServerInfo> config : whatever() )
{
  makeNewThreadAndProcessThisSite(config);
}

This is a very simplified view of the whole thing. I probably will not write code like this but I think it's an approximate proxy for the problem I am really trying to solve.

Would you kindly help me tease out a solution?

Thanks!

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/5lXTdY069zEJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Tim Boudreau

unread,
Aug 14, 2012, 2:19:39 AM8/14/12
to google...@googlegroups.com
I've run into that sort of thing a few times, and ended up developing a small library for easily writing custom Scopes, and then just have the code that is the front-door where requests come in enter the scope, passing in an array of objects that should be available for injection.  That way, objects don't leak outside of the call stack (unless you want them to - there's a way to wrap an ExecutorService so that will reenter the scope if you need to do async stuff).


Whether you use that code or your own stuff, that sounds like the pattern you want.  I gather writing your own scopes is discouraged for some reason or other, but with some well-tested base classes to avoid the obvious potential issues, they're darned useful - you could probably do it with assisted inject, but it would be considerably more verbose than
myScope.enter(someRunnable, connection, user);

-Tim

Reply all
Reply to author
Forward
0 new messages