001: package org.apache.ojb.broker.locking;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.servlet.ServletException;
019: import javax.servlet.ServletConfig;
020: import javax.servlet.http.HttpServlet;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.PrintWriter;
028:
029: import org.apache.commons.lang.math.NumberUtils;
030: import org.apache.ojb.broker.util.ClassHelper;
031:
032: /**
033: * @author Thomas Mahler
034: */
035: public class LockManagerServlet extends HttpServlet {
036: protected static LockManager lockmanager;
037: static final String STR_LOCK_TIMEOUT = "lockTimeout";
038: static final String STR_BLOCK_TIMEOUT = "blockTimeout";
039: static final String STR_LOCK_MANAGER = "lockManager";
040:
041: private static long numRequests;
042: private static Throwable lastError = null;
043:
044: public void init(ServletConfig servletConfig)
045: throws ServletException {
046: super .init(servletConfig);
047: // if lock manager was instantiated not yet
048: if (lockmanager == null) {
049: lastError = null;
050: numRequests = 0;
051: String strLockManager = servletConfig
052: .getInitParameter(STR_LOCK_MANAGER);
053: try {
054: lockmanager = (LockManager) (strLockManager != null ? ClassHelper
055: .newInstance(strLockManager)
056: : ClassHelper
057: .newInstance(LockManagerInMemoryImpl.class));
058: } catch (Exception e) {
059: lastError = new LockRuntimeException(
060: "Can't instance lock manager, init parameter 'lockManager': "
061: + strLockManager);
062: e.printStackTrace();
063: }
064: String strTimeout = servletConfig
065: .getInitParameter(STR_LOCK_TIMEOUT);
066: if (NumberUtils.isNumber(strTimeout)) {
067: try {
068: Long lockTimeout = NumberUtils
069: .createLong(strTimeout);
070: lockmanager.setLockTimeout(lockTimeout.longValue());
071: } catch (Exception e) {
072: if (lastError == null) {
073: lastError = new LockRuntimeException(
074: "Can't convert 'lockTimeout' init parameter: "
075: + strTimeout);
076: }
077: e.printStackTrace();
078: }
079: }
080: String strBlock = servletConfig
081: .getInitParameter(STR_BLOCK_TIMEOUT);
082: if (NumberUtils.isNumber(strBlock)) {
083: try {
084: Long blockTimeout = NumberUtils
085: .createLong(strBlock);
086: lockmanager
087: .setLockTimeout(blockTimeout.longValue());
088: } catch (Exception e) {
089: if (lastError == null) {
090: lastError = new LockRuntimeException(
091: "Can't convert 'blockTimeout' init parameter: "
092: + strBlock);
093: }
094: e.printStackTrace();
095: }
096: }
097: }
098: }
099:
100: /* (non-Javadoc)
101: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
102: */
103: protected void doGet(HttpServletRequest request,
104: HttpServletResponse response) throws ServletException,
105: IOException {
106: response.setContentType("text/html");
107: response.setHeader("Pragma", "no-cache");
108:
109: PrintWriter out = response.getWriter();
110:
111: out
112: .println("<html><head><title>OJB Distributed Locking Servlet Status Page</title>");
113: out
114: .println("</head><body><h1>OJB Distributed Locking Servlet</h1>");
115: out.println("The servlet is running.<p>");
116:
117: if (lastError == null) {
118: out.println("The LockServer is running.<p>");
119: out.println("LockManager info: "
120: + lockmanager.getLockInfo() + "<p>");
121: out.println("Processed Lock Request: " + numRequests
122: + "<p>");
123: } else {
124: out.println("<h2>The LockServer has a problem!</h2>");
125: out.println("The error message is:<p>");
126: out.println(lastError.getMessage() + "<p>");
127: lastError.printStackTrace(out);
128: lastError = null;
129: }
130:
131: out.println("</body></html>");
132: }
133:
134: /* (non-Javadoc)
135: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
136: */
137: protected void doPost(HttpServletRequest request,
138: HttpServletResponse response) throws ServletException,
139: IOException {
140: // update counter
141: numRequests++;
142:
143: try {
144: // read request:
145: LockManagerRemoteImpl.LockInfo info = (LockManagerRemoteImpl.LockInfo) buildObjectFromRequest(request);
146: Object result = null;
147: // now execute the command specified by the selector
148: try {
149: switch (info.methodName) {
150: case LockManagerRemoteImpl.METHOD_READ_LOCK: {
151: result = new Boolean(lockmanager.readLock(info.key,
152: info.resourceId, info.isolationLevel));
153: break;
154: }
155: case LockManagerRemoteImpl.METHOD_RELEASE_SINGLE_LOCK: {
156: result = new Boolean(lockmanager.releaseLock(
157: info.key, info.resourceId));
158: break;
159: }
160: case LockManagerRemoteImpl.METHOD_RELEASE_LOCKS: {
161: lockmanager.releaseLocks(info.key);
162: result = Boolean.TRUE;
163: break;
164: }
165: case LockManagerRemoteImpl.METHOD_WRITE_LOCK: {
166: result = new Boolean(lockmanager.writeLock(
167: info.key, info.resourceId,
168: info.isolationLevel));
169: break;
170: }
171: case LockManagerRemoteImpl.METHOD_UPGRADE_LOCK: {
172: result = new Boolean(lockmanager.upgradeLock(
173: info.key, info.resourceId,
174: info.isolationLevel));
175: break;
176: }
177: case LockManagerRemoteImpl.METHOD_CHECK_READ: {
178: result = new Boolean(lockmanager.hasRead(info.key,
179: info.resourceId));
180: break;
181: }
182: case LockManagerRemoteImpl.METHOD_CHECK_WRITE: {
183: result = new Boolean(lockmanager.hasWrite(info.key,
184: info.resourceId));
185: break;
186: }
187: case LockManagerRemoteImpl.METHOD_CHECK_UPGRADE: {
188: result = new Boolean(lockmanager.hasUpgrade(
189: info.key, info.resourceId));
190: break;
191: }
192: case LockManagerRemoteImpl.METHOD_LOCK_INFO: {
193: result = lockmanager.getLockInfo();
194: break;
195: }
196: case LockManagerRemoteImpl.METHOD_LOCK_TIMEOUT: {
197: result = new Long(lockmanager.getLockTimeout());
198: break;
199: }
200: case LockManagerRemoteImpl.METHOD_BLOCK_TIMEOUT: {
201: result = new Long(lockmanager.getBlockTimeout());
202: break;
203: }
204: // case LockManagerRemoteImpl.METHOD_LOCK_TIMEOUT_SET:
205: // {
206: // lockmanager.setLockTimeout(info.lockTimeout);
207: // break;
208: // }
209: //
210: // case LockManagerRemoteImpl.METHOD_BLOCK_TIMEOUT_SET:
211: // {
212: // lockmanager.setBlockTimeout(info.blockTimeout);
213: // break;
214: // }
215: default: {
216: throw new LockRuntimeException("Unknown command:"
217: + info.methodName);
218: }
219: }
220: } catch (RuntimeException e) {
221: result = new LockRuntimeException(
222: "Error while invoke specified method in servlet.",
223: e);
224: }
225:
226: ObjectOutputStream oos = new ObjectOutputStream(response
227: .getOutputStream());
228: oos.writeObject(result);
229: oos.flush();
230: oos.close();
231: } catch (Throwable t) {
232: lastError = t;
233: t.printStackTrace();
234: }
235: }
236:
237: private Object buildObjectFromRequest(HttpServletRequest request)
238: throws IOException, ClassNotFoundException {
239: Object obj = null;
240: // get the body of the request as binary data
241: InputStream is = request.getInputStream();
242: ObjectInputStream objInputStream = new ObjectInputStream(is);
243: obj = objInputStream.readObject();
244: objInputStream.close();
245: is.close();
246: return obj;
247: }
248: }
|