Search in sources :

Example 6 with FederatedService

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);
}
Also used : FederatedService(org.onebusaway.federations.FederatedService)

Example 7 with FederatedService

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);
}
Also used : FederatedService(org.onebusaway.federations.FederatedService) PropertyPathExpression(org.onebusaway.collections.beans.PropertyPathExpression) HashSet(java.util.HashSet)

Example 8 with FederatedService

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));
}
Also used : FederatedService(org.onebusaway.federations.FederatedService) SimpleFederatedService(org.onebusaway.federations.SimpleFederatedService) FederatedServiceCollection(org.onebusaway.federations.FederatedServiceCollection) List(java.util.List) Method(java.lang.reflect.Method) SimpleFederatedService(org.onebusaway.federations.SimpleFederatedService) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with FederatedService

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);
}
Also used : ServiceException(org.onebusaway.exceptions.ServiceException) FederatedService(org.onebusaway.federations.FederatedService) HashSet(java.util.HashSet)

Example 10 with FederatedService

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());
}
Also used : FederatedService(org.onebusaway.federations.FederatedService) SimpleFederatedService(org.onebusaway.federations.SimpleFederatedService) SimpleFederatedService(org.onebusaway.federations.SimpleFederatedService) Test(org.junit.Test)

Aggregations

FederatedService (org.onebusaway.federations.FederatedService)14 HashSet (java.util.HashSet)6 Test (org.junit.Test)3 SimpleFederatedService (org.onebusaway.federations.SimpleFederatedService)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 PropertyPathExpression (org.onebusaway.collections.beans.PropertyPathExpression)2 FederatedServiceCollection (org.onebusaway.federations.FederatedServiceCollection)2 CoordinateBounds (org.onebusaway.geospatial.model.CoordinateBounds)2 ArrayList (java.util.ArrayList)1 NoSuchAgencyServiceException (org.onebusaway.exceptions.NoSuchAgencyServiceException)1 ServiceException (org.onebusaway.exceptions.ServiceException)1 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)1