Coverage Report - org.apache.ojb.broker.locking.LockManagerServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
LockManagerServlet
N/A
N/A
7
 
 1  
 package org.apache.ojb.broker.locking;
 2  
 
 3  
 /* Copyright 2004-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 
 19  
 import javax.servlet.ServletException;
 20  
 import javax.servlet.ServletConfig;
 21  
 import javax.servlet.http.HttpServlet;
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 import javax.servlet.http.HttpServletResponse;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.ObjectInputStream;
 27  
 import java.io.ObjectOutputStream;
 28  
 import java.io.PrintWriter;
 29  
 
 30  
 import org.apache.commons.lang.math.NumberUtils;
 31  
 import org.apache.ojb.broker.util.ClassHelper;
 32  
 
 33  
 
 34  
 /**
 35  
  * @author Thomas Mahler
 36  
  */
 37  
 public class LockManagerServlet extends HttpServlet
 38  
 {
 39  
     protected static LockManager lockmanager;
 40  
     static final String STR_LOCK_TIMEOUT = "lockTimeout";
 41  
     static final String STR_BLOCK_TIMEOUT = "blockTimeout";
 42  
     static final String STR_LOCK_MANAGER = "lockManager";
 43  
 
 44  
     private static long numRequests;
 45  
     private static Throwable lastError = null;
 46  
 
 47  
     public void init(ServletConfig servletConfig) throws ServletException
 48  
     {
 49  
         super.init(servletConfig);
 50  
         // if lock manager was instantiated not yet
 51  
         if(lockmanager == null)
 52  
         {
 53  
             lastError = null;
 54  
             numRequests = 0;
 55  
             String strLockManager = servletConfig.getInitParameter(STR_LOCK_MANAGER);
 56  
             try
 57  
             {
 58  
                 lockmanager = (LockManager) (strLockManager != null ?
 59  
                         ClassHelper.newInstance(strLockManager) : ClassHelper.newInstance(LockManagerInMemoryImpl.class));
 60  
             }
 61  
             catch(Exception e)
 62  
             {
 63  
                 lastError = new LockRuntimeException("Can't instance lock manager, init parameter 'lockManager': " + strLockManager);
 64  
                 e.printStackTrace();
 65  
             }
 66  
             String strTimeout = servletConfig.getInitParameter(STR_LOCK_TIMEOUT);
 67  
             if(NumberUtils.isNumber(strTimeout))
 68  
             {
 69  
                 try
 70  
                 {
 71  
                     Long lockTimeout = NumberUtils.createLong(strTimeout);
 72  
                     lockmanager.setLockTimeout(lockTimeout.longValue());
 73  
                 }
 74  
                 catch(Exception e)
 75  
                 {
 76  
                     if(lastError == null)
 77  
                     {
 78  
                         lastError = new LockRuntimeException("Can't convert 'lockTimeout' init parameter: " + strTimeout);
 79  
                     }
 80  
                     e.printStackTrace();
 81  
                 }
 82  
             }
 83  
             String strBlock = servletConfig.getInitParameter(STR_BLOCK_TIMEOUT);
 84  
             if(NumberUtils.isNumber(strBlock))
 85  
             {
 86  
                 try
 87  
                 {
 88  
                     Long blockTimeout = NumberUtils.createLong(strBlock);
 89  
                     lockmanager.setLockTimeout(blockTimeout.longValue());
 90  
                 }
 91  
                 catch(Exception e)
 92  
                 {
 93  
                     if(lastError == null)
 94  
                     {
 95  
                         lastError = new LockRuntimeException("Can't convert 'blockTimeout' init parameter: " + strBlock);
 96  
                     }
 97  
                     e.printStackTrace();
 98  
                 }
 99  
             }
 100  
         }
 101  
     }
 102  
 
 103  
     /* (non-Javadoc)
 104  
      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 105  
      */
 106  
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
 107  
             throws ServletException, IOException
 108  
     {
 109  
         response.setContentType("text/html");
 110  
         response.setHeader("Pragma", "no-cache");
 111  
 
 112  
         PrintWriter out = response.getWriter();
 113  
 
 114  
         out.println("<html><head><title>OJB Distributed Locking Servlet Status Page</title>");
 115  
         out.println("</head><body><h1>OJB Distributed Locking Servlet</h1>");
 116  
         out.println("The servlet is running.<p>");
 117  
 
 118  
         if(lastError == null)
 119  
         {
 120  
             out.println("The LockServer is running.<p>");
 121  
             out.println("LockManager info: " + lockmanager.getLockInfo() + "<p>");
 122  
             out.println("Processed Lock Request: " + numRequests + "<p>");
 123  
         }
 124  
         else
 125  
         {
 126  
             out.println("<h2>The LockServer has a problem!</h2>");
 127  
             out.println("The error message is:<p>");
 128  
             out.println(lastError.getMessage() + "<p>");
 129  
             lastError.printStackTrace(out);
 130  
             lastError = null;
 131  
         }
 132  
 
 133  
         out.println("</body></html>");
 134  
     }
 135  
 
 136  
     /* (non-Javadoc)
 137  
      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 138  
      */
 139  
     protected void doPost(HttpServletRequest request, HttpServletResponse response)
 140  
             throws ServletException, IOException
 141  
     {
 142  
         // update counter
 143  
         numRequests++;
 144  
 
 145  
         try
 146  
         {
 147  
             // read request:
 148  
             LockManagerRemoteImpl.LockInfo info = (LockManagerRemoteImpl.LockInfo) buildObjectFromRequest(request);
 149  
             Object result = null;
 150  
             // now execute the command specified by the selector
 151  
             try
 152  
             {
 153  
                 switch(info.methodName)
 154  
                 {
 155  
                     case LockManagerRemoteImpl.METHOD_READ_LOCK:
 156  
                         {
 157  
                             result = new Boolean(lockmanager.readLock(info.key, info.resourceId, info.isolationLevel));
 158  
                             break;
 159  
                         }
 160  
                     case LockManagerRemoteImpl.METHOD_RELEASE_SINGLE_LOCK:
 161  
                         {
 162  
                             result = new Boolean(lockmanager.releaseLock(info.key, info.resourceId));
 163  
                             break;
 164  
                         }
 165  
                     case LockManagerRemoteImpl.METHOD_RELEASE_LOCKS:
 166  
                         {
 167  
                             lockmanager.releaseLocks(info.key);
 168  
                             result = Boolean.TRUE;
 169  
                             break;
 170  
                         }
 171  
                     case LockManagerRemoteImpl.METHOD_WRITE_LOCK:
 172  
                         {
 173  
                             result = new Boolean(lockmanager.writeLock(info.key, info.resourceId,
 174  
                                     info.isolationLevel));
 175  
                             break;
 176  
                         }
 177  
                     case LockManagerRemoteImpl.METHOD_UPGRADE_LOCK:
 178  
                         {
 179  
                             result = new Boolean(lockmanager.upgradeLock(info.key, info.resourceId, info.isolationLevel));
 180  
                             break;
 181  
                         }
 182  
                     case LockManagerRemoteImpl.METHOD_CHECK_READ:
 183  
                         {
 184  
                             result = new Boolean(lockmanager.hasRead(info.key, info.resourceId));
 185  
                             break;
 186  
                         }
 187  
                     case LockManagerRemoteImpl.METHOD_CHECK_WRITE:
 188  
                         {
 189  
                             result = new Boolean(lockmanager.hasWrite(info.key, info.resourceId));
 190  
                             break;
 191  
                         }
 192  
                     case LockManagerRemoteImpl.METHOD_CHECK_UPGRADE:
 193  
                         {
 194  
                             result = new Boolean(lockmanager.hasUpgrade(info.key, info.resourceId));
 195  
                             break;
 196  
                         }
 197  
                     case LockManagerRemoteImpl.METHOD_LOCK_INFO:
 198  
                         {
 199  
                             result = lockmanager.getLockInfo();
 200  
                             break;
 201  
                         }
 202  
                     case LockManagerRemoteImpl.METHOD_LOCK_TIMEOUT:
 203  
                         {
 204  
                             result = new Long(lockmanager.getLockTimeout());
 205  
                             break;
 206  
                         }
 207  
                     case LockManagerRemoteImpl.METHOD_BLOCK_TIMEOUT:
 208  
                         {
 209  
                             result = new Long(lockmanager.getBlockTimeout());
 210  
                             break;
 211  
                         }
 212  
 //                    case LockManagerRemoteImpl.METHOD_LOCK_TIMEOUT_SET:
 213  
 //                        {
 214  
 //                            lockmanager.setLockTimeout(info.lockTimeout);
 215  
 //                            break;
 216  
 //                        }
 217  
 //
 218  
 //                    case LockManagerRemoteImpl.METHOD_BLOCK_TIMEOUT_SET:
 219  
 //                        {
 220  
 //                            lockmanager.setBlockTimeout(info.blockTimeout);
 221  
 //                            break;
 222  
 //                        }
 223  
                     default :
 224  
                         {
 225  
                             throw new LockRuntimeException("Unknown command:" + info.methodName);
 226  
                         }
 227  
                 }
 228  
             }
 229  
             catch(RuntimeException e)
 230  
             {
 231  
                 result = new LockRuntimeException("Error while invoke specified method in servlet.", e);
 232  
             }
 233  
 
 234  
             ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
 235  
             oos.writeObject(result);
 236  
             oos.flush();
 237  
             oos.close();
 238  
         }
 239  
         catch(Throwable t)
 240  
         {
 241  
             lastError = t;
 242  
             t.printStackTrace();
 243  
         }
 244  
     }
 245  
 
 246  
     private Object buildObjectFromRequest(HttpServletRequest request) throws IOException, ClassNotFoundException
 247  
     {
 248  
         Object obj = null;
 249  
         // get the body of the request as binary data
 250  
         InputStream is = request.getInputStream();
 251  
         ObjectInputStream objInputStream = new ObjectInputStream(is);
 252  
         obj = objInputStream.readObject();
 253  
         objInputStream.close();
 254  
         is.close();
 255  
         return obj;
 256  
     }
 257  
 }