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              LOG.debug("{} associated to {}", exchange, this);
50          }
51          else
52          {
53              exchange.getRequest().abort(new UnsupportedOperationException("Pipelined requests not supported"));
54          }
55      }
56  
57      public HttpExchange disassociate()
58      {
59          HttpExchange exchange = this.exchange.getAndSet(null);
60          if (exchange != null)
61              exchange.disassociate(this);
62          LOG.debug("{} disassociated from {}", exchange, this);
63          return exchange;
64      }
65  
66      public HttpExchange getHttpExchange()
67      {
68          return exchange.get();
69      }
70  
71      public abstract void send();
72  
73      public abstract void proceed(HttpExchange exchange, Throwable failure);
74  
75      public abstract boolean abort(Throwable cause);
76  
77      public void exchangeTerminated(Result result)
78      {
79          disassociate();
80      }
81  
82      @Override
83      public String toString()
84      {
85          return String.format("%s@%h", getClass().getSimpleName(), this);
86      }
87  }