HttpTransportChannelSupport xref

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.http;
    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.TextWireFormat;
    24  import org.codehaus.activemq.transport.TransportChannelSupport;
    25
    26  import javax.jms.JMSException;
    27
    28  /***
    29   * @version $Revision: 1.3 $
    30   */
    31  public abstract class HttpTransportChannelSupport extends TransportChannelSupport implements Runnable {
    32      private static final Log log = LogFactory.getLog(HttpTransportChannelSupport.class);
    33
    34      private TextWireFormat wireFormat;
    35      private String remoteUrl;
    36      private Thread thread; // should use pool
    37      private SynchronizedBoolean closed = new SynchronizedBoolean(false);
    38      private SynchronizedBoolean started = new SynchronizedBoolean(false);
    39
    40      public HttpTransportChannelSupport(TextWireFormat wireFormat, String remoteUrl) {
    41          this.wireFormat = wireFormat;
    42          this.remoteUrl = remoteUrl;
    43      }
    44
    45      public boolean isMulticast() {
    46          return false;
    47      }
    48
    49      public void start() throws JMSException {
    50          if (started.commit(false, true)) {
    51              if (getClientID() != null) {
    52                  startThread();
    53              }
    54          }
    55      }
    56
    57      protected void startThread() {
    58          thread = new Thread(this, toString());
    59          thread.start();
    60      }
    61
    62      public void stop() {
    63          if (closed.commit(false, true)) {
    64              super.stop();
    65          }
    66      }
    67
    68      public synchronized void setClientID(String clientID) {
    69          super.setClientID(clientID);
    70          if (clientID != null && thread == null && started.get()) {
    71              startThread();
    72          }
    73      }
    74
    75      public String toString() {
    76          return "HTTP Reader " + getRemoteUrl();
    77      }
    78
    79      /***
    80       * Can this wireformat process packets of this version
    81       * @param version the version number to test
    82       * @return true if can accept the version
    83       */
    84      public boolean canProcessWireFormatVersion(int version){
    85          return wireFormat.canProcessWireFormatVersion(version);
    86      }
    87
    88      /***
    89       * @return the current version of this wire format
    90       */
    91      public int getCurrentWireFormatVersion(){
    92          return wireFormat.getCurrentWireFormatVersion();
    93      }
    94
    95      // Properties
    96      //-------------------------------------------------------------------------
    97      public String getRemoteUrl() {
    98          return remoteUrl;
    99      }
    100
    101     public TextWireFormat getWireFormat() {
    102         return wireFormat;
    103     }
    104
    105     public void setWireFormat(TextWireFormat wireFormat) {
    106         this.wireFormat = wireFormat;
    107     }
    108
    109     public SynchronizedBoolean getClosed() {
    110         return closed;
    111     }
    112
    113     public SynchronizedBoolean getStarted() {
    114         return started;
    115     }
    116 }