SCAInvoker extends BaseServiceHandler { @Override public void handleMessage(Exchange exchange) throws HandlerException { exchange.getContext().setProperty(ExchangeCompletionEvent.GATEWAY_NAME, _bindingName, Scope.EXCHANGE) .addLabels(BehaviorLabel.TRANSIENT.label()); if (getState() != State.STARTED) { throw SCAMessages.MESSAGES.referenceBindingNotStarted(_referenceName, _bindingName); } try { QName serviceName = getTargetServiceName(exchange); ServiceReference ref = exchange.getProvider().getDomain().getServiceReference(serviceName); if (_clustered) { if (_preferLocal && ref != null) { invokeLocal(exchange, ref); } else { invokeRemote(exchange, serviceName); } } else { if (ref == null) { throw SCAMessages.MESSAGES.serviceReferenceNotFoundInDomain(serviceName.toString(), exchange.getProvider().getDomain().getName().toString()); } invokeLocal(exchange, ref); } } catch (SwitchYardException syEx) { throw new HandlerException(syEx.getMessage()); } } SCAInvoker(SCABindingModel config);  SCAInvoker(SCABindingModel config, RemoteRegistry registry); @Override void handleMessage(Exchange exchange); SCAInvoker setDisableRemoteTransaction(boolean disable); static final String CONTEXT_PROPERTY_PREFIX; static final String KEY_TARGET_SERVICE; static final String KEY_TARGET_NAMESPACE; }
@Test public void localInvocationSameName() throws Exception { V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName TEST_SERVICE = new QName("urn:test", "SCAInvokerTest"); ServiceReference reference = _domain.createInOnlyService(TEST_SERVICE, _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.consumer(reference, new InOnlyOperation("Test")); ex.provider(_domain.getServices().get(0), new InOnlyOperation("Test")); invoker.handleMessage(ex); Assert.assertTrue(_provider.getMessages().size() == 1); Assert.assertEquals(msg.getContent(), _provider.getMessages().poll().getMessage().getContent()); }
@Test public void localInvocationTarget() throws Exception { V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; config.setTarget("ServiceB"); SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName SERVICE_A = new QName("urn:test", "ServiceA"); final QName SERVICE_B = new QName("urn:test", "ServiceB"); ServiceReference referenceA = _domain.registerServiceReference(SERVICE_A, new InOnlyService()); _domain.registerServiceReference(SERVICE_B, new InOnlyService()); Service serviceA = _domain.registerService(SERVICE_A, new InOnlyService(), new MockHandler()); _domain.registerService(SERVICE_B, new InOnlyService(), _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.consumer(referenceA, new InOnlyOperation(null)); ex.provider(serviceA, new InOnlyOperation(null)); invoker.handleMessage(ex); Assert.assertTrue(_provider.getMessages().size() == 1); Assert.assertEquals(msg.getContent(), _provider.getMessages().poll().getMessage().getContent()); }
@Test public void crossDomain() throws Exception { V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; config.setTargetNamespace("urn:testb"); SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName SERVICE_A = new QName("urn:testa", "Service"); final QName SERVICE_B = new QName("urn:testb", "Service"); MockDomain domain1 = _domain; MockDomain domain2 = new MockDomain(domain1.getServiceRegistry()); ServiceReference referenceA = domain1.registerServiceReference(SERVICE_A, new InOnlyService()); Service serviceA = domain1.registerService(SERVICE_A, new InOnlyService(), new MockHandler()); domain2.registerServiceReference(SERVICE_B, new InOnlyService()); domain2.registerService(SERVICE_B, new InOnlyService(), _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.consumer(referenceA, new InOnlyOperation(null)); ex.provider(serviceA, new InOnlyOperation(null)); invoker.handleMessage(ex); Assert.assertTrue(_provider.getMessages().size() == 1); Assert.assertEquals(msg.getContent(), _provider.getMessages().poll().getMessage().getContent()); }
@Test public void localInvocationWithInvalidServiceReference() throws Exception { boolean fail = false; V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; config.setTarget("Blah"); SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName SERVICE_A = new QName("urn:test", "ServiceA"); ServiceReference referenceA = _domain.registerServiceReference(SERVICE_A, new InOnlyService()); Service serviceA = _domain.registerService(SERVICE_A, new InOnlyService(), null); _domain.registerService(SERVICE_A, new InOnlyService(), _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.consumer(referenceA, new InOnlyOperation(null)); ex.provider(serviceA, new InOnlyOperation(null)); try { invoker.handleMessage(ex); } catch (HandlerException e) { String message = e.getMessage(); Assert.assertTrue(message.contains("SWITCHYARD039604")); fail = true; } Assert.assertTrue(fail); }
@Test public void localInvocationTargetNamespace() throws Exception { V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; config.setTargetNamespace("urn:app2"); SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName SERVICE_A = new QName("urn:app1", "ServiceA"); final QName SERVICE_B = new QName("urn:app2", "ServiceA"); ServiceReference referenceA = _domain.registerServiceReference(SERVICE_A, new InOnlyService()); _domain.registerServiceReference(SERVICE_B, new InOnlyService()); Service serviceA = _domain.registerService(SERVICE_A, new InOnlyService(), new MockHandler()); _domain.registerService(SERVICE_B, new InOnlyService(), _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.consumer(referenceA, new InOnlyOperation(null)); ex.provider(serviceA, new InOnlyOperation(null)); invoker.handleMessage(ex); Assert.assertTrue(_provider.getMessages().size() == 1); Assert.assertEquals(msg.getContent(), _provider.getMessages().poll().getMessage().getContent()); }
@Test public void contextPropertiesCopied() throws Exception { V1SCABindingModel config = new V1SCABindingModel(SwitchYardNamespace.DEFAULT.uri()) { @Override public String getName() { return "testBinding"; } public CompositeReferenceModel getReference() { return new V1CompositeReferenceModel(); }; }; SCAInvoker invoker = new SCAInvoker(config); invoker.start(); final QName TEST_SERVICE = new QName("urn:test", "SCAInvokerTest"); ServiceReference reference = _domain.createInOnlyService(TEST_SERVICE, _provider); MockExchange ex = new MockExchange(); Message msg = ex.createMessage().setContent("TEST"); ex.setMessage(msg); ex.getMessage().getContext().setProperty("message-prop", "abc"); ex.consumer(reference, new InOnlyOperation("Test")); ex.provider(_domain.getServices().get(0), new InOnlyOperation("Test")); invoker.handleMessage(ex); Assert.assertTrue(_provider.getMessages().size() == 1); Exchange receivedEx = _provider.getMessages().poll(); Assert.assertEquals("abc", receivedEx.getMessage().getContext().getPropertyValue("message-prop")); }