Skip to content

Commit

Permalink
SWITCHYARD-318: ClientProxyBean.createExchange does not set exchange …
Browse files Browse the repository at this point in the history
  • Loading branch information
tfennelly committed Jul 5, 2011
1 parent 6d7d84c commit ced22aa
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import org.switchyard.ServiceReference;
import org.switchyard.component.bean.deploy.BeanDeploymentMetaData;
import org.switchyard.metadata.BaseExchangeContract;
import org.switchyard.metadata.InvocationContract;
import org.switchyard.metadata.ServiceOperation;
import org.switchyard.metadata.java.JavaService;

/**
* Client Proxy CDI Bean.
Expand Down Expand Up @@ -109,8 +111,8 @@ public ClientProxyBean(QName serviceQName, Class<?> proxyInterface, Set<Annotati
}

_proxyBean = Proxy.newProxyInstance(beanDeploymentMetaData.getDeploymentClassLoader(),
new Class[]{proxyInterface},
new ClientProxyInvocationHandler());
new Class[]{_serviceInterface},
new ClientProxyInvocationHandler(_serviceInterface));
}

/**
Expand Down Expand Up @@ -267,6 +269,12 @@ public void destroy(Object instance, CreationalContext creationalContext) {
*/
private class ClientProxyInvocationHandler implements InvocationHandler {

private JavaService _invokerInterface;

public ClientProxyInvocationHandler(Class<?> invokerInterface) {
_invokerInterface = JavaService.fromClass(invokerInterface);
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (_service == null) {
throw new BeanComponentException("A service reference to service '" + _serviceQName + "' is not bound into "
Expand Down Expand Up @@ -297,7 +305,7 @@ public void handleFault(Exchange exchange) {

Exchange exchangeOut = responseQueue.take();
if (exchangeOut.getState() == ExchangeState.OK) {
return exchangeOut.getMessage().getContent();
return exchangeOut.getMessage().getContent(method.getReturnType());
} else {
Object exceptionObj = exchangeOut.getMessage().getContent();

Expand Down Expand Up @@ -331,13 +339,20 @@ public void handleFault(Exchange exchange) {

private Exchange createExchange(ServiceReference service, Method method, ExchangeHandler responseExchangeHandler) throws BeanComponentException {
String operationName = method.getName();
ServiceOperation operation = service.getInterface().getOperation(operationName);
InvocationContract clientInvocationContext = _invokerInterface.getOperation(operationName);
ServiceOperation serviceOperation = service.getInterface().getOperation(operationName);

if (operation == null) {
if (serviceOperation == null) {
throw new BeanComponentException("Bean Component invocation failure. Operation '" + operationName + "' is not defined on Service '" + _serviceQName + "'.");
}

return service.createExchange(new BaseExchangeContract(operation), responseExchangeHandler);
BaseExchangeContract exchangeContext = new BaseExchangeContract(serviceOperation);
exchangeContext.getInvokerInvocationMetaData()
.setInputType(clientInvocationContext.getInputType())
.setOutputType(clientInvocationContext.getOutputType())
.setFaultType(clientInvocationContext.getFaultType());

return service.createExchange(exchangeContext, responseExchangeHandler);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterEvent) {
}

private void addInjectableClientProxyBean(Field injectionPointField, Reference serviceReference, Set<Annotation> qualifiers, BeanManager beanManager) {
QName serviceQName = toServiceQName(injectionPointField.getType());
QName serviceQName;

if (serviceReference.value().length() > 0) {
serviceQName = QName.valueOf(serviceReference.value());
} else {
serviceQName = toServiceQName(injectionPointField.getType());
}

addClientProxyBean(serviceQName, injectionPointField.getType(), qualifiers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public interface InventoryClientService1 {

String doStuff(String input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref;

import org.switchyard.component.bean.Reference;
import org.switchyard.component.bean.Service;
import org.switchyard.component.bean.multiversionref.oldinvservice.A;
import org.switchyard.component.bean.multiversionref.oldinvservice.OldInventoryService;

import javax.inject.Inject;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
@Service(InventoryClientService1.class)
public class InventoryClientService1Bean implements InventoryClientService1 {

@Inject @Reference("InventoryService")
private OldInventoryService oldInventoryService;

@Override
public String doStuff(String input) {
A result = oldInventoryService.getInventory(new A());
return "old";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public interface InventoryClientService2 {

String doStuff(String input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref;

import org.switchyard.component.bean.Reference;
import org.switchyard.component.bean.Service;
import org.switchyard.component.bean.multiversionref.newinvservice.B;
import org.switchyard.component.bean.multiversionref.newinvservice.InventoryService;

import javax.inject.Inject;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
@Service(InventoryClientService2.class)
public class InventoryClientService2Bean implements InventoryClientService2 {

@Inject @Reference("InventoryService")
private InventoryService newInventoryService;

@Override
public String doStuff(String input) {
B result = newInventoryService.getInventory(new B());
return "new";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref;

import org.junit.Assert;
import org.junit.Test;
import org.switchyard.test.SwitchYardTestCase;
import org.switchyard.test.SwitchYardTestCaseConfig;
import org.switchyard.test.mixins.CDIMixIn;

/**
* This test is testing checking that @Referenced services can be invoked
* through old and new interfaces and that the appropriate input/output
* parameter transformations are automatically applied based on the ExchangeContract
* information specified in the ClientBeanProxyHandler.
* <p/>
* InventoryClientService1 uses the old inventory interface to invoke the inventory service,
* while InventoryClientService2 uses the new inventory interface. In both cases, they are
* invoking the same backend service (the new version). SwitchYard ensures that the appropriate
* type transformation happen for InventoryClientService1.
*
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
@SwitchYardTestCaseConfig(mixins = CDIMixIn.class)
public class MultiVersionServiceTest extends SwitchYardTestCase {

@Test
public void test_InventoryClientService1() {
String response = newInvoker("InventoryClientService1.doStuff").sendInOut("hello").getContent(String.class);

Assert.assertEquals("old", response);
}

@Test
public void test_InventoryClientService2() {
String response = newInvoker("InventoryClientService2.doStuff").sendInOut("hello").getContent(String.class);

Assert.assertEquals("new", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref.newinvservice;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public class B {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref.newinvservice;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public interface InventoryService {

B getInventory(B b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.switchyard.component.bean.multiversionref.newinvservice;

import org.switchyard.component.bean.Service;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
@Service(InventoryService.class)
public class NewInventoryServiceBean implements InventoryService {

@Override
public B getInventory(B b) {
return b;
}
}

0 comments on commit ced22aa

Please sign in to comment.