KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ActiveMQSslConnectionFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq;
20
21 import org.apache.activemq.transport.Transport;
22 import org.apache.activemq.transport.TransportFactory;
23 import org.apache.activemq.transport.tcp.SslTransportFactory;
24 import org.apache.activemq.util.JMSExceptionSupport;
25
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.security.KeyManagementException JavaDoc;
29 import java.security.NoSuchAlgorithmException JavaDoc;
30 import java.security.SecureRandom JavaDoc;
31
32 import javax.jms.JMSException JavaDoc;
33 import javax.net.ssl.KeyManager;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManager;
36
37 /**
38  * An ActiveMQConnectionFactory that allows access to the key and trust managers used for SslConnections.
39  *
40  * There is no reason to use this class unless SSL is being used AND the key and trust managers need to be specified
41  * from within code. In fact, if the URI passed to this class does not have an "ssl" scheme, this class will
42  * pass all work on to its superclass.
43  *
44  * @author sepandm@gmail.com
45  *
46  */

47 public class ActiveMQSslConnectionFactory extends ActiveMQConnectionFactory {
48     // The key and trust managers used to initialize the used SSLContext.
49
protected KeyManager[] keyManager = null;
50     protected TrustManager[] trustManager = null;
51     protected SecureRandom JavaDoc secureRandom = null;
52     
53     /**
54      * Sets the key and trust managers used when creating SSL connections.
55      *
56      * @param km The KeyManagers used.
57      * @param tm The TrustManagers used.
58      * @param random The SecureRandom number used.
59      */

60     public void setKeyAndTrustManagers(KeyManager[] km, TrustManager[] tm, SecureRandom JavaDoc random) {
61         keyManager = km;
62         trustManager = tm;
63         secureRandom = random;
64     }
65     
66     /**
67      * Overriding to make special considerations for SSL connections.
68      *
69      * If we are not using SSL, the superclass's method is called.
70      * If we are using SSL, an SslConnectionFactory is used and it is given the
71      * needed key and trust managers.
72      *
73      * @author sepandm@gmail.com
74      */

75     protected Transport createTransport() throws JMSException JavaDoc {
76         // If the given URI is non-ssl, let superclass handle it.
77
if (!brokerURL.getScheme().equals("ssl")) {
78             return super.createTransport();
79         }
80         
81         try {
82             SslTransportFactory sslFactory = new SslTransportFactory();
83             sslFactory.setKeyAndTrustManagers(keyManager, trustManager, secureRandom);
84             return sslFactory.doConnect(brokerURL);
85         } catch (Exception JavaDoc e) {
86             throw JMSExceptionSupport.create("Could not create Transport. Reason: " + e, e);
87         }
88     }
89 }
90
Popular Tags