1 22 package org.jboss.ejb.plugins; 23 24 import java.security.Principal ; 25 import javax.ejb.EJBContext ; 26 import javax.naming.InitialContext ; 27 28 import org.jboss.ejb.Container; 29 import org.jboss.ejb.EJBProxyFactoryContainer; 30 import org.jboss.ejb.EnterpriseContext; 31 import org.jboss.invocation.Invocation; 32 import org.jboss.logging.Logger; 33 34 import org.jboss.security.AuthenticationManager; 35 import org.jboss.security.SecurityProxy; 36 import org.jboss.security.SecurityProxyFactory; 37 38 48 public class SecurityProxyInterceptor 49 extends AbstractInterceptor 50 { 51 55 public final String SECURITY_PROXY_FACTORY_NAME = 56 "java:/SecurityProxyFactory"; 57 58 59 protected Logger log = Logger.getLogger(this.getClass()); 60 61 protected AuthenticationManager securityManager; 62 63 68 protected SecurityProxy securityProxy; 69 70 public SecurityProxyInterceptor() 71 { 72 super(); 73 } 74 75 public void setContainer(Container container) 76 { 77 super.setContainer(container); 78 if( container != null ) 79 { 80 securityManager = container.getSecurityManager(); 81 Object secProxy = container.getSecurityProxy(); 82 if( secProxy != null ) 83 { 84 if( (secProxy instanceof SecurityProxy) == false ) 87 { 88 try 89 { 90 InitialContext iniCtx = new InitialContext (); 92 SecurityProxyFactory proxyFactory = 93 (SecurityProxyFactory)iniCtx.lookup(SECURITY_PROXY_FACTORY_NAME); 94 securityProxy = proxyFactory.create(secProxy); 95 } 96 catch (Exception e) 97 { 98 log.error("Failed to initialze DefaultSecurityProxy", e); 99 } 100 } 101 else 102 { 103 securityProxy = (SecurityProxy) secProxy; 104 } 105 106 try 108 { 109 EJBProxyFactoryContainer ic = 110 (EJBProxyFactoryContainer)container; 111 Class beanHome = ic.getHomeClass(); 112 Class beanRemote = ic.getRemoteClass(); 113 Class beanLocalHome = ic.getLocalHomeClass(); 114 Class beanLocal = ic.getLocalClass(); 115 if( beanLocal == null ) 116 { 117 securityProxy.init(beanHome, beanRemote, securityManager); 118 } 119 else 120 { 121 securityProxy.init(beanHome, beanRemote, beanLocalHome, 122 beanLocal, securityManager); 123 } 124 } 125 catch(Exception e) 126 { 127 log.error("Failed to initialze SecurityProxy", e); 128 } 129 log.info("Initialized SecurityProxy=" + securityProxy); 130 } 131 } 132 } 133 134 136 public void start() throws Exception 137 { 138 super.start(); 139 } 140 141 public Object invokeHome(Invocation mi) throws Exception 142 { 143 if( securityProxy != null ) 145 { 146 EJBContext ctx = null; 147 EnterpriseContext ectx = (EnterpriseContext)mi.getEnterpriseContext(); 148 if( ectx != null ) 149 ctx = ectx.getEJBContext(); 150 Object [] args = mi.getArguments(); 151 securityProxy.setEJBContext(ctx); 152 try 153 { 154 securityProxy.invokeHome(mi.getMethod(), args); 155 } 156 catch(SecurityException e) 157 { 158 Principal principal = mi.getPrincipal(); 159 String msg = "SecurityProxy.invokeHome exception, principal=" + principal; 160 log.error(msg, e); 161 throw e; 162 } 163 } 164 return getNext().invokeHome(mi); 165 } 166 167 public Object invoke(Invocation mi) throws Exception 168 { 169 if( securityProxy != null ) 171 { 172 EnterpriseContext ectx = (EnterpriseContext)mi.getEnterpriseContext(); 173 Object bean = ectx.getInstance(); 174 EJBContext ctx = ectx.getEJBContext(); 175 Object [] args = mi.getArguments(); 176 securityProxy.setEJBContext(ctx); 177 try 178 { 179 securityProxy.invoke(mi.getMethod(), args, bean); 180 } 181 catch(SecurityException e) 182 { 183 Principal principal = mi.getPrincipal(); 184 String msg = "SecurityProxy.invoke exception, principal="+principal; 185 log.error(msg, e); 186 throw e; 187 } 188 } 189 return getNext().invoke(mi); 190 } 191 } 192 193 | Popular Tags |