View Javadoc

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.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.codehaus.activemq.message.WireFormat;
23  import org.codehaus.activemq.transport.TransportChannel;
24  import org.codehaus.activemq.transport.TransportChannelFactorySupport;
25  import org.codehaus.activemq.util.JMSExceptionHelper;
26  import org.jgroups.Channel;
27  import org.jgroups.ChannelException;
28  import org.jgroups.ChannelFactory;
29  import org.jgroups.JChannelFactory;
30  
31  import javax.jms.JMSException;
32  import java.net.URI;
33  
34  /***
35   * A JGroups implementation of a TransportChannelFactory
36   *
37   * @version $Revision: 1.5 $
38   */
39  public class JGroupsTransportChannelFactory extends TransportChannelFactorySupport {
40      private static final Log log = LogFactory.getLog(JGroupsTransportChannelFactory.class);
41  
42      private ChannelFactory channelFactory = new JChannelFactory();
43      private Object channelConfiguration;
44      private String channelName = "ActiveMQ";
45  
46      public JGroupsTransportChannelFactory() {
47      }
48  
49      public JGroupsTransportChannelFactory(ChannelFactory channelFactory, Object channelConfiguration, String channelName) {
50          this.channelFactory = channelFactory;
51          this.channelConfiguration = channelConfiguration;
52          this.channelName = channelName;
53      }
54  
55      public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
56          try {
57              Channel channel = createChannel(remoteLocation);
58              channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
59              channel.connect(channelName);
60              return populateProperties(new JGroupsTransportChannel(wireFormat, channel, null), remoteLocation);
61          }
62          catch (ChannelException e) {
63              throw JMSExceptionHelper.newJMSException("Failed to construct JGroups Channel: " + e, e);
64          }
65      }
66  
67      public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
68          return create(wireFormat, remoteLocation);
69      }
70  
71      public boolean requiresEmbeddedBroker() {
72          return true;
73      }
74  
75      // Properties
76      //-------------------------------------------------------------------------
77  
78  
79      public ChannelFactory getChannelFactory() {
80          return channelFactory;
81      }
82  
83      public void setChannelFactory(ChannelFactory channelFactory) {
84          this.channelFactory = channelFactory;
85      }
86  
87      public Object getChannelConfiguration() {
88          return channelConfiguration;
89      }
90  
91      public void setChannelConfiguration(Object channelConfiguration) {
92          this.channelConfiguration = channelConfiguration;
93      }
94  
95      public String getChannelName() {
96          return channelName;
97      }
98  
99      public void setChannelName(String channelName) {
100         this.channelName = channelName;
101     }
102 
103     protected Channel createChannel(URI remoteLocation) throws ChannelException {
104         Object config = channelConfiguration;
105         if (config == null) {
106             // lets use the URI
107             String text = remoteLocation.getSchemeSpecificPart();
108             if (!text.equalsIgnoreCase("default")) {
109                 config = text;
110             }
111         }
112         log.info("Configuring JGroups with: " + config);
113         return channelFactory.createChannel(config);
114     }
115 }