1    package com.instantbank.lettertemplate.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.lettertemplate.control.ejb.handlers.StateHandler;
14   import com.instantbank.common.utilcomponents.Debug;
15   import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
16   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
17   import com.instantbank.lettertemplate.control.event.LetterTemplateEvent;
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 August 2002
30    */
31   public class StateMachine
32       implements Serializable {
33   
34     // controller EJB :
35     private LetterTemplateControllerEJB 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(LetterTemplateControllerEJB sccejb, SessionContext sc) {
60       debug = new Debug();
61       debug.setDebugginOn(true);
62       debug.setPreMessage("** StateMachine: ");
63       debug.println("StateMachine instanced");
64       this.sccejb = sccejb;
65       this.sc = sc;
66       this.mum = new ModelUpdateManager();
67       attributeMap = new HashMap();
68       handlerMap = new HashMap();
69     }
70   
71   
72     /**
73      *  process event corresponding to user request, through associated handler
74      *
75      * @param ese event corresponding to user request
76      * @return list of names of model objects that changed with the event processing;
77      *      this list is builded by  num (ModelUpdateManager).
78      *      In addition, the first element of the list contains the service answer
79      *      (or the "" string when this answer is not required)
80      * @exception LetterTemplateEventException
81      * @throws RemoteException Description of the Exception
82      */
83     public Collection handleEvent(LetterTemplateEvent ese)
84        throws RemoteException, LetterTemplateEventException {
85   
86       debug.println("received event= " + ese);
87       Object answer = "";
88       // service answer given by the handler
89       String eventName = ese.getEventName();
90   
91       if(eventName != null) {
92         String handlerName = getHandlerName(eventName);
93   
94         StateHandler handler = null;
95         try {
96           if(handlerMap.get(eventName) != null) {
97             handler = (StateHandler)handlerMap.get(eventName);
98           }
99           else {
100            handler = (StateHandler)Class.forName(handlerName)
101              .newInstance();
102            handlerMap.put(eventName, handler);
103          }
104        }
105        catch(Exception ex) {
106          debug.println("error loading " + handlerName + " :" + ex);
107          throw new LetterTemplateEventException
108            (LetterTemplateExceptionMessage.HANDLER_NOT_LOAD + ex.getMessage());
109        }
110  
111        if(handler != null) {
112          handler.init(this);
113  
114          // do the magic:
115          handler.doStart();
116          answer = handler.perform(ese);
117          handler.doEnd();
118          debug.println("totally processed :" + eventName);
119          if(answer == null) {
120            throw new LetterTemplateEventException
121              (LetterTemplateExceptionMessage.NULL_ANSWER);
122          }
123        }
124      }
125      else {
126        debug.println("eventName is null");
127        throw new LetterTemplateEventException
128          (LetterTemplateExceptionMessage.EVENT_NOT_KNOWN + ese.toString());
129      }
130      return (mum.getUpdatedModels(ese, answer));
131    }
132  
133  
134    /**
135     *  get in context the name of handler class for the event
136     *
137     * @param eventName name of event corresponding to user request
138     * @return The handlerName value
139     * @exception LetterTemplateEventException
140     */
141    private String getHandlerName(String eventName)
142       throws LetterTemplateEventException {
143      // do the lookup
144      try {
145        InitialContext ic = new InitialContext();
146        return (String)ic.lookup(eventName);
147      }
148      catch(javax.naming.NamingException ex) {
149        debug.println("Handler caught: " + ex);
150        throw new LetterTemplateEventException
151          (LetterTemplateExceptionMessage.HANDLER_NOT_KNOWN + ex.getMessage());
152      }
153    }
154  
155  
156    /**
157     *  register a attribute of the user session
158     *
159     * @param key The new attribute key
160     * @param value The new attribute value
161     */
162    public void setAttribute(String key, Object value) {
163      attributeMap.put(key, value);
164    }
165  
166  
167    /**
168     *  get value of user session attribute
169     *
170     * @param key attribute key
171     * @return The attribute value
172     */
173    public Object getAttribute(String key) {
174      return attributeMap.get(key);
175    }
176  
177  
178    /**
179     *  get reference to controller ejb
180     *
181     * @return The LetterTemplateControllerEJB value
182     */
183    public LetterTemplateControllerEJB getLetterTemplateControllerEJB() {
184      return sccejb;
185    }
186  
187  
188    /**
189     *  get SessionContext
190     *
191     * @return The sessionContext value
192     */
193    public SessionContext getSessionContext() {
194      return sc;
195    }
196  
197  }
198  
199