Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mobicents/slee/runtime/ActivityContextInterfaceImpl.java


1   /*
2    * Created on Jul 30, 2004
3    *
4    *The Open SLEE project
5    *
6    * The source code contained in this file is in in the public domain.          
7    * It can be used in any project or product without prior permission,         
8    * license or royalty payments. There is no claim of correctness and
9    * NO WARRANTY OF ANY KIND provided with this code.
10   *
11   */
12  
13  package org.mobicents.slee.runtime;
14  
15  import javax.slee.ActivityContextInterface;
16  import javax.slee.SLEEException;
17  import javax.slee.SbbLocalObject;
18  import javax.slee.TransactionRequiredLocalException;
19  import javax.slee.TransactionRolledbackLocalException;
20  import javax.transaction.SystemException;
21  
22  import org.jboss.logging.Logger;
23  import org.mobicents.slee.container.SleeContainer;
24  import org.mobicents.slee.resource.SleeActivityHandle;
25  import org.mobicents.slee.runtime.transaction.SleeTransactionManager;
26  
27  /** 
28   * 
29   * Activity context interface - default implementation. The Sbb deployer has
30   * to imbed an instance of this as a proxy object in each sbb ACI. 
31   * 
32   * This is the SLEE wrapper data structure for Activity Contexts. The Sbb gets to
33   * access this rather than the activity. The reason this exists is because the 
34   * activity context can be at a different location than the activity context 
35   * interface (does not need to be co-located in the same jvm. )
36   * 
37   * @author M. Ranganathan
38   * @author Ralf Siedow
39   *
40   */
41  public class ActivityContextInterfaceImpl implements ActivityContextIDInterface, ActivityContextInterface {
42    // anchor
43    private SleeContainer serviceContainer;
44    
45    private ActivityContextFactory acif;
46    
47    private ActivityContext localAc;
48    
49    private Object activity;
50    
51    // The activity context to which I am assigned.
52    
53    private String acId;
54  
55    private static  Logger logger;
56  
57    
58    static {
59        logger = Logger.getLogger( ActivityContextInterfaceImpl.class);
60    }
61    
62    /** This is allocated by the Slee to wrap an incoming event (activity).
63     * 
64     * @param serviceContainer
65     * @param activity
66     * @param activity
67     */
68    public ActivityContextInterfaceImpl (SleeContainer serviceContainer, 
69        String activityContextId) {
70        if ( activityContextId == null) throw new NullPointerException("Null activityContextId Crap!");
71      this.serviceContainer = serviceContainer;
72      this.acif = serviceContainer.getActivityContextFactory();
73      this.acId = activityContextId;
74      this.localAc = this.acif.getActivityContextByKey(acId);
75      logger.debug("Local ac is" + this.localAc);
76      
77      logger.debug("Local ac is in state:" + this.localAc.getState() + " and has id " + this.localAc.getActivityContextId());
78      
79      
80      
81    }
82    
83    
84    public ActivityContext getActivityContext(){
85      logger.debug("Getting activity context");
86      
87        return localAc;
88    }
89    
90    /* (non-Javadoc)
91     * @see javax.slee.ActivityContextInterface#getActivity()
92     */
93    public Object getActivity() throws TransactionRequiredLocalException,
94        SLEEException {
95      //serviceContainer.getTransactionManager().checkTransactionMandatory();
96      Object activity = acif.getActivityFromKey(acId);
97      if (activity instanceof SleeActivityHandle){
98          SleeActivityHandle sleeHandle = (SleeActivityHandle) activity;
99          Object ret = sleeHandle.getActivity();
100         logger.info("ACTIVITY CONTEXT INTERFACE getActivity: " + ret);
101         return ret;
102         
103     } else
104     return activity;
105   }
106 
107   /* (non-Javadoc)
108    * @see javax.slee.ActivityContextInterface#attach(javax.slee.SbbLocalObject)
109    */
110   public void attach(SbbLocalObject sbbLocalObject) throws NullPointerException,
111       TransactionRequiredLocalException,
112       TransactionRolledbackLocalException, SLEEException {
113       
114       if (sbbLocalObject == null) throw new NullPointerException("null SbbLocalObject !");
115       SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
116       txMgr.mandateTransaction();
117     SbbLocalObjectImpl sbbLocalObjectImpl = (SbbLocalObjectImpl) sbbLocalObject;
118 
119     String sbbeId = sbbLocalObjectImpl.getSbbEntityId(); 
120     
121     // SLEE Spec 1.0, section 7.4.2:
122     assertValidSbbEntity(txMgr, sbbeId);
123         
124     localAc.attach(sbbeId);
125     logger.debug("ActivityContextInterface.attach(): ACI attach Called for " 
126               + sbbLocalObject + " ACID = " + this.acId + "SbbEntityId " + sbbeId  );
127     
128      
129     
130      // Remve sbbeid from delivered set upon re-attach ( see Test 1929 )
131     // Check Test 1429  ( sleestate )  when this code is enabled
132     /*
133     if(localAc.getDeliveredSet().remove(sbbeId)) {
134         if ( logger.isDebugEnabled()) {
135           logger.debug("Removed the SBB Entity [" + 
136                   sbbeId + "] from the delivered set of activity context [" + 
137                 localAc.getActivityContextId() + "]. Seems to be a reattachment.");
138         }
139     }
140     */
141   }
142 
143   /**
144    * 
145    * The sbbeId argument must represent a valid SBB entity when this method is invoked.
146    * Otherwise, this method marks the current transaction for rollback and throws a javax.
147    * slee.TransactionRolledbackLocalException.
148    * 
149      * @param txMgr SleeTransacionManager
150      * @param sbbeId The SbbEntityID that needs to be verified
151      * @throws TransactionRolledbackLocalException
152      */
153     private void assertValidSbbEntity(SleeTransactionManager txMgr, String sbbeId) throws TransactionRolledbackLocalException {
154         try {
155         SbbEntity sbbEntity = serviceContainer.getSbbEntityFactory().getSbbEntity(sbbeId);
156         if (sbbEntity == null || sbbEntity.isRemoved()) {
157           txMgr.setRollbackOnly();  
158           throw new TransactionRolledbackLocalException("Failed to attach invalid sbb entity. SbbID " + sbbeId);
159         }
160     } catch (Exception e) {
161         if (e instanceof TransactionRolledbackLocalException) throw (TransactionRolledbackLocalException)e;
162           try {
163                 txMgr.setRollbackOnly();
164             } catch (SystemException e1) {
165                 logger.error("Failed to setRollbackOnly()", e1);
166             }  
167           throw new TransactionRolledbackLocalException("Failed to attach invalid sbb entity. SbbID " + sbbeId);
168     }
169     }
170 
171 
172     /* (non-Javadoc)
173    * @see javax.slee.ActivityContextInterface#detach(javax.slee.SbbLocalObject)
174    */
175   public void detach(SbbLocalObject sbbLocalObject) throws NullPointerException,
176       TransactionRequiredLocalException,
177       TransactionRolledbackLocalException, SLEEException {
178         if (logger.isDebugEnabled()) {
179             logger.debug("ACI detach called for : " + sbbLocalObject + " ACID = " + this.acId);
180         }
181   
182         
183         if (sbbLocalObject == null) throw new NullPointerException("null SbbLocalObject !");
184         SleeTransactionManager txMgr = SleeContainer.getTransactionManager();
185         txMgr.mandateTransaction();
186       SbbLocalObjectImpl sbbLocalObjectImpl = (SbbLocalObjectImpl) sbbLocalObject;
187 
188       String sbbeId = sbbLocalObjectImpl.getSbbEntityId(); 
189 
190       assertValidSbbEntity(txMgr, sbbeId);
191       
192       //acif.getActivityContextByKey(acId).detach(entity);
193       localAc.detach(sbbeId);
194       if(localAc.getDeliveredSet().remove(sbbeId)) {
195           if ( logger.isDebugEnabled()) {
196             logger.debug("Removed the SBB Entity [" + 
197                     sbbeId + "] from the delivered set of activity context [" + 
198                   localAc.getActivityContextId() + "]. Seems to be a reattachment.");
199           }
200       }
201   }
202 
203   /* (non-Javadoc)
204    * @see javax.slee.ActivityContextInterface#isEnding()
205    */
206   public boolean isEnding() throws TransactionRequiredLocalException,
207       SLEEException {
208     //this.serviceContainer.getTransactionManager().checkTransaction();
209     return localAc.isEnding();
210   }
211 
212 
213 
214     public String retrieveActivityContextID() {
215        
216         return acId;
217     }
218 
219     
220     public ActivityContext retrieveActivityContext() {
221        
222         return this.getActivityContext();
223     }
224 
225 }