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.ember;
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.TransportChannelFactory;
25  import org.codehaus.activemq.util.IdGenerator;
26  import pyrasun.eio.EIOGlobalContext;
27  import pyrasun.eio.EIOPoolingStrategy;
28  import pyrasun.eio.services.EmberServiceController;
29  import pyrasun.eio.services.EmberServiceException;
30  import pyrasun.eio.services.bytearray.ByteArrayClientService;
31  import pyrasun.eio.services.bytearray.ByteArrayServerClient;
32  
33  import javax.jms.JMSException;
34  import java.io.IOException;
35  import java.net.URI;
36  
37  /***
38   * An EmberIO (using NIO) implementation of a TransportChannelFactory
39   *
40   * @version $Revision: 1.8 $
41   */
42  public class EmberTransportChannelFactory extends EmberSupport implements TransportChannelFactory {
43  
44      protected static final Log log = LogFactory.getLog(EmberTransportChannelFactory.class);
45  
46      private IdGenerator idGenerator = new IdGenerator();
47  
48      public EmberTransportChannelFactory() {
49      }
50  
51      public EmberTransportChannelFactory(EIOGlobalContext context, EIOPoolingStrategy ioPoolingStrategy) {
52          super(context, ioPoolingStrategy);
53      }
54  
55      /***
56       * Create a Channel to a remote Node - e.g. a Broker
57       *
58       * @param wireFormat
59       * @param remoteLocation
60       * @return the TransportChannel bound to the remote node
61       * @throws JMSException
62       */
63      public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
64          try {
65              String id = idGenerator.generateId();
66              EmberServiceController controller = getController();
67  
68              ByteArrayServerClient client = createClient(controller, remoteLocation, id);
69              return new EmberTransportChannel(wireFormat, getContext(), controller, client);
70          }
71          catch (IOException ioe) {
72              JMSException jmsEx = new JMSException("Initialization of TransportChannel failed: " + ioe);
73              jmsEx.setLinkedException(ioe);
74              throw jmsEx;
75          }
76          catch (EmberServiceException e) {
77              JMSException jmsEx = new JMSException("Initialization of TransportChannel failed: " + e);
78              jmsEx.setLinkedException(e);
79              throw jmsEx;
80          }
81      }
82  
83      /***
84       * Create a Channel to a remote Node - e.g. a Broker
85       *
86       * @param wireFormat
87       * @param remoteLocation
88       * @param localLocation  -
89       *                       e.g. local InetAddress and local port
90       * @return the TransportChannel bound to the remote node
91       * @throws JMSException
92       */
93      public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
94          return create(wireFormat, remoteLocation);
95      }
96  
97      public boolean requiresEmbeddedBroker() {
98          return false;
99      }
100 
101     /***
102      * @param remoteLocation
103      * @param id
104      * @return @throws
105      *         JMSException
106      * @throws EmberServiceException
107      * @throws IOException
108      */
109     protected ByteArrayServerClient createClient(EmberServiceController controller, URI remoteLocation, String id) throws JMSException,
110             EmberServiceException, IOException {
111         ByteArrayClientService service = createNioService(controller);
112         ByteArrayServerClient client = service.createClient(remoteLocation.getHost(), remoteLocation.getPort(), id, null);
113         return client;
114     }
115 
116     /***
117      * Factory method to create a new ObjectClientService
118      *
119      * @throws JMSException if it could not be created
120      */
121     protected ByteArrayClientService createNioService(EmberServiceController controller) throws JMSException {
122         ByteArrayClientService service;
123         try {
124             service = new ByteArrayClientService(getContext(), getIoPoolingStrategy());
125             controller.addService(service);
126         }
127         catch (IOException e) {
128             throw createJMSException("Creation of NIO service failed: ", e);
129         }
130         return service;
131     }
132 
133 }