container/activities/src/main/java/org/mobicents/slee/runtime/activity/ActivityContextInterfaceImpl.java
public void attach(SbbLocalObject sbbLocalObject)
			throws NullPointerException, TransactionRequiredLocalException,
			TransactionRolledbackLocalException, SLEEException {

		if (doTraceLogs) {
        	logger.trace("attach( ac = "+activityContext+" , sbbLocalObject = " + sbbLocalObject + " )");
		}
		
		if (sbbLocalObject == null)
			throw new NullPointerException("null SbbLocalObject !");

		sleeContainer.getTransactionManager().mandateTransaction();

		org.mobicents.slee.container.sbb.SbbLocalObject sbbLocalObjectImpl = (org.mobicents.slee.container.sbb.SbbLocalObject) sbbLocalObject;

		SbbEntity sbbEntity = sbbLocalObjectImpl.getSbbEntity();

		boolean attached = getActivityContext().attachSbbEntity(sbbEntity.getSbbEntityId());

		boolean setRollbackAndThrowException = false;

		if (attached) {
			try {
				if (sbbEntity.isRemoved()) {
					setRollbackAndThrowException = true;
				} else {
					// attach entity from ac
					sbbEntity.afterACAttach(getActivityContext().getActivityContextHandle());
				}
			} catch (Exception e) {
				setRollbackAndThrowException = true;
			}
		}

		if (setRollbackAndThrowException) {
			try {
				sleeContainer.getTransactionManager().setRollbackOnly();
			} catch (SystemException e) {
				logger
						.warn(
								"failed to set rollback flag while asserting valid sbb entity",
								e);
			}
			throw new TransactionRolledbackLocalException(
					"Failed to attach invalid sbb entity. SbbID " + sbbEntity.getSbbEntityId());
		}

		if (attached) {

			//            	JSLEE 1.0 Spec, Section 8.5.8 excerpt:
			//        		The SLEE delivers the event to an SBB entity that stays attached once. The SLEE may deliver the
			//        		event to the same SBB entity more than once if it has been detached and then re -attached. 
			final EventRoutingTask routingTask = activityContext.getLocalActivityContext().getCurrentEventRoutingTask();
			EventContext eventContextImpl = routingTask != null ? routingTask.getEventContext() : null; 
			if (eventContextImpl != null && eventContextImpl.getSbbEntitiesThatHandledEvent().remove(sbbEntity.getSbbEntityId())) {
				if (doTraceLogs) {
		        	logger.trace("Removed the SBB Entity ["
									+ sbbEntity.getSbbEntityId()
									+ "] from the delivered set of activity context ["
									+ getActivityContext().getActivityContextHandle()
									+ "]. Seems to be a reattachment after detachment in the same event delivery transaction. See JSLEE 1.0 Spec, Section 8.5.8.");
				}
			}
		}

	}
public void attach(SbbLocalObject sbbLocalObject)
			throws NullPointerException, TransactionRequiredLocalException,
			TransactionRolledbackLocalException, SLEEException {

		if (doTraceLogs) {
        	logger.trace("attach( ac = "+activityContext+" , sbbLocalObject = " + sbbLocalObject + " )");
		}
		
		if (sbbLocalObject == null)
			throw new NullPointerException("null SbbLocalObject !");

		sleeContainer.getTransactionManager().mandateTransaction();

		org.mobicents.slee.container.sbb.SbbLocalObject sbbLocalObjectImpl = (org.mobicents.slee.container.sbb.SbbLocalObject) sbbLocalObject;

		SbbEntity sbbEntity = sbbLocalObjectImpl.getSbbEntity();

		boolean attached = getActivityContext().attachSbbEntity(sbbEntity.getSbbEntityId());

		boolean setRollbackAndThrowException = false;

		if (attached) {
			try {
				if (sbbEntity.isRemoved()) {
					setRollbackAndThrowException = true;
				} else {
					// attach entity from ac
					sbbEntity.afterACAttach(getActivityContext().getActivityContextHandle());
				}
			} catch (Exception e) {
				setRollbackAndThrowException = true;
			}
		}

		if (setRollbackAndThrowException) {
			try {
				sleeContainer.getTransactionManager().setRollbackOnly();
			} catch (SystemException e) {
				logger
						.warn(
								"failed to set rollback flag while asserting valid sbb entity",
								e);
			}
			throw new TransactionRolledbackLocalException(
					"Failed to attach invalid sbb entity. SbbID " + sbbEntity.getSbbEntityId());
		}

		if (attached) {

			//            	JSLEE 1.0 Spec, Section 8.5.8 excerpt:
			//        		The SLEE delivers the event to an SBB entity that stays attached once. The SLEE may deliver the
			//        		event to the same SBB entity more than once if it has been detached and then re -attached. 
			final EventRoutingTask routingTask = activityContext.getLocalActivityContext().getCurrentEventRoutingTask();
			EventContext eventContextImpl = routingTask != null ? routingTask.getEventContext() : null; 
			if (eventContextImpl != null && eventContextImpl.getSbbEntitiesThatHandledEvent().remove(sbbEntity.getSbbEntityId())) {
				if (doTraceLogs) {
		        	logger.trace("Removed the SBB Entity ["
									+ sbbEntity.getSbbEntityId()
									+ "] from the delivered set of activity context ["
									+ getActivityContext().getActivityContextHandle()
									+ "]. Seems to be a reattachment after detachment in the same event delivery transaction. See JSLEE 1.0 Spec, Section 8.5.8.");
				}
			}
		}

	}
/*
(non-Javadoc)

@see javax.slee.ActivityContextInterface#attach(javax.slee.SbbLocalObject)