summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/jms/JMSOutTransportInfo.java
blob: 5fa6542eeca52242442fa408381574045471589f (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.sca.binding.ws.axis2.jms;

import java.util.Hashtable;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;

import org.apache.axis2.transport.OutTransportInfo;
import org.apache.axis2.transport.jms.JMSConstants;
import org.apache.axis2.transport.jms.JMSUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The JMS OutTransportInfo
 */
public class JMSOutTransportInfo implements OutTransportInfo {

    private static final Log log = LogFactory.getLog(JMSOutTransportInfo.class);

    private ConnectionFactory connectionFactory = null;
    private String connectionFactoryUser = null;
    private String connectionFactoryPassword = null;
    private Destination destination = null;

    private String contentType = null;

    /**
     * Creates an instance using the given connection factory and destination
     *
     * @param connectionFactory the connection factory
     * @param dest              the destination
     */
    JMSOutTransportInfo(ConnectionFactory connectionFactory, Destination dest) {
        this.connectionFactory = connectionFactory;
        this.destination = dest;
    }

    /**
     * Creates an instance using the given connection factory and destination
     *
     * @param connectionFactory the connection factory
     * @param dest              the destination
     */
    JMSOutTransportInfo(ConnectionFactory connectionFactory, String connectionFactoryUser, String connectionFactoryPassword, Destination dest) {
        this.connectionFactory = connectionFactory;
        this.connectionFactoryUser = connectionFactoryUser;
        this.connectionFactoryPassword = connectionFactoryPassword;
        this.destination = dest;
    }

    /**
     * Creates and instance using the given URL
     *
     * @param url the URL
     */
    JMSOutTransportInfo(String url) {
        if (!url.startsWith(JMSConstants.JMS_PREFIX)) {
            handleException("Invalid JMS URL : " + url +
                    " Must begin with the prefix " + JMSConstants.JMS_PREFIX);
        } else {
            Context context = null;
            Hashtable props = JMSUtils.getProperties(url);
            try {
                context = new InitialContext(props);
            } catch (NamingException e) {
                handleException("Could not get the initial context", e);
            }

            connectionFactory = getConnectionFactory(context, props);
            connectionFactoryUser = getConnectionFactoryUser(context, props);
            connectionFactoryPassword = getConnectionFactoryPass(context, props);
            destination = getDestination(context, url);
        }
    }

    /**
     * Get the referenced ConnectionFactory using the properties from the context
     *
     * @param context the context to use for lookup
     * @param props   the properties which contains the JNDI name of the factory
     * @return the connection factory
     */
    private ConnectionFactory getConnectionFactory(Context context, Hashtable props) {
        try {

            String conFacJndiName = (String) props.get(JMSConstants.CONFAC_JNDI_NAME_PARAM);
            if (conFacJndiName != null) {
                return (ConnectionFactory) context.lookup(conFacJndiName);
            } else {
                throw new NamingException(
                        "JMS Connection Factory JNDI name cannot be determined from url");
            }
        } catch (NamingException e) {
            handleException("Cannot get JMS Connection factory with props : " + props, e);
        }
        return null;
    }

    /**
     * Get the referenced ConnectionFactory Username (if supplied) using the properties from the context
     *
     * @param context the context to use for lookup
     * @param props   the properties which contains the JNDI name of the factory username
     * @return the connection factory username (or null if one is not in the JNDI tree)
     */
    private String getConnectionFactoryUser(Context context, Hashtable props) {
        try {

            String conFacJndiUser = (String) props.get(JMSConstants.CONFAC_JNDI_NAME_USER);
            if (conFacJndiUser != null) {
                return (String) context.lookup(conFacJndiUser);
            } else {
                return null;
            }
        } catch (NamingException e) {
            handleException("Cannot get JMS Connection factory username with props : " + props, e);
        }
        return null;
    }

    /**
     * Get the referenced ConnectionFactory Password (if supplied) using the properties from the context
     *
     * @param context the context to use for lookup
     * @param props   the properties which contains the JNDI name of the factory password
     * @return the connection factory password (or null if one is not in the JNDI tree)
     */
    private String getConnectionFactoryPass(Context context, Hashtable props) {
        try {

            String conFacJndiPass = (String) props.get(JMSConstants.CONFAC_JNDI_NAME_PASS);
            if (conFacJndiPass != null) {
                return (String) context.lookup(conFacJndiPass);
            } else {
                return null;
            }
        } catch (NamingException e) {
            handleException("Cannot get JMS Connection factory password with props : " + props, e);
        }
        return null;
    }

    /**
     * Get the JMS destination specified by the given URL from the context
     *
     * @param context the Context to lookup
     * @param url     URL
     * @return the JMS destination, or null if it does not exist
     */
    private Destination getDestination(Context context, String url) {
        String destinationName = JMSUtils.getDestination(url);
        try {
            return (Destination) context.lookup(destinationName);

        } catch (NameNotFoundException e) {
            log.warn("Cannot get or lookup JMS destination : " + destinationName +
                    " from url : " + url + " : " + e.getMessage());

        } catch (NamingException e) {
            handleException("Cannot get JMS destination : " + destinationName +
                    " from url : " + url, e);
        }
        return null;
    }


    private void handleException(String s) {
        log.error(s);
        throw new AxisJMSException(s);
    }

    private void handleException(String s, Exception e) {
        log.error(s, e);
        throw new AxisJMSException(s, e);
    }

    public Destination getDestination() {
        return destination;
    }

    public ConnectionFactory getConnectionFactory() {
        return connectionFactory;
    }

    public String getConnectionFactoryPassword() {
                return connectionFactoryPassword;
        }

        public String getConnectionFactoryUser() {
                return connectionFactoryUser;
        }

        public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}