1 16 17 18 package org.jboss.web.tomcat.filters; 19 20 21 import java.io.IOException ; 22 23 import javax.servlet.ServletException ; 24 25 import org.apache.catalina.Lifecycle; 26 import org.apache.catalina.LifecycleException; 27 import org.apache.catalina.LifecycleListener; 28 import org.apache.catalina.connector.Request; 29 import org.apache.catalina.connector.Response; 30 import org.apache.catalina.util.LifecycleSupport; 31 import org.apache.catalina.util.StringManager; 32 import org.apache.catalina.valves.ValveBase; 33 34 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore; 35 import EDU.oswego.cs.dl.util.concurrent.Sync; 36 37 47 48 public class SemaphoreValve 49 extends ValveBase 50 implements Lifecycle { 51 52 53 55 56 59 private static final String info = 60 "org.jboss.web.tomcat.filters.SemaphoreValve/1.0"; 61 62 63 66 protected Sync semaphore = null; 67 68 69 72 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 73 74 75 78 private boolean started = false; 79 80 81 83 84 87 protected int concurrency = 10; 88 public int getConcurrency() { return concurrency; } 89 public void setConcurrency(int concurrency) { this.concurrency = concurrency; } 90 91 92 94 95 100 public void addLifecycleListener(LifecycleListener listener) { 101 102 lifecycle.addLifecycleListener(listener); 103 104 } 105 106 107 111 public LifecycleListener[] findLifecycleListeners() { 112 113 return lifecycle.findLifecycleListeners(); 114 115 } 116 117 118 123 public void removeLifecycleListener(LifecycleListener listener) { 124 125 lifecycle.removeLifecycleListener(listener); 126 127 } 128 129 130 138 public void start() throws LifecycleException { 139 140 if (started) 142 throw new LifecycleException 143 ("Semaphore valve already started"); 144 lifecycle.fireLifecycleEvent(START_EVENT, null); 145 started = true; 146 147 semaphore = new FIFOSemaphore(concurrency); 148 149 } 150 151 152 160 public void stop() throws LifecycleException { 161 162 if (!started) 164 throw new LifecycleException 165 ("Semaphore valve not started"); 166 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 167 started = false; 168 169 semaphore = null; 170 171 } 172 173 174 176 177 180 public String getInfo() { 181 return (info); 182 } 183 184 185 194 public void invoke(Request request, Response response) 195 throws IOException , ServletException { 196 197 boolean shouldRelease = false; 198 try { 199 semaphore.acquire(); 200 shouldRelease = true; 201 getNext().invoke(request, response); 203 } catch( java.lang.InterruptedException e) { 204 container.getLogger().error(e.getMessage(), e); 205 } finally { 206 if (shouldRelease) 207 semaphore.release(); 208 } 209 210 } 211 212 213 } 214 | Popular Tags |