1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.transport.jgroups;
19
20 import org.codehaus.activemq.message.WireFormat;
21 import org.codehaus.activemq.transport.TransportChannel;
22 import org.codehaus.activemq.transport.TransportChannelFactory;
23 import org.codehaus.activemq.util.JMSExceptionHelper;
24 import org.jgroups.Channel;
25 import org.jgroups.ChannelException;
26 import org.jgroups.ChannelFactory;
27 import org.jgroups.JChannelFactory;
28
29 import javax.jms.JMSException;
30 import java.net.URI;
31
32 /***
33 * A JGroups implementation of a TransportChannelFactory
34 *
35 * @version $Revision: 1.3 $
36 */
37 public class JGroupsTransportChannelFactory implements TransportChannelFactory {
38
39 private ChannelFactory channelFactory = new JChannelFactory();
40 private Object channelConfiguration;
41 private String channelName = "ActiveMQ";
42
43 public JGroupsTransportChannelFactory() {
44 }
45
46 public JGroupsTransportChannelFactory(ChannelFactory channelFactory, Object channelConfiguration, String channelName) {
47 this.channelFactory = channelFactory;
48 this.channelConfiguration = channelConfiguration;
49 this.channelName = channelName;
50 }
51
52 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
53 try {
54 Channel channel = createChannel(remoteLocation);
55 channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
56 channel.connect(channelName);
57 return new JGroupsTransportChannel(wireFormat, channel, null);
58 }
59 catch (ChannelException e) {
60 throw JMSExceptionHelper.newJMSException("Failed to construct JGroups Channel: " + e, e);
61 }
62 }
63
64 public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
65 return create(wireFormat, remoteLocation);
66 }
67
68 public boolean requiresEmbeddedBroker() {
69 return true;
70 }
71
72
73
74 public ChannelFactory getChannelFactory() {
75 return channelFactory;
76 }
77
78 public void setChannelFactory(ChannelFactory channelFactory) {
79 this.channelFactory = channelFactory;
80 }
81
82 public Object getChannelConfiguration() {
83 return channelConfiguration;
84 }
85
86 public void setChannelConfiguration(Object channelConfiguration) {
87 this.channelConfiguration = channelConfiguration;
88 }
89
90 public String getChannelName() {
91 return channelName;
92 }
93
94 public void setChannelName(String channelName) {
95 this.channelName = channelName;
96 }
97
98 protected Channel createChannel(URI remoteLocation) throws ChannelException {
99 Object config = channelConfiguration;
100 if (config == null) {
101
102 String text = remoteLocation.getSchemeSpecificPart();
103 if (!text.equalsIgnoreCase("default")) {
104 config = text;
105 }
106 }
107 System.out.println("Configuring JGroups with: " + config);
108 return channelFactory.createChannel(channelConfiguration);
109 }
110 }