Carl, My problem is when I need to create another TcpInputSourceProvider and > need to bind a StreamFactory for BTcpStream.class or CTcpStream.class.
When do you need to do this? At configure() time? At runtime when the application is live? -Fred On Thu, Mar 11, 2010 at 7:27 AM, cdawg92 <[email protected]> wrote: > I've been looking at making an application Guicey, but I'm beginning > to think that I may be over complicating things trying to use Guice. > In short, the application has to listen to various input sources for > information. At this time these input sources are TCP with each source > having a different port from the same ip address, ending up with > potentially up to 3 inputs from a single host. Anyway, the input > source is configured the same way with only the host and port being > different. I won't know the host:port till runtime so I created a > factory to provide the underlying Stream interface passing in a > property object containing all the possible ports for the host. I have > then created a Provider for the final InputSource that takes in a list > of host properties and a Stream factory and returns a list of > InputSources. The classes bound to the factory know which ports they > are to retrieve from the host properties. Here's an example: > > The provider: > > public class TcpInputSourceProvider implements > Provider<List<InputSourceStringImpl>> { > > List<SdvServerProperties> servers; > TcpStreamFactory factory; > > @Inject > public TcpInputSourceProvider(@StreamFactory TcpStreamFactory > factory, > @ServerProperties List<SdvServerProperties> > servers){ > this.factory = factory; > this.servers = servers; > } > > @Override > public List<InputSourceStringImpl> get() { > List<InputSourceStringImpl> ret = new > ArrayList<InputSourceStringImpl>(servers.size()); > for(SdvServerProperties server : servers){ > ret.add(new TcpInputSource(factory.create(server))); > } > return ret; > } > } > > And the module: > public class InputSourceModule extends AbstractModule { > > List<SdvServerProperties> servers; > > public InputSourceModule(List<SdvServerProperties> servers){ > this.servers = servers; > } > /* (non-Javadoc) > * @see com.google.inject.AbstractModule#configure() > */ > @Override > protected void configure() { > bind(new TypeLiteral <List<SdvServerProperties>>() > {}).annotatedWith(ServerProperties.class).toInstance(servers); > > bind(TcpStreamFactory.class).annotatedWith(StreamFactory.class).toProvider( > > FactoryProvider.newFactory(TcpStreamFactory.class, > ATcpStream.class)); > bind(new TypeLiteral <List<InputSourceStringImpl>>() > {}).annotatedWith(TInput.class).toProvider(TcpInputSourceProvider.class); > } > } > > My problem is when I need to create another TcpInputSourceProvider and > need to bind a StreamFactory for BTcpStream.class or CTcpStream.class. > How can I bind different factories to the Provider? > > Thanks in advance for the help. > > Carl. > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
