1    package com.instantbank.servicing.control.ejb;
2    
3    import java.io.Serializable;
4    import java.util.Collection;
5    import java.util.List;
6    import java.util.HashMap;
7    
8    import java.rmi.RemoteException;
9    import javax.naming.InitialContext;
10   import javax.naming.NamingException;
11   import javax.ejb.SessionContext;
12   
13   import com.instantbank.servicing.control.ejb.handlers.StateHandler;
14   import com.instantbank.common.utilcomponents.Debug;
15   import com.instantbank.common.utilcomponents.ServicingExceptionMessage;
16   import com.instantbank.servicing.control.ServicingEventException;
17   import com.instantbank.servicing.control.event.ServicingEvent;
18   
19   /**
20    *  This class is a Universal front back end of an application which ties all
21    *  EJB components together dynamically at runtime. It is a javaBean which works
22    *  for Controller EJB. This class should not be updated to handle various event
23    *  types. This class will use StateHandler objects to handle events that
24    *  require processing beyond the scope of this class. A second option to event
25    *  handling is to do so in the XML descriptor itself. State may be stored in
26    *  the attributeMap Configuration of this file is via an XML descriptor.
27    *
28    * @author Instant-bank (Consuelo Franky)
29    * @created September 2002
30    */
31   public class StateMachine
32       implements Serializable {
33   
34     // controller EJB :
35     private ServicingControllerEJB sccejb;
36   
37     // builds list of model object names that were updated:
38     private ModelUpdateManager mum;
39   
40     // session attributes in Service Layer:
41     private HashMap attributeMap;
42   
43     // references to event handlers :
44     private HashMap handlerMap;
45   
46     // context:
47     private SessionContext sc;
48   
49     // debug:
50     private Debug debug = null;
51   
52   
53     /**
54      *  constructor: instances attributes
55      *
56      * @param sccejb controller ejb
57      * @param sc context
58      */
59     public StateMachine(ServicingControllerEJB sccejb, SessionContext sc) {
60       debug = new Debug();
61       debug.setDebugginOn(true);
62       debug.setPreMessage("** StateMachine: ");
63       this.sccejb = sccejb;
64       this.sc = sc;
65       this.mum = new ModelUpdateManager();
66       attributeMap = new HashMap();
67       handlerMap = new HashMap();
68     }
69   
70   
71     /**
72      *  process event corresponding to user request, through associated handler
73      *
74      * @param ese event corresponding to user request
75      * @return list of names of model objects that changed with the event processing;
76      *      this list is builded by  num (ModelUpdateManager).
77      *      In addition, the first element of the list contains the service answer
78      *      (or the "" string when this answer is not required)
79      * @exception ServicingEventException
80      * @throws RemoteException Description of the Exception
81      */
82     public Collection handleEvent(ServicingEvent ese)
83        throws RemoteException, ServicingEventException {
84   
85       Object answer = "";
86       // service answer given by the handler
87       debug.println("received event= " + ese);
88       String eventName = ese.getEventName();
89   
90       if(eventName != null) {
91         String handlerName = getHandlerName(eventName);
92   
93         StateHandler handler = null;
94         try {
95           if(handlerMap.get(eventName) != null) {
96             handler = (StateHandler)handlerMap.get(eventName);
97           }
98           else {
99             handler = (StateHandler)Class.forName(handlerName)
100              .newInstance();
101            handlerMap.put(eventName, handler);
102          }
103        }
104        catch(Exception ex) {
105          debug.println("error loading " + handlerName + " :" + ex);
106          throw new ServicingEventException
107            (ServicingExceptionMessage.HANDLER_NOT_LOAD + ex.getMessage());
108        }
109  
110        if(handler != null) {
111          handler.init(this);
112  
113          // do the magic:
114          handler.doStart();
115          answer = handler.perform(ese);
116          handler.doEnd();
117          debug.println("totally processed :" + eventName);
118          if(answer == null) {
119            throw new ServicingEventException
120              (ServicingExceptionMessage.NULL_ANSWER);
121          }
122        }
123      }
124      else {
125        debug.println("eventName is null");
126        throw new ServicingEventException
127          (ServicingExceptionMessage.EVENT_NOT_KNOWN + ese.toString());
128      }
129      return (mum.getUpdatedModels(ese, answer));
130    }
131  
132  
133    /**
134     *  get in context the name of handler class for the event
135     *
136     * @param eventName name of event corresponding to user request
137     * @return The handlerName value
138     * @exception ServicingEventException
139     */
140    private String getHandlerName(String eventName)
141       throws ServicingEventException {
142      // do the lookup
143      try {
144        InitialContext ic = new InitialContext();
145        return (String)ic.lookup(eventName);
146      }
147      catch(javax.naming.NamingException ex) {
148        debug.println("Handler caught: " + ex);
149        throw new ServicingEventException
150          (ServicingExceptionMessage.HANDLER_NOT_KNOWN + ex.getMessage());
151      }
152    }
153  
154  
155    /**
156     *  register a attribute of the user session
157     *
158     * @param key The new attribute key
159     * @param value The new attribute value
160     */
161    public void setAttribute(String key, Object value) {
162      attributeMap.put(key, value);
163    }
164  
165  
166    /**
167     *  get value of user session attribute
168     *
169     * @param key attribute key
170     * @return The attribute value
171     */
172    public Object getAttribute(String key) {
173      return attributeMap.get(key);
174    }
175  
176  
177    /**
178     *  get reference to controller ejb
179     *
180     * @return The ServicingControllerEJB value
181     */
182    public ServicingControllerEJB getServicingControllerEJB() {
183      return sccejb;
184    }
185  
186  
187    /**
188     *  get SessionContext
189     *
190     * @return The sessionContext value
191     */
192    public SessionContext getSessionContext() {
193      return sc;
194    }
195  
196  }
197  
198