1 17 18 19 package org.apache.catalina.valves; 20 21 22 import java.io.IOException ; 23 import java.util.concurrent.Semaphore ; 24 25 import javax.servlet.ServletException ; 26 27 import org.apache.catalina.Lifecycle; 28 import org.apache.catalina.LifecycleException; 29 import org.apache.catalina.LifecycleListener; 30 import org.apache.catalina.connector.Request; 31 import org.apache.catalina.connector.Response; 32 import org.apache.catalina.util.LifecycleSupport; 33 import org.apache.catalina.util.StringManager; 34 35 36 45 46 public class SemaphoreValve 47 extends ValveBase 48 implements Lifecycle { 49 50 51 53 54 57 private static final String info = 58 "org.apache.catalina.valves.SemaphoreValve/1.0"; 59 60 61 64 private StringManager sm = 65 StringManager.getManager(Constants.Package); 66 67 68 71 protected Semaphore semaphore = null; 72 73 74 77 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 78 79 80 83 private boolean started = false; 84 85 86 88 89 92 protected int concurrency = 10; 93 public int getConcurrency() { return concurrency; } 94 public void setConcurrency(int concurrency) { this.concurrency = concurrency; } 95 96 97 100 protected boolean fairness = false; 101 public boolean getFairness() { return fairness; } 102 public void setFairness(boolean fairness) { this.fairness = fairness; } 103 104 105 108 protected boolean block = true; 109 public boolean getBlock() { return block; } 110 public void setBlock(boolean block) { this.block = block; } 111 112 113 116 protected boolean interruptible = false; 117 public boolean getInterruptible() { return interruptible; } 118 public void setInterruptible(boolean interruptible) { this.interruptible = interruptible; } 119 120 121 123 124 129 public void addLifecycleListener(LifecycleListener listener) { 130 131 lifecycle.addLifecycleListener(listener); 132 133 } 134 135 136 140 public LifecycleListener[] findLifecycleListeners() { 141 142 return lifecycle.findLifecycleListeners(); 143 144 } 145 146 147 152 public void removeLifecycleListener(LifecycleListener listener) { 153 154 lifecycle.removeLifecycleListener(listener); 155 156 } 157 158 159 167 public void start() throws LifecycleException { 168 169 if (started) 171 throw new LifecycleException 172 (sm.getString("semaphoreValve.alreadyStarted")); 173 lifecycle.fireLifecycleEvent(START_EVENT, null); 174 started = true; 175 176 semaphore = new Semaphore (concurrency, fairness); 177 178 } 179 180 181 189 public void stop() throws LifecycleException { 190 191 if (!started) 193 throw new LifecycleException 194 (sm.getString("semaphoreValve.notStarted")); 195 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 196 started = false; 197 198 semaphore = null; 199 200 } 201 202 203 205 206 209 public String getInfo() { 210 return (info); 211 } 212 213 214 223 public void invoke(Request request, Response response) 224 throws IOException , ServletException { 225 226 if (controlConcurrency(request, response)) { 227 boolean shouldRelease = true; 228 try { 229 if (block) { 230 if (interruptible) { 231 try { 232 semaphore.acquire(); 233 } catch (InterruptedException e) { 234 shouldRelease = false; 235 permitDenied(request, response); 236 return; 237 } 238 } else { 239 semaphore.acquireUninterruptibly(); 240 } 241 } else { 242 if (!semaphore.tryAcquire()) { 243 shouldRelease = false; 244 permitDenied(request, response); 245 return; 246 } 247 } 248 getNext().invoke(request, response); 249 } finally { 250 if (shouldRelease) { 251 semaphore.release(); 252 } 253 } 254 } else { 255 getNext().invoke(request, response); 256 } 257 258 } 259 260 261 264 public boolean controlConcurrency(Request request, Response response) { 265 return true; 266 } 267 268 269 272 public void permitDenied(Request request, Response response) 273 throws IOException , ServletException { 274 } 275 276 277 } 278 | Popular Tags |