/** * * Copyright 2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.tuscany.binding.jms; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.apache.tuscany.spi.component.CompositeComponent; import org.apache.tuscany.spi.component.Reference; import org.apache.tuscany.spi.component.Service; import org.apache.tuscany.spi.deployer.DeploymentContext; import org.apache.tuscany.spi.extension.BindingBuilderExtension; import org.apache.tuscany.spi.model.BoundReferenceDefinition; import org.apache.tuscany.spi.model.BoundServiceDefinition; import commonj.sdo.helper.TypeHelper; /** * Builds a Service or Reference for an RMI binding. * * @version $Rev$ $Date$ */ public class JMSBindingBuilder extends BindingBuilderExtension { private static String DEFAULT_JMS_RESOURCE_FACTORY = "org.apache.tuscany.binding.jms.SimpleJMSResourceFactory"; private static String DEFAULT_OPERATION_SELECTOR = "org.apache.tuscany.binding.jms.DefaultOperationSelector"; protected Class getBindingType() { return JMSBinding.class; } @SuppressWarnings({"unchecked"}) public Service build(CompositeComponent parent, BoundServiceDefinition serviceDefinition, DeploymentContext deploymentContext) { JMSBinding jmsBinding = serviceDefinition.getBinding(); Class interfaze = serviceDefinition.getServiceContract().getInterfaceClass(); TypeHelper typeHelper = (TypeHelper) deploymentContext.getExtension(TypeHelper.class.getName()); if(typeHelper==null) typeHelper = TypeHelper.INSTANCE; JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding); OperationSelector opSec = getOperationSelector(jmsBinding); return new JMSService(serviceDefinition.getName(),parent, wireService, jmsBinding, jmsResourceFactory,opSec,interfaze,typeHelper); } @SuppressWarnings({"unchecked"}) public Reference build(CompositeComponent parent, BoundReferenceDefinition referenceDefinition, DeploymentContext deploymentContext) { String name = referenceDefinition.getName(); Class interfaze = referenceDefinition.getServiceContract().getInterfaceClass(); JMSBinding jmsBinding = referenceDefinition.getBinding(); JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding); OperationSelector opSec = getOperationSelector(jmsBinding); return new JMSReference(name,parent,wireService, jmsBinding, jmsResourceFactory,opSec,interfaze); } private JMSResourceFactory getJMSResourceFactory(JMSBinding jmsBinding) { String className = jmsBinding.getJmsResourceFactoryName(); if (className != null && !className.equals("")){ try { Class factoryClass = Class.forName(className != null ? className : DEFAULT_JMS_RESOURCE_FACTORY); Constructor constructor = factoryClass.getDeclaredConstructor(new Class[]{JMSBinding.class}); return (JMSResourceFactory) constructor.newInstance(jmsBinding); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return new SimpleJMSResourceFactory(jmsBinding); }else{ return new SimpleJMSResourceFactory(jmsBinding); } } private OperationSelector getOperationSelector(JMSBinding jmsBinding) { String className = jmsBinding.getOperationSelectorName(); if (className != null && !className.equals("")){ try { Class factoryClass = Class.forName(className != null ? className : DEFAULT_OPERATION_SELECTOR); Constructor constructor = factoryClass.getDeclaredConstructor(new Class[]{JMSBinding.class}); return (OperationSelector) constructor.newInstance(jmsBinding); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return new DefaultOperationSelector(jmsBinding); }else{ return new DefaultOperationSelector(jmsBinding); } } }