1 23 package com.sun.enterprise.util; 24 25 import java.io.IOException ; 26 import java.util.*; 27 import javax.transaction.SystemException ; 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 import java.util.logging.*; 41 import com.sun.logging.*; 42 44 45 52 public class InvocationManagerImpl implements InvocationManager { 53 54 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 56 58 static public boolean debug = com.sun.enterprise.util.logging.Debug.enabled; 61 63 static private LocalStringManagerImpl localStrings = 64 new LocalStringManagerImpl(InvocationManagerImpl.class); 65 66 private InheritableThreadLocal frames; 71 72 private J2EETransactionManager tm; 73 74 public InvocationManagerImpl() { 75 frames = new InheritableThreadLocal () { 76 protected Object initialValue() { 77 return new InvocationArray(); 78 } 79 80 protected Object childValue(Object parentValue) { 85 InvocationArray result = new InvocationArray(); 87 InvocationArray v = (InvocationArray) parentValue; 88 if (v.size() > 0 && v.outsideStartup()) { 89 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 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 120 if(debug && _logger.isLoggable(Level.FINE)) { 122 _logger.log(Level.FINE,"IM: preInvoke" + inv.instance); 123 } 124 126 127 InvocationArray v = (InvocationArray) frames.get(); 129 if (inv.getInvocationType() == ComponentInvocation.SERVICE_STARTUP) { 130 v.setInvocationAttribute(ComponentInvocation.SERVICE_STARTUP); 131 return; 132 } 133 134 int invType = inv.getInvocationType(); 136 if (invType == inv.EJB_INVOCATION) { 137 SecurityManager 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 v.add(inv); 150 151 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 if (tm == null) 161 tm = Switch.getSwitch().getTransactionManager(); 162 tm.preInvoke(prev); 163 } 164 165 public void postInvoke(ComponentInvocation inv) throws InvocationException 166 { 167 170 if(debug && _logger.isLoggable(Level.FINE)) { 172 _logger.log(Level.FINE,"IM: postInvoke" + inv.instance); 173 } 174 176 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 int invType = inv.getInvocationType(); 190 if (invType == inv.EJB_INVOCATION){ 191 SecurityManager 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 } } 204 205 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 v.remove(size - 1); 220 } 221 } 222 223 226 public boolean isInvocationStackEmpty() { 227 ArrayList v = (ArrayList) frames.get(); 228 return (v.size() == 0); 229 } 230 231 236 public ComponentInvocation getCurrentInvocation() { 237 239 ArrayList v = (ArrayList) frames.get(); 240 int size = v.size(); 241 if (size == 0) return null; 243 return (ComponentInvocation) v.get(size-1); 245 } 246 247 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 { 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 |