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 EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.codehaus.activemq.message.WireFormat;
24  import org.codehaus.activemq.transport.TransportServerChannelSupport;
25  import pyrasun.eio.EIOGlobalContext;
26  import pyrasun.eio.services.EmberServiceController;
27  import pyrasun.eio.services.EmberServiceException;
28  import pyrasun.eio.services.bytearray.ByteArrayServerClient;
29  import pyrasun.eio.services.bytearray.ByteArrayServerClientListener;
30  import pyrasun.eio.services.bytearray.ByteArrayServerListener;
31  
32  import javax.jms.JMSException;
33  import java.net.URI;
34  
35  /***
36   * An EmberIO (using NIO) implementation of TransportServerChannel
37   *
38   * @version $Revision: 1.12 $
39   */
40  public class EmberTransportServerChannel extends TransportServerChannelSupport implements ByteArrayServerListener, ByteArrayServerClientListener {
41  
42      private static final Log log = LogFactory.getLog(EmberTransportServerChannel.class);
43  
44      private WireFormat wireFormat;
45      private EIOGlobalContext context;
46      private EmberServiceController controller;
47      protected URI bindAddress;
48      private SynchronizedBoolean closed;
49      private SynchronizedBoolean started;
50  
51      public EmberTransportServerChannel(WireFormat wireFormat, URI bindAddr, EIOGlobalContext context, EmberServiceController controller) {
52          this.wireFormat = wireFormat;
53          this.bindAddress = bindAddr;
54          this.context = context;
55          this.controller = controller;
56          closed = new SynchronizedBoolean(false);
57          started = new SynchronizedBoolean(false);
58          log.info("ServerChannel at: " + bindAddress);
59      }
60  
61      /***
62       * close the ServerChannel
63       */
64      public void stop() {
65          if (closed.commit(false, true)) {
66              try {
67                  controller.stopAll();
68                  context.stop();
69              }
70              catch (EmberServiceException e) {
71                  log.error("Caught while closing: " + e, e);
72              }
73          }
74      }
75  
76      /***
77       * start listeneing for events
78       *
79       * @throws JMSException if an error occurs
80       */
81      public void start() throws JMSException {
82          super.start();
83          if (started.commit(false, true)) {
84              super.stop();
85              try {
86                  context.start();
87                  controller.startAll();
88              }
89              catch (EmberServiceException e) {
90                  JMSException jmsEx = new JMSException("Could not start EmberIOController: " + e);
91                  jmsEx.setLinkedException(e);
92                  throw jmsEx;
93              }
94          }
95      }
96  
97      /***
98       * @return pretty print of this
99       */
100     public String toString() {
101         return "EmberTransportServerChannel@" + bindAddress;
102     }
103 
104     protected void handleException(ByteArrayServerClient client, JMSException e) {
105         log.error("Could not create new TransportChannel for client: " + client, e);
106     }
107 
108     public void newClient(ByteArrayServerClient client) {
109         log.trace("New client received!");
110 
111         addClient(new EmberTransportChannel(wireFormat, null, null, client));
112     }
113 
114     public void clientClosed(ByteArrayServerClient client) {
115         log.info("Client has disconnected: " + client);
116         /*** TODO implement client closing! */
117         // listener.removeClient(channel);
118     }
119 
120     public void newMessage(ByteArrayServerClient byteArrayServerClient, Object msg) {
121         log.warn("New message received!: " + msg);
122     }
123 
124 }