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