From a9fe3055cfd607a6e3dba32f6ea87f69b091fdd1 Mon Sep 17 00:00:00 2001 From: Christoph Gostner Date: Fri, 3 May 2013 21:07:13 +0200 Subject: [PATCH 1/6] SWITCHYARD-1458 service versioning support --- adapter/pom.xml | 45 +++++ .../adapter/AdapterRegistryLoader.java | 148 ++++++++++++++ .../org/switchyard/adapter/AdapterTypes.java | 68 +++++++ .../org/switchyard/adapter/AdapterUtil.java | 184 ++++++++++++++++++ .../model/AdapterSwitchYardScanner.java | 182 +++++++++++++++++ .../adapter/config/model/JavaAdaptModel.java | 50 +++++ .../config/model/v1/V1AdapterMarshaller.java | 59 ++++++ .../config/model/v1/V1JavaAdaptModel.java | 69 +++++++ .../adapter/config/model/v1/adapter-v1.xsd | 49 +++++ .../config/model/descriptor.properties | 21 ++ .../adapter/AdapterRegistryLoaderTest.java | 106 ++++++++++ .../switchyard/adapter/AdapterUtilTest.java | 99 ++++++++++ .../config/model/AdapterModelTests.java | 124 ++++++++++++ .../model/AdapterSwitchYardScannerTest.java | 71 +++++++ .../model/adapters/AnnoV1toV2Adapter.java | 47 +++++ .../config/model/adapters/V1toV2Adapter.java | 42 ++++ adapter/src/test/resources/META-INF/beans.xml | 0 .../test/resources/META-INF/switchyard.xml | 13 ++ .../config/model/AdapterModelTests.xml | 28 +++ addressing/pom.xml | 40 ++++ .../addressing/ServiceResolver.java | 58 ++++++ .../resolvers/AdapterServiceResolver.java | 79 ++++++++ .../resolvers/BaseServiceResolver.java | 50 +++++ .../resolvers/DefaultServiceResolver.java | 55 ++++++ .../java/org/switchyard/ServiceDomain.java | 7 + .../java/org/switchyard/adapter/Adapter.java | 67 +++++++ .../switchyard/adapter/AdapterRegistry.java | 72 +++++++ .../org/switchyard/adapter/BaseAdapter.java | 67 +++++++ .../org/switchyard/annotations/Adapter.java | 45 +++++ .../switchyard/event/AdapterAddedEvent.java | 48 +++++ .../switchyard/event/AdapterRemovedEvent.java | 47 +++++ .../exception/DuplicateAdapterException.java | 49 +++++ .../config/model/adapter/AdaptModel.java | 79 ++++++++ .../config/model/adapter/AdaptersModel.java | 58 ++++++ .../model/adapter/v1/V1AdaptersModel.java | 94 +++++++++ .../model/adapter/v1/V1BaseAdaptModel.java | 91 +++++++++ .../model/switchyard/SwitchYardModel.java | 16 ++ .../switchyard/v1/V1SwitchYardMarshaller.java | 4 + .../switchyard/v1/V1SwitchYardModel.java | 23 +++ .../model/switchyard/v1/switchyard-v1.xsd | 14 ++ .../deploy/ServiceDomainManager.java | 5 +- .../deploy/internal/AbstractDeployment.java | 15 ++ .../deploy/internal/Deployment.java | 8 + pom.xml | 2 + runtime/pom.xml | 5 + .../handlers/AddressingHandler.java | 41 ++-- .../org/switchyard/internal/DomainImpl.java | 12 ++ .../internal/adapter/BaseAdapterRegistry.java | 120 ++++++++++++ 48 files changed, 2660 insertions(+), 16 deletions(-) create mode 100644 adapter/pom.xml create mode 100644 adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java create mode 100644 adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java create mode 100644 adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd create mode 100644 adapter/src/main/resources/org/switchyard/config/model/descriptor.properties create mode 100644 adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java create mode 100644 adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java create mode 100644 adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java create mode 100644 adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java create mode 100644 adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java create mode 100644 adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java create mode 100644 adapter/src/test/resources/META-INF/beans.xml create mode 100644 adapter/src/test/resources/META-INF/switchyard.xml create mode 100644 adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml create mode 100644 addressing/pom.xml create mode 100644 addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java create mode 100644 addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java create mode 100644 addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java create mode 100644 addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java create mode 100644 api/src/main/java/org/switchyard/adapter/Adapter.java create mode 100644 api/src/main/java/org/switchyard/adapter/AdapterRegistry.java create mode 100644 api/src/main/java/org/switchyard/adapter/BaseAdapter.java create mode 100644 api/src/main/java/org/switchyard/annotations/Adapter.java create mode 100644 api/src/main/java/org/switchyard/event/AdapterAddedEvent.java create mode 100644 api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java create mode 100644 api/src/main/java/org/switchyard/exception/DuplicateAdapterException.java create mode 100644 config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java create mode 100644 config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java create mode 100644 config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java create mode 100644 config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java create mode 100644 runtime/src/main/java/org/switchyard/internal/adapter/BaseAdapterRegistry.java diff --git a/adapter/pom.xml b/adapter/pom.xml new file mode 100644 index 000000000..7837617f0 --- /dev/null +++ b/adapter/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.switchyard + switchyard-core-parent + 1.0.0-SNAPSHOT + + switchyard-adapter + SwitchYard: Adapter + The SwitchYard Adapter library. + http://switchyard.org + + + org.switchyard + switchyard-config + + + org.switchyard + switchyard-api + + + + org.mockito + mockito-all + test + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java b/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java new file mode 100644 index 000000000..b358a1de9 --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java @@ -0,0 +1,148 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.switchyard.adapter.config.model.JavaAdaptModel; +import org.switchyard.common.type.Classes; +import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.exception.DuplicateAdapterException; +import org.switchyard.exception.SwitchYardException; + +/** + * Adapter registry loader implementation. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterRegistryLoader { + /** + * Logger. + */ + private static Logger _log = Logger.getLogger(AdapterRegistryLoader.class); + /** + * Adapters. + */ + private List _adapters = new LinkedList(); + /** + * The registry instance into which the adapters were loaded. + */ + private AdapterRegistry _adapterRegistry; + + /** + * Public constructor. + * + * @param adapterRegistry The registry instance. + */ + public AdapterRegistryLoader(AdapterRegistry adapterRegistry) { + if (adapterRegistry == null) { + throw new IllegalArgumentException("null 'adapterRegistry' argument."); + } + this._adapterRegistry = adapterRegistry; + } + + /** + * Register a set of adapters in the adapter registry associated with this deployment. + * + * @param adaptersModel The adapters model. + * @throws DuplicateAdapterException an existing adapter has already been registered for the from and to types + */ + public void registerAdapters(AdaptersModel adaptersModel) throws DuplicateAdapterException { + if (adaptersModel == null) { + return; + } + + try { + for (AdaptModel adaptModel : adaptersModel.getAdapts()) { + Collection adapters = newAdapters(adaptModel); + for (Adapter adapter : adapters) { + if (_adapterRegistry.hasAdapter(adapter.getFrom())) { + Adapter registeredAdapter = _adapterRegistry.getAdapter(adapter.getFrom()); + String msg = "Failed to register Adapter '" + toDescription(adapter) + + "'. An Adapter for these services is already registered: '" + + toDescription(registeredAdapter) + "'."; + throw new DuplicateAdapterException(msg); + } else { + _log.debug("Adding adapter =>" + + " From: " + adapter.getFrom() + + ", To:" + adapter.getTo()); + _adapterRegistry.addAdapter(adapter); + _adapters.add(adapter); + } + } + } + } catch (DuplicateAdapterException e) { + throw e; + } catch (RuntimeException e) { + // If there was an exception for any reason... remove all Adapter instance that have + // already been registered with the domain... + unregisterAdapters(); + throw e; + } + } + + /** + * Unregister all adapters. + */ + public void unregisterAdapters() { + for (Adapter adapter : _adapters) { + _adapterRegistry.removeAdapter(adapter); + } + } + + /** + * Create adapters based on the adapt Model. + * + * @param adaptModel The adapt Model. + * @return Adapters represented through the adapt Model. + */ + public Collection newAdapters(AdaptModel adaptModel) { + Collection adapters = null; + + if (adaptModel instanceof JavaAdaptModel) { + String className = ((JavaAdaptModel) adaptModel).getClazz(); + Class adapterClass = Classes.forName(className, AdapterUtil.class); + if (adapterClass == null) { + throw new SwitchYardException("Unable to load adapter class " + className); + } + adapters = AdapterUtil.newAdapter(adapterClass, adaptModel.getFrom(), adaptModel.getTo()); + + } else { + throw new SwitchYardException("Only JavaAdapterModel supported"); + } + + if (adapters == null || adapters.isEmpty()) { + throw new SwitchYardException("Unknown AModel type '" + adaptModel.getClass().getName() + "'."); + } + + return adapters; + } + + List getAdapters() { + return _adapters; + } + + private String toDescription(Adapter adapter) { + return adapter.getClass().getName() + "(" + adapter.getFrom() + ", " + adapter.getTo() + ")"; + } +} diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java b/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java new file mode 100644 index 000000000..b1a6c4eee --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java @@ -0,0 +1,68 @@ +/* + * 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.adapter; + +import javax.xml.namespace.QName; + +/** + * Adapter data types. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public abstract class AdapterTypes { + private QName _from; + private QName _to; + + /** + * Public constructor. + * + * @param from From type. + * @param to To type. + */ + AdapterTypes(QName from, QName to) { + this._from = from; + this._to = to; + } + + /** + * Get from. + * + * @return from. + */ + public QName getFrom() { + return _from; + } + + /** + * Get to. + * + * @return to. + */ + public QName getTo() { + return _to; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return String.format("%s [from=%s, to=%s]", getClass().getSimpleName(), getFrom(), getTo()); + } +} diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java b/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java new file mode 100644 index 000000000..6e09df1f7 --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java @@ -0,0 +1,184 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.switchyard.exception.SwitchYardException; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * Utility class to create adapters. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public final class AdapterUtil { + private AdapterUtil() {} + + /** + * Create adapters based on a generic class. + * + * @see #isAdapter(Class) + * @param clazz The class representing the adapters. + * @param from The consumer side service name. + * @param to The provider side service name. + * @return The adapters created based on the generic class. + */ + public static List newAdapter(Class clazz, QName from, QName to) { + if (!isAdapter(clazz)) { + throw new SwitchYardException("Invalid Adapter class '" + clazz.getName() + "'. Must implement the Adapter interface, or have methods annotated with the @Adapter annotation."); + } + List adapters = new ArrayList(); + Object adapterObject = createAdapterObject(clazz); + Collection annotatedAdapters = tryCreateAnnotatedAdapters(clazz, from, to, adapterObject); + adapters.addAll(annotatedAdapters); + + if (adapterObject instanceof Adapter) { + Collection adapterImplementations = createAdaptersImplementingAdapter(clazz, adapterObject, from, to); + adapters.addAll(adapterImplementations); + } + return adapters; + } + + /** + * Test if the class represents adapters. + * + * @param clazz The class to test. + * @return True if the class represents adapters, otherwise false. + */ + public static boolean isAdapter(Class clazz) { + if (clazz.isInterface()) { + return false; + } + if (clazz.isAnnotation()) { + return false; + } + if (Modifier.isAbstract(clazz.getModifiers())) { + return false; + } + try { + // Must have a default constructor... + clazz.getConstructor(); + } catch (NoSuchMethodException e) { + return false; + } + if (org.switchyard.adapter.Adapter.class.isAssignableFrom(clazz)) { + return true; + } + + Method[] publicMethods = clazz.getMethods(); + for (Method publicMethod : publicMethods) { + if (publicMethod.isAnnotationPresent(org.switchyard.annotations.Adapter.class)) { + return true; + } + } + + return false; + } + + private static Collection tryCreateAnnotatedAdapters(Class clazz, QName from, QName to, Object adapterObject) { + Collection adapters = new ArrayList(); + for (Method publicMethod : clazz.getMethods()) { + org.switchyard.annotations.Adapter adapterAnno = publicMethod.getAnnotation(org.switchyard.annotations.Adapter.class); + if (adapterAnno != null) { + AdapterMethod adapterMethod = toAdapterMethod(publicMethod, adapterAnno); + if (adapterMethod.getFrom().equals(from) && adapterMethod.getTo().equals(to)) { + adapters.add(newAdapter(adapterObject, adapterMethod.getMethod(), adapterMethod.getFrom(), adapterMethod.getTo())); + } + } + } + return adapters; + } + + private static Collection createAdaptersImplementingAdapter(Class clazz, Object adapterObject, QName from, QName to) { + Collection adapters = new ArrayList(); + for (Method publicMethod : clazz.getMethods()) { + Class returnType = publicMethod.getReturnType(); + Class[] parameterTypes = publicMethod.getParameterTypes(); + + if (ServiceOperation.class.equals(returnType) && parameterTypes.length == 2 && String.class.equals(parameterTypes[0]) && ServiceInterface.class.equals(parameterTypes[1])) { + Adapter adapter = (Adapter) adapterObject; + AdapterMethod adapterMethod = toAdapterMethod(publicMethod, adapter); + if (adapterMethod.getFrom().equals(from) && adapterMethod.getTo().equals(to)) { + adapters.add(newAdapter(adapterObject, adapterMethod.getMethod(), adapterMethod.getFrom(), adapterMethod.getTo())); + } + } + } + return adapters; + } + + private static Adapter newAdapter(final Object adapterObject, final Method publicMethod, QName from, QName to) { + Adapter adapter = new BaseAdapter(from, to) { + @Override + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + Object[] parameters = Arrays.asList(consumerOperation, targetInterface).toArray(); + + try { + return (ServiceOperation) publicMethod.invoke(adapterObject, parameters); + } catch (InvocationTargetException e) { + throw new SwitchYardException("Error executing @Adapter method '" + publicMethod.getName() + "' on class '" + publicMethod.getDeclaringClass().getName() + "'.", e.getCause()); + } catch (Exception e) { + throw new SwitchYardException("Error executing @Adapter method '" + publicMethod.getName() + "' on class '" + publicMethod.getDeclaringClass().getName() + "'.", e); + } + } + }; + return adapter; + } + + private static AdapterMethod toAdapterMethod(Method publicMethod, org.switchyard.annotations.Adapter adapterAnno) { + QName from = QName.valueOf(adapterAnno.from().trim()); + QName to = QName.valueOf(adapterAnno.to().trim()); + + return new AdapterMethod(from, to, publicMethod); + } + + private static AdapterMethod toAdapterMethod(Method publicMethod, Adapter adapter) { + return new AdapterMethod(adapter.getFrom(), adapter.getTo(), publicMethod); + } + + private static Object createAdapterObject(Class clazz) { + try { + return clazz.newInstance(); + } catch (Exception e) { + throw new SwitchYardException("Error constructing Adapter instance for class '" + clazz.getName() + "'. Class must have a public default constructor.", e); + } + } + + private static class AdapterMethod extends AdapterTypes { + private Method _publicMethod; + + AdapterMethod(QName from, QName to, Method publicMethod) { + super(from, to); + this._publicMethod = publicMethod; + } + + public Method getMethod() { + return _publicMethod; + } + } +} diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java b/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java new file mode 100644 index 000000000..a45893ab6 --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java @@ -0,0 +1,182 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import javax.xml.namespace.QName; + +import org.switchyard.adapter.Adapter; +import org.switchyard.adapter.AdapterUtil; +import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; +import org.switchyard.common.type.classpath.AbstractTypeFilter; +import org.switchyard.common.type.classpath.ClasspathScanner; +import org.switchyard.config.model.Scannable; +import org.switchyard.config.model.Scanner; +import org.switchyard.config.model.ScannerInput; +import org.switchyard.config.model.ScannerOutput; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.adapter.v1.V1AdaptersModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; +import org.switchyard.config.model.switchyard.v1.V1SwitchYardModel; +import org.switchyard.exception.DuplicateAdapterException; +import org.switchyard.exception.SwitchYardException; + +/** + * Scanner for {@link org.switchyard.adapter.Adapt} implementations. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterSwitchYardScanner implements Scanner { + + @Override + public ScannerOutput scan(ScannerInput input) throws IOException { + SwitchYardModel switchyardModel = new V1SwitchYardModel(); + AdaptersModel adapters = new V1AdaptersModel(); + + List> adaptClasses = scanForAdapts(input.getURLs()); + for (Class adaptClass : adaptClasses) { + Collection adapterTypes = listAdaptTypes(adaptClass); + + for (AdapterTypes adapterType : adapterTypes) { + JavaAdaptModel adaptModel = new V1JavaAdaptModel(); + adaptModel.setClazz(adaptClass.getName()); + adaptModel.setFrom(adapterType.getFrom()); + adaptModel.setTo(adapterType.getTo()); + + adapters.addAdapt(adaptModel); + } + } + switchyardModel.setAdapters(adapters); + + return new ScannerOutput().setModel(switchyardModel); + } + + private Collection listAdaptTypes(Class adaptClass) { + Map map = new TreeMap(new QNameComparable()); + + AdapterTypes adapterType = adapterTypesFromAdapterInstance(adaptClass); + addToMap(map, adapterType); + + List adapterTypes = adapterTypesFromAdapterAnnotation(adaptClass); + addToMap(map, adapterTypes); + + return map.values(); + } + + private void addToMap(Map map, List adapterTypes) { + for (AdapterTypes adapter : adapterTypes) { + addToMap(map, adapter); + } + } + + private void addToMap(Map map, AdapterTypes adapterType) { + if (adapterType != null) { + if (!map.containsKey(adapterType.getFrom())) { + map.put(adapterType.getFrom(), adapterType); + } else { + String msg = "An Adapter for the service is already registered: '" + adapterType.getFrom() + "'."; + throw new DuplicateAdapterException(msg); + } + } + } + + List adapterTypesFromAdapterAnnotation(Class adaptClass) { + List adapterTypes = new ArrayList(); + + Method[] publicMethods = adaptClass.getMethods(); + for (Method method : publicMethods) { + org.switchyard.annotations.Adapter adapter = method.getAnnotation(org.switchyard.annotations.Adapter.class); + if (adapter != null && adapter.from() != null && adapter.to() != null) { + adapterTypes.add(new AdapterTypes(QName.valueOf(adapter.from()), QName.valueOf(adapter.to()))); + } + } + return adapterTypes; + } + + AdapterTypes adapterTypesFromAdapterInstance(Class adaptClass) { + if (Adapter.class.isAssignableFrom(adaptClass)) { + try { + Adapter adapter = (Adapter) adaptClass.newInstance(); + if (adapter.getFrom() != null && adapter.getTo() != null) { + return new AdapterTypes(adapter.getFrom(), adapter.getTo()); + } + } catch (Exception e) { + throw new SwitchYardException("Error constructing Adapter instance for class '" + adaptClass.getName() + "'. Class must have a public default constructor.", e); + } + } + return null; + } + + private List> scanForAdapts(List urls) throws IOException { + AbstractTypeFilter filter = new AdaptInstanceOfFilter(); + ClasspathScanner scanner = new ClasspathScanner(filter); + + for (URL url : urls) { + scanner.scan(url); + } + + return filter.getMatchedTypes(); + } + + private class AdapterTypes { + private QName _from; + private QName _to; + + public AdapterTypes(QName from, QName to) { + this._from = from; + this._to = to; + } + + public QName getFrom() { + return _from; + } + + public QName getTo() { + return _to; + } + } + + private final class AdaptInstanceOfFilter extends AbstractTypeFilter { + @Override + public boolean matches(Class clazz) { + Scannable scannable = clazz.getAnnotation(Scannable.class); + if (scannable != null && !scannable.value()) { + // Marked as being non-scannable... + return false; + } + return AdapterUtil.isAdapter(clazz); + } + } + + private final class QNameComparable implements Comparator { + @Override + public int compare(QName arg0, QName arg1) { + return arg0.toString().compareTo(arg1.toString()); + } + } +} diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java b/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java new file mode 100644 index 000000000..b8370f909 --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model; + +import org.switchyard.config.model.adapter.AdaptModel; + +/** + * A "adapt.java" configuration model. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface JavaAdaptModel extends AdaptModel { + + /** The "java" name. */ + public static final String JAVA = "java"; + + /** The "class" name. */ + public static final String CLASS = "class"; + + /** + * Gets the class attribute. + * + * @return the class attribute + */ + public String getClazz(); + + /** + * Sets the class attribute. + * + * @param clazz the class attribute + * @return this JavaAdaptModel (useful for chaining) + */ + public JavaAdaptModel setClazz(String clazz); +} diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java new file mode 100644 index 000000000..f964563a9 --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java @@ -0,0 +1,59 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model.v1; + +import org.switchyard.adapter.config.model.JavaAdaptModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseMarshaller; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.Model; +import org.switchyard.config.model.adapter.AdaptModel; + +/** + * Marshalls adapter Models. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class V1AdapterMarshaller extends BaseMarshaller { + + private static final String ADAPT_JAVA = AdaptModel.ADAPT + "." + JavaAdaptModel.JAVA; + + /** + * Constructs a new V1AdapterMarshaller with the specified Descriptor. + * + * @param desc the Descriptor + */ + protected V1AdapterMarshaller(Descriptor desc) { + super(desc); + } + + /** + * {@inheritDoc} + */ + @Override + public Model read(Configuration config) { + String name = config.getName(); + Descriptor desc = getDescriptor(); + + if (name.equals(ADAPT_JAVA)) { + return new V1JavaAdaptModel(config, desc); + } + return null; + } +} diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java new file mode 100644 index 000000000..84bdaadbe --- /dev/null +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java @@ -0,0 +1,69 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model.v1; + +import javax.xml.namespace.QName; + +import org.switchyard.adapter.config.model.JavaAdaptModel; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.adapter.v1.V1BaseAdaptModel; + +/** + * A version 1 JavaAdaptModel. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class V1JavaAdaptModel extends V1BaseAdaptModel implements JavaAdaptModel { + + /** + * Constructs a new V1JavaAdaptModel. + */ + public V1JavaAdaptModel() { + super(new QName(AdaptModel.DEFAULT_NAMESPACE, AdaptModel.ADAPT + '.' + JAVA)); + } + + /** + * Constructs a new V1JavaAdaptModel with the specified Configuration and Descriptor. + * + * @param config the Configuration + * @param desc the Descriptor + */ + public V1JavaAdaptModel(Configuration config, Descriptor desc) { + super(config, desc); + } + + /** + * {@inheritDoc} + */ + @Override + public String getClazz() { + return getModelAttribute(CLASS); + } + + /** + * {@inheritDoc} + */ + @Override + public JavaAdaptModel setClazz(String clazz) { + setModelAttribute(CLASS, clazz); + return this; + } +} diff --git a/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd b/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd new file mode 100644 index 000000000..91b213b17 --- /dev/null +++ b/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd @@ -0,0 +1,49 @@ + + + + + + + + + + + Generic/Custom Java Adapter Configuration. + + + + + + + + The name of the Java Adapter implementation class. + + + + + + + + + diff --git a/adapter/src/main/resources/org/switchyard/config/model/descriptor.properties b/adapter/src/main/resources/org/switchyard/config/model/descriptor.properties new file mode 100644 index 000000000..b45a868a5 --- /dev/null +++ b/adapter/src/main/resources/org/switchyard/config/model/descriptor.properties @@ -0,0 +1,21 @@ +# 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. + +adpt1.namespace=urn\:switchyard-config\:adapter\:1.0 +adpt1.schema=adapter-v1.xsd +adpt1.location=/org/switchyard/adapter/config/model/v1/ +adpt1.marshaller=org.switchyard.adapter.config.model.v1.V1AdapterMarshaller diff --git a/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java b/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java new file mode 100644 index 000000000..383b8587c --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java @@ -0,0 +1,106 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import java.util.Collection; + +import javax.xml.namespace.QName; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.switchyard.adapter.config.model.adapters.V1toV2Adapter; +import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.adapter.v1.V1AdaptersModel; +import org.switchyard.exception.DuplicateAdapterException; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterRegistryLoaderTest { + private AdapterRegistryLoader loader; + private V1JavaAdaptModel adaptModel; + private AdapterRegistry registy; + + @Before + public void setup() { + registy = Mockito.mock(AdapterRegistry.class); + loader = new AdapterRegistryLoader(registy); + + adaptModel = new V1JavaAdaptModel(); + adaptModel.setFrom(V1toV2Adapter.FROM); + adaptModel.setTo(V1toV2Adapter.TO); + adaptModel.setClazz(V1toV2Adapter.class.getName()); + } + + @Test + public void testNewAdapters() { + Collection adapters = loader.newAdapters(adaptModel); + Assert.assertEquals(1, adapters.size()); + } + + @Test + public void testRegisterAdapters() { + AdaptersModel adaptersModel = new V1AdaptersModel(); + adaptersModel.addAdapt(adaptModel); + + loader.registerAdapters(adaptersModel); + Assert.assertEquals(1, loader.getAdapters().size()); + } + + @Test + public void testRegisterAdaptersDuplicateFrom() { + AdaptersModel adaptersModel = new V1AdaptersModel(); + adaptersModel.addAdapt(adaptModel); + + loader.registerAdapters(adaptersModel); + + Mockito.when(registy.hasAdapter(adaptModel.getFrom())).thenReturn(true); + Mockito.when(registy.getAdapter(Mockito.any(QName.class))).thenReturn( + new TestAdapter(adaptModel.getFrom(), adaptModel.getTo())); + + try { + loader.registerAdapters(adaptersModel); + Assert.fail(); + } catch (DuplicateAdapterException e) { + Assert.assertEquals( + "Failed to register Adapter " + + "'org.switchyard.adapter.AdapterUtil$1({urn:org.switchyard.adapter.config.model}TestServiceV1, {urn:org.switchyard.adapter.config.model}TestServiceV2)'. " + + "An Adapter for these services is already registered: " + + "'org.switchyard.adapter.AdapterRegistryLoaderTest$TestAdapter({urn:org.switchyard.adapter.config.model}TestServiceV1, {urn:org.switchyard.adapter.config.model}TestServiceV2)'.", + e.getMessage()); + } + } + + public static class TestAdapter extends BaseAdapter { + public TestAdapter(QName from, QName to) { + super(from, to); + } + + @Override + public ServiceOperation lookup(String consumerOperation, + ServiceInterface targetInterface) { + return null; + } + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java b/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java new file mode 100644 index 000000000..1a7a1afba --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java @@ -0,0 +1,99 @@ +/* + * 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.adapter; + +import java.util.List; + +import javax.xml.namespace.QName; + +import org.junit.Assert; +import org.junit.Test; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterUtilTest { + private static final QName TEST_SERVICE_V1 = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV1"); + private static final QName TEST_SERVICE_V2 = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV2"); + + @Test + public void testIsAdapter() { + Assert.assertTrue(AdapterUtil.isAdapter(TestAdapter.class)); + Assert.assertTrue(AdapterUtil.isAdapter(AnnoTestAdapter.class)); + + Assert.assertFalse(AdapterUtil.isAdapter(Adapter.class)); + Assert.assertFalse(AdapterUtil.isAdapter(org.switchyard.annotations.Adapter.class)); + Assert.assertFalse(AdapterUtil.isAdapter(AbstractTestAdapter.class)); + Assert.assertFalse(AdapterUtil.isAdapter(Object.class)); + Assert.assertFalse(AdapterUtil.isAdapter(ConstructerTestAdapter.class)); + } + + @Test + public void testNewAdapter() { + List adapters = AdapterUtil.newAdapter(TestAdapter.class, TEST_SERVICE_V1, TEST_SERVICE_V2); + Assert.assertEquals(1, adapters.size()); + Adapter adapter = adapters.get(0); + Assert.assertEquals(TEST_SERVICE_V1, adapter.getFrom()); + Assert.assertEquals(TEST_SERVICE_V2, adapter.getTo()); + + adapters = AdapterUtil.newAdapter(AnnoTestAdapter.class, TEST_SERVICE_V1, TEST_SERVICE_V2); + Assert.assertEquals(1, adapters.size()); + adapter = adapters.get(0); + Assert.assertEquals(TEST_SERVICE_V1, adapter.getFrom()); + Assert.assertEquals(TEST_SERVICE_V2, adapter.getTo()); + } + + public static class TestAdapter extends BaseAdapter { + public TestAdapter() { + super(TEST_SERVICE_V1, TEST_SERVICE_V2); + } + + @Override + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return null; + } + } + + public static class AnnoTestAdapter { + + @org.switchyard.annotations.Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return null; + } + } + + public abstract static class AbstractTestAdapter extends BaseAdapter { + public AbstractTestAdapter() { + super(null, null); + } + } + + public static class ConstructerTestAdapter extends BaseAdapter { + public ConstructerTestAdapter(QName from, QName to) { + super(from, to); + } + + @Override + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return null; + } + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java new file mode 100644 index 000000000..aecbab717 --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java @@ -0,0 +1,124 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model; + +import java.io.StringReader; + +import javax.xml.namespace.QName; + +import junit.framework.Assert; + +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.junit.Test; +import org.switchyard.adapter.config.model.adapters.V1toV2Adapter; +import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; +import org.switchyard.common.io.pull.StringPuller; +import org.switchyard.common.xml.XMLHelper; +import org.switchyard.config.model.Model; +import org.switchyard.config.model.ModelPuller; +import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.adapter.v1.V1AdaptersModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; +import org.switchyard.config.model.switchyard.v1.V1SwitchYardModel; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterModelTests { + + private static final String XML = "/org/switchyard/adapter/config/model/AdapterModelTests.xml"; + + private ModelPuller _puller; + + @Before + public void before() throws Exception { + _puller = new ModelPuller(); + } + + @Test + public void testCreateEmptyModel() throws Exception { + String namespace = AdaptModel.DEFAULT_NAMESPACE; + String name = AdaptModel.ADAPT + '.' + JavaAdaptModel.JAVA; + Model model = new ModelPuller().pull(XMLHelper.createQName(namespace, name)); + Assert.assertTrue(model instanceof JavaAdaptModel); + Assert.assertEquals(name, model.getModelConfiguration().getName()); + Assert.assertEquals(new QName(namespace, name), model.getModelConfiguration().getQName()); + } + + @Test + public void testCreate() throws Exception { + SwitchYardModel switchyard = new V1SwitchYardModel(); + AdaptersModel adapters = new V1AdaptersModel(); + JavaAdaptModel javaAdapters = new V1JavaAdaptModel(); + javaAdapters.setFrom(new QName("{urn:org.switchyard.adapter.config.model}TestServiceV1")); + javaAdapters.setTo(new QName("{urn:org.switchyard.adapter.config.model}TestServiceV2")); + javaAdapters.setClazz("org.switchyard.adapter.config.model.adapters.V1toV2Adapter"); + adapters.addAdapt(javaAdapters); + + switchyard.setAdapters(adapters); + String new_xml = switchyard.toString(); + String old_xml = new ModelPuller().pull(XML, getClass()).toString(); + + XMLUnit.setIgnoreWhitespace(true); + Diff diff = XMLUnit.compareXML(old_xml, new_xml); + Assert.assertTrue(diff.toString(), diff.identical()); + } + + @Test + public void testRead() throws Exception { + SwitchYardModel switchyard = _puller.pull(XML, getClass()); + AdaptersModel adapters = switchyard.getAdapters(); + + JavaAdaptModel java_adapt = (JavaAdaptModel)adapters.getAdapts().get(0); + Assert.assertEquals("TestServiceV1", java_adapt.getFrom().getLocalPart()); + Assert.assertEquals("TestServiceV2", java_adapt.getTo().getLocalPart()); + Assert.assertEquals(V1toV2Adapter.class.getName(), java_adapt.getClazz()); + } + + @Test + public void testWrite() throws Exception { + String old_xml = new StringPuller().pull(XML, getClass()); + SwitchYardModel switchyard = _puller.pull(new StringReader(old_xml)); + String new_xml = switchyard.toString(); + XMLUnit.setIgnoreWhitespace(true); + Diff diff = XMLUnit.compareXML(old_xml, new_xml); + Assert.assertTrue(diff.toString(), diff.identical()); + } + + @Test + public void testParenthood() throws Exception { + SwitchYardModel switchyard_1 = _puller.pull(XML, getClass()); + AdaptersModel transforms_1 = switchyard_1.getAdapters(); + AdaptModel transform = transforms_1.getAdapts().get(0); + AdaptersModel transforms_2 = transform.getAdapters(); + + SwitchYardModel switchyard_2 = transforms_2.getSwitchYard(); + Assert.assertEquals(transforms_1, transforms_2); + Assert.assertEquals(switchyard_1, switchyard_2); + } + + @Test + public void testValidation() throws Exception { + SwitchYardModel switchyard = _puller.pull(XML, getClass()); + switchyard.assertModelValid(); + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java new file mode 100644 index 000000000..9ab7e04a2 --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java @@ -0,0 +1,71 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.switchyard.adapter.config.model.adapters.AnnoV1toV2Adapter; +import org.switchyard.config.model.ScannerInput; +import org.switchyard.config.model.switchyard.SwitchYardModel; +import org.switchyard.exception.DuplicateAdapterException; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterSwitchYardScannerTest { + + private AdapterSwitchYardScanner scanner; + private ArrayList testUrls; + + @Before + public void setUp() throws MalformedURLException { + this.scanner = new AdapterSwitchYardScanner(); + + testUrls = new ArrayList(); + // If running this test inside your IDE... you need to set the cwd to be the + // root of the transform module !! + testUrls.add(new File("./target/test-classes").toURI().toURL()); + } + + @Test + public void testAnnotatedAdapter() { + List adapterTypes = scanner.adapterTypesFromAdapterAnnotation(AnnoV1toV2Adapter.class); + + Assert.assertEquals(2, adapterTypes.size()); + } + + @Test + public void testScanDuplicateAdapters() throws IOException { + ScannerInput input = new ScannerInput().setURLs(testUrls); + try { + scanner.scan(input).getModel(); + Assert.fail(); + } catch (DuplicateAdapterException e) { + Assert.assertEquals("An Adapter for the service is already registered: '{urn:org.switchyard.adapter.config.model}TestServiceV1'.", e.getMessage()); + } + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java new file mode 100644 index 000000000..082436895 --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java @@ -0,0 +1,47 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model.adapters; + +import org.switchyard.ExchangePattern; +import org.switchyard.annotations.Adapter; +import org.switchyard.metadata.BaseServiceOperation; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; +import org.switchyard.metadata.java.JavaService; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AnnoV1toV2Adapter { + + @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); + } + + // Duplicate from not allowed -> AdapterRegistryLoaderTest#testRegisterAdaptersDuplicateFrom + @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") + public ServiceOperation lookup2(String consumerOperation, ServiceInterface targetInterface) { + return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); + } + + public ServiceOperation dummyLookup(String consumerOperation, ServiceInterface targetInterface) { + return null; + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java new file mode 100644 index 000000000..f1be380ae --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java @@ -0,0 +1,42 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter.config.model.adapters; + +import javax.xml.namespace.QName; + +import org.switchyard.adapter.BaseAdapter; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class V1toV2Adapter extends BaseAdapter { + public static final QName FROM = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV1"); + public static final QName TO = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV2"); + + public V1toV2Adapter() { + super(FROM, TO); + } + + @Override + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return null; + } +} diff --git a/adapter/src/test/resources/META-INF/beans.xml b/adapter/src/test/resources/META-INF/beans.xml new file mode 100644 index 000000000..e69de29bb diff --git a/adapter/src/test/resources/META-INF/switchyard.xml b/adapter/src/test/resources/META-INF/switchyard.xml new file mode 100644 index 000000000..ed4e09115 --- /dev/null +++ b/adapter/src/test/resources/META-INF/switchyard.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml b/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml new file mode 100644 index 000000000..f74573ab2 --- /dev/null +++ b/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml @@ -0,0 +1,28 @@ + + + + + + + diff --git a/addressing/pom.xml b/addressing/pom.xml new file mode 100644 index 000000000..cf1db0527 --- /dev/null +++ b/addressing/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + org.switchyard + switchyard-core-parent + 1.0.0-SNAPSHOT + + switchyard-addressing + SwitchYard: Addressing + The SwitchYard Addressing library. + http://switchyard.org + + + org.switchyard + switchyard-api + + + org.switchyard + switchyard-adapter + ${project.version} + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + jboss-public-repository + JBoss Public Maven Repository + http://repository.jboss.org/nexus/content/groups/public + + + diff --git a/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java new file mode 100644 index 000000000..ccc94678c --- /dev/null +++ b/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java @@ -0,0 +1,58 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.addressing; + +import javax.xml.namespace.QName; + +import org.switchyard.HandlerException; +import org.switchyard.Service; +import org.switchyard.ServiceDomain; +import org.switchyard.ServiceReference; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * Resolves the provider service and operation. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface ServiceResolver { + + /** + * Resolves the service provider. + * + * @param domain Service Domain + * @param consumerReference Consumer's service reference. + * @return Provider's service reference. + * @throws HandlerException If no unique service is found. + */ + Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) throws HandlerException; + + /** + * Resolve the provider's service operation. + * + * @param fromServiceName The service's name requested by the consumer. + * @param toServiceName The service's name resolved by getProviderService. + * @param consumerOperation The consumer's service operation. + * @param serviceInterface The consumer's service interface. + * @return The provider's service interface. + * @throws HandlerException If no service operation could be resolved. + */ + ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException; +} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java new file mode 100644 index 000000000..b65cfcf76 --- /dev/null +++ b/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java @@ -0,0 +1,79 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.addressing.resolvers; + +import java.util.List; + +import javax.xml.namespace.QName; + +import org.switchyard.HandlerException; +import org.switchyard.Service; +import org.switchyard.ServiceDomain; +import org.switchyard.ServiceReference; +import org.switchyard.adapter.Adapter; +import org.switchyard.adapter.AdapterRegistry; +import org.switchyard.addressing.ServiceResolver; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * Service and operation selection based on user defined adapters. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterServiceResolver implements ServiceResolver { + + private AdapterRegistry _adapterRegistry; + + /** + * Public constructor. + * + * @param adapterRegistry The registry instance. + */ + public AdapterServiceResolver(AdapterRegistry adapterRegistry) { + _adapterRegistry = adapterRegistry; + } + + /** + * {@inheritDoc} + */ + @Override + public ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException { + Adapter adapter = _adapterRegistry.getAdapter(fromServiceName); + if (adapter.getTo().equals(toServiceName)) { + return adapter.lookup(consumerOperation.getName(), serviceInterface); + } + throw new HandlerException(String.format("No adapter available ('%s' -> '%s')", fromServiceName, toServiceName)); + } + + /** + * {@inheritDoc} + */ + @Override + public Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) throws HandlerException { + QName from = consumerReference.getName(); + Adapter adapter = _adapterRegistry.getAdapter(from); + + List services = domain.getServices(adapter.getTo()); + if (services.size() >= 1) { + return services.get(0); + } + throw new HandlerException("Target service not found: " + adapter.getTo()); + } +} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java new file mode 100644 index 000000000..b80fa76c8 --- /dev/null +++ b/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.addressing.resolvers; + +import java.util.List; + +import org.switchyard.Service; +import org.switchyard.ServiceDomain; +import org.switchyard.ServiceReference; +import org.switchyard.addressing.ServiceResolver; +import org.switchyard.exception.SwitchYardException; + +/** + * Basic service resolver implementation. + * Provides the implementation for getProviderService + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public abstract class BaseServiceResolver implements ServiceResolver { + + /** + * {@inheritDoc} + */ + @Override + public Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) { + List services = domain.getServices(consumerReference.getTargetServiceName()); + if (services == null || services.isEmpty()) { + throw new SwitchYardException("No registered service found for " + consumerReference.getName()); + } + // At this stage, just pick the first service implementation we find and go with + // it. In the future, it would be nice if we could make this pluggable. + return services.get(0); + } +} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java new file mode 100644 index 000000000..329c0c26c --- /dev/null +++ b/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java @@ -0,0 +1,55 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.addressing.resolvers; + +import javax.xml.namespace.QName; + +import org.switchyard.HandlerException; +import org.switchyard.addressing.ServiceResolver; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * Default implementation for the service resolver interface. + * + * @see ServiceResolver + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class DefaultServiceResolver extends BaseServiceResolver implements ServiceResolver { + + /** + * {@inheritDoc} + */ + @Override + public ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException { + ServiceOperation providerOp = serviceInterface.getOperation(consumerOperation.getName()); + + if (providerOp == null) { + // try for a default operation + if (serviceInterface.getOperations().size() == 1) { + providerOp = serviceInterface.getOperations().iterator().next(); + } else { + throw new HandlerException("Operation " + consumerOperation.getName() + + " is not included in interface for service: " + toServiceName); + } + } + return providerOp; + } + +} diff --git a/api/src/main/java/org/switchyard/ServiceDomain.java b/api/src/main/java/org/switchyard/ServiceDomain.java index 35c49d215..44ffe489f 100644 --- a/api/src/main/java/org/switchyard/ServiceDomain.java +++ b/api/src/main/java/org/switchyard/ServiceDomain.java @@ -25,6 +25,7 @@ import javax.xml.namespace.QName; +import org.switchyard.adapter.AdapterRegistry; import org.switchyard.event.EventObserver; import org.switchyard.event.EventPublisher; import org.switchyard.metadata.Registrant; @@ -143,6 +144,12 @@ ServiceReference registerServiceReference(QName serviceName, */ public ServiceSecurity getServiceSecurity(); + /** + * Returns a reference to the adapter registry for this domain. + * @return adapter registry instance + */ + AdapterRegistry getAdapterRegistry(); + /** * Returns a reference to the transformer registry for this domain. * @return transformer registry instance diff --git a/api/src/main/java/org/switchyard/adapter/Adapter.java b/api/src/main/java/org/switchyard/adapter/Adapter.java new file mode 100644 index 000000000..aad622013 --- /dev/null +++ b/api/src/main/java/org/switchyard/adapter/Adapter.java @@ -0,0 +1,67 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import javax.xml.namespace.QName; + +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface Adapter { + + /** + * Set the name of the from, or source, message type. + * + * @param fromType From type. + * @return a reference to the current Adapter. + */ + Adapter setFrom(QName fromType); + + /** + * Set the name of the to, or source, message type. + * + * @param toType To type. + * @return a reference to the current Adapter. + */ + Adapter setTo(QName toType); + + /** + * The name of the from, or source, message. + * @return from message + */ + QName getFrom(); + + /** + * The name of the to, or target, message. + * @return to message + */ + QName getTo(); + + /** + * Adapter operation. + * + * @param consumerOperation The invoked consumers service operation. + * @param targetInterface The re-targeted service interface. + * @return The {@link ServiceOperation} to invoke. + */ + ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface); +} diff --git a/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java b/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java new file mode 100644 index 000000000..e95ce2af7 --- /dev/null +++ b/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java @@ -0,0 +1,72 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import javax.xml.namespace.QName; + +/** + * Adapter registry. + * An adapter is identified by the consumer's service name (from). + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface AdapterRegistry { + + /** + * Add an adapter. + * + * @param adapter adapter + * @return {@code this} AdapterRegistry instance. + */ + AdapterRegistry addAdapter(Adapter adapter); + + /** + * Add an adapter. + * + * @param adapter adapter + * @param from from + * @param to to + * @return {@code this} AdapterRegistry instance. + */ + AdapterRegistry addAdapter(Adapter adapter, QName from, QName to); + + /** + * Remove an adapter. + * + * @param adapter adapter + * @return status of removal + */ + boolean removeAdapter(Adapter adapter); + + /** + * Does the registry have an adapter for the specified type. + * + * @param from from + * @return True if it has an adapter, otherwise false. + */ + boolean hasAdapter(QName from); + + /** + * Get an adapter. + * + * @param from from + * @return adapter + */ + Adapter getAdapter(QName from); +} diff --git a/api/src/main/java/org/switchyard/adapter/BaseAdapter.java b/api/src/main/java/org/switchyard/adapter/BaseAdapter.java new file mode 100644 index 000000000..c70a73937 --- /dev/null +++ b/api/src/main/java/org/switchyard/adapter/BaseAdapter.java @@ -0,0 +1,67 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.adapter; + +import javax.xml.namespace.QName; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public abstract class BaseAdapter implements Adapter { + private QName _from; + private QName _to; + + /** + * Constructor. + * @param from from + * @param to to + */ + public BaseAdapter(QName from, QName to) { + super(); + this._from = from; + this._to = to; + } + + @Override + public Adapter setFrom(QName fromType) { + _from = fromType; + return this; + } + + @Override + public Adapter setTo(QName toType) { + _to = toType; + return this; + } + + @Override + public QName getFrom() { + return _from; + } + + @Override + public QName getTo() { + return _to; + } + + @Override + public String toString() { + return getClass().getName() + ": from=\"" + getFrom() + "\", to=\"" + getTo() + "\""; + } +} diff --git a/api/src/main/java/org/switchyard/annotations/Adapter.java b/api/src/main/java/org/switchyard/annotations/Adapter.java new file mode 100644 index 000000000..ba958d7d9 --- /dev/null +++ b/api/src/main/java/org/switchyard/annotations/Adapter.java @@ -0,0 +1,45 @@ +/* + * 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.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +@Target({METHOD }) +@Retention(RUNTIME) +@Documented +public @interface Adapter { + + /** + * Adapt from service (QName). + */ + String from(); + + /** + * Adapt to service (QName). + */ + String to(); +} diff --git a/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java b/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java new file mode 100644 index 000000000..2747fec34 --- /dev/null +++ b/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java @@ -0,0 +1,48 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.event; + +import java.util.EventObject; + +import org.switchyard.adapter.Adapter; + +/** + * Fired when an adapter is added to the domain. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class AdapterAddedEvent extends EventObject { + private static final long serialVersionUID = -6264437639353632902L; + + /** + * Creates a new AdapterAddedEvent. + * @param adapter the transformer that was added + */ + public AdapterAddedEvent(Adapter adapter) { + super(adapter); + } + + /** + * Get the added adapter. + * @return added adapter + */ + public Adapter getTransformer() { + return (Adapter) getSource(); + } +} diff --git a/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java b/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java new file mode 100644 index 000000000..92176d104 --- /dev/null +++ b/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java @@ -0,0 +1,47 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.event; + +import java.util.EventObject; + +import org.switchyard.adapter.Adapter; + +/** + * Fired when an adapter is removed from the domain. + */ +public class AdapterRemovedEvent extends EventObject { + + private static final long serialVersionUID = -5796235997474241451L; + + /** + * Creates a new AdapterRemovedEvent. + * @param adapter the adapter that was removed + */ + public AdapterRemovedEvent(Adapter adapter) { + super(adapter); + } + + /** + * Get the removed adapter. + * @return removed adapter + */ + public Adapter getTransformer() { + return (Adapter) getSource(); + } +} diff --git a/api/src/main/java/org/switchyard/exception/DuplicateAdapterException.java b/api/src/main/java/org/switchyard/exception/DuplicateAdapterException.java new file mode 100644 index 000000000..dbbc202e7 --- /dev/null +++ b/api/src/main/java/org/switchyard/exception/DuplicateAdapterException.java @@ -0,0 +1,49 @@ +/* + * 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.exception; + +/** + * A DuplicateAdapterException is thrown by SwitchYard when a duplicate adapter + * is trying to be registered for a 'from' server for which there already exists + * an adapter in the adapter registry. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class DuplicateAdapterException extends SwitchYardException { + private static final long serialVersionUID = 3368823467745672974L; + + /** + * Public constructor. + * + * @param message Exception message. + */ + public DuplicateAdapterException(final String message) { + super(message); + } + + /** + * Public constructor. + * + * @param message Exception message. + * @param cause Throwable cause. + */ + public DuplicateAdapterException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java b/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java new file mode 100644 index 000000000..c6b0ba3c8 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java @@ -0,0 +1,79 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.config.model.adapter; + +import javax.xml.namespace.QName; + +import org.switchyard.config.model.Model; + +/** + * Single adapter model. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface AdaptModel extends Model { + /** The default "adapt" namespace. */ + public static final String DEFAULT_NAMESPACE = "urn:switchyard-config:adapter:1.0"; + + /** The "adapt" name. */ + public static final String ADAPT = "adapt"; + + /** The "from" name. */ + public static final String FROM = "from"; + + /** The "to" name. */ + public static final String TO = "to"; + + /** + * Gets the parent adapters model. + * + * @return the parent adapters model. + */ + public AdaptersModel getAdapters(); + + /** + * Gets the from attribute. + * + * @return the from attribute + */ + public QName getFrom(); + + /** + * Sets the from attribute. + * + * @param from the from attribute + * @return this AdaptModel (useful for chaining) + */ + public AdaptModel setFrom(QName from); + + /** + * Gets the to attribute. + * + * @return the to attribute + */ + public QName getTo(); + + /** + * Sets the to attribute. + * + * @param to the to attribute + * @return this AdaptModel (useful for chaining) + */ + public AdaptModel setTo(QName to); +} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java b/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java new file mode 100644 index 000000000..45532cd24 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java @@ -0,0 +1,58 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.config.model.adapter; + +import java.util.List; + +import org.switchyard.config.model.Model; +import org.switchyard.config.model.switchyard.SwitchYardModel; + +/** + * Collection model for adapters. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public interface AdaptersModel extends Model { + + /** The "adapters" name. */ + public static final String ADAPTERS = "adapters"; + + /** + * Gets the parent switchyard model. + * + * @return the parent switchyard model + */ + public SwitchYardModel getSwitchYard(); + + /** + * Gets the child adapt models. + * + * @return the child adapt models + */ + public List getAdapts(); + + /** + * Adds a child adapt model. + * + * @param adapt the child adapt model to add + * @return this AdaptersModel (useful for chaining) + */ + public AdaptersModel addAdapt(AdaptModel adapt); + +} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java new file mode 100644 index 000000000..554e86bdd --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java @@ -0,0 +1,94 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.config.model.adapter.v1; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseModel; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; + +/** + * A version 1 AdaptersModel. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public class V1AdaptersModel extends BaseModel implements AdaptersModel { + + private List _adapts = new ArrayList(); + + /** + * Constructs a new V1AdaptersModel. + */ + public V1AdaptersModel() { + super(new QName(SwitchYardModel.DEFAULT_NAMESPACE, AdaptersModel.ADAPTERS)); + setModelChildrenOrder(AdaptModel.ADAPT); + } + + /** + * Constructs a new V1AdaptersModel with the specified Configuration and Descriptor. + * + * @param config the Configuration + * @param desc the Descriptor + */ + public V1AdaptersModel(Configuration config, Descriptor desc) { + super(config, desc); + for (Configuration adapt_config : config.getChildrenStartsWith(AdaptModel.ADAPT)) { + AdaptModel adapt = (AdaptModel)readModel(adapt_config); + if (adapt != null) { + _adapts.add(adapt); + } + } + setModelChildrenOrder(AdaptModel.ADAPT); + } + + /** + * {@inheritDoc} + */ + @Override + public SwitchYardModel getSwitchYard() { + return (SwitchYardModel)getModelParent(); + } + + /** + * {@inheritDoc} + */ + @Override + public List getAdapts() { + return Collections.unmodifiableList(_adapts); + } + + /** + * {@inheritDoc} + */ + @Override + public synchronized AdaptersModel addAdapt(AdaptModel adapt) { + addChildModel(adapt); + _adapts.add(adapt); + return this; + } + +} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java new file mode 100644 index 000000000..527d6eff4 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java @@ -0,0 +1,91 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. 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.config.model.adapter.v1; + +import javax.xml.namespace.QName; + +import org.switchyard.common.xml.XMLHelper; +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseTypedModel; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; + +/** + * A version 1 AdaptModel. + * + * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay + */ +public abstract class V1BaseAdaptModel extends BaseTypedModel implements AdaptModel { + + protected V1BaseAdaptModel(String type) { + this(new QName(SwitchYardModel.DEFAULT_NAMESPACE, AdaptModel.ADAPT + '.' + type)); + } + + protected V1BaseAdaptModel(QName qname) { + super(qname); + } + + protected V1BaseAdaptModel(Configuration config, Descriptor desc) { + super(config, desc); + } + + /** + * {@inheritDoc} + */ + @Override + public AdaptersModel getAdapters() { + return (AdaptersModel) getModelParent(); + } + + /** + * {@inheritDoc} + */ + @Override + public QName getFrom() { + return XMLHelper.createQName(getModelAttribute(AdaptModel.FROM)); + } + + /** + * {@inheritDoc} + */ + @Override + public AdaptModel setFrom(QName from) { + setModelAttribute(AdaptModel.FROM, from != null ? from.toString() : null); + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public QName getTo() { + return XMLHelper.createQName(getModelAttribute(AdaptModel.TO)); + } + + /** + * {@inheritDoc} + */ + @Override + public AdaptModel setTo(QName to) { + setModelAttribute(AdaptModel.TO, to != null ? to.toString() : null); + return this; + } +} diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java b/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java index ef7bd8e2d..d05a91e46 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java @@ -19,6 +19,7 @@ package org.switchyard.config.model.switchyard; import org.switchyard.config.model.NamedModel; +import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.CompositeModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.transform.TransformsModel; @@ -50,6 +51,21 @@ public interface SwitchYardModel extends NamedModel { */ public SwitchYardModel setComposite(CompositeModel composite); + /** + * Get the child adapters model. + * + * @return the child adapters model. + */ + public AdaptersModel getAdapters(); + + /** + * Sets the child adapters model. + * + * @param adapters the child adapters model. + * @return this SwitchYardModel (useful for chaining) + */ + public SwitchYardModel setAdapters(AdaptersModel adapters); + /** * Gets the child transforms model. * @return the child transforms model diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java index 0ae1d9702..0443351fd 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java @@ -22,6 +22,8 @@ import org.switchyard.config.model.BaseMarshaller; import org.switchyard.config.model.Descriptor; import org.switchyard.config.model.Model; +import org.switchyard.config.model.adapter.AdaptersModel; +import org.switchyard.config.model.adapter.v1.V1AdaptersModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.domain.HandlerModel; import org.switchyard.config.model.domain.HandlersModel; @@ -77,6 +79,8 @@ public Model read(Configuration config) { Descriptor desc = getDescriptor(); if (name.equals(SwitchYardModel.SWITCHYARD)) { return new V1SwitchYardModel(config, desc); + } else if (name.equals(AdaptersModel.ADAPTERS)) { + return new V1AdaptersModel(config, desc); } else if (name.equals(TransformsModel.TRANSFORMS)) { return new V1TransformsModel(config, desc); } else if (name.equals(ValidatesModel.VALIDATES)) { diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java index 8e41fa667..53f9cc4d7 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java @@ -26,6 +26,7 @@ import org.switchyard.config.Configuration; import org.switchyard.config.model.BaseNamedModel; import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.CompositeModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.property.PropertiesModel; @@ -43,6 +44,7 @@ public class V1SwitchYardModel extends BaseNamedModel implements SwitchYardModel private CompositeModel _composite; private ArtifactsModel _artifacts; + private AdaptersModel _adapters; private TransformsModel _transforms; private ValidatesModel _validates; private DomainModel _domain; @@ -88,6 +90,27 @@ public SwitchYardModel setComposite(CompositeModel composite) { return this; } + /** + * {@inheritDoc} + */ + @Override + public AdaptersModel getAdapters() { + if (_adapters == null) { + _adapters = (AdaptersModel) getFirstChildModelStartsWith(AdaptersModel.ADAPTERS); + } + return _adapters; + } + + /** + * {@inheritDoc} + */ + @Override + public SwitchYardModel setAdapters(AdaptersModel adapters) { + setChildModel(adapters); + _adapters = adapters; + return this; + } + /** * {@inheritDoc} */ diff --git a/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd b/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd index f3223da88..2dc3cda9d 100644 --- a/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd +++ b/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd @@ -30,6 +30,7 @@ MA 02110-1301, USA. + @@ -143,6 +144,19 @@ MA 02110-1301, USA. + + + + + + + + + + + + + diff --git a/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java b/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java index f83728038..07ec82a06 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java @@ -28,6 +28,7 @@ import org.switchyard.ExchangeHandler; import org.switchyard.ServiceDomain; import org.switchyard.ServiceSecurity; +import org.switchyard.adapter.AdapterRegistry; import org.switchyard.bus.camel.CamelExchangeBus; import org.switchyard.common.camel.SwitchYardCamelContext; import org.switchyard.common.type.Classes; @@ -41,6 +42,7 @@ import org.switchyard.internal.DefaultServiceSecurity; import org.switchyard.internal.DomainImpl; import org.switchyard.internal.EventManager; +import org.switchyard.internal.adapter.BaseAdapterRegistry; import org.switchyard.internal.transform.BaseTransformerRegistry; import org.switchyard.internal.validate.BaseValidatorRegistry; import org.switchyard.spi.ServiceRegistry; @@ -100,6 +102,7 @@ public ServiceDomain createDomain() { */ public ServiceDomain createDomain(QName domainName, SwitchYardModel switchyardConfig) { ServiceSecurity serviceSecurity = getServiceSecurity(switchyardConfig); + AdapterRegistry adapterRegistry = new BaseAdapterRegistry(); TransformerRegistry transformerRegistry = new BaseTransformerRegistry(); ValidatorRegistry validatorRegistry = new BaseValidatorRegistry(); @@ -107,7 +110,7 @@ public ServiceDomain createDomain(QName domainName, SwitchYardModel switchyardCo CamelExchangeBus bus = new CamelExchangeBus(camelContext); DomainImpl domain = new DomainImpl( - domainName, serviceSecurity, _registry, bus, transformerRegistry, validatorRegistry, _eventManager); + domainName, serviceSecurity, _registry, bus, adapterRegistry, transformerRegistry, validatorRegistry, _eventManager); camelContext.setServiceDomain(domain); if (switchyardConfig != null) { diff --git a/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java b/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java index a7db2369c..9b9cee69c 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java @@ -24,6 +24,7 @@ import javax.xml.namespace.QName; import org.switchyard.ServiceDomain; +import org.switchyard.adapter.AdapterRegistryLoader; import org.switchyard.common.type.Classes; import org.switchyard.config.model.switchyard.SwitchYardModel; import org.switchyard.deploy.Activator; @@ -54,6 +55,10 @@ public abstract class AbstractDeployment { * The Service Domain. */ private ServiceDomain _serviceDomain; + /** + * Adapter registry loaded for the deployment. + */ + private AdapterRegistryLoader _adapterRegistryLoader; /** * Transform registry loaded for the deployment. */ @@ -133,6 +138,7 @@ public final void init(ServiceDomain appServiceDomain, List activator _serviceDomain = appServiceDomain; _serviceDomain.getProperties().put(CLASSLOADER_PROPERTY, Classes.getTCCL()); + _adapterRegistryLoader = new AdapterRegistryLoader(appServiceDomain.getAdapterRegistry()); _transformerRegistryLoader = new TransformerRegistryLoader(appServiceDomain.getTransformerRegistry()); _transformerRegistryLoader.loadOOTBTransforms(); @@ -164,6 +170,15 @@ public ServiceDomain getDomain() { return _parentDeployment.getDomain(); } } + + /** + * Get the {@link AdapterRegistryLoader} acciciated with the deployment. + * + * @return The AdapterRegistryLoader instance. + */ + public AdapterRegistryLoader getAdapterRegistryLoader() { + return _adapterRegistryLoader; + } /** * Get the {@link TransformerRegistryLoader} associated with the deployment. diff --git a/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java b/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java index c590664a6..847e4fae2 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java @@ -36,6 +36,7 @@ import org.switchyard.ServiceReference; import org.switchyard.common.type.Classes; import org.switchyard.config.model.ModelPuller; +import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.BindingModel; import org.switchyard.config.model.composite.ComponentImplementationModel; import org.switchyard.config.model.composite.ComponentModel; @@ -110,6 +111,7 @@ public Deployment(SwitchYardModel configModel) { protected void doInit(List activators) { _log.debug("Initializing deployment " + getName()); // create a new domain and load transformer , validator and activator instances for lifecycle + registerAdapters(); registerTransformers(); registerValidators(); if (activators != null) { @@ -244,6 +246,12 @@ private Activator findActivator(ComponentModel component) throws SwitchYardExcep } return findActivator(component.getImplementation().getType()); } + + private void registerAdapters() { + _log.debug("Registering configured Adapters for deployment " + getName()); + AdaptersModel adapters = getConfig().getAdapters(); + getAdapterRegistryLoader().registerAdapters(adapters); + } private void registerTransformers() { _log.debug("Registering configured Transformers for deployment " + getName()); diff --git a/pom.xml b/pom.xml index 54e9d1dab..60035c434 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,8 @@ common/camel config api + adapter + addressing serial/base serial/jackson org.mockito diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java b/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java deleted file mode 100644 index b358a1de9..000000000 --- a/adapter/src/main/java/org/switchyard/adapter/AdapterRegistryLoader.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import org.apache.log4j.Logger; -import org.switchyard.adapter.config.model.JavaAdaptModel; -import org.switchyard.common.type.Classes; -import org.switchyard.config.model.adapter.AdaptModel; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.exception.DuplicateAdapterException; -import org.switchyard.exception.SwitchYardException; - -/** - * Adapter registry loader implementation. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterRegistryLoader { - /** - * Logger. - */ - private static Logger _log = Logger.getLogger(AdapterRegistryLoader.class); - /** - * Adapters. - */ - private List _adapters = new LinkedList(); - /** - * The registry instance into which the adapters were loaded. - */ - private AdapterRegistry _adapterRegistry; - - /** - * Public constructor. - * - * @param adapterRegistry The registry instance. - */ - public AdapterRegistryLoader(AdapterRegistry adapterRegistry) { - if (adapterRegistry == null) { - throw new IllegalArgumentException("null 'adapterRegistry' argument."); - } - this._adapterRegistry = adapterRegistry; - } - - /** - * Register a set of adapters in the adapter registry associated with this deployment. - * - * @param adaptersModel The adapters model. - * @throws DuplicateAdapterException an existing adapter has already been registered for the from and to types - */ - public void registerAdapters(AdaptersModel adaptersModel) throws DuplicateAdapterException { - if (adaptersModel == null) { - return; - } - - try { - for (AdaptModel adaptModel : adaptersModel.getAdapts()) { - Collection adapters = newAdapters(adaptModel); - for (Adapter adapter : adapters) { - if (_adapterRegistry.hasAdapter(adapter.getFrom())) { - Adapter registeredAdapter = _adapterRegistry.getAdapter(adapter.getFrom()); - String msg = "Failed to register Adapter '" + toDescription(adapter) - + "'. An Adapter for these services is already registered: '" - + toDescription(registeredAdapter) + "'."; - throw new DuplicateAdapterException(msg); - } else { - _log.debug("Adding adapter =>" - + " From: " + adapter.getFrom() - + ", To:" + adapter.getTo()); - _adapterRegistry.addAdapter(adapter); - _adapters.add(adapter); - } - } - } - } catch (DuplicateAdapterException e) { - throw e; - } catch (RuntimeException e) { - // If there was an exception for any reason... remove all Adapter instance that have - // already been registered with the domain... - unregisterAdapters(); - throw e; - } - } - - /** - * Unregister all adapters. - */ - public void unregisterAdapters() { - for (Adapter adapter : _adapters) { - _adapterRegistry.removeAdapter(adapter); - } - } - - /** - * Create adapters based on the adapt Model. - * - * @param adaptModel The adapt Model. - * @return Adapters represented through the adapt Model. - */ - public Collection newAdapters(AdaptModel adaptModel) { - Collection adapters = null; - - if (adaptModel instanceof JavaAdaptModel) { - String className = ((JavaAdaptModel) adaptModel).getClazz(); - Class adapterClass = Classes.forName(className, AdapterUtil.class); - if (adapterClass == null) { - throw new SwitchYardException("Unable to load adapter class " + className); - } - adapters = AdapterUtil.newAdapter(adapterClass, adaptModel.getFrom(), adaptModel.getTo()); - - } else { - throw new SwitchYardException("Only JavaAdapterModel supported"); - } - - if (adapters == null || adapters.isEmpty()) { - throw new SwitchYardException("Unknown AModel type '" + adaptModel.getClass().getName() + "'."); - } - - return adapters; - } - - List getAdapters() { - return _adapters; - } - - private String toDescription(Adapter adapter) { - return adapter.getClass().getName() + "(" + adapter.getFrom() + ", " + adapter.getTo() + ")"; - } -} diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java b/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java deleted file mode 100644 index b1a6c4eee..000000000 --- a/adapter/src/main/java/org/switchyard/adapter/AdapterTypes.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.adapter; - -import javax.xml.namespace.QName; - -/** - * Adapter data types. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public abstract class AdapterTypes { - private QName _from; - private QName _to; - - /** - * Public constructor. - * - * @param from From type. - * @param to To type. - */ - AdapterTypes(QName from, QName to) { - this._from = from; - this._to = to; - } - - /** - * Get from. - * - * @return from. - */ - public QName getFrom() { - return _from; - } - - /** - * Get to. - * - * @return to. - */ - public QName getTo() { - return _to; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return String.format("%s [from=%s, to=%s]", getClass().getSimpleName(), getFrom(), getTo()); - } -} diff --git a/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java b/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java index 6e09df1f7..4430901e7 100644 --- a/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java +++ b/adapter/src/main/java/org/switchyard/adapter/AdapterUtil.java @@ -21,16 +21,23 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -import javax.xml.namespace.QName; +import org.switchyard.adapter.config.model.JavaAdapterModel; +import org.switchyard.common.type.Classes; +import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; +import org.switchyard.config.model.switchyard.EsbInterfaceModel; import org.switchyard.exception.SwitchYardException; +import org.switchyard.extensions.wsdl.WSDLReaderException; +import org.switchyard.extensions.wsdl.WSDLService; +import org.switchyard.metadata.InOnlyOperation; +import org.switchyard.metadata.InOnlyService; +import org.switchyard.metadata.InOutOperation; +import org.switchyard.metadata.InOutService; import org.switchyard.metadata.ServiceInterface; import org.switchyard.metadata.ServiceOperation; +import org.switchyard.metadata.java.JavaService; + /** * Utility class to create adapters. @@ -39,39 +46,90 @@ */ public final class AdapterUtil { private AdapterUtil() {} - - /** - * Create adapters based on a generic class. - * - * @see #isAdapter(Class) - * @param clazz The class representing the adapters. - * @param from The consumer side service name. - * @param to The provider side service name. - * @return The adapters created based on the generic class. - */ - public static List newAdapter(Class clazz, QName from, QName to) { - if (!isAdapter(clazz)) { - throw new SwitchYardException("Invalid Adapter class '" + clazz.getName() + "'. Must implement the Adapter interface, or have methods annotated with the @Adapter annotation."); + + public static Adapter newAdapter(AdapterModel adapterModel) { + if (adapterModel instanceof JavaAdapterModel) { + JavaAdapterModel jAdapterModel = (JavaAdapterModel) adapterModel; + Class clazz = loadClass(jAdapterModel.getClazz()); + if (clazz == null) { + throw new SwitchYardException("Failed to load Adapter class '" + clazz + "'."); + } + return createAdapter(clazz, adapterModel.getInterfaceModel()); + } + return null; + } + + private static ServiceInterface createServiceInterface(InterfaceModel intfModel) { + if (isJavaInterface(intfModel.getType())) { + String interfaceClass = intfModel.getInterface(); + Class serviceInterfaceType = loadClass(interfaceClass); + + if (serviceInterfaceType == null) { + throw new SwitchYardException("Failed to load Service interface class '" + interfaceClass + "'."); + } + return JavaService.fromClass(serviceInterfaceType); + } else if (InterfaceModel.WSDL.equals(intfModel.getType())) { + try { + return WSDLService.fromWSDL(intfModel.getInterface()); + } catch (WSDLReaderException wsdlre) { + throw new SwitchYardException(wsdlre); + } + } else if (EsbInterfaceModel.ESB.equals(intfModel.getType())) { + EsbInterfaceModel esbIntf = (EsbInterfaceModel)intfModel; + validateEsbInterface(esbIntf); + if (esbIntf.getOutputType() == null) { + return new InOnlyService(new InOnlyOperation( + ServiceInterface.DEFAULT_OPERATION, esbIntf.getInputType())); + } else { + return new InOutService(new InOutOperation( + ServiceInterface.DEFAULT_OPERATION, + esbIntf.getInputType(), esbIntf.getOutputType(), esbIntf.getFaultType())); + } + } + throw new SwitchYardException("Failed to create Service interface from model '" + intfModel + "'."); + } + + // Checks for invalid input/output/fault combinations on ESB interfaces. + private static void validateEsbInterface(EsbInterfaceModel esbIntf) { + if (esbIntf.getInputType() == null) { + throw new SwitchYardException("inputType required on ESB interface definition: " + esbIntf); } - List adapters = new ArrayList(); - Object adapterObject = createAdapterObject(clazz); - Collection annotatedAdapters = tryCreateAnnotatedAdapters(clazz, from, to, adapterObject); - adapters.addAll(annotatedAdapters); - if (adapterObject instanceof Adapter) { - Collection adapterImplementations = createAdaptersImplementingAdapter(clazz, adapterObject, from, to); - adapters.addAll(adapterImplementations); + if (esbIntf.getFaultType() != null && esbIntf.getOutputType() == null) { + throw new SwitchYardException("faultType must be acommpanied by outputType in ESB interface: " + esbIntf); } - return adapters; } + + private static boolean isJavaInterface(final String type) { + return InterfaceModel.JAVA.equals(type); + } + + private static Class loadClass(String clazz) { + return Classes.forName(clazz); + } - /** - * Test if the class represents adapters. - * - * @param clazz The class to test. - * @return True if the class represents adapters, otherwise false. - */ - public static boolean isAdapter(Class clazz) { + private static Method getAdapterMethod(Class clazz) { + if (!isAdapter(clazz)) { + throw new SwitchYardException("Invalid Adapter class '" + clazz + "'."); + } + for (Method publicMethod : clazz.getMethods()) { + if (isAdapterMethod(publicMethod)) { + return publicMethod; + } + } + throw new SwitchYardException("Failed to locate Adapter method in class '" + clazz + "'."); + } + + private static boolean isAdapterMethod(Method publicMethod) { + Class returnType = publicMethod.getReturnType(); + Class[] parameterTypes = publicMethod.getParameterTypes(); + + if (parameterTypes.length != 2) + return false; + return ServiceOperation.class.equals(returnType) && String.class.equals(parameterTypes[0]) && ServiceInterface.class.equals(parameterTypes[1]); + } + + static boolean isAdapter(Class clazz) { if (clazz.isInterface()) { return false; } @@ -97,88 +155,53 @@ public static boolean isAdapter(Class clazz) { return true; } } - return false; } - private static Collection tryCreateAnnotatedAdapters(Class clazz, QName from, QName to, Object adapterObject) { - Collection adapters = new ArrayList(); - for (Method publicMethod : clazz.getMethods()) { - org.switchyard.annotations.Adapter adapterAnno = publicMethod.getAnnotation(org.switchyard.annotations.Adapter.class); - if (adapterAnno != null) { - AdapterMethod adapterMethod = toAdapterMethod(publicMethod, adapterAnno); - if (adapterMethod.getFrom().equals(from) && adapterMethod.getTo().equals(to)) { - adapters.add(newAdapter(adapterObject, adapterMethod.getMethod(), adapterMethod.getFrom(), adapterMethod.getTo())); - } - } - } - return adapters; + private static Adapter createAdapter(Class clazz, InterfaceModel interfaceModel) { + ServiceInterface serviceInterface = createServiceInterface(interfaceModel); + Object instance = newInstance(clazz); + Adapter adapter = null; + if (instance instanceof Adapter) { + adapter = (Adapter) instance; + } else { + adapter = createAnnotatedAdapter(instance, interfaceModel); + } + adapter.setServiceInterface(serviceInterface); + return adapter; } - - private static Collection createAdaptersImplementingAdapter(Class clazz, Object adapterObject, QName from, QName to) { - Collection adapters = new ArrayList(); - for (Method publicMethod : clazz.getMethods()) { - Class returnType = publicMethod.getReturnType(); - Class[] parameterTypes = publicMethod.getParameterTypes(); - if (ServiceOperation.class.equals(returnType) && parameterTypes.length == 2 && String.class.equals(parameterTypes[0]) && ServiceInterface.class.equals(parameterTypes[1])) { - Adapter adapter = (Adapter) adapterObject; - AdapterMethod adapterMethod = toAdapterMethod(publicMethod, adapter); - if (adapterMethod.getFrom().equals(from) && adapterMethod.getTo().equals(to)) { - adapters.add(newAdapter(adapterObject, adapterMethod.getMethod(), adapterMethod.getFrom(), adapterMethod.getTo())); - } - } - } - return adapters; - } - - private static Adapter newAdapter(final Object adapterObject, final Method publicMethod, QName from, QName to) { - Adapter adapter = new BaseAdapter(from, to) { - @Override - public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { - Object[] parameters = Arrays.asList(consumerOperation, targetInterface).toArray(); - - try { - return (ServiceOperation) publicMethod.invoke(adapterObject, parameters); - } catch (InvocationTargetException e) { - throw new SwitchYardException("Error executing @Adapter method '" + publicMethod.getName() + "' on class '" + publicMethod.getDeclaringClass().getName() + "'.", e.getCause()); - } catch (Exception e) { - throw new SwitchYardException("Error executing @Adapter method '" + publicMethod.getName() + "' on class '" + publicMethod.getDeclaringClass().getName() + "'.", e); - } - } - }; - return adapter; - } - - private static AdapterMethod toAdapterMethod(Method publicMethod, org.switchyard.annotations.Adapter adapterAnno) { - QName from = QName.valueOf(adapterAnno.from().trim()); - QName to = QName.valueOf(adapterAnno.to().trim()); - - return new AdapterMethod(from, to, publicMethod); - } - - private static AdapterMethod toAdapterMethod(Method publicMethod, Adapter adapter) { - return new AdapterMethod(adapter.getFrom(), adapter.getTo(), publicMethod); - } + private static Object newInstance(Class clazz) { + try { + return clazz.newInstance(); + } catch (Exception e) { + throw new SwitchYardException("Error constructing Adapter instance of type '" + clazz + "'. Class must have a public default constructor.", e); + } + } - private static Object createAdapterObject(Class clazz) { - try { - return clazz.newInstance(); - } catch (Exception e) { - throw new SwitchYardException("Error constructing Adapter instance for class '" + clazz.getName() + "'. Class must have a public default constructor.", e); - } - } - - private static class AdapterMethod extends AdapterTypes { - private Method _publicMethod; - - AdapterMethod(QName from, QName to, Method publicMethod) { - super(from, to); - this._publicMethod = publicMethod; - } - - public Method getMethod() { - return _publicMethod; - } + private static Adapter createAnnotatedAdapter(final Object instance, InterfaceModel interfaceModel) { + if (!InterfaceModel.JAVA.equals(interfaceModel.getType())) { + throw new SwitchYardException("Annotated Adapters only supported for Java interfaces: " + interfaceModel + "."); + } + final Method adapterMethod = getAdapterMethod(instance.getClass()); + org.switchyard.annotations.Adapter adapter = adapterMethod.getAnnotation(org.switchyard.annotations.Adapter.class); + if (adapter == null) { + throw new SwitchYardException("Failed to locate Adapter annotation method: " + instance.getClass() + "."); + } + return new BaseAdapter() { + @Override + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + Object[] parameters = new Object[] { consumerOperation, targetInterface }; + try { + return (ServiceOperation) adapterMethod.invoke(instance, parameters); + } catch (IllegalAccessException e) { + throw new SwitchYardException("Invokation of adpater method " + adapterMethod + " failed.", e); + } catch (IllegalArgumentException e) { + throw new SwitchYardException("Invokation of adpater method " + adapterMethod + " failed.", e); + } catch (InvocationTargetException e) { + throw new SwitchYardException("Invokation of adpater method " + adapterMethod + " failed.", e); + } + } + }; } } diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java b/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java index a45893ab6..c012829b2 100644 --- a/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/AdapterSwitchYardScanner.java @@ -1,182 +1,182 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter.config.model; - -import java.io.IOException; -import java.lang.reflect.Method; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import javax.xml.namespace.QName; - -import org.switchyard.adapter.Adapter; -import org.switchyard.adapter.AdapterUtil; -import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; -import org.switchyard.common.type.classpath.AbstractTypeFilter; -import org.switchyard.common.type.classpath.ClasspathScanner; -import org.switchyard.config.model.Scannable; -import org.switchyard.config.model.Scanner; -import org.switchyard.config.model.ScannerInput; -import org.switchyard.config.model.ScannerOutput; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.adapter.v1.V1AdaptersModel; -import org.switchyard.config.model.switchyard.SwitchYardModel; -import org.switchyard.config.model.switchyard.v1.V1SwitchYardModel; -import org.switchyard.exception.DuplicateAdapterException; -import org.switchyard.exception.SwitchYardException; - -/** - * Scanner for {@link org.switchyard.adapter.Adapt} implementations. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterSwitchYardScanner implements Scanner { - - @Override - public ScannerOutput scan(ScannerInput input) throws IOException { - SwitchYardModel switchyardModel = new V1SwitchYardModel(); - AdaptersModel adapters = new V1AdaptersModel(); - - List> adaptClasses = scanForAdapts(input.getURLs()); - for (Class adaptClass : adaptClasses) { - Collection adapterTypes = listAdaptTypes(adaptClass); - - for (AdapterTypes adapterType : adapterTypes) { - JavaAdaptModel adaptModel = new V1JavaAdaptModel(); - adaptModel.setClazz(adaptClass.getName()); - adaptModel.setFrom(adapterType.getFrom()); - adaptModel.setTo(adapterType.getTo()); - - adapters.addAdapt(adaptModel); - } - } - switchyardModel.setAdapters(adapters); - - return new ScannerOutput().setModel(switchyardModel); - } - - private Collection listAdaptTypes(Class adaptClass) { - Map map = new TreeMap(new QNameComparable()); - - AdapterTypes adapterType = adapterTypesFromAdapterInstance(adaptClass); - addToMap(map, adapterType); - - List adapterTypes = adapterTypesFromAdapterAnnotation(adaptClass); - addToMap(map, adapterTypes); - - return map.values(); - } - - private void addToMap(Map map, List adapterTypes) { - for (AdapterTypes adapter : adapterTypes) { - addToMap(map, adapter); - } - } - - private void addToMap(Map map, AdapterTypes adapterType) { - if (adapterType != null) { - if (!map.containsKey(adapterType.getFrom())) { - map.put(adapterType.getFrom(), adapterType); - } else { - String msg = "An Adapter for the service is already registered: '" + adapterType.getFrom() + "'."; - throw new DuplicateAdapterException(msg); - } - } - } - - List adapterTypesFromAdapterAnnotation(Class adaptClass) { - List adapterTypes = new ArrayList(); - - Method[] publicMethods = adaptClass.getMethods(); - for (Method method : publicMethods) { - org.switchyard.annotations.Adapter adapter = method.getAnnotation(org.switchyard.annotations.Adapter.class); - if (adapter != null && adapter.from() != null && adapter.to() != null) { - adapterTypes.add(new AdapterTypes(QName.valueOf(adapter.from()), QName.valueOf(adapter.to()))); - } - } - return adapterTypes; - } - - AdapterTypes adapterTypesFromAdapterInstance(Class adaptClass) { - if (Adapter.class.isAssignableFrom(adaptClass)) { - try { - Adapter adapter = (Adapter) adaptClass.newInstance(); - if (adapter.getFrom() != null && adapter.getTo() != null) { - return new AdapterTypes(adapter.getFrom(), adapter.getTo()); - } - } catch (Exception e) { - throw new SwitchYardException("Error constructing Adapter instance for class '" + adaptClass.getName() + "'. Class must have a public default constructor.", e); - } - } - return null; - } - - private List> scanForAdapts(List urls) throws IOException { - AbstractTypeFilter filter = new AdaptInstanceOfFilter(); - ClasspathScanner scanner = new ClasspathScanner(filter); - - for (URL url : urls) { - scanner.scan(url); - } - - return filter.getMatchedTypes(); - } - - private class AdapterTypes { - private QName _from; - private QName _to; - - public AdapterTypes(QName from, QName to) { - this._from = from; - this._to = to; - } - - public QName getFrom() { - return _from; - } - - public QName getTo() { - return _to; - } - } - - private final class AdaptInstanceOfFilter extends AbstractTypeFilter { - @Override - public boolean matches(Class clazz) { - Scannable scannable = clazz.getAnnotation(Scannable.class); - if (scannable != null && !scannable.value()) { - // Marked as being non-scannable... - return false; - } - return AdapterUtil.isAdapter(clazz); - } - } - - private final class QNameComparable implements Comparator { - @Override - public int compare(QName arg0, QName arg1) { - return arg0.toString().compareTo(arg1.toString()); - } - } -} +///* +// * JBoss, Home of Professional Open Source +// * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors +// * as indicated by the @author tags. 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.adapter.config.model; +// +//import java.io.IOException; +//import java.lang.reflect.Method; +//import java.net.URL; +//import java.util.ArrayList; +//import java.util.Collection; +//import java.util.Comparator; +//import java.util.List; +//import java.util.Map; +//import java.util.TreeMap; +// +//import javax.xml.namespace.QName; +// +//import org.switchyard.adapter.Adapter; +//import org.switchyard.adapter.AdapterUtil; +//import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; +//import org.switchyard.common.type.classpath.AbstractTypeFilter; +//import org.switchyard.common.type.classpath.ClasspathScanner; +//import org.switchyard.config.model.Scannable; +//import org.switchyard.config.model.Scanner; +//import org.switchyard.config.model.ScannerInput; +//import org.switchyard.config.model.ScannerOutput; +//import org.switchyard.config.model.adapter.AdaptersModel; +//import org.switchyard.config.model.adapter.v1.V1AdaptersModel; +//import org.switchyard.config.model.switchyard.SwitchYardModel; +//import org.switchyard.config.model.switchyard.v1.V1SwitchYardModel; +//import org.switchyard.exception.DuplicateAdapterException; +//import org.switchyard.exception.SwitchYardException; +// +///** +// * Scanner for {@link org.switchyard.adapter.Adapt} implementations. +// * +// * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay +// */ +//public class AdapterSwitchYardScanner implements Scanner { +// +// @Override +// public ScannerOutput scan(ScannerInput input) throws IOException { +// SwitchYardModel switchyardModel = new V1SwitchYardModel(); +// AdaptersModel adapters = new V1AdaptersModel(); +// +// List> adaptClasses = scanForAdapts(input.getURLs()); +// for (Class adaptClass : adaptClasses) { +// Collection adapterTypes = listAdaptTypes(adaptClass); +// +// for (AdapterTypes adapterType : adapterTypes) { +// JavaAdaptModel adaptModel = new V1JavaAdaptModel(); +// adaptModel.setClazz(adaptClass.getName()); +// adaptModel.setFrom(adapterType.getFrom()); +// adaptModel.setTo(adapterType.getTo()); +// +// adapters.addAdapt(adaptModel); +// } +// } +// switchyardModel.setAdapters(adapters); +// +// return new ScannerOutput().setModel(switchyardModel); +// } +// +// private Collection listAdaptTypes(Class adaptClass) { +// Map map = new TreeMap(new QNameComparable()); +// +// AdapterTypes adapterType = adapterTypesFromAdapterInstance(adaptClass); +// addToMap(map, adapterType); +// +// List adapterTypes = adapterTypesFromAdapterAnnotation(adaptClass); +// addToMap(map, adapterTypes); +// +// return map.values(); +// } +// +// private void addToMap(Map map, List adapterTypes) { +// for (AdapterTypes adapter : adapterTypes) { +// addToMap(map, adapter); +// } +// } +// +// private void addToMap(Map map, AdapterTypes adapterType) { +// if (adapterType != null) { +// if (!map.containsKey(adapterType.getFrom())) { +// map.put(adapterType.getFrom(), adapterType); +// } else { +// String msg = "An Adapter for the service is already registered: '" + adapterType.getFrom() + "'."; +// throw new DuplicateAdapterException(msg); +// } +// } +// } +// +// List adapterTypesFromAdapterAnnotation(Class adaptClass) { +// List adapterTypes = new ArrayList(); +// +// Method[] publicMethods = adaptClass.getMethods(); +// for (Method method : publicMethods) { +// org.switchyard.annotations.Adapter adapter = method.getAnnotation(org.switchyard.annotations.Adapter.class); +// if (adapter != null && adapter.from() != null && adapter.to() != null) { +// adapterTypes.add(new AdapterTypes(QName.valueOf(adapter.from()), QName.valueOf(adapter.to()))); +// } +// } +// return adapterTypes; +// } +// +// AdapterTypes adapterTypesFromAdapterInstance(Class adaptClass) { +// if (Adapter.class.isAssignableFrom(adaptClass)) { +// try { +// Adapter adapter = (Adapter) adaptClass.newInstance(); +// if (adapter.getFrom() != null && adapter.getTo() != null) { +// return new AdapterTypes(adapter.getFrom(), adapter.getTo()); +// } +// } catch (Exception e) { +// throw new SwitchYardException("Error constructing Adapter instance for class '" + adaptClass.getName() + "'. Class must have a public default constructor.", e); +// } +// } +// return null; +// } +// +// private List> scanForAdapts(List urls) throws IOException { +// AbstractTypeFilter filter = new AdaptInstanceOfFilter(); +// ClasspathScanner scanner = new ClasspathScanner(filter); +// +// for (URL url : urls) { +// scanner.scan(url); +// } +// +// return filter.getMatchedTypes(); +// } +// +// private class AdapterTypes { +// private QName _from; +// private QName _to; +// +// public AdapterTypes(QName from, QName to) { +// this._from = from; +// this._to = to; +// } +// +// public QName getFrom() { +// return _from; +// } +// +// public QName getTo() { +// return _to; +// } +// } +// +// private final class AdaptInstanceOfFilter extends AbstractTypeFilter { +// @Override +// public boolean matches(Class clazz) { +// Scannable scannable = clazz.getAnnotation(Scannable.class); +// if (scannable != null && !scannable.value()) { +// // Marked as being non-scannable... +// return false; +// } +// return AdapterUtil.isAdapter(clazz); +// } +// } +// +// private final class QNameComparable implements Comparator { +// @Override +// public int compare(QName arg0, QName arg1) { +// return arg0.toString().compareTo(arg1.toString()); +// } +// } +//} diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java b/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdapterModel.java similarity index 90% rename from adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java rename to adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdapterModel.java index b8370f909..458ab35af 100644 --- a/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdaptModel.java +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/JavaAdapterModel.java @@ -18,14 +18,14 @@ */ package org.switchyard.adapter.config.model; -import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; /** * A "adapt.java" configuration model. * * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay */ -public interface JavaAdaptModel extends AdaptModel { +public interface JavaAdapterModel extends AdapterModel { /** The "java" name. */ public static final String JAVA = "java"; @@ -46,5 +46,6 @@ public interface JavaAdaptModel extends AdaptModel { * @param clazz the class attribute * @return this JavaAdaptModel (useful for chaining) */ - public JavaAdaptModel setClazz(String clazz); + public JavaAdapterModel setClazz(String clazz); + } diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java index f964563a9..3f49026bc 100644 --- a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1AdapterMarshaller.java @@ -18,12 +18,12 @@ */ package org.switchyard.adapter.config.model.v1; -import org.switchyard.adapter.config.model.JavaAdaptModel; +import org.switchyard.adapter.config.model.JavaAdapterModel; import org.switchyard.config.Configuration; import org.switchyard.config.model.BaseMarshaller; import org.switchyard.config.model.Descriptor; import org.switchyard.config.model.Model; -import org.switchyard.config.model.adapter.AdaptModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; /** * Marshalls adapter Models. @@ -32,7 +32,7 @@ */ public class V1AdapterMarshaller extends BaseMarshaller { - private static final String ADAPT_JAVA = AdaptModel.ADAPT + "." + JavaAdaptModel.JAVA; + private static final String ADAPTER_JAVA = AdapterModel.ADAPTER + "." + JavaAdapterModel.JAVA; /** * Constructs a new V1AdapterMarshaller with the specified Descriptor. @@ -51,8 +51,8 @@ public Model read(Configuration config) { String name = config.getName(); Descriptor desc = getDescriptor(); - if (name.equals(ADAPT_JAVA)) { - return new V1JavaAdaptModel(config, desc); + if (name.equals(ADAPTER_JAVA)) { + return new V1JavaAdapterModel(config, desc); } return null; } diff --git a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdapterModel.java similarity index 77% rename from adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java rename to adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdapterModel.java index 84bdaadbe..3bd904c39 100644 --- a/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdaptModel.java +++ b/adapter/src/main/java/org/switchyard/adapter/config/model/v1/V1JavaAdapterModel.java @@ -20,24 +20,24 @@ import javax.xml.namespace.QName; -import org.switchyard.adapter.config.model.JavaAdaptModel; +import org.switchyard.adapter.config.model.JavaAdapterModel; import org.switchyard.config.Configuration; import org.switchyard.config.model.Descriptor; -import org.switchyard.config.model.adapter.AdaptModel; -import org.switchyard.config.model.adapter.v1.V1BaseAdaptModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; +import org.switchyard.config.model.extensions.adapter.v1.V1BaseAdaptModel; /** * A version 1 JavaAdaptModel. * * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay */ -public class V1JavaAdaptModel extends V1BaseAdaptModel implements JavaAdaptModel { +public class V1JavaAdapterModel extends V1BaseAdaptModel implements JavaAdapterModel { /** * Constructs a new V1JavaAdaptModel. */ - public V1JavaAdaptModel() { - super(new QName(AdaptModel.DEFAULT_NAMESPACE, AdaptModel.ADAPT + '.' + JAVA)); + public V1JavaAdapterModel() { + super(new QName(AdapterModel.DEFAULT_NAMESPACE, AdapterModel.ADAPTER + '.' + JAVA)); } /** @@ -46,7 +46,7 @@ public V1JavaAdaptModel() { * @param config the Configuration * @param desc the Descriptor */ - public V1JavaAdaptModel(Configuration config, Descriptor desc) { + public V1JavaAdapterModel(Configuration config, Descriptor desc) { super(config, desc); } @@ -62,7 +62,7 @@ public String getClazz() { * {@inheritDoc} */ @Override - public JavaAdaptModel setClazz(String clazz) { + public JavaAdapterModel setClazz(String clazz) { setModelAttribute(CLASS, clazz); return this; } diff --git a/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd b/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd index 91b213b17..dfd498e35 100644 --- a/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd +++ b/adapter/src/main/resources/org/switchyard/adapter/config/model/v1/adapter-v1.xsd @@ -25,15 +25,15 @@ MA 02110-1301, USA. - - + + Generic/Custom Java Adapter Configuration. - + diff --git a/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java b/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java deleted file mode 100644 index 383b8587c..000000000 --- a/adapter/src/test/java/org/switchyard/adapter/AdapterRegistryLoaderTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter; - -import java.util.Collection; - -import javax.xml.namespace.QName; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.switchyard.adapter.config.model.adapters.V1toV2Adapter; -import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.adapter.v1.V1AdaptersModel; -import org.switchyard.exception.DuplicateAdapterException; -import org.switchyard.metadata.ServiceInterface; -import org.switchyard.metadata.ServiceOperation; - -/** - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterRegistryLoaderTest { - private AdapterRegistryLoader loader; - private V1JavaAdaptModel adaptModel; - private AdapterRegistry registy; - - @Before - public void setup() { - registy = Mockito.mock(AdapterRegistry.class); - loader = new AdapterRegistryLoader(registy); - - adaptModel = new V1JavaAdaptModel(); - adaptModel.setFrom(V1toV2Adapter.FROM); - adaptModel.setTo(V1toV2Adapter.TO); - adaptModel.setClazz(V1toV2Adapter.class.getName()); - } - - @Test - public void testNewAdapters() { - Collection adapters = loader.newAdapters(adaptModel); - Assert.assertEquals(1, adapters.size()); - } - - @Test - public void testRegisterAdapters() { - AdaptersModel adaptersModel = new V1AdaptersModel(); - adaptersModel.addAdapt(adaptModel); - - loader.registerAdapters(adaptersModel); - Assert.assertEquals(1, loader.getAdapters().size()); - } - - @Test - public void testRegisterAdaptersDuplicateFrom() { - AdaptersModel adaptersModel = new V1AdaptersModel(); - adaptersModel.addAdapt(adaptModel); - - loader.registerAdapters(adaptersModel); - - Mockito.when(registy.hasAdapter(adaptModel.getFrom())).thenReturn(true); - Mockito.when(registy.getAdapter(Mockito.any(QName.class))).thenReturn( - new TestAdapter(adaptModel.getFrom(), adaptModel.getTo())); - - try { - loader.registerAdapters(adaptersModel); - Assert.fail(); - } catch (DuplicateAdapterException e) { - Assert.assertEquals( - "Failed to register Adapter " + - "'org.switchyard.adapter.AdapterUtil$1({urn:org.switchyard.adapter.config.model}TestServiceV1, {urn:org.switchyard.adapter.config.model}TestServiceV2)'. " + - "An Adapter for these services is already registered: " + - "'org.switchyard.adapter.AdapterRegistryLoaderTest$TestAdapter({urn:org.switchyard.adapter.config.model}TestServiceV1, {urn:org.switchyard.adapter.config.model}TestServiceV2)'.", - e.getMessage()); - } - } - - public static class TestAdapter extends BaseAdapter { - public TestAdapter(QName from, QName to) { - super(from, to); - } - - @Override - public ServiceOperation lookup(String consumerOperation, - ServiceInterface targetInterface) { - return null; - } - } -} diff --git a/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java b/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java index 1a7a1afba..4d7ec091b 100644 --- a/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java +++ b/adapter/src/test/java/org/switchyard/adapter/AdapterUtilTest.java @@ -18,82 +18,64 @@ */ package org.switchyard.adapter; -import java.util.List; - -import javax.xml.namespace.QName; +import static org.junit.Assert.*; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import org.switchyard.metadata.ServiceInterface; -import org.switchyard.metadata.ServiceOperation; +import org.switchyard.adapter.config.model.adapters.AnnoV1toV2Adapter; +import org.switchyard.adapter.config.model.adapters.MockServiceContract; +import org.switchyard.adapter.config.model.adapters.NoAnnotationAdapter; +import org.switchyard.adapter.config.model.adapters.V1toV2Adapter; +import org.switchyard.adapter.config.model.v1.V1JavaAdapterModel; +import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.composite.v1.V1InterfaceModel; +import org.switchyard.exception.SwitchYardException; + /** * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay */ public class AdapterUtilTest { - private static final QName TEST_SERVICE_V1 = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV1"); - private static final QName TEST_SERVICE_V2 = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV2"); - @Test - public void testIsAdapter() { - Assert.assertTrue(AdapterUtil.isAdapter(TestAdapter.class)); - Assert.assertTrue(AdapterUtil.isAdapter(AnnoTestAdapter.class)); - - Assert.assertFalse(AdapterUtil.isAdapter(Adapter.class)); - Assert.assertFalse(AdapterUtil.isAdapter(org.switchyard.annotations.Adapter.class)); - Assert.assertFalse(AdapterUtil.isAdapter(AbstractTestAdapter.class)); - Assert.assertFalse(AdapterUtil.isAdapter(Object.class)); - Assert.assertFalse(AdapterUtil.isAdapter(ConstructerTestAdapter.class)); + private V1JavaAdapterModel model; + + @Before + public void before() { + model = new V1JavaAdapterModel(); + InterfaceModel interfaceModel = new V1InterfaceModel(InterfaceModel.JAVA); + interfaceModel.setInterface(MockServiceContract.class.getName()); + model.setInterfaceModel(interfaceModel); } @Test - public void testNewAdapter() { - List adapters = AdapterUtil.newAdapter(TestAdapter.class, TEST_SERVICE_V1, TEST_SERVICE_V2); - Assert.assertEquals(1, adapters.size()); - Adapter adapter = adapters.get(0); - Assert.assertEquals(TEST_SERVICE_V1, adapter.getFrom()); - Assert.assertEquals(TEST_SERVICE_V2, adapter.getTo()); + public void testAnnotationBasedAdapter() { + model.setClazz(AnnoV1toV2Adapter.class.getName()); - adapters = AdapterUtil.newAdapter(AnnoTestAdapter.class, TEST_SERVICE_V1, TEST_SERVICE_V2); - Assert.assertEquals(1, adapters.size()); - adapter = adapters.get(0); - Assert.assertEquals(TEST_SERVICE_V1, adapter.getFrom()); - Assert.assertEquals(TEST_SERVICE_V2, adapter.getTo()); + Adapter adapter = AdapterUtil.newAdapter(model); + assertNotNull(adapter); + assertNotNull(adapter.getServiceInterface()); } - public static class TestAdapter extends BaseAdapter { - public TestAdapter() { - super(TEST_SERVICE_V1, TEST_SERVICE_V2); - } - - @Override - public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { - return null; - } - } - - public static class AnnoTestAdapter { + @Test + public void testImplementationBasedAdapter() { + model.setClazz(V1toV2Adapter.class.getName()); - @org.switchyard.annotations.Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") - public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { - return null; - } - } - - public abstract static class AbstractTestAdapter extends BaseAdapter { - public AbstractTestAdapter() { - super(null, null); - } + Adapter adapter = AdapterUtil.newAdapter(model); + assertNotNull(adapter); + assertEquals(V1toV2Adapter.class, adapter.getClass()); + assertNotNull(adapter.getServiceInterface()); } - public static class ConstructerTestAdapter extends BaseAdapter { - public ConstructerTestAdapter(QName from, QName to) { - super(from, to); - } - - @Override - public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { - return null; + @Test + public void testNoAnnotationFoundAdapter() { + model.setClazz(NoAnnotationAdapter.class.getName()); + + try { + AdapterUtil.newAdapter(model); + fail(); + } catch (SwitchYardException e) { + Assert.assertEquals("Invalid Adapter class 'class org.switchyard.adapter.config.model.adapters.NoAnnotationAdapter'.", e.getMessage()); } } } diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java index aecbab717..6229b18ac 100644 --- a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterModelTests.java @@ -19,26 +19,28 @@ package org.switchyard.adapter.config.model; import java.io.StringReader; +import java.util.List; import javax.xml.namespace.QName; -import junit.framework.Assert; - import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.switchyard.adapter.config.model.adapters.V1toV2Adapter; -import org.switchyard.adapter.config.model.v1.V1JavaAdaptModel; +import org.switchyard.adapter.config.model.v1.V1JavaAdapterModel; import org.switchyard.common.io.pull.StringPuller; import org.switchyard.common.xml.XMLHelper; import org.switchyard.config.model.Model; import org.switchyard.config.model.ModelPuller; -import org.switchyard.config.model.adapter.AdaptModel; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.adapter.v1.V1AdaptersModel; +import org.switchyard.config.model.composite.CompositeServiceModel; +import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.composite.v1.V1InterfaceModel; +import org.switchyard.config.model.extensions.ExtensionsModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; +import org.switchyard.config.model.extensions.v1.V1ExtensionsModel; import org.switchyard.config.model.switchyard.SwitchYardModel; -import org.switchyard.config.model.switchyard.v1.V1SwitchYardModel; /** * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay @@ -46,6 +48,7 @@ public class AdapterModelTests { private static final String XML = "/org/switchyard/adapter/config/model/AdapterModelTests.xml"; + private static final String CREATE_XML = "/org/switchyard/adapter/config/model/CreateAdapterModelTests.xml"; private ModelPuller _puller; @@ -55,43 +58,54 @@ public void before() throws Exception { } @Test - public void testCreateEmptyModel() throws Exception { - String namespace = AdaptModel.DEFAULT_NAMESPACE; - String name = AdaptModel.ADAPT + '.' + JavaAdaptModel.JAVA; + public void testCreateInvalidEmptyModel() throws Exception { + String namespace = AdapterModel.DEFAULT_NAMESPACE; + String name = AdapterModel.ADAPTER + '.' + JavaAdapterModel.JAVA; Model model = new ModelPuller().pull(XMLHelper.createQName(namespace, name)); - Assert.assertTrue(model instanceof JavaAdaptModel); + Assert.assertTrue(model instanceof JavaAdapterModel); Assert.assertEquals(name, model.getModelConfiguration().getName()); Assert.assertEquals(new QName(namespace, name), model.getModelConfiguration().getQName()); + Assert.assertFalse(model.isModelValid()); } - @Test - public void testCreate() throws Exception { - SwitchYardModel switchyard = new V1SwitchYardModel(); - AdaptersModel adapters = new V1AdaptersModel(); - JavaAdaptModel javaAdapters = new V1JavaAdaptModel(); - javaAdapters.setFrom(new QName("{urn:org.switchyard.adapter.config.model}TestServiceV1")); - javaAdapters.setTo(new QName("{urn:org.switchyard.adapter.config.model}TestServiceV2")); - javaAdapters.setClazz("org.switchyard.adapter.config.model.adapters.V1toV2Adapter"); - adapters.addAdapt(javaAdapters); - - switchyard.setAdapters(adapters); - String new_xml = switchyard.toString(); - String old_xml = new ModelPuller().pull(XML, getClass()).toString(); - - XMLUnit.setIgnoreWhitespace(true); - Diff diff = XMLUnit.compareXML(old_xml, new_xml); - Assert.assertTrue(diff.toString(), diff.identical()); - } +// @Test +// public void testCreate() throws Exception { +// SwitchYardModel switchyard = _puller.pull(CREATE_XML, getClass()); +// Assert.assertTrue(switchyard.isModelValid()); +// +// InterfaceModel interfaceModel = new V1InterfaceModel(InterfaceModel.JAVA); +// interfaceModel.setInterface("org.switchyard.adapter.config.model.ServiceContractV1"); +// JavaAdapterModel javaAdapter = new V1JavaAdapterModel(); +// javaAdapter.setInterfaceModel(interfaceModel); +// javaAdapter.setClazz(V1toV2Adapter.class.getName()); +// ExtensionsModel extensionsModel = new V1ExtensionsModel(); +// extensionsModel.setAdapterModel(javaAdapter); +// switchyard.getComposite().getServices().get(0).setExtensions(extensionsModel); +// +// String new_xml = switchyard.toString(); +// String old_xml = new ModelPuller().pull(XML, getClass()).toString(); +// XMLUnit.setIgnoreWhitespace(true); +// Diff diff = XMLUnit.compareXML(old_xml, new_xml); +// Assert.assertTrue(diff.toString(), diff.identical()); +// } @Test public void testRead() throws Exception { SwitchYardModel switchyard = _puller.pull(XML, getClass()); - AdaptersModel adapters = switchyard.getAdapters(); - - JavaAdaptModel java_adapt = (JavaAdaptModel)adapters.getAdapts().get(0); - Assert.assertEquals("TestServiceV1", java_adapt.getFrom().getLocalPart()); - Assert.assertEquals("TestServiceV2", java_adapt.getTo().getLocalPart()); - Assert.assertEquals(V1toV2Adapter.class.getName(), java_adapt.getClazz()); + List serviceModels = switchyard.getComposite().getServices(); + Assert.assertEquals(1, serviceModels.size()); + + CompositeServiceModel compositeServiceModel = serviceModels.get(0); + ExtensionsModel extensionsModel = compositeServiceModel.getExtensionsModel(); + Assert.assertNotNull(extensionsModel); + AdapterModel adapterModel = extensionsModel.getAdapterModel(); + Assert.assertNotNull(adapterModel); + Assert.assertTrue(adapterModel instanceof JavaAdapterModel); + InterfaceModel interfaceModel = adapterModel.getInterfaceModel(); + Assert.assertEquals(InterfaceModel.JAVA, interfaceModel.getType()); + Assert.assertEquals("org.switchyard.adapter.config.model.ServiceContractV1", interfaceModel.getInterface()); + JavaAdapterModel javaAdapterModel = (JavaAdapterModel) adapterModel; + Assert.assertEquals(V1toV2Adapter.class.getName(), javaAdapterModel.getClazz()); } @Test @@ -104,21 +118,9 @@ public void testWrite() throws Exception { Assert.assertTrue(diff.toString(), diff.identical()); } - @Test - public void testParenthood() throws Exception { - SwitchYardModel switchyard_1 = _puller.pull(XML, getClass()); - AdaptersModel transforms_1 = switchyard_1.getAdapters(); - AdaptModel transform = transforms_1.getAdapts().get(0); - AdaptersModel transforms_2 = transform.getAdapters(); - - SwitchYardModel switchyard_2 = transforms_2.getSwitchYard(); - Assert.assertEquals(transforms_1, transforms_2); - Assert.assertEquals(switchyard_1, switchyard_2); - } - @Test public void testValidation() throws Exception { - SwitchYardModel switchyard = _puller.pull(XML, getClass()); + SwitchYardModel switchyard = _puller.pull(XML, getClass()); switchyard.assertModelValid(); } } diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java index 9ab7e04a2..10d057d37 100644 --- a/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/AdapterSwitchYardScannerTest.java @@ -1,71 +1,71 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter.config.model; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.switchyard.adapter.config.model.adapters.AnnoV1toV2Adapter; -import org.switchyard.config.model.ScannerInput; -import org.switchyard.config.model.switchyard.SwitchYardModel; -import org.switchyard.exception.DuplicateAdapterException; - -/** - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterSwitchYardScannerTest { - - private AdapterSwitchYardScanner scanner; - private ArrayList testUrls; - - @Before - public void setUp() throws MalformedURLException { - this.scanner = new AdapterSwitchYardScanner(); - - testUrls = new ArrayList(); - // If running this test inside your IDE... you need to set the cwd to be the - // root of the transform module !! - testUrls.add(new File("./target/test-classes").toURI().toURL()); - } - - @Test - public void testAnnotatedAdapter() { - List adapterTypes = scanner.adapterTypesFromAdapterAnnotation(AnnoV1toV2Adapter.class); - - Assert.assertEquals(2, adapterTypes.size()); - } - - @Test - public void testScanDuplicateAdapters() throws IOException { - ScannerInput input = new ScannerInput().setURLs(testUrls); - try { - scanner.scan(input).getModel(); - Assert.fail(); - } catch (DuplicateAdapterException e) { - Assert.assertEquals("An Adapter for the service is already registered: '{urn:org.switchyard.adapter.config.model}TestServiceV1'.", e.getMessage()); - } - } -} +///* +// * JBoss, Home of Professional Open Source +// * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors +// * as indicated by the @author tags. 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.adapter.config.model; +// +//import java.io.File; +//import java.io.IOException; +//import java.net.MalformedURLException; +//import java.net.URL; +//import java.util.ArrayList; +//import java.util.List; +// +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Test; +//import org.switchyard.adapter.config.model.adapters.AnnoV1toV2Adapter; +//import org.switchyard.config.model.ScannerInput; +//import org.switchyard.config.model.switchyard.SwitchYardModel; +//import org.switchyard.exception.DuplicateAdapterException; +// +///** +// * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay +// */ +//public class AdapterSwitchYardScannerTest { +// +// private AdapterSwitchYardScanner scanner; +// private ArrayList testUrls; +// +// @Before +// public void setUp() throws MalformedURLException { +// this.scanner = new AdapterSwitchYardScanner(); +// +// testUrls = new ArrayList(); +// // If running this test inside your IDE... you need to set the cwd to be the +// // root of the transform module !! +// testUrls.add(new File("./target/test-classes").toURI().toURL()); +// } +// +// @Test +// public void testAnnotatedAdapter() { +// List adapterTypes = scanner.adapterTypesFromAdapterAnnotation(AnnoV1toV2Adapter.class); +// +// Assert.assertEquals(2, adapterTypes.size()); +// } +// +// @Test +// public void testScanDuplicateAdapters() throws IOException { +// ScannerInput input = new ScannerInput().setURLs(testUrls); +// try { +// scanner.scan(input).getModel(); +// Assert.fail(); +// } catch (DuplicateAdapterException e) { +// Assert.assertEquals("An Adapter for the service is already registered: '{urn:org.switchyard.adapter.config.model}TestServiceV1'.", e.getMessage()); +// } +// } +//} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java index 082436895..550933fbf 100644 --- a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/AnnoV1toV2Adapter.java @@ -30,18 +30,23 @@ */ public class AnnoV1toV2Adapter { - @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") - public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { - return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); - } +// @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") +// public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { +// return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); +// } +// +// // Duplicate from not allowed -> AdapterRegistryLoaderTest#testRegisterAdaptersDuplicateFrom +// @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") +// public ServiceOperation lookup2(String consumerOperation, ServiceInterface targetInterface) { +// return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); +// } +// +// public ServiceOperation dummyLookup(String consumerOperation, ServiceInterface targetInterface) { +// return null; +// } - // Duplicate from not allowed -> AdapterRegistryLoaderTest#testRegisterAdaptersDuplicateFrom - @Adapter(from = "{urn:org.switchyard.adapter.config.model}TestServiceV1", to = "{urn:org.switchyard.adapter.config.model}TestServiceV2") - public ServiceOperation lookup2(String consumerOperation, ServiceInterface targetInterface) { + @Adapter(service = "{urn:switchyard.adapter:1.0}TestServiceV2", contract = MockServiceContract.class) + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); } - - public ServiceOperation dummyLookup(String consumerOperation, ServiceInterface targetInterface) { - return null; - } } diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/MockServiceContract.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/MockServiceContract.java new file mode 100644 index 000000000..809356c7e --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/MockServiceContract.java @@ -0,0 +1,5 @@ +package org.switchyard.adapter.config.model.adapters; + +public interface MockServiceContract { + +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/NoAnnotationAdapter.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/NoAnnotationAdapter.java new file mode 100644 index 000000000..70033a043 --- /dev/null +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/NoAnnotationAdapter.java @@ -0,0 +1,13 @@ +package org.switchyard.adapter.config.model.adapters; + +import org.switchyard.ExchangePattern; +import org.switchyard.metadata.BaseServiceOperation; +import org.switchyard.metadata.ServiceInterface; +import org.switchyard.metadata.ServiceOperation; +import org.switchyard.metadata.java.JavaService; + +public class NoAnnotationAdapter { + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { + return new BaseServiceOperation(ExchangePattern.IN_ONLY, "test", JavaService.toMessageType(Void.class), JavaService.toMessageType(String.class), null); + } +} diff --git a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java index f1be380ae..8b12dd521 100644 --- a/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java +++ b/adapter/src/test/java/org/switchyard/adapter/config/model/adapters/V1toV2Adapter.java @@ -18,8 +18,6 @@ */ package org.switchyard.adapter.config.model.adapters; -import javax.xml.namespace.QName; - import org.switchyard.adapter.BaseAdapter; import org.switchyard.metadata.ServiceInterface; import org.switchyard.metadata.ServiceOperation; @@ -28,12 +26,6 @@ * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay */ public class V1toV2Adapter extends BaseAdapter { - public static final QName FROM = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV1"); - public static final QName TO = QName.valueOf("{urn:org.switchyard.adapter.config.model}TestServiceV2"); - - public V1toV2Adapter() { - super(FROM, TO); - } @Override public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface) { diff --git a/adapter/src/test/resources/META-INF/switchyard.xml b/adapter/src/test/resources/META-INF/switchyard.xml deleted file mode 100644 index ed4e09115..000000000 --- a/adapter/src/test/resources/META-INF/switchyard.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml b/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml index f74573ab2..381d6d68f 100644 --- a/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml +++ b/adapter/src/test/resources/org/switchyard/adapter/config/model/AdapterModelTests.xml @@ -17,12 +17,22 @@ 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. --> - - - - + + + + + + + + + + + + + + + + + + diff --git a/adapter/src/test/resources/org/switchyard/adapter/config/model/CreateAdapterModelTests.xml b/adapter/src/test/resources/org/switchyard/adapter/config/model/CreateAdapterModelTests.xml new file mode 100644 index 000000000..cc7819cbf --- /dev/null +++ b/adapter/src/test/resources/org/switchyard/adapter/config/model/CreateAdapterModelTests.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + diff --git a/addressing/pom.xml b/addressing/pom.xml deleted file mode 100644 index cf1db0527..000000000 --- a/addressing/pom.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - 4.0.0 - - org.switchyard - switchyard-core-parent - 1.0.0-SNAPSHOT - - switchyard-addressing - SwitchYard: Addressing - The SwitchYard Addressing library. - http://switchyard.org - - - org.switchyard - switchyard-api - - - org.switchyard - switchyard-adapter - ${project.version} - - - - - jboss-public-repository - JBoss Public Maven Repository - http://repository.jboss.org/nexus/content/groups/public - - - - - jboss-public-repository - JBoss Public Maven Repository - http://repository.jboss.org/nexus/content/groups/public - - - diff --git a/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java deleted file mode 100644 index ccc94678c..000000000 --- a/addressing/src/main/java/org/switchyard/addressing/ServiceResolver.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.addressing; - -import javax.xml.namespace.QName; - -import org.switchyard.HandlerException; -import org.switchyard.Service; -import org.switchyard.ServiceDomain; -import org.switchyard.ServiceReference; -import org.switchyard.metadata.ServiceInterface; -import org.switchyard.metadata.ServiceOperation; - -/** - * Resolves the provider service and operation. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public interface ServiceResolver { - - /** - * Resolves the service provider. - * - * @param domain Service Domain - * @param consumerReference Consumer's service reference. - * @return Provider's service reference. - * @throws HandlerException If no unique service is found. - */ - Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) throws HandlerException; - - /** - * Resolve the provider's service operation. - * - * @param fromServiceName The service's name requested by the consumer. - * @param toServiceName The service's name resolved by getProviderService. - * @param consumerOperation The consumer's service operation. - * @param serviceInterface The consumer's service interface. - * @return The provider's service interface. - * @throws HandlerException If no service operation could be resolved. - */ - ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException; -} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java deleted file mode 100644 index b65cfcf76..000000000 --- a/addressing/src/main/java/org/switchyard/addressing/resolvers/AdapterServiceResolver.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.addressing.resolvers; - -import java.util.List; - -import javax.xml.namespace.QName; - -import org.switchyard.HandlerException; -import org.switchyard.Service; -import org.switchyard.ServiceDomain; -import org.switchyard.ServiceReference; -import org.switchyard.adapter.Adapter; -import org.switchyard.adapter.AdapterRegistry; -import org.switchyard.addressing.ServiceResolver; -import org.switchyard.metadata.ServiceInterface; -import org.switchyard.metadata.ServiceOperation; - -/** - * Service and operation selection based on user defined adapters. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterServiceResolver implements ServiceResolver { - - private AdapterRegistry _adapterRegistry; - - /** - * Public constructor. - * - * @param adapterRegistry The registry instance. - */ - public AdapterServiceResolver(AdapterRegistry adapterRegistry) { - _adapterRegistry = adapterRegistry; - } - - /** - * {@inheritDoc} - */ - @Override - public ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException { - Adapter adapter = _adapterRegistry.getAdapter(fromServiceName); - if (adapter.getTo().equals(toServiceName)) { - return adapter.lookup(consumerOperation.getName(), serviceInterface); - } - throw new HandlerException(String.format("No adapter available ('%s' -> '%s')", fromServiceName, toServiceName)); - } - - /** - * {@inheritDoc} - */ - @Override - public Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) throws HandlerException { - QName from = consumerReference.getName(); - Adapter adapter = _adapterRegistry.getAdapter(from); - - List services = domain.getServices(adapter.getTo()); - if (services.size() >= 1) { - return services.get(0); - } - throw new HandlerException("Target service not found: " + adapter.getTo()); - } -} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java deleted file mode 100644 index b80fa76c8..000000000 --- a/addressing/src/main/java/org/switchyard/addressing/resolvers/BaseServiceResolver.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.addressing.resolvers; - -import java.util.List; - -import org.switchyard.Service; -import org.switchyard.ServiceDomain; -import org.switchyard.ServiceReference; -import org.switchyard.addressing.ServiceResolver; -import org.switchyard.exception.SwitchYardException; - -/** - * Basic service resolver implementation. - * Provides the implementation for getProviderService - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public abstract class BaseServiceResolver implements ServiceResolver { - - /** - * {@inheritDoc} - */ - @Override - public Service serviceLookup(ServiceDomain domain, ServiceReference consumerReference) { - List services = domain.getServices(consumerReference.getTargetServiceName()); - if (services == null || services.isEmpty()) { - throw new SwitchYardException("No registered service found for " + consumerReference.getName()); - } - // At this stage, just pick the first service implementation we find and go with - // it. In the future, it would be nice if we could make this pluggable. - return services.get(0); - } -} diff --git a/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java b/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java deleted file mode 100644 index 329c0c26c..000000000 --- a/addressing/src/main/java/org/switchyard/addressing/resolvers/DefaultServiceResolver.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.addressing.resolvers; - -import javax.xml.namespace.QName; - -import org.switchyard.HandlerException; -import org.switchyard.addressing.ServiceResolver; -import org.switchyard.metadata.ServiceInterface; -import org.switchyard.metadata.ServiceOperation; - -/** - * Default implementation for the service resolver interface. - * - * @see ServiceResolver - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class DefaultServiceResolver extends BaseServiceResolver implements ServiceResolver { - - /** - * {@inheritDoc} - */ - @Override - public ServiceOperation getProviderOp(QName fromServiceName, QName toServiceName, ServiceOperation consumerOperation, ServiceInterface serviceInterface) throws HandlerException { - ServiceOperation providerOp = serviceInterface.getOperation(consumerOperation.getName()); - - if (providerOp == null) { - // try for a default operation - if (serviceInterface.getOperations().size() == 1) { - providerOp = serviceInterface.getOperations().iterator().next(); - } else { - throw new HandlerException("Operation " + consumerOperation.getName() - + " is not included in interface for service: " + toServiceName); - } - } - return providerOp; - } - -} diff --git a/api/src/main/java/org/switchyard/Service.java b/api/src/main/java/org/switchyard/Service.java index 586a3fa49..555a76b4e 100644 --- a/api/src/main/java/org/switchyard/Service.java +++ b/api/src/main/java/org/switchyard/Service.java @@ -23,6 +23,7 @@ import javax.xml.namespace.QName; +import org.switchyard.adapter.Adapter; import org.switchyard.metadata.Registrant; import org.switchyard.metadata.ServiceInterface; import org.switchyard.policy.Policy; @@ -72,4 +73,10 @@ public interface Service { * @return provider metadata */ Registrant getProviderMetadata(); + + boolean hasAdapter(); + + Service setAdapter(Adapter adapter); + + Adapter getAdapter(); } diff --git a/api/src/main/java/org/switchyard/ServiceDomain.java b/api/src/main/java/org/switchyard/ServiceDomain.java index 44ffe489f..939fce8ea 100644 --- a/api/src/main/java/org/switchyard/ServiceDomain.java +++ b/api/src/main/java/org/switchyard/ServiceDomain.java @@ -25,7 +25,6 @@ import javax.xml.namespace.QName; -import org.switchyard.adapter.AdapterRegistry; import org.switchyard.event.EventObserver; import org.switchyard.event.EventPublisher; import org.switchyard.metadata.Registrant; @@ -39,7 +38,7 @@ * resources, configuration, and policy definitions. The ServiceDomain * interface is used by software components to provide and/or consume * services. These software components include protocol gateways, service - * containers, translation engines, adapters, orchestration and routing + * containers, translation engines, orchestration and routing * engines. */ public interface ServiceDomain { @@ -144,12 +143,6 @@ ServiceReference registerServiceReference(QName serviceName, */ public ServiceSecurity getServiceSecurity(); - /** - * Returns a reference to the adapter registry for this domain. - * @return adapter registry instance - */ - AdapterRegistry getAdapterRegistry(); - /** * Returns a reference to the transformer registry for this domain. * @return transformer registry instance diff --git a/api/src/main/java/org/switchyard/adapter/Adapter.java b/api/src/main/java/org/switchyard/adapter/Adapter.java index aad622013..4b694b5f4 100644 --- a/api/src/main/java/org/switchyard/adapter/Adapter.java +++ b/api/src/main/java/org/switchyard/adapter/Adapter.java @@ -1,67 +1,13 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter; -import javax.xml.namespace.QName; - import org.switchyard.metadata.ServiceInterface; import org.switchyard.metadata.ServiceOperation; -/** - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ public interface Adapter { - - /** - * Set the name of the from, or source, message type. - * - * @param fromType From type. - * @return a reference to the current Adapter. - */ - Adapter setFrom(QName fromType); - - /** - * Set the name of the to, or source, message type. - * - * @param toType To type. - * @return a reference to the current Adapter. - */ - Adapter setTo(QName toType); - - /** - * The name of the from, or source, message. - * @return from message - */ - QName getFrom(); - - /** - * The name of the to, or target, message. - * @return to message - */ - QName getTo(); - - /** - * Adapter operation. - * - * @param consumerOperation The invoked consumers service operation. - * @param targetInterface The re-targeted service interface. - * @return The {@link ServiceOperation} to invoke. - */ - ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface); + + public Adapter setServiceInterface(ServiceInterface serviceInterface); + + public ServiceInterface getServiceInterface(); + + public ServiceOperation lookup(String consumerOperation, ServiceInterface targetInterface); } diff --git a/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java b/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java deleted file mode 100644 index e95ce2af7..000000000 --- a/api/src/main/java/org/switchyard/adapter/AdapterRegistry.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter; - -import javax.xml.namespace.QName; - -/** - * Adapter registry. - * An adapter is identified by the consumer's service name (from). - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public interface AdapterRegistry { - - /** - * Add an adapter. - * - * @param adapter adapter - * @return {@code this} AdapterRegistry instance. - */ - AdapterRegistry addAdapter(Adapter adapter); - - /** - * Add an adapter. - * - * @param adapter adapter - * @param from from - * @param to to - * @return {@code this} AdapterRegistry instance. - */ - AdapterRegistry addAdapter(Adapter adapter, QName from, QName to); - - /** - * Remove an adapter. - * - * @param adapter adapter - * @return status of removal - */ - boolean removeAdapter(Adapter adapter); - - /** - * Does the registry have an adapter for the specified type. - * - * @param from from - * @return True if it has an adapter, otherwise false. - */ - boolean hasAdapter(QName from); - - /** - * Get an adapter. - * - * @param from from - * @return adapter - */ - Adapter getAdapter(QName from); -} diff --git a/api/src/main/java/org/switchyard/adapter/BaseAdapter.java b/api/src/main/java/org/switchyard/adapter/BaseAdapter.java index c70a73937..f72b95af5 100644 --- a/api/src/main/java/org/switchyard/adapter/BaseAdapter.java +++ b/api/src/main/java/org/switchyard/adapter/BaseAdapter.java @@ -1,67 +1,16 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.adapter; -import javax.xml.namespace.QName; +import org.switchyard.metadata.ServiceInterface; -/** - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ public abstract class BaseAdapter implements Adapter { - private QName _from; - private QName _to; + private ServiceInterface _serviceInterface; - /** - * Constructor. - * @param from from - * @param to to - */ - public BaseAdapter(QName from, QName to) { - super(); - this._from = from; - this._to = to; - } - - @Override - public Adapter setFrom(QName fromType) { - _from = fromType; - return this; - } - - @Override - public Adapter setTo(QName toType) { - _to = toType; - return this; - } - - @Override - public QName getFrom() { - return _from; - } - - @Override - public QName getTo() { - return _to; - } - - @Override - public String toString() { - return getClass().getName() + ": from=\"" + getFrom() + "\", to=\"" + getTo() + "\""; - } + public Adapter setServiceInterface(ServiceInterface serviceInterface) { + _serviceInterface = serviceInterface; + return this; + } + + public ServiceInterface getServiceInterface() { + return _serviceInterface; + } } diff --git a/api/src/main/java/org/switchyard/annotations/Adapter.java b/api/src/main/java/org/switchyard/annotations/Adapter.java index ba958d7d9..729df8da2 100644 --- a/api/src/main/java/org/switchyard/annotations/Adapter.java +++ b/api/src/main/java/org/switchyard/annotations/Adapter.java @@ -32,14 +32,10 @@ @Retention(RUNTIME) @Documented public @interface Adapter { - - /** + /** * Adapt from service (QName). */ - String from(); - - /** - * Adapt to service (QName). - */ - String to(); + String service(); + + Class contract(); } diff --git a/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java b/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java deleted file mode 100644 index 2747fec34..000000000 --- a/api/src/main/java/org/switchyard/event/AdapterAddedEvent.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.event; - -import java.util.EventObject; - -import org.switchyard.adapter.Adapter; - -/** - * Fired when an adapter is added to the domain. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class AdapterAddedEvent extends EventObject { - private static final long serialVersionUID = -6264437639353632902L; - - /** - * Creates a new AdapterAddedEvent. - * @param adapter the transformer that was added - */ - public AdapterAddedEvent(Adapter adapter) { - super(adapter); - } - - /** - * Get the added adapter. - * @return added adapter - */ - public Adapter getTransformer() { - return (Adapter) getSource(); - } -} diff --git a/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java b/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java deleted file mode 100644 index 92176d104..000000000 --- a/api/src/main/java/org/switchyard/event/AdapterRemovedEvent.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.event; - -import java.util.EventObject; - -import org.switchyard.adapter.Adapter; - -/** - * Fired when an adapter is removed from the domain. - */ -public class AdapterRemovedEvent extends EventObject { - - private static final long serialVersionUID = -5796235997474241451L; - - /** - * Creates a new AdapterRemovedEvent. - * @param adapter the adapter that was removed - */ - public AdapterRemovedEvent(Adapter adapter) { - super(adapter); - } - - /** - * Get the removed adapter. - * @return removed adapter - */ - public Adapter getTransformer() { - return (Adapter) getSource(); - } -} diff --git a/bus/camel/src/test/java/org/switchyard/bus/camel/MockService.java b/bus/camel/src/test/java/org/switchyard/bus/camel/MockService.java index fc96111cb..75d3d599c 100644 --- a/bus/camel/src/test/java/org/switchyard/bus/camel/MockService.java +++ b/bus/camel/src/test/java/org/switchyard/bus/camel/MockService.java @@ -26,6 +26,7 @@ import org.switchyard.ExchangeHandler; import org.switchyard.Service; import org.switchyard.ServiceDomain; +import org.switchyard.adapter.Adapter; import org.switchyard.metadata.InOnlyService; import org.switchyard.metadata.Registrant; import org.switchyard.metadata.ServiceInterface; @@ -36,6 +37,7 @@ public class MockService implements Service { private QName _serviceName; private ServiceInterface _serviceInterface; private ExchangeHandler _handler; + private Adapter _adapter; public MockService(QName serviceName, ExchangeHandler handler) { this(serviceName, new InOnlyService(), handler); @@ -76,6 +78,21 @@ public List getRequiredPolicies() { public ExchangeHandler getProviderHandler() { return _handler; } + + @Override + public Service setAdapter(Adapter adapter) { + _adapter = adapter; + return this; + } + + public Adapter getAdapter() { + return _adapter; + } + + @Override + public boolean hasAdapter() { + return _adapter != null; + } @Override public Registrant getProviderMetadata() { diff --git a/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java b/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java deleted file mode 100644 index c6b0ba3c8..000000000 --- a/config/src/main/java/org/switchyard/config/model/adapter/AdaptModel.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.config.model.adapter; - -import javax.xml.namespace.QName; - -import org.switchyard.config.model.Model; - -/** - * Single adapter model. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public interface AdaptModel extends Model { - /** The default "adapt" namespace. */ - public static final String DEFAULT_NAMESPACE = "urn:switchyard-config:adapter:1.0"; - - /** The "adapt" name. */ - public static final String ADAPT = "adapt"; - - /** The "from" name. */ - public static final String FROM = "from"; - - /** The "to" name. */ - public static final String TO = "to"; - - /** - * Gets the parent adapters model. - * - * @return the parent adapters model. - */ - public AdaptersModel getAdapters(); - - /** - * Gets the from attribute. - * - * @return the from attribute - */ - public QName getFrom(); - - /** - * Sets the from attribute. - * - * @param from the from attribute - * @return this AdaptModel (useful for chaining) - */ - public AdaptModel setFrom(QName from); - - /** - * Gets the to attribute. - * - * @return the to attribute - */ - public QName getTo(); - - /** - * Sets the to attribute. - * - * @param to the to attribute - * @return this AdaptModel (useful for chaining) - */ - public AdaptModel setTo(QName to); -} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java b/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java deleted file mode 100644 index 45532cd24..000000000 --- a/config/src/main/java/org/switchyard/config/model/adapter/AdaptersModel.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.config.model.adapter; - -import java.util.List; - -import org.switchyard.config.model.Model; -import org.switchyard.config.model.switchyard.SwitchYardModel; - -/** - * Collection model for adapters. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public interface AdaptersModel extends Model { - - /** The "adapters" name. */ - public static final String ADAPTERS = "adapters"; - - /** - * Gets the parent switchyard model. - * - * @return the parent switchyard model - */ - public SwitchYardModel getSwitchYard(); - - /** - * Gets the child adapt models. - * - * @return the child adapt models - */ - public List getAdapts(); - - /** - * Adds a child adapt model. - * - * @param adapt the child adapt model to add - * @return this AdaptersModel (useful for chaining) - */ - public AdaptersModel addAdapt(AdaptModel adapt); - -} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java deleted file mode 100644 index 554e86bdd..000000000 --- a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1AdaptersModel.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.config.model.adapter.v1; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import javax.xml.namespace.QName; - -import org.switchyard.config.Configuration; -import org.switchyard.config.model.BaseModel; -import org.switchyard.config.model.Descriptor; -import org.switchyard.config.model.adapter.AdaptModel; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.switchyard.SwitchYardModel; - -/** - * A version 1 AdaptersModel. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public class V1AdaptersModel extends BaseModel implements AdaptersModel { - - private List _adapts = new ArrayList(); - - /** - * Constructs a new V1AdaptersModel. - */ - public V1AdaptersModel() { - super(new QName(SwitchYardModel.DEFAULT_NAMESPACE, AdaptersModel.ADAPTERS)); - setModelChildrenOrder(AdaptModel.ADAPT); - } - - /** - * Constructs a new V1AdaptersModel with the specified Configuration and Descriptor. - * - * @param config the Configuration - * @param desc the Descriptor - */ - public V1AdaptersModel(Configuration config, Descriptor desc) { - super(config, desc); - for (Configuration adapt_config : config.getChildrenStartsWith(AdaptModel.ADAPT)) { - AdaptModel adapt = (AdaptModel)readModel(adapt_config); - if (adapt != null) { - _adapts.add(adapt); - } - } - setModelChildrenOrder(AdaptModel.ADAPT); - } - - /** - * {@inheritDoc} - */ - @Override - public SwitchYardModel getSwitchYard() { - return (SwitchYardModel)getModelParent(); - } - - /** - * {@inheritDoc} - */ - @Override - public List getAdapts() { - return Collections.unmodifiableList(_adapts); - } - - /** - * {@inheritDoc} - */ - @Override - public synchronized AdaptersModel addAdapt(AdaptModel adapt) { - addChildModel(adapt); - _adapts.add(adapt); - return this; - } - -} diff --git a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java b/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java deleted file mode 100644 index 527d6eff4..000000000 --- a/config/src/main/java/org/switchyard/config/model/adapter/v1/V1BaseAdaptModel.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors - * as indicated by the @author tags. 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.config.model.adapter.v1; - -import javax.xml.namespace.QName; - -import org.switchyard.common.xml.XMLHelper; -import org.switchyard.config.Configuration; -import org.switchyard.config.model.BaseTypedModel; -import org.switchyard.config.model.Descriptor; -import org.switchyard.config.model.adapter.AdaptModel; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.switchyard.SwitchYardModel; - -/** - * A version 1 AdaptModel. - * - * @author Christoph Gostner <christoph.gostner@objectbay.com> © 2013 Objectbay - */ -public abstract class V1BaseAdaptModel extends BaseTypedModel implements AdaptModel { - - protected V1BaseAdaptModel(String type) { - this(new QName(SwitchYardModel.DEFAULT_NAMESPACE, AdaptModel.ADAPT + '.' + type)); - } - - protected V1BaseAdaptModel(QName qname) { - super(qname); - } - - protected V1BaseAdaptModel(Configuration config, Descriptor desc) { - super(config, desc); - } - - /** - * {@inheritDoc} - */ - @Override - public AdaptersModel getAdapters() { - return (AdaptersModel) getModelParent(); - } - - /** - * {@inheritDoc} - */ - @Override - public QName getFrom() { - return XMLHelper.createQName(getModelAttribute(AdaptModel.FROM)); - } - - /** - * {@inheritDoc} - */ - @Override - public AdaptModel setFrom(QName from) { - setModelAttribute(AdaptModel.FROM, from != null ? from.toString() : null); - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public QName getTo() { - return XMLHelper.createQName(getModelAttribute(AdaptModel.TO)); - } - - /** - * {@inheritDoc} - */ - @Override - public AdaptModel setTo(QName to) { - setModelAttribute(AdaptModel.TO, to != null ? to.toString() : null); - return this; - } -} diff --git a/config/src/main/java/org/switchyard/config/model/composite/CompositeServiceModel.java b/config/src/main/java/org/switchyard/config/model/composite/CompositeServiceModel.java index 71812dfc1..548691a26 100644 --- a/config/src/main/java/org/switchyard/config/model/composite/CompositeServiceModel.java +++ b/config/src/main/java/org/switchyard/config/model/composite/CompositeServiceModel.java @@ -21,6 +21,7 @@ import java.util.List; import org.switchyard.config.model.NamedModel; +import org.switchyard.config.model.extensions.ExtensionsModel; /** * The "composite/service" model. @@ -52,7 +53,13 @@ public interface CompositeServiceModel extends NamedModel { * @return the child component service model */ public ComponentServiceModel getComponentService(); - + + /** + * Gets the child extensions model. + * @return the child extensions model + */ + public ExtensionsModel getExtensionsModel(); + /** * Gets the promote attribute. * @return the promote attribute @@ -91,5 +98,9 @@ public interface CompositeServiceModel extends NamedModel { * @return this CompositeServiceModel (useful for chaining) */ public CompositeServiceModel setInterface(InterfaceModel interfaze); + + public CompositeServiceModel setExtensions(ExtensionsModel extensionsModel); + + public boolean hasExtensionsModel(); } diff --git a/config/src/main/java/org/switchyard/config/model/composite/v1/BarService.java b/config/src/main/java/org/switchyard/config/model/composite/v1/BarService.java new file mode 100644 index 000000000..9d07b63e8 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/composite/v1/BarService.java @@ -0,0 +1,5 @@ +package org.switchyard.config.model.composite.v1; + +public interface BarService { + +} diff --git a/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeMarshaller.java b/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeMarshaller.java index 3e9f2da75..beb145ef3 100644 --- a/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeMarshaller.java +++ b/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeMarshaller.java @@ -31,6 +31,8 @@ import org.switchyard.config.model.composite.CompositeServiceModel; import org.switchyard.config.model.composite.InterfaceModel; import org.switchyard.config.model.composite.SCABindingModel; +import org.switchyard.config.model.extensions.ExtensionsModel; +import org.switchyard.config.model.extensions.v1.V1ExtensionsModel; import org.switchyard.config.model.property.PropertyModel; import org.switchyard.config.model.property.v1.V1PropertyModel; @@ -96,6 +98,8 @@ public Model read(Configuration config) { } } else if (name.equals(PropertyModel.PROPERTY)) { return new V1PropertyModel(config,desc); + } else if (name.equals(ExtensionsModel.EXTENSIONS)) { + return new V1ExtensionsModel(config, desc); } return null; diff --git a/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeServiceModel.java b/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeServiceModel.java index 016c5f537..afdfb1c7c 100644 --- a/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeServiceModel.java +++ b/config/src/main/java/org/switchyard/config/model/composite/v1/V1CompositeServiceModel.java @@ -36,6 +36,7 @@ import org.switchyard.config.model.composite.CompositeModel; import org.switchyard.config.model.composite.CompositeServiceModel; import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.extensions.ExtensionsModel; /** * A version 1 CompositeServiceModel. @@ -48,6 +49,7 @@ public class V1CompositeServiceModel extends BaseNamedModel implements Composite private List _bindings = new ArrayList(); private InterfaceModel _interface; + private ExtensionsModel _extensionsModel; /** * Constructs a new V1CompositeServiceModel. @@ -69,6 +71,20 @@ public V1CompositeServiceModel(Configuration config, Descriptor desc) { _bindings.add(binding); } } + List configs = config.getChildren(ExtensionsModel.EXTENSIONS); + for (Configuration extensions_config : configs) { + ExtensionsModel extensionsModel = (ExtensionsModel) readModel(extensions_config); + if (extensionsModel != null) { + _extensionsModel = extensionsModel; + break; + } + } + + } + + @Override + public ExtensionsModel getExtensionsModel() { + return _extensionsModel; } /** @@ -192,4 +208,16 @@ public CompositeServiceModel setInterface(InterfaceModel interfaze) { _interface = interfaze; return this; } + + @Override + public CompositeServiceModel setExtensions(ExtensionsModel extensionsModel) { + setChildModel(extensionsModel); + _extensionsModel = extensionsModel; + return this; + } + + @Override + public boolean hasExtensionsModel() { + return _extensionsModel != null; + } } diff --git a/config/src/main/java/org/switchyard/config/model/extensions/ExtensionsModel.java b/config/src/main/java/org/switchyard/config/model/extensions/ExtensionsModel.java new file mode 100644 index 000000000..4ec0382bc --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/extensions/ExtensionsModel.java @@ -0,0 +1,16 @@ +package org.switchyard.config.model.extensions; + +import org.switchyard.config.model.TypedModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; + +public interface ExtensionsModel extends TypedModel { + + /** The "extensions" name. */ + public static final String EXTENSIONS = "extensions"; + + public AdapterModel getAdapterModel(); + + public ExtensionsModel setAdapterModel(AdapterModel adapterModel); + + public boolean hasAdapterModel(); +} diff --git a/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterClassModel.java b/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterClassModel.java new file mode 100644 index 000000000..3fc7a81a4 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterClassModel.java @@ -0,0 +1,7 @@ +package org.switchyard.config.model.extensions.adapter; + +import org.switchyard.config.model.TypedModel; + +public interface AdapterClassModel extends TypedModel { + +} diff --git a/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterModel.java b/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterModel.java new file mode 100644 index 000000000..134772bf1 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/extensions/adapter/AdapterModel.java @@ -0,0 +1,17 @@ +package org.switchyard.config.model.extensions.adapter; + +import org.switchyard.config.model.TypedModel; +import org.switchyard.config.model.composite.InterfaceModel; + +public interface AdapterModel extends TypedModel { + + /** The default "adapter" namespace. */ + public static final String DEFAULT_NAMESPACE = "urn:switchyard-config:adapter:1.0"; + + /** The "adapter" name. */ + public static final String ADAPTER = "adapter"; + + public InterfaceModel getInterfaceModel(); + + public AdapterModel setInterfaceModel(InterfaceModel interfaceModel); +} diff --git a/config/src/main/java/org/switchyard/config/model/extensions/adapter/v1/V1BaseAdaptModel.java b/config/src/main/java/org/switchyard/config/model/extensions/adapter/v1/V1BaseAdaptModel.java new file mode 100644 index 000000000..0c19b7bd0 --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/extensions/adapter/v1/V1BaseAdaptModel.java @@ -0,0 +1,49 @@ +package org.switchyard.config.model.extensions.adapter.v1; + +import javax.xml.namespace.QName; + +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseTypedModel; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; +import org.switchyard.config.model.switchyard.SwitchYardModel; + +public abstract class V1BaseAdaptModel extends BaseTypedModel implements AdapterModel { + + protected static final String INTERFACE = "interface"; + private InterfaceModel _interfaceModel; + + protected V1BaseAdaptModel(String type) { + this(new QName(SwitchYardModel.DEFAULT_NAMESPACE, AdapterModel.ADAPTER + '.' + type)); + } + + protected V1BaseAdaptModel(QName qname) { + super(qname); + } + + protected V1BaseAdaptModel(Configuration config, Descriptor desc) { + super(config, desc); + + // only one interface is supported + for (Configuration adater_config : config.getChildrenStartsWith(InterfaceModel.INTERFACE)) { + InterfaceModel interfaceModel = (InterfaceModel)readModel(adater_config); + if (interfaceModel != null) { + _interfaceModel = interfaceModel; + break; + } + } + } + + @Override + public InterfaceModel getInterfaceModel() { + return _interfaceModel; + } + + @Override + public AdapterModel setInterfaceModel(InterfaceModel interfaceModel) { + setChildModel(interfaceModel); + _interfaceModel = interfaceModel; + return this; + } +} diff --git a/config/src/main/java/org/switchyard/config/model/extensions/v1/V1ExtensionsModel.java b/config/src/main/java/org/switchyard/config/model/extensions/v1/V1ExtensionsModel.java new file mode 100644 index 000000000..d1440f12b --- /dev/null +++ b/config/src/main/java/org/switchyard/config/model/extensions/v1/V1ExtensionsModel.java @@ -0,0 +1,48 @@ +package org.switchyard.config.model.extensions.v1; + +import org.switchyard.config.Configuration; +import org.switchyard.config.model.BaseNamedModel; +import org.switchyard.config.model.Descriptor; +import org.switchyard.config.model.extensions.ExtensionsModel; +import org.switchyard.config.model.extensions.adapter.AdapterModel; + +public class V1ExtensionsModel extends BaseNamedModel implements ExtensionsModel { + + private AdapterModel _adapterModel; + + + public V1ExtensionsModel(Configuration config, Descriptor desc) { + super(config, desc); + + // only one adapter is supported + for (Configuration adater_config : config.getChildrenStartsWith(AdapterModel.ADAPTER)) { + AdapterModel adapter = (AdapterModel)readModel(adater_config); + if (adapter != null) { + _adapterModel = adapter; + break; + } + } + } + + @Override + public String getType() { + return EXTENSIONS; + } + + @Override + public AdapterModel getAdapterModel() { + return _adapterModel; + } + + @Override + public ExtensionsModel setAdapterModel(AdapterModel adapterModel) { + setChildModel(adapterModel); + _adapterModel = adapterModel; + return this; + } + + @Override + public boolean hasAdapterModel() { + return _adapterModel != null; + } +} diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java b/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java index d05a91e46..ef7bd8e2d 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/SwitchYardModel.java @@ -19,7 +19,6 @@ package org.switchyard.config.model.switchyard; import org.switchyard.config.model.NamedModel; -import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.CompositeModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.transform.TransformsModel; @@ -51,21 +50,6 @@ public interface SwitchYardModel extends NamedModel { */ public SwitchYardModel setComposite(CompositeModel composite); - /** - * Get the child adapters model. - * - * @return the child adapters model. - */ - public AdaptersModel getAdapters(); - - /** - * Sets the child adapters model. - * - * @param adapters the child adapters model. - * @return this SwitchYardModel (useful for chaining) - */ - public SwitchYardModel setAdapters(AdaptersModel adapters); - /** * Gets the child transforms model. * @return the child transforms model diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java index 0443351fd..0ae1d9702 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardMarshaller.java @@ -22,8 +22,6 @@ import org.switchyard.config.model.BaseMarshaller; import org.switchyard.config.model.Descriptor; import org.switchyard.config.model.Model; -import org.switchyard.config.model.adapter.AdaptersModel; -import org.switchyard.config.model.adapter.v1.V1AdaptersModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.domain.HandlerModel; import org.switchyard.config.model.domain.HandlersModel; @@ -79,8 +77,6 @@ public Model read(Configuration config) { Descriptor desc = getDescriptor(); if (name.equals(SwitchYardModel.SWITCHYARD)) { return new V1SwitchYardModel(config, desc); - } else if (name.equals(AdaptersModel.ADAPTERS)) { - return new V1AdaptersModel(config, desc); } else if (name.equals(TransformsModel.TRANSFORMS)) { return new V1TransformsModel(config, desc); } else if (name.equals(ValidatesModel.VALIDATES)) { diff --git a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java index 53f9cc4d7..8e41fa667 100644 --- a/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java +++ b/config/src/main/java/org/switchyard/config/model/switchyard/v1/V1SwitchYardModel.java @@ -26,7 +26,6 @@ import org.switchyard.config.Configuration; import org.switchyard.config.model.BaseNamedModel; import org.switchyard.config.model.Descriptor; -import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.CompositeModel; import org.switchyard.config.model.domain.DomainModel; import org.switchyard.config.model.property.PropertiesModel; @@ -44,7 +43,6 @@ public class V1SwitchYardModel extends BaseNamedModel implements SwitchYardModel private CompositeModel _composite; private ArtifactsModel _artifacts; - private AdaptersModel _adapters; private TransformsModel _transforms; private ValidatesModel _validates; private DomainModel _domain; @@ -90,27 +88,6 @@ public SwitchYardModel setComposite(CompositeModel composite) { return this; } - /** - * {@inheritDoc} - */ - @Override - public AdaptersModel getAdapters() { - if (_adapters == null) { - _adapters = (AdaptersModel) getFirstChildModelStartsWith(AdaptersModel.ADAPTERS); - } - return _adapters; - } - - /** - * {@inheritDoc} - */ - @Override - public SwitchYardModel setAdapters(AdaptersModel adapters) { - setChildModel(adapters); - _adapters = adapters; - return this; - } - /** * {@inheritDoc} */ diff --git a/config/src/main/resources/org/switchyard/config/model/descriptor.properties b/config/src/main/resources/org/switchyard/config/model/descriptor.properties index 1f4695a83..68706bf0b 100644 --- a/config/src/main/resources/org/switchyard/config/model/descriptor.properties +++ b/config/src/main/resources/org/switchyard/config/model/descriptor.properties @@ -24,3 +24,4 @@ sca.namespace=http://docs.oasis-open.org/ns/opencsa/sca/200912 sca.schema=sca-1.1-cd06.xsd sca.location=/org/oasis-open/docs/ns/opencsa/sca/200912/ sca.marshaller=org.switchyard.config.model.composite.v1.V1CompositeMarshaller + diff --git a/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd b/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd index 2dc3cda9d..fc12b3ddb 100644 --- a/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd +++ b/config/src/main/resources/org/switchyard/config/model/switchyard/v1/switchyard-v1.xsd @@ -30,7 +30,6 @@ MA 02110-1301, USA. - @@ -145,18 +144,12 @@ MA 02110-1301, USA. - - + + - + - - - - - - diff --git a/deploy/base/pom.xml b/deploy/base/pom.xml index 190deee5b..a39cf05ce 100644 --- a/deploy/base/pom.xml +++ b/deploy/base/pom.xml @@ -63,6 +63,11 @@ org.switchyard switchyard-bus-camel + + org.switchyard + switchyard-adapter + ${project.version} + log4j diff --git a/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java b/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java index 07ec82a06..f83728038 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/ServiceDomainManager.java @@ -28,7 +28,6 @@ import org.switchyard.ExchangeHandler; import org.switchyard.ServiceDomain; import org.switchyard.ServiceSecurity; -import org.switchyard.adapter.AdapterRegistry; import org.switchyard.bus.camel.CamelExchangeBus; import org.switchyard.common.camel.SwitchYardCamelContext; import org.switchyard.common.type.Classes; @@ -42,7 +41,6 @@ import org.switchyard.internal.DefaultServiceSecurity; import org.switchyard.internal.DomainImpl; import org.switchyard.internal.EventManager; -import org.switchyard.internal.adapter.BaseAdapterRegistry; import org.switchyard.internal.transform.BaseTransformerRegistry; import org.switchyard.internal.validate.BaseValidatorRegistry; import org.switchyard.spi.ServiceRegistry; @@ -102,7 +100,6 @@ public ServiceDomain createDomain() { */ public ServiceDomain createDomain(QName domainName, SwitchYardModel switchyardConfig) { ServiceSecurity serviceSecurity = getServiceSecurity(switchyardConfig); - AdapterRegistry adapterRegistry = new BaseAdapterRegistry(); TransformerRegistry transformerRegistry = new BaseTransformerRegistry(); ValidatorRegistry validatorRegistry = new BaseValidatorRegistry(); @@ -110,7 +107,7 @@ public ServiceDomain createDomain(QName domainName, SwitchYardModel switchyardCo CamelExchangeBus bus = new CamelExchangeBus(camelContext); DomainImpl domain = new DomainImpl( - domainName, serviceSecurity, _registry, bus, adapterRegistry, transformerRegistry, validatorRegistry, _eventManager); + domainName, serviceSecurity, _registry, bus, transformerRegistry, validatorRegistry, _eventManager); camelContext.setServiceDomain(domain); if (switchyardConfig != null) { diff --git a/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java b/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java index 9b9cee69c..208079f9c 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/internal/AbstractDeployment.java @@ -24,7 +24,6 @@ import javax.xml.namespace.QName; import org.switchyard.ServiceDomain; -import org.switchyard.adapter.AdapterRegistryLoader; import org.switchyard.common.type.Classes; import org.switchyard.config.model.switchyard.SwitchYardModel; import org.switchyard.deploy.Activator; @@ -55,10 +54,6 @@ public abstract class AbstractDeployment { * The Service Domain. */ private ServiceDomain _serviceDomain; - /** - * Adapter registry loaded for the deployment. - */ - private AdapterRegistryLoader _adapterRegistryLoader; /** * Transform registry loaded for the deployment. */ @@ -138,7 +133,6 @@ public final void init(ServiceDomain appServiceDomain, List activator _serviceDomain = appServiceDomain; _serviceDomain.getProperties().put(CLASSLOADER_PROPERTY, Classes.getTCCL()); - _adapterRegistryLoader = new AdapterRegistryLoader(appServiceDomain.getAdapterRegistry()); _transformerRegistryLoader = new TransformerRegistryLoader(appServiceDomain.getTransformerRegistry()); _transformerRegistryLoader.loadOOTBTransforms(); @@ -171,15 +165,6 @@ public ServiceDomain getDomain() { } } - /** - * Get the {@link AdapterRegistryLoader} acciciated with the deployment. - * - * @return The AdapterRegistryLoader instance. - */ - public AdapterRegistryLoader getAdapterRegistryLoader() { - return _adapterRegistryLoader; - } - /** * Get the {@link TransformerRegistryLoader} associated with the deployment. * @return The TransformerRegistryLoader instance. diff --git a/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java b/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java index 847e4fae2..117cd3ed6 100644 --- a/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java +++ b/deploy/base/src/main/java/org/switchyard/deploy/internal/Deployment.java @@ -34,9 +34,10 @@ import org.apache.log4j.Logger; import org.switchyard.Service; import org.switchyard.ServiceReference; +import org.switchyard.adapter.Adapter; +import org.switchyard.adapter.AdapterUtil; import org.switchyard.common.type.Classes; import org.switchyard.config.model.ModelPuller; -import org.switchyard.config.model.adapter.AdaptersModel; import org.switchyard.config.model.composite.BindingModel; import org.switchyard.config.model.composite.ComponentImplementationModel; import org.switchyard.config.model.composite.ComponentModel; @@ -46,6 +47,7 @@ import org.switchyard.config.model.composite.CompositeReferenceModel; import org.switchyard.config.model.composite.CompositeServiceModel; import org.switchyard.config.model.composite.InterfaceModel; +import org.switchyard.config.model.extensions.ExtensionsModel; import org.switchyard.config.model.switchyard.EsbInterfaceModel; import org.switchyard.config.model.switchyard.SwitchYardModel; import org.switchyard.config.model.transform.TransformsModel; @@ -111,7 +113,6 @@ public Deployment(SwitchYardModel configModel) { protected void doInit(List activators) { _log.debug("Initializing deployment " + getName()); // create a new domain and load transformer , validator and activator instances for lifecycle - registerAdapters(); registerTransformers(); registerValidators(); if (activators != null) { @@ -155,7 +156,7 @@ public void start() { } } - /** + /** * Stops the deployment. All services are unregistered and the appropriate * activators are triggered. */ @@ -247,12 +248,6 @@ private Activator findActivator(ComponentModel component) throws SwitchYardExcep return findActivator(component.getImplementation().getType()); } - private void registerAdapters() { - _log.debug("Registering configured Adapters for deployment " + getName()); - AdaptersModel adapters = getConfig().getAdapters(); - getAdapterRegistryLoader().registerAdapters(adapters); - } - private void registerTransformers() { _log.debug("Registering configured Transformers for deployment " + getName()); TransformsModel transforms = getConfig().getTransforms(); @@ -477,9 +472,11 @@ private void deployImplementations() { for (CompositeServiceModel compositeService : getConfig().getComposite().getServices()) { ComponentServiceModel componentService = compositeService.getComponentService(); if (componentService != null && componentService.equals(service)) { + svc.setAdapter(createAdapter(compositeService)); // avoid duplicates if (!service.getQName().equals(compositeService.getQName())) { validateServiceRegistration(compositeService.getQName()); + Service promotedService = getDomain().registerService( compositeService.getQName(), serviceIntf, handler); activation.addPromotion(promotedService); @@ -499,7 +496,17 @@ private void deployImplementations() { } } - private void deployServiceBindings() { + private Adapter createAdapter(CompositeServiceModel compositeService) { + if (compositeService == null) + return null; + ExtensionsModel extensions = compositeService.getExtensionsModel(); + if ((extensions != null) && extensions.hasAdapterModel()) { + return AdapterUtil.newAdapter(extensions.getAdapterModel()); + } + return null; + } + + private void deployServiceBindings() { _log.debug("Deploying service bindings for deployment " + getName()); // activate bindings for each service for (CompositeServiceModel service : getConfig().getComposite().getServices()) { diff --git a/pom.xml b/pom.xml index 60035c434..0be4b33dd 100644 --- a/pom.xml +++ b/pom.xml @@ -39,8 +39,7 @@ common/camel config api - adapter - addressing + serial/base serial/jackson