1 25 26 28 package org.objectweb.jonas.jtests.beans.secured; 29 30 import java.io.Serializable ; 31 import java.rmi.RemoteException ; 32 import java.security.Principal ; 33 import java.util.Hashtable ; 34 35 import javax.ejb.EJBContext ; 36 import javax.ejb.EJBException ; 37 import javax.ejb.TimedObject ; 38 import javax.ejb.Timer ; 39 import javax.ejb.TimerService ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.rmi.PortableRemoteObject ; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 47 public abstract class BaseCommon implements TimedObject { 48 49 static Logger logger = null; 50 static protected int timercount = 0; 51 52 public abstract EJBContext getEJBContext(); 53 54 57 protected static String PRINCIPAL1_NAME = "principal1"; 58 59 60 63 protected static String ROLE1_NAME = "role1"; 64 65 69 public String getPrincipalName() throws RemoteException { 70 71 Principal callerPrincipal = getEJBContext().getCallerPrincipal(); 72 logger.log(BasicLevel.DEBUG, "principal = "+callerPrincipal.getName()); 73 return callerPrincipal.getName(); 74 } 75 76 public String localGetPrincipalName() { 77 Principal callerPrincipal = getEJBContext().getCallerPrincipal(); 78 logger.log(BasicLevel.DEBUG, "principal = "+callerPrincipal.getName()); 79 return callerPrincipal.getName(); 80 } 81 82 85 86 public String getPrincipalNameOfAnotherBean() throws RemoteException { 87 logger.log(BasicLevel.DEBUG, ""); 88 try { 89 Context it = new InitialContext (); 90 DerivedHome h = (DerivedHome) PortableRemoteObject.narrow(it.lookup("securedDerivedSFHome"), 91 DerivedHome.class); 92 Derived other = h.create(); 93 return other.getPrincipalName(); 94 } catch (RemoteException e) { 95 throw e; 96 } catch (Exception e) { 97 e.printStackTrace(); 98 return null; 99 } 100 } 101 102 106 public void excludedMethod() throws RemoteException { 107 logger.log(BasicLevel.DEBUG, ""); 108 } 109 110 114 public boolean callAnotherMethod() throws RemoteException { 115 logger.log(BasicLevel.DEBUG, ""); 116 try { 117 Context it = new InitialContext (); 118 DerivedLocalHome h = (DerivedLocalHome)it.lookup("java:comp/env/ejb/derivedlocal"); 119 DerivedLocal other = h.create(); 120 other.anotherMethod(); 121 } catch (EJBException e) { 122 return false; 123 }catch (Exception e) { 124 e.printStackTrace(); 125 return false; 126 } 127 return true; 128 } 129 130 public boolean isCallerInRole(String role) throws RemoteException { 131 logger.log(BasicLevel.DEBUG, "isCallerInRole "+role); 132 return getEJBContext().isCallerInRole(role); 133 } 134 135 public boolean localIsCallerInRole(String role) { 136 logger.log(BasicLevel.DEBUG, "isCallerInRole "+role); 137 return getEJBContext().isCallerInRole(role); 138 } 139 140 public void simpleMethod() throws RemoteException { 141 logger.log(BasicLevel.DEBUG, ""); 142 } 143 144 public void complexMethod(Hashtable h, Object [] o) throws RemoteException { 145 logger.log(BasicLevel.DEBUG, ""); 146 } 147 148 public int getTimerCount() throws RemoteException { 149 logger.log(BasicLevel.DEBUG, ""); 150 return timercount; 151 } 152 153 public int setTimer(int dur, int period, int action) throws RemoteException { 154 logger.log(BasicLevel.DEBUG, ""); 155 TimerService timerservice = getEJBContext().getTimerService(); 156 if (period > 0) { 157 timerservice.createTimer(dur * 1000, period * 1000, new Integer (action)); 158 } else { 159 timerservice.createTimer(dur * 1000, new Integer (action)); 160 } 161 return action; 162 } 163 164 168 171 public void ejbTimeout(Timer timer) { 172 logger.log(BasicLevel.DEBUG, ""); 173 Serializable sz = timer.getInfo(); 174 if (!(sz instanceof Integer )) { 175 logger.log(BasicLevel.ERROR, "Bad Info"); 176 return; 177 } 178 int action = ((Integer )sz).intValue(); 179 boolean ok = true; 180 switch (action) { 181 case 0: 182 try { 183 simpleMethod(); 184 } catch (RemoteException e) { 185 logger.log(BasicLevel.ERROR, "Can't call a simple Method"); 186 return; 187 } 188 break; 189 case 1: 190 try { 191 ok = callBeanNoRunAsWithRole1(); 192 } catch (RemoteException e) { 193 logger.log(BasicLevel.ERROR, "run as role1 failed"); 194 return; 195 } 196 break; 197 case 2: 198 try { 199 ok = callBeanNoRunAsWithRole2(); 200 } catch (RemoteException e) { 201 logger.log(BasicLevel.ERROR, "run as role2 failed"); 202 return; 203 } 204 break; 205 } 206 if (ok) { 207 timercount++; 208 } 209 } 210 211 215 216 public boolean callBeanNoRunAsWithRole1() throws RemoteException { 218 logger.log(BasicLevel.DEBUG, ""); 219 try { 220 Context ictx = new InitialContext (); 221 DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivednorunaslocal"); 222 DerivedLocal other = h.create(); 223 other.noRunAsWithRole1(); 224 if (!(getPrincipalName().equals(other.localGetPrincipalName()))) { 225 throw new Exception ("Principal of the other bean must be the same"); 226 } 227 if (!other.localIsCallerInRole("role1")) { 228 throw new Exception ("Role of the other bean must be role1"); 229 } 230 } catch (EJBException e) { 231 return false; 232 }catch (Exception e) { 233 e.printStackTrace(); 234 return false; 235 } 236 return true; 237 } 238 239 public boolean callBeanNoRunAsWithRole2() throws RemoteException { 241 logger.log(BasicLevel.DEBUG, ""); 242 try { 243 Context ictx = new InitialContext (); 244 DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivednorunaslocal"); 245 DerivedLocal other = h.create(); 246 other.noRunAsWithRole2(); 247 if (!("role1".equals(other.localGetPrincipalName()))) { 249 throw new Exception ("Principal of the other bean must be the same"); 250 } 251 if (!other.localIsCallerInRole("role1")) { 253 throw new Exception ("Role of the other bean must be role2"); 254 } 255 } catch (EJBException e) { 256 return false; 257 } catch (Exception e) { 258 e.printStackTrace(); 259 return false; 260 } 261 return true; 262 } 263 264 265 public boolean callBeanRunAsWithRole1() throws RemoteException { 267 logger.log(BasicLevel.DEBUG, ""); 268 try { 269 Context ictx = new InitialContext (); 270 DerivedLocalHome h = (DerivedLocalHome)ictx.lookup("java:comp/env/ejb/derivedrunaslocal"); 271 try { 272 DerivedLocal other = h.create(); 273 } catch (EJBException e) { 274 return true; 277 } 278 } catch (Exception e) { 279 e.printStackTrace(); 280 return false; 281 } 282 return false; 284 } 285 286 287 public boolean callBeanRunAsWithRole2() throws RemoteException { 289 logger.log(BasicLevel.DEBUG, ""); 290 try { 291 Context ictx = new InitialContext (); 292 DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivedrunas2local"); 293 try { 294 DerivedLocal other = h.create(); 295 other.runAsWithRole2(); 296 if (!(getPrincipalName().equals(other.localGetPrincipalName()))) { 297 throw new Exception ("Principal of the other bean must be the same"); 298 } 299 if (!other.localIsCallerInRole("role2")) { 300 throw new Exception ("Role of the other bean must be role2"); 301 } 302 } catch (EJBException e) { 303 return true; 305 } 306 } catch (Exception e) { 307 e.printStackTrace(); 308 return false; 309 } 310 return false; 311 } 312 313 314 } 315 | Popular Tags |