summaryrefslogtreecommitdiffstats
path: root/sandbox/old/contrib/binding-jms/src/main/java/org/apache/tuscany/binding/jms/JMSBindingBuilder.java
blob: 6b38743b2063873d693a27bf2edd1144efbe4f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 *
 * 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 javax.jms.Destination;
import javax.naming.NamingException;

import org.apache.tuscany.spi.component.CompositeComponent;
import org.apache.tuscany.spi.component.ServiceBinding;
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 org.apache.tuscany.spi.model.ServiceContract;

import org.apache.axiom.om.OMElement;
import org.apache.tuscany.idl.wsdl.WSDLServiceContract;

/**
 * Builds a Service or Reference for JMS binding.
 *
 * @version $Rev: 449970 $ $Date: 2006-09-26 06:05:35 -0400 (Tue, 26 Sep 2006) $
 */

public class JMSBindingBuilder extends BindingBuilderExtension<JMSBindingDefinition> {

    private static final String DEFAULT_JMS_RESOURCE_FACTORY =
        "org.apache.tuscany.binding.jms.SimpleJMSResourceFactory";

    private static final String OM_DATA_BINDING = OMElement.class.getName();

    protected Class<JMSBindingDefinition> getBindingType() {
        return JMSBindingDefinition.class;
    }

    public ServiceBinding build(CompositeComponent parent,
                                BoundServiceDefinition serviceDefinition,
                                JMSBindingDefinition jmsBinding,
                                DeploymentContext deploymentContext) {

        Class<?> interfaze = serviceDefinition.getServiceContract().getInterfaceClass();

        ServiceContract serviceContract = serviceDefinition.getServiceContract();
        jmsBinding.setXMLFormat(serviceContract instanceof WSDLServiceContract);

        JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding);

        if (serviceContract instanceof WSDLServiceContract) {
            serviceContract.setDataBinding(OM_DATA_BINDING);
        }

        OperationAndDataBinding requestODB =
            getRequestOperationAndDatabinding(jmsBinding, deploymentContext.getClassLoader());
        OperationAndDataBinding responseODB =
            getRequestOperationAndDatabinding(jmsBinding, deploymentContext.getClassLoader());

        return new JMSServiceBinding(serviceDefinition.getName(), parent, jmsBinding, jmsResourceFactory,
            serviceContract, requestODB, responseODB, interfaze);
    }

    public JMSReferenceBinding build(CompositeComponent parent,
                                     BoundReferenceDefinition referenceDefinition,
                                     JMSBindingDefinition jmsBinding,
                                     DeploymentContext deploymentContext) {

        String name = referenceDefinition.getName();
        ServiceContract serviceContract;
        try {
            serviceContract = (ServiceContract) referenceDefinition.getServiceContract().clone();
        } catch (CloneNotSupportedException e) {
            throw new JMSBindingException("Couldn't clone the Service Contract", e);
        }
        serviceContract.setDataBinding(OM_DATA_BINDING);

        JMSResourceFactory jmsResourceFactory = getJMSResourceFactory(jmsBinding);

        Destination requestDest;
        Destination replyDest = null;
        try {
            requestDest = jmsResourceFactory.lookupDestination(jmsBinding.getDestinationName());
            if (jmsBinding.getResponseDestinationName() != null) {
                replyDest = jmsResourceFactory.lookupDestination(jmsBinding.getResponseDestinationName());
            }
        } catch (NamingException e) {
            throw new JMSBindingException(e);
        }

        OperationAndDataBinding requestODB =
            getRequestOperationAndDatabinding(jmsBinding, deploymentContext.getClassLoader());
        OperationAndDataBinding responseODB =
            getRequestOperationAndDatabinding(jmsBinding, deploymentContext.getClassLoader());

        return new JMSReferenceBinding(name, parent, jmsBinding, jmsResourceFactory, serviceContract, requestODB, responseODB,
            requestDest, replyDest);

    }

    private JMSResourceFactory getJMSResourceFactory(JMSBindingDefinition 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[]{JMSBindingDefinition.class});
                return (JMSResourceFactory) constructor.newInstance(jmsBinding);
            } catch (ClassNotFoundException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (SecurityException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (NoSuchMethodException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (IllegalArgumentException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (InstantiationException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (IllegalAccessException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            } catch (InvocationTargetException e) {
                throw new JMSBindingException("Error loading the JMSResourceFactory", e);
            }
        } else {
            return new SimpleJMSResourceFactory(jmsBinding);
        }

    }

    protected OperationAndDataBinding getRequestOperationAndDatabinding(JMSBindingDefinition jmsBinding,
                                                                        ClassLoader cl) {
        String className = jmsBinding.getRequestOperationAndDatabindingName();
        OperationAndDataBinding operationAndDataBinding = instantiateClass(jmsBinding, cl, className);
        return operationAndDataBinding;
    }

    protected OperationAndDataBinding getResponseOperationAndDatabinding(JMSBindingDefinition jmsBinding,
                                                                         ClassLoader cl) {
        String className = jmsBinding.getResponseOperationAndDatabindingName();
        OperationAndDataBinding operationAndDataBinding = instantiateClass(jmsBinding, cl, className);
        return operationAndDataBinding;
    }

    protected OperationAndDataBinding instantiateClass(JMSBindingDefinition jmsBinding, ClassLoader cl,
                                                       String className) {
        OperationAndDataBinding operationAndDataBinding;
        if (cl == null) {
            cl = this.getClass().getClassLoader();
        }
        try {
            Class clazz;
            try {
                clazz = cl.loadClass(className);
            } catch (ClassNotFoundException e) {
                clazz = this.getClass().getClassLoader().loadClass(className);
            }
            Constructor constructor = clazz.getDeclaredConstructor(new Class[]{JMSBindingDefinition.class});
            operationAndDataBinding = (OperationAndDataBinding) constructor.newInstance(jmsBinding);

        } catch (Throwable e) {
            throw new JMSBindingException("Exception instantiating OperationAndDataBinding class", e);
        }
        return operationAndDataBinding;
    }
}