use of org.onebusaway.federations.FederatedService in project onebusaway-application-modules by camsys.
the class FederatedByAgencyIdMethodInvocationHandlerImpl method invoke.
public Object invoke(FederatedServiceCollection collection, Method method, Object[] args) throws ServiceException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Object value = (Object) args[_argumentIndex];
if (_expression != null)
value = _expression.invoke(value);
String agencyId = (String) value;
FederatedService service = collection.getServiceForAgencyId(agencyId);
return method.invoke(service, args);
}
use of org.onebusaway.federations.FederatedService in project onebusaway-application-modules by camsys.
the class FederatedByAnyEntityIdMethodInvocationHandlerImpl method invoke.
public Object invoke(FederatedServiceCollection collection, Method method, Object[] args) throws ServiceException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Object target = args[_argumentIndex];
Set<String> agencyIds = new HashSet<String>();
for (PropertyPathExpression expression : _expressions) {
String entityId = (String) expression.invoke(target);
if (entityId != null)
agencyIds.add(AgencyIdSupport.getAgencyIdFromEntityId(entityId));
}
for (PropertyPathExpression expression : _agencyIdExpressions) {
String agencyId = (String) expression.invoke(target);
if (agencyId != null) {
agencyIds.add(agencyId);
}
}
FederatedService service = collection.getServiceForAgencyIds(agencyIds);
return method.invoke(service, args);
}
use of org.onebusaway.federations.FederatedService in project onebusaway-application-modules by camsys.
the class FederatedByAggregateMethodInvocationHandlerImplTest method testList.
@SuppressWarnings("unchecked")
@Test
public void testList() throws Exception {
SimpleFederatedService mockServiceA = Mockito.mock(SimpleFederatedService.class);
Mockito.when(mockServiceA.getValuesAsList()).thenReturn(Arrays.asList("a", "b"));
SimpleFederatedService mockServiceB = Mockito.mock(SimpleFederatedService.class);
Mockito.when(mockServiceB.getValuesAsList()).thenReturn(Arrays.asList("c", "d", "a"));
Set<FederatedService> services = new HashSet<FederatedService>();
services.add(mockServiceA);
services.add(mockServiceB);
FederatedServiceCollection mockCollection = Mockito.mock(FederatedServiceCollectionImpl.class);
Mockito.when(mockCollection.getAllServices()).thenReturn(services);
Method method = SimpleFederatedService.class.getDeclaredMethod("getValuesAsList");
FederatedServiceMethodInvocationHandler handler = new FederatedByAggregateMethodInvocationHandlerImpl(EMethodAggregationType.LIST);
List<String> results = (List<String>) handler.invoke(mockCollection, method, new Object[] {});
Mockito.verify(mockServiceA).getValuesAsList();
Mockito.verify(mockServiceB).getValuesAsList();
assertEquals(5, results.size());
Collections.sort(results);
assertEquals("a", results.get(0));
assertEquals("a", results.get(1));
assertEquals("b", results.get(2));
assertEquals("c", results.get(3));
assertEquals("d", results.get(4));
}
use of org.onebusaway.federations.FederatedService in project onebusaway-application-modules by camsys.
the class SituationQueryBeanFederatedServiceMethodInvocationHandler method invoke.
@Override
public Object invoke(FederatedServiceCollection collection, Method method, Object[] args) throws ServiceException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
if (args.length == 0) {
throw new ServiceException("unexpected number of arguments");
}
SituationQueryBean query = (SituationQueryBean) args[0];
Set<String> agencyIds = new HashSet<String>();
if (query.getAffects() != null) {
for (SituationQueryBean.AffectsBean affects : query.getAffects()) {
if (affects.getAgencyId() != null) {
agencyIds.add(affects.getAgencyId());
}
addAgencyId(affects.getRouteId(), agencyIds);
addAgencyId(affects.getTripId(), agencyIds);
addAgencyId(affects.getStopId(), agencyIds);
}
}
FederatedService service = collection.getServiceForAgencyIds(agencyIds);
return method.invoke(service, args);
}
use of org.onebusaway.federations.FederatedService in project onebusaway-application-modules by camsys.
the class DynamicFederatedServiceCollectionImplTest method test.
@Test
public void test() throws Exception {
Set<FederatedService> services = _collection.getAllServices();
assertTrue(services.isEmpty());
_registry.addService("http://localhost:" + PORT + "/service-A/service", SimpleFederatedService.class.getName(), new HashMap<String, String>());
Thread.sleep(TIMEOUT);
services = _collection.getAllServices();
assertEquals(1, services.size());
SimpleFederatedService service = (SimpleFederatedService) _collection.getServiceForAgencyId("A");
assertEquals("A", service.getValueForId(""));
_registry.addService("http://localhost:" + PORT + "/service-B/service", SimpleFederatedService.class.getName(), new HashMap<String, String>());
Thread.sleep(TIMEOUT);
services = _collection.getAllServices();
assertEquals(2, services.size());
service = (SimpleFederatedService) _collection.getServiceForAgencyId("A");
assertEquals("A", service.getValueForId(""));
service = (SimpleFederatedService) _collection.getServiceForAgencyId("B");
assertEquals("B", service.getValueForId(""));
// Simulate a server crash
_server.stop();
_server = null;
Thread.sleep(TIMEOUT);
services = _collection.getAllServices();
assertTrue(services.isEmpty());
}
Aggregations