|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EasyBeansEJBContext.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /** | |
| 2 | * EasyBeans | |
| 3 | * Copyright (C) 2006 Bull S.A.S. | |
| 4 | * Contact: easybeans@objectweb.org | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation; either | |
| 9 | * version 2.1 of the License, or any later version. | |
| 10 | * | |
| 11 | * This library is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | * Lesser General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU Lesser General Public | |
| 17 | * License along with this library; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
| 19 | * USA | |
| 20 | * | |
| 21 | * -------------------------------------------------------------------------- | |
| 22 | * $Id$ | |
| 23 | * -------------------------------------------------------------------------- | |
| 24 | */ | |
| 25 | ||
| 26 | package org.objectweb.easybeans.container; | |
| 27 | ||
| 28 | ||
| 29 | import static javax.ejb.TransactionManagementType.BEAN; | |
| 30 | import static javax.ejb.TransactionManagementType.CONTAINER; | |
| 31 | ||
| 32 | import java.io.Serializable; | |
| 33 | import java.lang.reflect.InvocationTargetException; | |
| 34 | import java.lang.reflect.Method; | |
| 35 | import java.security.Identity; | |
| 36 | import java.security.Principal; | |
| 37 | import java.util.Collection; | |
| 38 | import java.util.Date; | |
| 39 | import java.util.Properties; | |
| 40 | ||
| 41 | import javax.ejb.EJBContext; | |
| 42 | import javax.ejb.EJBException; | |
| 43 | import javax.ejb.EJBHome; | |
| 44 | import javax.ejb.EJBLocalHome; | |
| 45 | import javax.ejb.Timer; | |
| 46 | import javax.ejb.TimerService; | |
| 47 | import javax.ejb.TransactionManagementType; | |
| 48 | import javax.naming.InitialContext; | |
| 49 | import javax.naming.NamingException; | |
| 50 | import javax.transaction.Status; | |
| 51 | import javax.transaction.SystemException; | |
| 52 | import javax.transaction.TransactionManager; | |
| 53 | import javax.transaction.UserTransaction; | |
| 54 | ||
| 55 | import org.objectweb.easybeans.api.Factory; | |
| 56 | ||
| 57 | /** | |
| 58 | * Class implementing the EJBContext interface. | |
| 59 | * It is extended for Session context or MessageDriven Context | |
| 60 | * @author Florent Benoit | |
| 61 | */ | |
| 62 | public class EasyBeansEJBContext implements EJBContext { | |
| 63 | ||
| 64 | /** | |
| 65 | * java:comp/env prefix. | |
| 66 | */ | |
| 67 | private static final String JAVA_COMP_ENV = "java:comp/env/"; | |
| 68 | ||
| 69 | /** | |
| 70 | * Default principal. | |
| 71 | */ | |
| 72 | private static Principal dummyPrincipal = null; | |
| 73 | ||
| 74 | /** | |
| 75 | * Reference to the transaction manager. | |
| 76 | */ | |
| 77 | private TransactionManager transactionManager = null; | |
| 78 | ||
| 79 | /** | |
| 80 | * Type of transaction. | |
| 81 | */ | |
| 82 | private TransactionManagementType transactionManagementType = null; | |
| 83 | ||
| 84 | /** | |
| 85 | * Builds a default EJB Context implementation. | |
| 86 | * @param easyBeansFactory used to get the transaction management type. | |
| 87 | */ | |
| 88 | 0 | public EasyBeansEJBContext(final Factory easyBeansFactory) { |
| 89 | 0 | this.transactionManager = null; |
| 90 | ||
| 91 | //TODO : Don't use reflection here !!! | |
| 92 | 0 | Class cls = null; |
| 93 | 0 | try { |
| 94 | 0 | cls = Thread.currentThread().getContextClassLoader().loadClass( |
| 95 | "org.objectweb.easybeans.transaction.JTransactionManager"); | |
| 96 | } catch (ClassNotFoundException e) { | |
| 97 | 0 | throw new RuntimeException(e); |
| 98 | } | |
| 99 | ||
| 100 | 0 | try { |
| 101 | 0 | Method m = cls.getMethod("getTransactionManager", new Class[] {}); |
| 102 | 0 | transactionManager = (TransactionManager) m.invoke(null, new Object[] {}); |
| 103 | } catch (SecurityException e) { | |
| 104 | 0 | throw new RuntimeException(e); |
| 105 | } catch (NoSuchMethodException e) { | |
| 106 | 0 | throw new RuntimeException(e); |
| 107 | } catch (IllegalArgumentException e) { | |
| 108 | 0 | throw new RuntimeException(e); |
| 109 | } catch (IllegalAccessException e) { | |
| 110 | 0 | throw new RuntimeException(e); |
| 111 | } catch (InvocationTargetException e) { | |
| 112 | 0 | throw new RuntimeException(e); |
| 113 | } | |
| 114 | ||
| 115 | //JTransactionManager.getTransactionManager(); | |
| 116 | 0 | this.transactionManagementType = easyBeansFactory.getBeanInfo().getTransactionManagementType(); |
| 117 | 0 | if (dummyPrincipal == null) { |
| 118 | 0 | dummyPrincipal = new DummyPrincipal(); |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Obtain the enterprise bean's remote home interface. | |
| 124 | * @return The enterprise bean's remote home interface. | |
| 125 | * @throws IllegalStateException if the enterprise bean does not have a | |
| 126 | * remote home interface. | |
| 127 | */ | |
| 128 | 0 | public EJBHome getEJBHome() throws IllegalStateException { |
| 129 | 0 | throw new IllegalStateException("No Home"); |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * Obtain the enterprise bean's local home interface. | |
| 134 | * @return The enterprise bean's local home interface. | |
| 135 | * @throws IllegalStateException - if the enterprise bean does not have a | |
| 136 | * local home interface. | |
| 137 | */ | |
| 138 | 0 | public EJBLocalHome getEJBLocalHome() throws IllegalStateException { |
| 139 | 0 | throw new IllegalStateException("No Local Home"); |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * Use the JNDI naming context java:comp/env to access enterprise bean's | |
| 144 | * environment. Obtain the enterprise bean's environment properties. Note: | |
| 145 | * If the enterprise bean has no environment properties this method returns | |
| 146 | * an empty java.util.Properties object. This method never returns null. | |
| 147 | * @return The environment properties for the enterprise bean. | |
| 148 | */ | |
| 149 | 0 | @Deprecated |
| 150 | public Properties getEnvironment() { | |
| 151 | 0 | throw new UnsupportedOperationException(); |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Use Principal getCallerPrincipal() instead. Obtain the | |
| 156 | * java.security.Identity of the caller. This method is deprecated in EJB | |
| 157 | * 1.1. The Container is allowed to return alway null from this method. The | |
| 158 | * enterprise bean should use the getCallerPrincipal method instead. | |
| 159 | * @return The Identity object that identifies the caller. | |
| 160 | */ | |
| 161 | 0 | @Deprecated |
| 162 | public Identity getCallerIdentity() { | |
| 163 | 0 | throw new UnsupportedOperationException(); |
| 164 | } | |
| 165 | ||
| 166 | /** | |
| 167 | * Obtain the java.security.Principal that identifies the caller. | |
| 168 | * @return The Principal object that identifies the caller. This method | |
| 169 | * never returns null. | |
| 170 | */ | |
| 171 | 0 | public Principal getCallerPrincipal() { |
| 172 | 0 | return dummyPrincipal; |
| 173 | } | |
| 174 | ||
| 175 | /** | |
| 176 | * Use boolean isCallerInRole(String roleName) instead. Test if the caller | |
| 177 | * has a given role. This method is deprecated in EJB 1.1. The enterprise | |
| 178 | * bean should use the isCallerInRole(String roleName) method instead. | |
| 179 | * @param role The java.security.Identity of the role to be tested. | |
| 180 | * @return True if the caller has the specified role. | |
| 181 | */ | |
| 182 | 0 | @Deprecated |
| 183 | public boolean isCallerInRole(final Identity role) { | |
| 184 | 0 | throw new UnsupportedOperationException(); |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Test if the caller has a given security role. | |
| 189 | * @param roleName The name of the security role. The role must be one of | |
| 190 | * the security roles that is defined in the deployment descriptor. | |
| 191 | * @return True if the caller has the specified role. | |
| 192 | */ | |
| 193 | 0 | public boolean isCallerInRole(final String roleName) { |
| 194 | 0 | throw new UnsupportedOperationException(); |
| 195 | } | |
| 196 | ||
| 197 | /** | |
| 198 | * Obtain the transaction demarcation interface. Only enterprise beans with | |
| 199 | * bean-managed transactions are allowed to to use the UserTransaction | |
| 200 | * interface. As entity beans must always use container-managed | |
| 201 | * transactions, only session beans with bean-managed transactions are | |
| 202 | * allowed to invoke this method. | |
| 203 | * @return The UserTransaction interface that the enterprise bean instance | |
| 204 | * can use for transaction demarcation. | |
| 205 | * @throws java.lang.IllegalStateException - The Container throws the | |
| 206 | * exception if the instance is not allowed to use the | |
| 207 | * UserTransaction interface (i.e. the instance is of a bean with | |
| 208 | * container-managed transactions). | |
| 209 | */ | |
| 210 | 0 | public UserTransaction getUserTransaction() throws IllegalStateException { |
| 211 | 0 | if (transactionManagementType == CONTAINER) { |
| 212 | 0 | throw new IllegalStateException("This bean is not allowed to use getUserTransaction() " |
| 213 | + " method as it is in ContainerManagedTransaction"); | |
| 214 | } | |
| 215 | 0 | return (UserTransaction) transactionManager; |
| 216 | } | |
| 217 | ||
| 218 | /** | |
| 219 | * Mark the current transaction for rollback. The transaction will become | |
| 220 | * permanently marked for rollback. A transaction marked for rollback can | |
| 221 | * never commit. Only enterprise beans with container-managed transactions | |
| 222 | * are allowed to use this method. | |
| 223 | * @throws java.lang.IllegalStateException - The Container throws the | |
| 224 | * exception if the instance is not allowed to use this method (i.e. | |
| 225 | * the instance is of a bean with bean-managed transactions). | |
| 226 | */ | |
| 227 | 0 | public void setRollbackOnly() throws IllegalStateException { |
| 228 | 0 | if (transactionManagementType == BEAN) { |
| 229 | 0 | throw new IllegalStateException("This bean is not allowed to use setRollbackOnly() " |
| 230 | + " method as it is in BeanManagedTransaction"); | |
| 231 | } | |
| 232 | ||
| 233 | // Check if there is a transaction, as it is mandatory | |
| 234 | 0 | try { |
| 235 | 0 | if (transactionManager.getTransaction() == null) { |
| 236 | 0 | throw new IllegalStateException("Cannot use setRollbackOnly() outside transaction"); |
| 237 | } | |
| 238 | } catch (SystemException e) { | |
| 239 | 0 | throw new IllegalStateException("Cannot get transaction on transaction manager", e); |
| 240 | } | |
| 241 | ||
| 242 | ||
| 243 | 0 | try { |
| 244 | 0 | transactionManager.setRollbackOnly(); |
| 245 | } catch (SystemException e) { | |
| 246 | 0 | throw new RuntimeException("setRollbackOnly() raised an unexpected exception:", e); |
| 247 | } | |
| 248 | } | |
| 249 | ||
| 250 | /** | |
| 251 | * Test if the transaction has been marked for rollback only. An enterprise | |
| 252 | * bean instance can use this operation, for example, to test after an | |
| 253 | * exception has been caught, whether it is fruitless to continue | |
| 254 | * computation on behalf of the current transaction. Only enterprise beans | |
| 255 | * with container-managed transactions are allowed to use this method. | |
| 256 | * @return True if the current transaction is marked for rollback, false | |
| 257 | * otherwise. | |
| 258 | * @throws java.lang.IllegalStateException - The Container throws the | |
| 259 | * exception if the instance is not allowed to use this method (i.e. | |
| 260 | * the instance is of a bean with bean-managed transactions). | |
| 261 | */ | |
| 262 | 0 | public boolean getRollbackOnly() throws IllegalStateException { |
| 263 | 0 | if (transactionManagementType == BEAN) { |
| 264 | 0 | throw new IllegalStateException("This bean is not allowed to use getRollbackOnly() " |
| 265 | + " method as it is in BeanManagedTransaction"); | |
| 266 | } | |
| 267 | 0 | try { |
| 268 | 0 | switch (transactionManager.getStatus()) { |
| 269 | 0 | case Status.STATUS_MARKED_ROLLBACK: |
| 270 | 0 | case Status.STATUS_ROLLING_BACK: |
| 271 | 0 | return true; |
| 272 | 0 | case Status.STATUS_ACTIVE: |
| 273 | 0 | case Status.STATUS_COMMITTING: |
| 274 | 0 | case Status.STATUS_PREPARED: |
| 275 | 0 | case Status.STATUS_PREPARING: |
| 276 | 0 | return false; |
| 277 | 0 | case Status.STATUS_ROLLEDBACK: |
| 278 | 0 | throw new IllegalStateException("Transaction already rolled back"); |
| 279 | 0 | case Status.STATUS_COMMITTED: |
| 280 | 0 | throw new IllegalStateException("Transaction already committed"); |
| 281 | 0 | case Status.STATUS_NO_TRANSACTION: |
| 282 | 0 | case Status.STATUS_UNKNOWN: |
| 283 | 0 | throw new IllegalStateException("Cannot getRollbackOnly outside transaction"); |
| 284 | 0 | default: |
| 285 | 0 | throw new IllegalStateException("Invalid status"); |
| 286 | } | |
| 287 | } catch (SystemException e) { | |
| 288 | 0 | throw new IllegalStateException("Cannot get transaction status", e); |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * Get access to the EJB Timer Service. | |
| 294 | * @return Timer service. | |
| 295 | * @throws java.lang.IllegalStateException The Container throws the | |
| 296 | * exception if the instance is not allowed to use this method (e.g. | |
| 297 | * if the bean is a stateful session bean) | |
| 298 | */ | |
| 299 | 0 | public TimerService getTimerService() throws IllegalStateException { |
| 300 | 0 | return new DummyTimerService(); |
| 301 | } | |
| 302 | ||
| 303 | /** | |
| 304 | * @return string representation. | |
| 305 | */ | |
| 306 | 0 | @Override |
| 307 | public String toString() { | |
| 308 | 0 | StringBuilder sb = new StringBuilder(); |
| 309 | // classname | |
| 310 | 0 | sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1)); |
| 311 | 0 | return sb.toString(); |
| 312 | } | |
| 313 | ||
| 314 | /** | |
| 315 | * Lookup object with given name. | |
| 316 | * @param name given name | |
| 317 | * @return result of the lookup | |
| 318 | */ | |
| 319 | 0 | public Object lookup(final String name) { |
| 320 | // Search in java:comp/env first | |
| 321 | 0 | try { |
| 322 | 0 | return new InitialContext().lookup(JAVA_COMP_ENV + name); |
| 323 | } catch (NamingException ne) { | |
| 324 | // try in registry | |
| 325 | 0 | try { |
| 326 | 0 | return new InitialContext().lookup(name); |
| 327 | } catch (NamingException e) { | |
| 328 | 0 | throw new IllegalArgumentException("Lookup on '" + name + "' was not found"); |
| 329 | } | |
| 330 | } | |
| 331 | } | |
| 332 | ||
| 333 | /** | |
| 334 | * Dummy class for handling Principal. | |
| 335 | * @author Florent Benoit | |
| 336 | */ | |
| 337 | class DummyPrincipal implements Principal { | |
| 338 | ||
| 339 | /** | |
| 340 | * @return an unauthenticated name | |
| 341 | */ | |
| 342 | 0 | public String getName() { |
| 343 | 0 | return "UNAUTHENTICATED_EASYBEANS"; |
| 344 | } | |
| 345 | ||
| 346 | } | |
| 347 | ||
| 348 | /** | |
| 349 | * Dummy class for handling Timer Service. | |
| 350 | * @author Florent Benoit | |
| 351 | */ | |
| 352 | class DummyTimerService implements TimerService { | |
| 353 | ||
| 354 | /** | |
| 355 | * Create an interval timer whose first expiration occurs after a | |
| 356 | * specified duration, and whose subsequent expirations occur after a | |
| 357 | * specified interval. | |
| 358 | * @param initialDuration - The number of milliseconds that must elapse | |
| 359 | * before the first timer expiration notification. | |
| 360 | * @param intervalDuration - The number of milliseconds that must elapse | |
| 361 | * between timer expiration notifications. Expiration | |
| 362 | * notifications are scheduled relative to the time of the first | |
| 363 | * expiration. If expiration is delayed(e.g. due to the | |
| 364 | * interleaving of other method calls on the bean) two or more | |
| 365 | * expiration notifications may occur in close succession to | |
| 366 | * "catch up". | |
| 367 | * @param info - Application information to be delivered along with the | |
| 368 | * timer expiration. This can be null. | |
| 369 | * @return The newly created Timer. | |
| 370 | * @throws IllegalArgumentException - If initialDuration is negative, or | |
| 371 | * intervalDuration is negative. | |
| 372 | * @throws IllegalStateException - If this method is invoked while the | |
| 373 | * instance is in a state that does not allow access to this | |
| 374 | * method. | |
| 375 | * @throws EJBException - If this method could not complete due to a | |
| 376 | * system-level failure. | |
| 377 | */ | |
| 378 | 0 | public Timer createTimer(final Date initialDuration, final long intervalDuration, final Serializable info) |
| 379 | throws IllegalArgumentException, IllegalStateException, EJBException { | |
| 380 | 0 | throw new IllegalStateException("Not yet implemented"); |
| 381 | } | |
| 382 | ||
| 383 | /** | |
| 384 | * Create a single-action timer that expires at a given point in time. | |
| 385 | * @param expiration - The point in time at which the timer must expire. | |
| 386 | * @param info - Application information to be delivered along with the | |
| 387 | * timer expiration notification. This can be null. | |
| 388 | * @return The newly created Timer. | |
| 389 | * @throws IllegalArgumentException - If expiration is null, or | |
| 390 | * expiration.getTime() is negative. | |
| 391 | * @throws IllegalStateException - If this method is invoked while the | |
| 392 | * instance is in a state that does not allow access to this | |
| 393 | * method. | |
| 394 | * @throws EJBException - If this method could not complete due to a | |
| 395 | * system-level failure. | |
| 396 | */ | |
| 397 | 0 | public Timer createTimer(final Date expiration, final Serializable info) throws IllegalArgumentException, |
| 398 | IllegalStateException, EJBException { | |
| 399 | 0 | throw new IllegalStateException("Not yet implemented"); |
| 400 | } | |
| 401 | ||
| 402 | /** | |
| 403 | * Create an interval timer whose first expiration occurs after a | |
| 404 | * specified duration, and whose subsequent expirations occur after a | |
| 405 | * specified interval. | |
| 406 | * @param initialDuration - The number of milliseconds that must elapse | |
| 407 | * before the first timer expiration notification. | |
| 408 | * @param intervalDuration - The number of milliseconds that must elapse | |
| 409 | * between timer expiration notifications. Expiration | |
| 410 | * notifications are scheduled relative to the time of the first | |
| 411 | * expiration. If expiration is delayed(e.g. due to the | |
| 412 | * interleaving of other method calls on the bean) two or more | |
| 413 | * expiration notifications may occur in close succession to | |
| 414 | * "catch up". | |
| 415 | * @param info - Application information to be delivered along with the | |
| 416 | * timer expiration. This can be null. | |
| 417 | * @return The newly created Timer. | |
| 418 | * @throws IllegalArgumentException - If initialDuration is negative, or | |
| 419 | * intervalDuration is negative. | |
| 420 | * @throws IllegalStateException - If this method is invoked while the | |
| 421 | * instance is in a state that does not allow access to this | |
| 422 | * method. | |
| 423 | * @throws EJBException - If this method could not complete due to a | |
| 424 | * system-level failure. | |
| 425 | */ | |
| 426 | 0 | public Timer createTimer(final long initialDuration, final long intervalDuration, final Serializable info) |
| 427 | throws IllegalArgumentException, IllegalStateException, EJBException { | |
| 428 | 0 | throw new IllegalStateException("Not yet implemented"); |
| 429 | } | |
| 430 | ||
| 431 | /** | |
| 432 | * Create a single-action timer that expires after a specified duration. | |
| 433 | * @param duration - The number of milliseconds that must elapse before | |
| 434 | * the timer expires. | |
| 435 | * @param info - Application information to be delivered along with the | |
| 436 | * timer expiration notification. This can be null. | |
| 437 | * @return The newly created Timer. | |
| 438 | * @throws IllegalArgumentException - If duration is negative | |
| 439 | * @throws IllegalStateException - If this method is invoked while the | |
| 440 | * instance is in a state that does not allow access to this | |
| 441 | * method. | |
| 442 | * @throws EJBException - If this method fails due to a system-level | |
| 443 | * failure. | |
| 444 | */ | |
| 445 | 0 | public Timer createTimer(final long duration, final Serializable info) throws IllegalArgumentException, |
| 446 | IllegalStateException, EJBException { | |
| 447 | 0 | throw new IllegalStateException("Not yet implemented"); |
| 448 | } | |
| 449 | ||
| 450 | /** | |
| 451 | * Get all the active timers associated with this bean. | |
| 452 | * @return A collection of javax.ejb.Timer objects. | |
| 453 | * @throws IllegalStateException - If this method is invoked while the | |
| 454 | * instance is in a state that does not allow access to this | |
| 455 | * method. | |
| 456 | * @throws EJBException - If this method could not complete due to a | |
| 457 | * system-level failure. | |
| 458 | */ | |
| 459 | 0 | public Collection getTimers() throws IllegalStateException, EJBException { |
| 460 | 0 | throw new IllegalStateException("Not yet implemented"); |
| 461 | } | |
| 462 | ||
| 463 | } | |
| 464 | } |
|
||||||||||