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
31 public class StateMachine
32 implements Serializable {
33
34
35 private ServicingControllerEJB sccejb;
36
37
38 private ModelUpdateManager mum;
39
40
41 private HashMap attributeMap;
42
43
44 private HashMap handlerMap;
45
46
47 private SessionContext sc;
48
49
50 private Debug debug = null;
51
52
53
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
82 public Collection handleEvent(ServicingEvent ese)
83 throws RemoteException, ServicingEventException {
84
85 Object answer = "";
86
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
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
140 private String getHandlerName(String eventName)
141 throws ServicingEventException {
142
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
161 public void setAttribute(String key, Object value) {
162 attributeMap.put(key, value);
163 }
164
165
166
172 public Object getAttribute(String key) {
173 return attributeMap.get(key);
174 }
175
176
177
182 public ServicingControllerEJB getServicingControllerEJB() {
183 return sccejb;
184 }
185
186
187
192 public SessionContext getSessionContext() {
193 return sc;
194 }
195
196 }
197
198