KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > SslBrokerService


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.broker;
20
21 import org.apache.activemq.transport.TransportFactory;
22 import org.apache.activemq.transport.TransportServer;
23 import org.apache.activemq.transport.tcp.SslTransportFactory;
24
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.security.KeyManagementException JavaDoc;
28 import java.security.SecureRandom JavaDoc;
29
30 import javax.net.ssl.KeyManager;
31 import javax.net.ssl.TrustManager;
32
33 /**
34  * A BrokerService that allows access to the key and trust managers used by SSL connections.
35  *
36  * There is no reason to use this class unless SSL is being used AND the key and trust managers need to be specified
37  * from within code. In fact, if the URI passed to this class does not have an "ssl" scheme, this class will
38  * pass all work on to its superclass.
39  *
40  * @author sepandm@gmail.com (Sepand)
41  */

42 public class SslBrokerService extends BrokerService {
43     /**
44      * Adds a new transport connector for the given bind address.
45      *
46      * If the transport created uses SSL, it will also use the key and trust
47      * managers provided. Otherwise, this is the same as calling
48      * addConnector.
49      *
50      * @param bindAddress The address to bind to.
51      * @param km The KeyManager to be used.
52      * @param tm The trustmanager to be used.
53      * @param random The source of randomness for the generator.
54      * @return the newly connected and added transport connector.
55      * @throws Exception
56      */

57     
58     public TransportConnector addSslConnector(
59             String JavaDoc bindAddress,
60             KeyManager[] km,
61             TrustManager[] tm,
62             SecureRandom JavaDoc random) throws Exception JavaDoc {
63         return addSslConnector( new URI JavaDoc(bindAddress), km, tm, random );
64     }
65
66     /**
67      * Adds a new transport connector for the given bind address.
68      *
69      * If the transport created uses SSL, it will also use the key and trust
70      * managers provided. Otherwise, this is the same as calling
71      * addConnector.
72      *
73      * @param bindAddress The URI to bind to.
74      * @param km The KeyManager to be used.
75      * @param tm The trustmanager to be used.
76      * @param random The source of randomness for the generator.
77      * @return the newly created and added transport connector.
78      * @throws Exception
79      */

80     public TransportConnector addSslConnector(
81             URI JavaDoc bindAddress,
82             KeyManager[] km,
83             TrustManager[] tm,
84             SecureRandom JavaDoc random) throws Exception JavaDoc {
85         return addConnector(createSslTransportServer(bindAddress, km, tm, random));
86     }
87     
88     /**
89      * Creates a TransportServer that uses the given key and trust managers.
90      *
91      * The last three parameters will be eventually passed to SSLContext.init.
92      *
93      * @param brokerURI The URI to bind to.
94      * @param km The KeyManager to be used.
95      * @param tm The trustmanager to be used.
96      * @param random The source of randomness for the generator.
97      * @return A new TransportServer that uses the given managers.
98      * @throws IOException If cannot handle URI.
99      * @throws KeyManagementException Passed on from SSL.
100      */

101     protected TransportServer createSslTransportServer(
102             URI JavaDoc brokerURI,
103             KeyManager[] km,
104             TrustManager[] tm,
105             SecureRandom JavaDoc random) throws IOException JavaDoc, KeyManagementException JavaDoc {
106
107         if (brokerURI.getScheme().equals("ssl")) {
108             // If given an SSL URI, use an SSL TransportFactory and configure
109
// it to use the given key and trust managers.
110
SslTransportFactory transportFactory = new SslTransportFactory();
111             transportFactory.setKeyAndTrustManagers(km, tm, random);
112             
113             return transportFactory.doBind(getBrokerName(),brokerURI);
114         } else {
115             // Else, business as usual.
116
return TransportFactory.bind(getBrokerName(), brokerURI);
117         }
118     }
119 }
120
Popular Tags