001: package org.concern.controller;
002:
003: import org.hibernate.HibernateException;
004: import org.hibernate.SessionFactory;
005: import org.hibernate.cfg.ImprovedNamingStrategy;
006: import org.concern.ControllerException;
007: import org.concern.model.Activity;
008: import org.concern.model.Condition;
009: import org.concern.model.*;
010: import org.concern.model.Actor;
011: import org.concern.model.Event;
012: import org.concern.model.Listener;
013: import org.concern.model.Process;
014: import org.concern.model.cpd.Dispatcher;
015: import org.concern.model.cpd.ProcessHandler;
016: import org.apache.commons.logging.LogFactory;
017:
018: import javax.transaction.TransactionManager;
019: import javax.xml.parsers.SAXParser;
020: import javax.xml.parsers.SAXParserFactory;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.beans.*;
024: import java.util.*;
025:
026: /**
027: * Set the URL or the process and classLoader. Optionally set the hibernateConfiguration.
028: * Then call buildController().
029: */
030: public class Configuration {
031: private static org.apache.commons.logging.Log LOG = LogFactory
032: .getLog(Configuration.class);
033:
034: private Process process;
035: private ClassLoader classLoader;
036: private TransactionManager transactionManager;
037: private SessionFactory sessionFactory;
038: private ResourceLocator resourceLocator;
039: private ParameterResolver parameterResolver;
040: private long timerInterval = 10000;
041:
042: public void setClassLoader(ClassLoader classLoader) {
043: //this.classLoader = classLoader;
044: }
045:
046: public void setProcess(Process process) {
047: this .process = process;
048: }
049:
050: public void setResourceLocator(ResourceLocator resourceLocator) {
051: this .resourceLocator = resourceLocator;
052: }
053:
054: public void setSessionFactory(SessionFactory sessionFactory) {
055: this .sessionFactory = sessionFactory;
056: }
057:
058: public ParameterResolver getParameterResolver() {
059: return parameterResolver;
060: }
061:
062: public void setParameterResolver(ParameterResolver parameterResolver) {
063: this .parameterResolver = parameterResolver;
064: }
065:
066: protected SessionFactory getSessionFactory()
067: throws HibernateException {
068: return sessionFactory;
069: }
070:
071: public void setTransactionManager(
072: TransactionManager transactionManager) {
073: this .transactionManager = transactionManager;
074: }
075:
076: protected TransactionManager getTransactionManager()
077: throws HibernateException {
078: return transactionManager;
079: }
080:
081: public long getTimerInterval() {
082: return timerInterval;
083: }
084:
085: public void setTimerInterval(long timerInterval) {
086: this .timerInterval = timerInterval;
087: }
088:
089: public Process getProcess() {
090: return process;
091: }
092:
093: public ResourceLocator getResourceLocator() {
094: return resourceLocator;
095: }
096:
097: public static Process loadProcess(InputStream config) {
098: SAXParserFactory factory = SAXParserFactory.newInstance();
099: try {
100: SAXParser saxParser = factory.newSAXParser();
101: ProcessHandler processHandler = new ProcessHandler();
102: Dispatcher dispatcher = new Dispatcher(processHandler);
103: saxParser.parse(config, dispatcher);
104: return processHandler.getProcess();
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: } finally {
108: try {
109: if (config != null)
110: config.close();
111: } catch (IOException ignore) {
112: }
113: }
114: }
115:
116: public Controller buildController() {
117: try {
118: Controller controller = new Controller();
119: controller.setResourceLocator(resourceLocator);
120: controller.setTimerInterval(timerInterval);
121: controller.setSessionFactory(getSessionFactory());
122: controller.setConfiguration(this );
123: TransactionManager transactionManager = getTransactionManager();
124: if (transactionManager != null)
125: controller.setTransactionManager(transactionManager);
126:
127: initializeController(controller);
128: return controller;
129: } catch (Exception e) {
130: e.printStackTrace();
131: throw new ControllerException(e);
132: }
133: }
134:
135: public void initializeController(Controller controller)
136: throws ClassNotFoundException, InstantiationException,
137: IllegalAccessException {
138: controller.setProcessDescription(process);
139: LOG.info("Process: " + process.getName());
140:
141: {
142: Map environment = processEnvironment(process);
143: controller.setEnvironment(environment);
144: }
145:
146: if (process.getLoader() != null) {
147: org.concern.model.Loader loaderModel = process.getLoader();
148: Class loaderClass = getClassLoader().loadClass(
149: loaderModel.getImplementation());
150: org.concern.controller.Loader loader = (org.concern.controller.Loader) loaderClass
151: .newInstance();
152: loader.setController(controller);
153:
154: Map environment = loaderEnvironment(loaderModel);
155: loader.setEnvironment(environment);
156: configure(loader, environment);
157: controller.setLoader(loader);
158: LOG.info("Loader: " + loader.getName());
159: }
160:
161: List actors = new LinkedList();
162: for (Iterator iterator = process.getActors().iterator(); iterator
163: .hasNext();) {
164: org.concern.model.Actor actorModel = (Actor) iterator
165: .next();
166: Class actorClass = getClassLoader().loadClass(
167: actorModel.getImplementation());
168: org.concern.controller.Actor actor = (org.concern.controller.Actor) actorClass
169: .newInstance();
170: actor.setController(controller);
171:
172: Map environment = actorEnvironment(actorModel);
173: actor.setEnvironment(environment);
174: configure(actor, environment);
175: actors.add(actor);
176: LOG.info("Actor: " + actor.getName());
177: }
178: controller.setActors(actors);
179:
180: List conditions = new LinkedList();
181: for (Iterator iterator = process.getConditions().iterator(); iterator
182: .hasNext();) {
183: org.concern.model.Condition conditionModel = (Condition) iterator
184: .next();
185: Class conditionClass = getClassLoader().loadClass(
186: conditionModel.getImplementation());
187: org.concern.controller.Condition condition = (org.concern.controller.Condition) conditionClass
188: .newInstance();
189: condition.setController(controller);
190:
191: Map environment = conditionEnvironment(conditionModel);
192: condition.setEnvironment(environment);
193: configure(condition, environment);
194: conditions.add(condition);
195: LOG.info("Condition: " + condition.getName());
196: }
197: controller.setConditions(conditions);
198:
199: List activities = new LinkedList();
200: for (Iterator iterator = process.getActivities().iterator(); iterator
201: .hasNext();) {
202: Activity activityModel = (Activity) iterator.next();
203: Class activityClass = getClassLoader().loadClass(
204: activityModel.getImplementation());
205: org.concern.controller.Activity activity = (org.concern.controller.Activity) activityClass
206: .newInstance();
207: activity.setController(controller);
208:
209: Map environment = activityEnvironment(activityModel);
210: activity.setEnvironment(environment);
211: configure(activity, environment);
212: activities.add(activity);
213: LOG.info("Activity: " + activity.getName());
214: }
215: controller.setActivities(activities);
216:
217: List events = new LinkedList();
218: for (Iterator iterator = process.getEvents().iterator(); iterator
219: .hasNext();) {
220: Event eventModel = (Event) iterator.next();
221: Class eventClass = getClassLoader().loadClass(
222: eventModel.getImplementation());
223: org.concern.controller.Event event = (org.concern.controller.Event) eventClass
224: .newInstance();
225: event.setController(controller);
226:
227: Map environment = eventEnvironment(eventModel);
228: event.setEnvironment(environment);
229: configure(event, environment);
230: events.add(event);
231: LOG.info("Event: " + event.getName());
232: }
233: controller.setEvents(events);
234:
235: List listeners = new LinkedList();
236: for (Iterator iterator = process.getListeners().iterator(); iterator
237: .hasNext();) {
238: Listener listenerModel = (Listener) iterator.next();
239: Class listenerClass = getClassLoader().loadClass(
240: listenerModel.getImplementation());
241: org.concern.controller.Listener listener = (org.concern.controller.Listener) listenerClass
242: .newInstance();
243: listener.setController(controller);
244:
245: Map environment = listenerEnvironment(listenerModel);
246: listener.setEnvironment(environment);
247: configure(listener, environment);
248: listeners.add(listener);
249: LOG.info("Listener: " + listener.getName());
250: }
251: controller.setListeners(listeners);
252: }
253:
254: protected void configure(Node node, Map environment) {
255: PropertyDescriptor[] descriptors = new PropertyDescriptor[0];
256: try {
257: descriptors = Introspector.getBeanInfo(node.getClass())
258: .getPropertyDescriptors();
259: for (int i = 0; i < descriptors.length; i++) {
260: PropertyDescriptor descriptor = descriptors[i];
261: if (descriptor.getWriteMethod() == null)
262: continue;
263:
264: Object value = environment.get(descriptor.getName());
265: if (value == null)
266: continue;
267:
268: descriptor.getWriteMethod().invoke(node,
269: new Object[] { value });
270: }
271: } catch (Exception e) {
272: e.printStackTrace();
273: }
274: }
275:
276: private Map actorEnvironment(Actor actorModel) {
277: Map environment = environment(actorModel.getEnvironment());
278: environment.put("actor.name", actorModel.getName());
279: return environment;
280: }
281:
282: private Map processEnvironment(
283: org.concern.model.Process processModel) {
284: Map environment = environment(processModel.getEnvironment());
285: environment.put("process.name", processModel.getName());
286: return environment;
287: }
288:
289: private Map loaderEnvironment(org.concern.model.Loader loaderModel) {
290: Map environment = environment(loaderModel.getEnvironment());
291: environment.put("loader.name", loaderModel.getName());
292: return environment;
293: }
294:
295: private Map conditionEnvironment(
296: org.concern.model.Condition conditionModel) {
297: Map environment = environment(conditionModel.getEnvironment());
298: environment.put("condition.name", conditionModel.getName());
299: return environment;
300: }
301:
302: private Map activityEnvironment(
303: org.concern.model.Activity activityModel) {
304: Map environment = environment(activityModel.getEnvironment());
305: environment.put("activity.name", activityModel.getName());
306: environment.put("activity.precondition", activityModel
307: .getPrecondition());
308: environment.put("activity.postcondition", activityModel
309: .getPostcondition());
310: environment.put("activity.reentrant", Boolean
311: .valueOf(activityModel.isReentrant()));
312: if (activityModel instanceof org.concern.model.AsynchronousActivity) {
313: environment
314: .put(
315: "activity.actor",
316: ((org.concern.model.AsynchronousActivity) activityModel)
317: .getActor());
318: environment
319: .put(
320: "activity.timeout",
321: new Integer(
322: ((org.concern.model.AsynchronousActivity) activityModel)
323: .getTimeout()));
324: environment
325: .put(
326: "activity.optional",
327: ((org.concern.model.AsynchronousActivity) activityModel)
328: .isOptional() ? Boolean.TRUE
329: : Boolean.FALSE);
330: environment
331: .put(
332: "activity.user",
333: ((org.concern.model.AsynchronousActivity) activityModel)
334: .isUser() ? Boolean.TRUE
335: : Boolean.FALSE);
336: } else if (activityModel instanceof org.concern.model.SynchronousActivity) {
337: environment
338: .put(
339: "activity.retryDelay",
340: new Integer(
341: ((org.concern.model.SynchronousActivity) activityModel)
342: .getRetryDelay()));
343: environment
344: .put(
345: "activity.trials",
346: new Integer(
347: ((org.concern.model.SynchronousActivity) activityModel)
348: .getTrials()));
349: } else {
350: System.out.println("dji ******************* class: "
351: + activityModel.getClass().getName());
352: throw new ClassCastException("No matching activity found.");
353: }
354:
355: return environment;
356: }
357:
358: private Map eventEnvironment(org.concern.model.Event eventModel) {
359: Map environment = environment(eventModel.getEnvironment());
360: environment.put("event.name", eventModel.getName());
361: environment.put("event.postcondition", eventModel
362: .getPostcondition());
363: return environment;
364: }
365:
366: private Map listenerEnvironment(
367: org.concern.model.Listener listenerModel) {
368: Map environment = environment(listenerModel.getEnvironment());
369: environment.put("listener.name", listenerModel.getName());
370: environment.put("listener.precondition", listenerModel
371: .getPrecondition());
372: environment.put("listener.retryDelay", new Integer(
373: listenerModel.getRetryDelay()));
374: environment.put("listener.trials", new Integer(listenerModel
375: .getTrials()));
376: return environment;
377: }
378:
379: private Map environment(List list) {
380: Map environment = new HashMap();
381: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
382: EnvironmentEntry entry = (EnvironmentEntry) iterator.next();
383: Object value;
384: if (entry.getValue().startsWith("resource:"))
385: value = resourceLocator.lookup(entry.getValue()
386: .substring("resource:".length()));
387: else if (entry.getValue().startsWith("parameter:"))
388: value = parameterResolver
389: .resolve(process.getName(), entry.getValue()
390: .substring("parameter:".length()),
391: entry.getType());
392: else
393: value = EnvironmentEntry.createInstance(
394: entry.getType(), entry.getValue());
395: environment.put(entry.getName(), value);
396: }
397: return environment;
398: }
399:
400: public ClassLoader getClassLoader() {
401: return classLoader != null ? classLoader : Thread
402: .currentThread().getContextClassLoader();
403: }
404:
405: public static class PrefixNamingStrategy extends
406: ImprovedNamingStrategy {
407: private final String processName;
408:
409: public PrefixNamingStrategy(String processName) {
410: this .processName = processName.replace(' ', '_');
411: }
412:
413: public String propertyToTableName(String className,
414: String propertyName) {
415: return processName + "_" + propertyName;
416: }
417:
418: public String classToTableName(String string) {
419: return processName + "_" + super .classToTableName(string);
420: }
421:
422: public String tableName(String string) {
423: return processName + "_" + super.tableName(string);
424: }
425: }
426: }
|