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

Quick Search    Search Deep

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


1   /*
2    * SbbContextImpl.java
3    *
4    * Created on June 29, 2004, 12:51 PM
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   * BLAH BLAH...
12   */
13  
14  package org.mobicents.slee.runtime;
15  
16  import java.io.Serializable;
17  import java.util.Iterator;
18  import java.util.Set;
19  
20  import javax.slee.ActivityContextInterface;
21  import javax.slee.NotAttachedException;
22  import javax.slee.SLEEException;
23  import javax.slee.SbbContext;
24  import javax.slee.SbbID;
25  import javax.slee.SbbLocalObject;
26  import javax.slee.ServiceID;
27  import javax.slee.TransactionRequiredLocalException;
28  import javax.slee.UnrecognizedEventException;
29  import javax.transaction.SystemException;
30  
31  import org.jboss.logging.Logger;
32  import org.mobicents.slee.container.SleeContainer;
33  
34  /**
35   * SBB Context Implementation.
36   * 
37   * The SLEE provides each SBB object with an SbbContext object. The SbbContext
38   * object gives the SBB object access to the SBB object’s context maintained by
39   * the SLEE, allows the SBB object to invoke functions provided by the SLEE, and
40   * obtain information about the SBB entity assigned to the SBB object. The
41   * SbbContext object implements the SbbContext interface. The SbbContext
42   * interface declares the following methods: · Methods to access information
43   * determined at runtime. o A getSbbLocalObject method to get an SBB local
44   * object that represents the SBB entity assigned to the SBB object of the
45   * SbbContext object. o A getService method to get a ServiceID object that
46   * encapsulates the component identity of the Service that the SBB entity is a
47   * descendent of, i.e. the SBB entity is in an SBB entity tree whose root SBB
48   * entity is instantiated by the SLEE for the child relation (from SLEE as
49   * parent to the root SBB) specified by the Service. o A getSbb method to get an
50   * SbbID object that encapsulates the component identity of the SBB. · Activity
51   * Context methods. These methods are discussed in greater detail in Section
52   * 7.7.1. o A getActivities method. This method returns all the Activity
53   * Contexts that the SBB entity assigned to the SBB object of the SbbContext
54   * object is attached to. More precisely, it returns an Activity Context
55   * Interface object for each Activity Context attached to the SBB entity
56   * assigned to the SBB object of the SbbContext object. · Event mask methods.
57   * These methods are described in Section 8.4.3. o A maskEvent method. This
58   * method masks event types that the SBB entity assigned to the SBB object of
59   * the SbbContext object no longer wishes to receive from an Activity Context. o
60   * A getEventMask method. This returns the set of masked event types for an
61   * Activity Context that SBB entity assigned to the SBB object of the SbbContext
62   * object is attached to. · Transaction methods. These methods are described in
63   * Section 6.10. o A setRollbackOnly method. The SBB Developer uses this method
64   * to mark the transaction of the current method invocation for rollback. o A
65   * getRollbackOnly method. The SBB Developer uses this method to determine if
66   * the transaction of the current method invocation has been marked for
67   * rollback.
68   * 
69   * @author M. Ranganathan
70   * @author F. Moggia
71   */
72  public class SbbContextImpl implements SbbContext, Serializable {
73      
74      volatile private static Logger logger;
75     
76    
77  
78      /** The SBB entity to which I am assigned. */
79      private SbbObject sbbObject;
80  
81      /** The service container to which I am associated. */
82      volatile protected SleeContainer serviceContainer;
83      
84      static {
85          logger  = Logger.getLogger(SbbContextImpl.class );
86      }
87  
88      /** Creates a new instance of SbbContextImpl */
89      public SbbContextImpl(SbbObject sbbObject, SleeContainer serviceContainer) {
90          this.sbbObject = sbbObject;
91          this.serviceContainer = serviceContainer;
92  
93      }
94  
95      public ActivityContextInterface[] getActivities()
96              throws TransactionRequiredLocalException, IllegalStateException,
97              SLEEException {
98          
99          logger.debug("getActivities() " + this.sbbObject.getState() );
100         if (SbbObjectState.READY !=  this.sbbObject.getState()) {
101             throw new IllegalStateException(
102                     "Cannot call SbbContext getActivities in "
103                             + this.sbbObject.getState());
104         }
105         Set activities = sbbObject.getSbbEntity().getActivityContexts();
106         ActivityContextInterface[] aci = new ActivityContextInterface[activities
107                 .size()];
108         if (logger.isDebugEnabled()) {
109             logger.debug("The Sbb is attached to " + activities.size()
110                 + "activities");
111         }
112         Iterator it = activities.iterator();
113         int i = 0;
114         while (it.hasNext()) {
115             String acId = (String) it.next();
116             aci[i++] = new ActivityContextInterfaceImpl(this.serviceContainer, acId);
117         }
118         return aci;
119 
120     }
121 
122     public String[] getEventMask(ActivityContextInterface aci)
123             throws NullPointerException, TransactionRequiredLocalException,
124             IllegalStateException, NotAttachedException, SLEEException {
125       if(aci == null) throw new NullPointerException("Activity Context Interface cannot be null.");
126       if(sbbObject == null || 
127               sbbObject.getState() != SbbObjectState.READY) 
128           throw new IllegalStateException("Wrong state! " + ( sbbObject == null ? null : sbbObject.getState() ));
129 
130       SleeContainer.getTransactionManager().mandateTransaction();
131       //ActivityContextInterface sbbObjectAci = sbbObject.getActivityContextInterface(aci);
132       String acId = ((ActivityContextIDInterface)aci).retrieveActivityContextID();
133       if ( ! sbbObject.getSbbEntity().checkAttached(acId))
134           throw new NotAttachedException("ACI not attached to SBB");
135       
136         return sbbObject.getSbbEntity().getEventMask(acId);
137     }
138 
139     public boolean getRollbackOnly() throws TransactionRequiredLocalException,
140             SLEEException {
141         SleeContainer.getTransactionManager().mandateTransaction();
142         logger.debug("in getRollbackOnly on " + this);
143         try {
144             return SleeContainer.getTransactionManager().getRollbackOnly();
145         } catch (SystemException e) {
146             throw new SLEEException ("Problem with the tx manager!"  );
147         }
148     }
149 
150     public SbbID getSbb() throws SLEEException {
151         return (SbbID) this.sbbObject.getSbbDescriptor().getID();
152     }
153 
154     public SbbLocalObject getSbbLocalObject()
155             throws TransactionRequiredLocalException, IllegalStateException,
156             SLEEException {
157         SleeContainer.getTransactionManager().mandateTransaction();
158         if (this.sbbObject == null || this.sbbObject.getSbbEntity() == null ||
159                 ! this.sbbObject.getState().equals(SbbObjectState.READY))
160             throw new IllegalStateException("Bad state : " + this.sbbObject.getState());
161         Class sbbLocalClass;
162         if((sbbLocalClass = sbbObject.getSbbDescriptor().getLocalInterfaceConcreteClass())!= null) {
163             Object[] objs = {sbbObject.getSbbEntity()};
164             Class[] types = {SbbEntity.class};
165             try {
166                 return (SbbLocalObject) sbbLocalClass.getConstructor(types).newInstance(objs);
167             } catch (Exception e) {
168                 // TODO Auto-generated catch block
169                 e.printStackTrace();
170                 throw new RuntimeException("Failed to create Sbb Local Interface.", e);
171             }
172         }else {
173             return new SbbLocalObjectImpl(this.sbbObject.getSbbEntity());
174         }
175     }
176 
177     public ServiceID getService() throws SLEEException {
178         return this.sbbObject.getSbbEntity().getService().getServiceID();
179     }
180 
181     public void maskEvent(String[] eventNames, ActivityContextInterface aci)
182             throws NullPointerException, TransactionRequiredLocalException,
183             IllegalStateException, UnrecognizedEventException,
184             NotAttachedException, SLEEException {
185         if (SbbObjectState.READY != this.sbbObject.getState()) {
186             throw new IllegalStateException(
187                     "Cannot call SbbContext maskEvent in "
188                             + this.sbbObject.getState());
189         }
190         SleeContainer.getTransactionManager().mandateTransaction();
191         String acId = ((ActivityContextIDInterface)aci).retrieveActivityContextID();
192       
193         if ( !sbbObject.getSbbEntity().checkAttached(acId)) throw new NotAttachedException("ACI is not attached to SBB ");
194         sbbObject.getSbbEntity().setEventMask(acId, eventNames);
195 
196     }
197     /**
198      * A setRollbackOnly method. The SBB Developer uses this method
199      * to mark the transaction of the current method invocation for rollback.  A
200      * getRollbackOnly method. The SBB Developer uses this method to determine if
201      * the transaction of the current method invocation has been marked for
202      * rollback.
203      */
204     public void setRollbackOnly() throws TransactionRequiredLocalException,
205             SLEEException {
206         SleeContainer.getTransactionManager().mandateTransaction();
207         
208         logger.debug("in setRollbackOnly on " + this);
209         
210         try {
211             SleeContainer.getTransactionManager().setRollbackOnly();
212         } catch (SystemException e) {
213             throw new SLEEException ("tx manager failure!");
214         }
215         
216 
217 
218     }
219 
220     
221 
222 }