Statistics
| Branch: | Revision:

root / extensions / httpnio / src / main / java / org / jclouds / http / pool / HttpCommandConnectionHandle.java @ 35e7942d

History | View | Annotate | Download (2.8 kB)

1
/**
2
 *
3
 * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
4
 *
5
 * ====================================================================
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 * ====================================================================
18
 */
19
package org.jclouds.http.pool;
20

    
21
import java.io.IOException;
22
import java.net.URI;
23
import java.util.concurrent.BlockingQueue;
24
import java.util.concurrent.Semaphore;
25

    
26
import org.jclouds.http.HttpCommandRendezvous;
27

    
28
/**
29
 * Associates a command with an open connection to a service.
30
 * 
31
 * @author Adrian Cole
32
 */
33
public abstract class HttpCommandConnectionHandle<C> {
34
   protected final BlockingQueue<C> available;
35
   protected final Semaphore maxConnections;
36
   protected final Semaphore completed;
37
   protected final URI endPoint;
38
   protected C conn;
39
   protected HttpCommandRendezvous<?> command;
40

    
41
   public HttpCommandConnectionHandle(Semaphore maxConnections, BlockingQueue<C> available,
42
            URI endPoint, HttpCommandRendezvous<?> command, C conn) throws InterruptedException {
43
      this.available = available;
44
      this.maxConnections = maxConnections;
45
      this.completed = new Semaphore(1);
46
      this.endPoint = endPoint;
47
      this.command = command;
48
      this.conn = conn;
49
      completed.acquire();
50
   }
51

    
52
   public HttpCommandRendezvous<?> getCommandRendezvous() {
53
      return command;
54
   }
55

    
56
   public abstract void startConnection();
57

    
58
   public boolean isCompleted() {
59
      return (completed.availablePermits() == 1);
60
   }
61

    
62
   public void release() throws InterruptedException {
63
      if (isCompleted() || alreadyReleased()) {
64
         return;
65
      }
66
      available.put(conn);
67
      conn = null;
68
      command = null;
69
      completed.release();
70
   }
71

    
72
   private boolean alreadyReleased() {
73
      return conn == null;
74
   }
75

    
76
   public void cancel() throws IOException {
77
      if (isCompleted()) {
78
         return;
79
      }
80
      if (conn != null) {
81
         try {
82
            shutdownConnection();
83
         } finally {
84
            conn = null;
85
            command = null;
86
            maxConnections.release();
87
         }
88
      }
89
      completed.release();
90
   }
91

    
92
   public abstract void shutdownConnection() throws IOException;
93

    
94
   public void waitFor() throws InterruptedException {
95
      completed.acquire();
96
      completed.release();
97
   }
98
}