| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.pack; |
| 16 | |
| 17 | import java.io.IOException; |
| 18 | import java.sql.DriverManager; |
| 19 | import java.sql.SQLException; |
| 20 | |
| 21 | import javax.servlet.ServletException; |
| 22 | import javax.servlet.http.HttpServletRequest; |
| 23 | import javax.servlet.http.HttpServletResponse; |
| 24 | |
| 25 | import org.mortbay.jetty.Handler; |
| 26 | import org.mortbay.jetty.handler.ContextHandler; |
| 27 | import org.mortbay.jetty.handler.HandlerWrapper; |
| 28 | import org.mortbay.log.Log; |
| 29 | |
| 30 | /** |
| 31 | * @author Jean-Francois Poilpret |
| 32 | */ |
| 33 | public class HiveBoardPackHandler extends HandlerWrapper |
| 34 | { |
| 35 | protected void doStart() throws Exception |
| 36 | { |
| 37 | super.doStart(); |
| 38 | _pendingRequests = 0; |
| 39 | } |
| 40 | |
| 41 | protected void doStop() throws Exception |
| 42 | { |
| 43 | super.doStop(); |
| 44 | // Cleanly stop embedded Derby DBMS |
| 45 | try |
| 46 | { |
| 47 | Log.info("Stopping Derby engine..."); |
| 48 | DriverManager.getConnection("jdbc:derby:;shutdown=true"); |
| 49 | Log.warn("Derby engine was not stopped!"); |
| 50 | } |
| 51 | catch (SQLException e) |
| 52 | { |
| 53 | // Just ignore it: Derby shutdown always throws SQLException |
| 54 | Log.info("Derby engine stopped"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public void setShutdown(boolean shutdown) |
| 59 | { |
| 60 | Handler handler = getHandler(); |
| 61 | if (handler instanceof ContextHandler) |
| 62 | { |
| 63 | ((ContextHandler) handler).setShutdown(shutdown); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public void handle( String target, |
| 68 | HttpServletRequest request, |
| 69 | HttpServletResponse response, |
| 70 | int dispatch) |
| 71 | throws IOException, ServletException |
| 72 | { |
| 73 | try |
| 74 | { |
| 75 | _pendingRequests++; |
| 76 | super.handle(target, request, response, dispatch); |
| 77 | } |
| 78 | finally |
| 79 | { |
| 80 | _pendingRequests--; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public int getPendingRequests() |
| 85 | { |
| 86 | return (isStarted() ? _pendingRequests : 0); |
| 87 | } |
| 88 | |
| 89 | private int _pendingRequests = 0; |
| 90 | } |