View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.util.concurrent.atomic.AtomicReference;
22  
23  import org.eclipse.jetty.client.api.Result;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  
27  public abstract class HttpChannel
28  {
29      protected static final Logger LOG = Log.getLogger(HttpChannel.class);
30  
31      private final AtomicReference<HttpExchange> exchange = new AtomicReference<>();
32      private final HttpDestination destination;
33  
34      protected HttpChannel(HttpDestination destination)
35      {
36          this.destination = destination;
37      }
38  
39      public HttpDestination getHttpDestination()
40      {
41          return destination;
42      }
43  
44      public void associate(HttpExchange exchange)
45      {
46          if (this.exchange.compareAndSet(null, exchange))
47          {
48              exchange.associate(this);
49              if (LOG.isDebugEnabled())
50                  LOG.debug("{} associated to {}", exchange, this);
51          }
52          else
53          {
54              exchange.getRequest().abort(new UnsupportedOperationException("Pipelined requests not supported"));
55          }
56      }
57  
58      public HttpExchange disassociate()
59      {
60          HttpExchange exchange = this.exchange.getAndSet(null);
61          if (exchange != null)
62              exchange.disassociate(this);
63          if (LOG.isDebugEnabled())
64              LOG.debug("{} disassociated from {}", exchange, this);
65          return exchange;
66      }
67  
68      public HttpExchange getHttpExchange()
69      {
70          return exchange.get();
71      }
72  
73      public abstract void send();
74  
75      public abstract void proceed(HttpExchange exchange, Throwable failure);
76  
77      public abstract boolean abort(Throwable cause);
78  
79      public void exchangeTerminated(Result result)
80      {
81          disassociate();
82      }
83  
84      @Override
85      public String toString()
86      {
87          return String.format("%s@%h", getClass().getSimpleName(), this);
88      }
89  }