1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.ejb.plugins;
23
24 import org.jboss.ejb.BeanLock;
25 import org.jboss.invocation.Invocation;
26
27 /**
28 * The lock interceptors role is to schedule thread wanting to invoke method on a target bean
29 *
30 * <p>The policies for implementing scheduling (pessimistic locking etc) is implemented by pluggable
31 * locks
32 *
33 * <p>We also implement serialization of calls in here (this is a spec
34 * requirement). This is a fine grained notify, notifyAll mechanism. We
35 * notify on ctx serialization locks and notifyAll on global transactional
36 * locks.
37 *
38 * <p><b>WARNING: critical code</b>, get approval from senior developers
39 * before changing.
40 *
41 * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
42 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
43 * @version $Revision: 37459 $
44 *
45 * <p><b>Revisions:</b><br>
46 * <p><b>2001/07/30: marcf</b>
47 * <ol>
48 * <li>Initial revision
49 * <li>Factorization of the lock out of the context in cache
50 * <li>The new locking is implement as "scheduling" in the lock which allows for pluggable locks
51 * </ol>
52 * <p><b>2001/08/07: billb</b>
53 * <ol>
54 * <li>Removed while loop and moved it to SimplePessimisticEJBLock where it belongs.
55 * </ol>
56 */
57 public class EntityLockInterceptor
58 extends AbstractInterceptor
59 {
60 // Constants -----------------------------------------------------
61
62 // Attributes ----------------------------------------------------
63
64 // Static --------------------------------------------------------
65
66 // Constructors --------------------------------------------------
67
68 // Public --------------------------------------------------------
69
70 // Interceptor implementation --------------------------------------
71
72 public Object invokeHome(Invocation mi)
73 throws Exception
74 {
75 // Invoke through interceptors
76 return getNext().invokeHome(mi);
77
78 }
79
80 public Object invoke(Invocation mi)
81 throws Exception
82 {
83
84 // The key.
85 Object key = mi.getId();
86
87 // The lock.
88 BeanLock lock ;
89
90 boolean threadIsScheduled = false;
91 boolean trace = log.isTraceEnabled();
92
93 if( trace ) log.trace("Begin invoke, key="+key);
94
95
96 lock = container.getLockManager().getLock(key);
97 try
98 {
99
100 lock.schedule(mi);
101
102 try {
103
104 return getNext().invoke(mi);
105 }
106
107 finally
108 {
109
110 // we are done with the method, decrease the count, if it reaches 0 it will wake up
111 // the next thread
112 lock.sync();
113 lock.endInvocation(mi);
114 lock.releaseSync();
115 }
116 }
117 finally
118 {
119
120 // We are done with the lock in general
121 container.getLockManager().removeLockRef(key);
122
123 if( trace ) log.trace("End invoke, key="+key);
124 }
125 }
126 }
127