private Map<Agent, String> runAgents( List<Agent> endpoints, ParameterizedRunnable<BoxAgentHelper> runnable) throws ServiceException { try { Map<Agent, ByteArrayOutputStream> streams = createAgentStreams(); BoxAgentHelper helper = createAgentHelper(endpoints, streams); runnable.run(helper); helper.join(); return getAgentOutput(streams); } catch (Exception ex) { throw new ServiceException("Failed calling agents", ex); } }
private BoxAgentHelper createAgentHelper( final List<Agent> agents, final Map<Agent, ByteArrayOutputStream> streams) { BoxAgentHelper agentHelper = new BoxAgentHelper(); agentHelper.setStreamFactory( new PrintStreamFactory() { public PrintStream create(Endpoint endpoint) { ByteArrayOutputStream bstream = new ByteArrayOutputStream(); streams.put(getAgent(endpoint), bstream); return new PrintStream(bstream); } }); CompositeEndpointConnectionFactory<BoxInterface> connectionFactory = new CompositeEndpointConnectionFactory<BoxInterface>(); connectionFactory.add(new BoxDirectConnectionFactory()); connectionFactory.add(new BoxTunnelConnectionFactory()); agentHelper.setEndpointConnectionFactory(connectionFactory); agentHelper.setEndpoints(getEndpoints(agents)); return agentHelper; }