KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > InvocationManagerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.util;
24
25 import java.io.IOException JavaDoc;
26 import java.util.*;
27 import javax.transaction.SystemException JavaDoc;
28 import com.sun.enterprise.Switch;
29 import com.sun.enterprise.J2EETransactionManager;
30 import com.sun.enterprise.ComponentInvocation;
31 import com.sun.enterprise.InvocationManager;
32 import com.sun.enterprise.InvocationException;
33 import com.sun.enterprise.SecurityManager;
34 import org.apache.catalina.Realm;
35 import org.apache.catalina.Context;
36 import com.sun.web.security.RealmAdapter;
37 import com.sun.ejb.Container;
38
39 //START OF IASRI 4660742
40
import java.util.logging.*;
41 import com.sun.logging.*;
42 //END OF IASRI 4660742
43

44
45 /**
46  * Implementation of InvocationManager. Use ThreadLocal variable
47  * to keep track of per thread data
48  *
49  * @author Tony Ng
50  * @author Harpreet Singh
51  */

52 public class InvocationManagerImpl implements InvocationManager {
53
54     // START OF IASRI 4660742
55
static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
56     // END OF IASRI 4660742
57

58     // START OF IASRI 4679641
59
// static public boolean debug = false;
60
static public boolean debug = com.sun.enterprise.util.logging.Debug.enabled;
61     // END OF IASRI 4679641
62

63     static private LocalStringManagerImpl localStrings =
64         new LocalStringManagerImpl(InvocationManagerImpl.class);
65
66     // This TLS variable stores an ArrayList.
67
// The ArrayList contains ComponentInvocation objects which represent
68
// the stack of invocations on this thread. Accesses to the ArrayList
69
// dont need to be synchronized because each thread has its own ArrayList.
70
private InheritableThreadLocal JavaDoc frames;
71
72     private J2EETransactionManager tm;
73
74     public InvocationManagerImpl() {
75         frames = new InheritableThreadLocal JavaDoc() {
76             protected Object JavaDoc initialValue() {
77                 return new InvocationArray();
78             }
79
80             // if this is a thread created by user in servlet's service method
81
// create a new ComponentInvocation with transaction
82
// set to null and instance set to null
83
// so that the resource won't be enlisted or registered
84
protected Object JavaDoc childValue(Object JavaDoc parentValue) {
85                 // always creates a new ArrayList
86
InvocationArray result = new InvocationArray();
87                 InvocationArray v = (InvocationArray) parentValue;
88                 if (v.size() > 0 && v.outsideStartup()) {
89                     // get current invocation
90
ComponentInvocation parentInv =
91                         (ComponentInvocation) v.get(v.size()-1);
92                     if (parentInv.getInvocationType() ==
93                         parentInv.SERVLET_INVOCATION) {
94
95                         ComponentInvocation inv =
96                             new ComponentInvocation(null,
97                                             parentInv.getContainerContext());
98                         result.add(inv);
99                     } else if (parentInv.getInvocationType() != parentInv.EJB_INVOCATION) {
100             // Push a copy of invocation onto the new result ArrayList
101
ComponentInvocation cpy =
102                 new ComponentInvocation
103                 ( parentInv.getInstance(),
104                   parentInv.getContainerContext());
105             cpy.setTransaction (parentInv.getTransaction());
106             result.add(cpy);
107             }
108             
109                 }
110                 return result;
111             }
112         };
113     }
114
115     public void preInvoke(ComponentInvocation inv) throws InvocationException {
116
117            /** IASRI 4660742
118            if ( debug ) System.err.println("IM: preInvoke" + inv.instance);
119            **/

120              // START OF IASRI 4660742
121
if(debug && _logger.isLoggable(Level.FINE)) {
122               _logger.log(Level.FINE,"IM: preInvoke" + inv.instance);
123            }
124            // END OF IASRI 4660742
125

126
127     // Get this thread's ArrayList
128
InvocationArray v = (InvocationArray) frames.get();
129         if (inv.getInvocationType() == ComponentInvocation.SERVICE_STARTUP) {
130             v.setInvocationAttribute(ComponentInvocation.SERVICE_STARTUP);
131             return;
132         }
133
134     // if ejb call EJBSecurityManager, for servlet call RealmAdapter
135
int invType = inv.getInvocationType();
136     if (invType == inv.EJB_INVOCATION) {
137         SecurityManager JavaDoc sm =
138         ((Container)inv.getContainerContext()).getSecurityManager();
139         sm.preInvoke(inv);
140     } else if (inv.getInvocationType() == inv.SERVLET_INVOCATION){
141         Realm rlm = ((Context)inv.getContainerContext()).getRealm();
142         if (rlm instanceof RealmAdapter) {
143         RealmAdapter rad = (RealmAdapter) rlm;
144         rad.preSetRunAsIdentity(inv);
145         }
146     }
147
148     // push this invocation on the stack
149
v.add(inv);
150
151     // Get the previous invocation on the stack
152
int size = v.size();
153     ComponentInvocation prev;
154         if (size < 2)
155         prev = null;
156     else
157         prev = (ComponentInvocation) v.get(size - 2);
158
159     // Call the TM
160
if (tm == null)
161             tm = Switch.getSwitch().getTransactionManager();
162         tm.preInvoke(prev);
163     }
164
165     public void postInvoke(ComponentInvocation inv) throws InvocationException
166     {
167         /** IASRI 4660742
168         if ( debug ) System.err.println("IM: postInvoke" + inv.instance);
169         **/

170           // START OF IASRI 4660742
171
if(debug && _logger.isLoggable(Level.FINE)) {
172                _logger.log(Level.FINE,"IM: postInvoke" + inv.instance);
173         }
174         // END OF IASRI 4660742
175

176     // Get this thread's ArrayList
177
InvocationArray v = (InvocationArray) frames.get();
178         if (inv.getInvocationType() == ComponentInvocation.SERVICE_STARTUP) {
179             v.setInvocationAttribute(ComponentInvocation.UN_INITIALIZED);
180             return;
181         }
182
183     int size = v.size();
184     if (size == 0)
185         throw new InvocationException();
186
187         try {
188         // if ejb call EJBSecurityManager, for servlet call RealmAdapter
189
int invType = inv.getInvocationType();
190         if (invType == inv.EJB_INVOCATION){
191         SecurityManager JavaDoc sm =
192             ((Container)inv.getContainerContext()).getSecurityManager();
193         
194         sm.postInvoke(inv);
195         } else if (invType == inv.SERVLET_INVOCATION){
196         Realm rlm = ((Context)inv.getContainerContext()).getRealm();
197         if (rlm instanceof RealmAdapter) {
198             RealmAdapter rad = (RealmAdapter) rlm;
199             rad.postSetRunAsIdentity (inv);
200         }// else {
201
// throw new InvocationException();
202
// }
203
}
204
205         // Get current and previous ComponentInvocation objects
206
ComponentInvocation prev, curr;
207         if (size < 2)
208         prev = null;
209         else
210         prev = (ComponentInvocation)v.get(size - 2);
211         curr = (ComponentInvocation)v.get(size - 1);
212
213         tm.postInvoke(curr, prev);
214
215             Switch.getSwitch().getPoolManager().postInvoke();
216     }
217     finally {
218         // pop the stack
219
v.remove(size - 1);
220     }
221     }
222
223     /**
224      * return true iff no invocations on the stack for this thread
225      */

226     public boolean isInvocationStackEmpty() {
227         ArrayList v = (ArrayList) frames.get();
228         return (v.size() == 0);
229     }
230
231     // BEGIN IASRI# 4646060
232
/**
233      * return the Invocation object of the component
234      * being called
235      */

236     public ComponentInvocation getCurrentInvocation() {
237     // END IASRI# 4646060
238

239         ArrayList v = (ArrayList) frames.get();
240     int size = v.size();
241         // BEGIN IASRI# 4646060
242
if (size == 0) return null;
243         // END IASRI# 4646060
244
return (ComponentInvocation) v.get(size-1);
245     }
246
247     /**
248      * return the Inovcation object of the caller
249      * return null if none exist (e.g. caller is from
250      * another VM)
251      */

252     public ComponentInvocation getPreviousInvocation()
253         throws InvocationException {
254
255         ArrayList v = (ArrayList) frames.get();
256         int i = v.size();
257         if (i < 2) return null;
258         return (ComponentInvocation) v.get(i - 2);
259     }
260
261     public List getAllInvocations() {
262         return (ArrayList) frames.get();
263     }
264
265     class InvocationArray extends java.util.ArrayList JavaDoc {
266         private int invocationAttribute;
267         
268         public void setInvocationAttribute (int attribute) {
269             this.invocationAttribute = attribute;
270         }
271
272         public int getInvocationAttribute() {
273             return invocationAttribute;
274         }
275
276         public boolean outsideStartup() {
277             return getInvocationAttribute()
278             != ComponentInvocation.SERVICE_STARTUP;
279         }
280     }
281 }
282
283
284
285
286
287
288
Popular Tags