001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: SubmitScheduleRequest.java 9243 2007-09-25 00:49:54Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.messages;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import javax.xml.namespace.QName;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.dom.DOMSource;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.jdom.Document;
037: import org.jdom.Element;
038: import org.jdom.Namespace;
039: import org.jdom.input.DOMBuilder;
040: import org.jdom.output.DOMOutputter;
041:
042: import com.bostechcorp.cbesb.runtime.scheduler.AutoRetrySchedule;
043: import com.bostechcorp.cbesb.runtime.scheduler.ComponentSchedule;
044: import com.bostechcorp.cbesb.runtime.scheduler.ISchedule;
045: import com.bostechcorp.cbesb.runtime.scheduler.StandardSchedule;
046:
047: public class SubmitScheduleRequest extends SchedulerMessage {
048:
049: private QName triggerEndpointServiceName;
050: private String triggerEndpointEPName;
051: private QName jobEndpointServiceName;
052: private String jobEndpointEPName;
053: private ComponentSchedule schedule;
054:
055: /**
056: * @return the jobEndpointEPName
057: */
058: public String getJobEndpointEPName() {
059: return jobEndpointEPName;
060: }
061:
062: /**
063: * @param jobEndpointEPName the jobEndpointEPName to set
064: */
065: public void setJobEndpointEPName(String jobEndpointEPName) {
066: this .jobEndpointEPName = jobEndpointEPName;
067: }
068:
069: /**
070: * @return the jobEndpointServiceName
071: */
072: public QName getJobEndpointServiceName() {
073: return jobEndpointServiceName;
074: }
075:
076: /**
077: * @param jobEndpointServiceName the jobEndpointServiceName to set
078: */
079: public void setJobEndpointServiceName(QName jobEndpointServiceName) {
080: this .jobEndpointServiceName = jobEndpointServiceName;
081: }
082:
083: /**
084: * @return the schedule
085: */
086: public ComponentSchedule getSchedule() {
087: return schedule;
088: }
089:
090: /**
091: * @param schedule the schedule to set
092: */
093: public void setSchedule(ComponentSchedule schedule) {
094: this .schedule = schedule;
095: }
096:
097: /**
098: * @return the triggerEndpointEPName
099: */
100: public String getTriggerEndpointEPName() {
101: return triggerEndpointEPName;
102: }
103:
104: /**
105: * @param triggerEndpointEPName the triggerEndpointEPName to set
106: */
107: public void setTriggerEndpointEPName(String triggerEndpointEPName) {
108: this .triggerEndpointEPName = triggerEndpointEPName;
109: }
110:
111: /**
112: * @return the triggerEndpointServiceName
113: */
114: public QName getTriggerEndpointServiceName() {
115: return triggerEndpointServiceName;
116: }
117:
118: /**
119: * @param triggerEndpointServiceName the triggerEndpointServiceName to set
120: */
121: public void setTriggerEndpointServiceName(
122: QName triggerEndpointServiceName) {
123: this .triggerEndpointServiceName = triggerEndpointServiceName;
124: }
125:
126: /* (non-Javadoc)
127: * @see com.bostechcorp.cbesb.runtime.component.scheduler.messages.SchedulerMessage#getMessageType()
128: */
129: @Override
130: public QName getMessageType() {
131: return SchedulerMessageFactory.TYPE_SUBMIT_REQUEST;
132: }
133:
134: public static SchedulerMessage createFromSource(Source src)
135: throws Exception {
136: Log logger = LogFactory.getLog(SubmitScheduleRequest.class);
137: DOMBuilder domBuilder = new DOMBuilder();
138: Document jdomDoc = domBuilder.build(getDocFromSource(src));
139: Element rootElem = jdomDoc.getRootElement();
140:
141: HashMap<String, Namespace> nsMap = getNamespaceMap(rootElem);
142: if (rootElem.getNamespace().equals(schedulerNS)
143: && rootElem.getName().equals(SUBMIT_SCHEDULE)) {
144: SubmitScheduleRequest message = new SubmitScheduleRequest();
145:
146: // Element triggerEP = rootElem.getChild(TRIGGER_ENDPOINT, schedulerNS);
147: // if (triggerEP != null) {
148: // Element serviceName = triggerEP.getChild(SERVICE_NAME, schedulerNS);
149: // if (serviceName != null) {
150: // QName serviceNameQName = getQNameFromPrefixedName(serviceName.getValue(), nsMap);
151: // message.setTriggerEndpointServiceName(serviceNameQName);
152: // }
153: // Element endpointName = triggerEP.getChild(ENDPOINT_NAME, schedulerNS);
154: // if (endpointName != null) {
155: // String epName = endpointName.getValue();
156: // message.setTriggerEndpointEPName(epName);
157: // }
158: // } else {
159: // throw new Exception("Missing required element: " + TRIGGER_ENDPOINT);
160: // }
161:
162: Element jobEP = rootElem
163: .getChild(JOB_ENDPOINT, schedulerNS);
164: if (jobEP != null) {
165: Element serviceName = jobEP.getChild(SERVICE_NAME,
166: schedulerNS);
167: if (serviceName != null) {
168: QName serviceNameQName = getQNameFromPrefixedName(
169: serviceName.getValue(), nsMap);
170: message.setJobEndpointServiceName(serviceNameQName);
171: }
172: Element endpointName = jobEP.getChild(ENDPOINT_NAME,
173: schedulerNS);
174: if (endpointName != null) {
175: String epName = endpointName.getValue();
176: message.setJobEndpointEPName(epName);
177: }
178: } else {
179: throw new Exception("Missing required element: "
180: + JOB_ENDPOINT);
181: }
182:
183: Element schedules = rootElem.getChild(SCHEDULES,
184: schedulerNS);
185: if (schedules != null) {
186: ComponentSchedule compSched = new ComponentSchedule();
187: message.setSchedule(compSched);
188:
189: List children = schedules.getChildren();
190: for (Iterator iter = children.iterator(); iter
191: .hasNext();) {
192: Element schedule = (Element) iter.next();
193: if (schedule.getName().equals(STANDARD_SCHEDULE)) {
194: compSched
195: .addSchedule(parseStandardSchedule(schedule));
196: } else if (schedule.getName().equals(
197: AUTO_RETRY_SCHEDULE)) {
198: compSched.addSchedule(parseAutoRetrySchedule(
199: schedule, nsMap));
200: } else {
201: throw new Exception("Unrecognized element: "
202: + schedule.getName());
203: }
204: }
205: } else {
206: throw new Exception("Missing required element: "
207: + SCHEDULES);
208: }
209:
210: return message;
211:
212: } else {
213: throw new Exception("XML message is not {" + SCHEDULER_NS
214: + "}" + SUBMIT_SCHEDULE + " Recieved: "
215: + rootElem.getQualifiedName());
216: }
217: }
218:
219: private static StandardSchedule parseStandardSchedule(
220: Element scheduleElem) throws Exception {
221: StandardSchedule standSched = new StandardSchedule();
222: String value = scheduleElem.getChildText(SECONDS, schedulerNS);
223: if (value != null) {
224: standSched.setSeconds(value);
225: } else {
226: throw new Exception("Missing required element: " + SECONDS);
227: }
228: value = scheduleElem.getChildText(MINUTES, schedulerNS);
229: if (value != null) {
230: standSched.setMinutes(value);
231: } else {
232: throw new Exception("Missing required element: " + MINUTES);
233: }
234: value = scheduleElem.getChildText(HOURS, schedulerNS);
235: if (value != null) {
236: standSched.setHours(value);
237: } else {
238: throw new Exception("Missing required element: " + HOURS);
239: }
240: value = scheduleElem.getChildText(DAY_OF_MONTH, schedulerNS);
241: if (value != null) {
242: standSched.setDayOfMonth(value);
243: } else {
244: throw new Exception("Missing required element: "
245: + DAY_OF_MONTH);
246: }
247: value = scheduleElem.getChildText(MONTH, schedulerNS);
248: if (value != null) {
249: standSched.setMonth(value);
250: } else {
251: throw new Exception("Missing required element: " + MONTH);
252: }
253: value = scheduleElem.getChildText(DAY_OF_WEEK, schedulerNS);
254: if (value != null) {
255: standSched.setDayOfWeek(value);
256: } else {
257: throw new Exception("Missing required element: "
258: + DAY_OF_WEEK);
259: }
260: value = scheduleElem
261: .getChildText(HOLIDAY_SCHEDULE, schedulerNS);
262: if (value != null) {
263: standSched.setHolidaySchedule(value);
264: }
265:
266: return standSched;
267: }
268:
269: private static AutoRetrySchedule parseAutoRetrySchedule(
270: Element scheduleElem, HashMap<String, Namespace> nsMap)
271: throws Exception {
272: AutoRetrySchedule autoRetrySched = new AutoRetrySchedule();
273: String value = scheduleElem.getChildText(START_TIME,
274: schedulerNS);
275: if (value != null) {
276: autoRetrySched.setStartTime(value);
277: } else {
278: throw new Exception("Missing required element: "
279: + START_TIME);
280: }
281: value = scheduleElem.getChildText(END_TIME, schedulerNS);
282: if (value != null) {
283: autoRetrySched.setEndTime(value);
284: } else {
285: throw new Exception("Missing required element: " + END_TIME);
286: }
287: value = scheduleElem.getChildText(RETRY_INTERVAL, schedulerNS);
288: if (value != null) {
289: autoRetrySched.setRetryInterval(Integer.parseInt(value));
290: } else {
291: throw new Exception("Missing required element: "
292: + RETRY_INTERVAL);
293: }
294: value = scheduleElem.getChildText(DAY_OF_MONTH, schedulerNS);
295: if (value != null) {
296: autoRetrySched.setDayOfMonth(value);
297: } else {
298: throw new Exception("Missing required element: "
299: + DAY_OF_MONTH);
300: }
301: value = scheduleElem.getChildText(MONTH, schedulerNS);
302: if (value != null) {
303: autoRetrySched.setMonth(value);
304: } else {
305: throw new Exception("Missing required element: " + MONTH);
306: }
307: value = scheduleElem.getChildText(DAY_OF_WEEK, schedulerNS);
308: if (value != null) {
309: autoRetrySched.setDayOfWeek(value);
310: } else {
311: throw new Exception("Missing required element: "
312: + DAY_OF_WEEK);
313: }
314: value = scheduleElem
315: .getChildText(HOLIDAY_SCHEDULE, schedulerNS);
316: if (value != null) {
317: autoRetrySched.setHolidaySchedule(value);
318: }
319: Element successNotification = scheduleElem.getChild(
320: SUCCESS_NOTIFICATION_EP, schedulerNS);
321: if (successNotification != null) {
322: Element serviceName = successNotification.getChild(
323: SERVICE_NAME, schedulerNS);
324: if (serviceName != null) {
325: QName serviceNameQName = getQNameFromPrefixedName(
326: serviceName.getValue(), nsMap);
327: autoRetrySched
328: .setSuccessNotificationService(serviceNameQName);
329: }
330: Element endpointName = successNotification.getChild(
331: ENDPOINT_NAME, schedulerNS);
332: if (endpointName != null) {
333: String epName = endpointName.getValue();
334: autoRetrySched.setSuccessNotificationEndpoint(epName);
335: }
336: }
337: Element failureNotification = scheduleElem.getChild(
338: FAILURE_NOTIFICATION_EP, schedulerNS);
339: if (failureNotification != null) {
340: Element serviceName = failureNotification.getChild(
341: SERVICE_NAME, schedulerNS);
342: if (serviceName != null) {
343: QName serviceNameQName = getQNameFromPrefixedName(
344: serviceName.getValue(), nsMap);
345: autoRetrySched
346: .setFailureNotificationService(serviceNameQName);
347: }
348: Element endpointName = failureNotification.getChild(
349: ENDPOINT_NAME, schedulerNS);
350: if (endpointName != null) {
351: String epName = endpointName.getValue();
352: autoRetrySched.setFailureNotificationEndpoint(epName);
353: }
354: }
355:
356: return autoRetrySched;
357: }
358:
359: /* (non-Javadoc)
360: * @see com.bostechcorp.cbesb.runtime.component.scheduler.messages.SchedulerMessage#toSource()
361: */
362: @Override
363: public Source toSource() throws Exception {
364: HashMap<String, Namespace> nsMap = new HashMap<String, Namespace>();
365:
366: Element rootElem = new Element(SUBMIT_SCHEDULE, schedulerNS);
367: Document jdomDoc = new Document(rootElem);
368:
369: // Element triggerEP = new Element(TRIGGER_ENDPOINT, schedulerNS);
370: // rootElem.addContent(triggerEP);
371: // Element serviceName = new Element(SERVICE_NAME, schedulerNS);
372: // serviceName.setText(getPrefixedNameFromQName(getTriggerEndpointServiceName(), nsMap));
373: // triggerEP.addContent(serviceName);
374: // Element endpointName = new Element(ENDPOINT_NAME, schedulerNS);
375: // endpointName.setText(getTriggerEndpointEPName());
376: // triggerEP.addContent(endpointName);
377:
378: Element jobEP = new Element(JOB_ENDPOINT, schedulerNS);
379: rootElem.addContent(jobEP);
380: Element serviceName = new Element(SERVICE_NAME, schedulerNS);
381: serviceName.setText(getPrefixedNameFromQName(
382: getJobEndpointServiceName(), nsMap));
383: jobEP.addContent(serviceName);
384: Element endpointName = new Element(ENDPOINT_NAME, schedulerNS);
385: endpointName.setText(getJobEndpointEPName());
386: jobEP.addContent(endpointName);
387:
388: Element schedules = new Element(SCHEDULES, schedulerNS);
389: rootElem.addContent(schedules);
390:
391: List schedList = getSchedule().getSchedules();
392: for (Iterator iter = schedList.iterator(); iter.hasNext();) {
393: ISchedule sched = (ISchedule) iter.next();
394: if (sched instanceof StandardSchedule) {
395: schedules
396: .addContent(buildStandardScheduleElement((StandardSchedule) sched));
397: } else if (sched instanceof AutoRetrySchedule) {
398: schedules.addContent(buildAutoRetryScheduleElement(
399: (AutoRetrySchedule) sched, nsMap));
400: }
401: }
402:
403: addNamespaceDeclsToRoot(rootElem, nsMap);
404: DOMOutputter domOut = new DOMOutputter();
405: org.w3c.dom.Document domDoc = domOut.output(jdomDoc);
406: return new DOMSource(domDoc);
407: }
408:
409: private Element buildStandardScheduleElement(StandardSchedule sched) {
410: Element schedElem = new Element(STANDARD_SCHEDULE, schedulerNS);
411:
412: Element secondsElem = new Element(SECONDS, schedulerNS);
413: secondsElem.setText(sched.getSeconds());
414: schedElem.addContent(secondsElem);
415:
416: Element minutesElem = new Element(MINUTES, schedulerNS);
417: minutesElem.setText(sched.getMinutes());
418: schedElem.addContent(minutesElem);
419:
420: Element hoursElem = new Element(HOURS, schedulerNS);
421: hoursElem.setText(sched.getHours());
422: schedElem.addContent(hoursElem);
423:
424: Element dayOfMonthElem = new Element(DAY_OF_MONTH, schedulerNS);
425: dayOfMonthElem.setText(sched.getDayOfMonth());
426: schedElem.addContent(dayOfMonthElem);
427:
428: Element monthElem = new Element(MONTH, schedulerNS);
429: monthElem.setText(sched.getMonth());
430: schedElem.addContent(monthElem);
431:
432: Element dayOfWeekElem = new Element(DAY_OF_WEEK, schedulerNS);
433: dayOfWeekElem.setText(sched.getDayOfWeek());
434: schedElem.addContent(dayOfWeekElem);
435:
436: if (sched.getHolidaySchedule() != null
437: && sched.getHolidaySchedule().length() > 0) {
438: Element holidaySchedElem = new Element(HOLIDAY_SCHEDULE,
439: schedulerNS);
440: holidaySchedElem.setText(sched.getHolidaySchedule());
441: schedElem.addContent(holidaySchedElem);
442: }
443: return schedElem;
444: }
445:
446: private Element buildAutoRetryScheduleElement(
447: AutoRetrySchedule sched, HashMap<String, Namespace> nsMap) {
448: Element schedElem = new Element(AUTO_RETRY_SCHEDULE,
449: schedulerNS);
450:
451: Element startTimeElem = new Element(START_TIME, schedulerNS);
452: startTimeElem.setText(sched.getStartTime());
453: schedElem.addContent(startTimeElem);
454:
455: Element endTimeElem = new Element(END_TIME, schedulerNS);
456: endTimeElem.setText(sched.getEndTime());
457: schedElem.addContent(endTimeElem);
458:
459: Element retryIntervalElem = new Element(RETRY_INTERVAL,
460: schedulerNS);
461: retryIntervalElem.setText(Integer.toString(sched
462: .getRetryInterval()));
463: schedElem.addContent(retryIntervalElem);
464:
465: Element dayOfMonthElem = new Element(DAY_OF_MONTH, schedulerNS);
466: dayOfMonthElem.setText(sched.getDayOfMonth());
467: schedElem.addContent(dayOfMonthElem);
468:
469: Element monthElem = new Element(MONTH, schedulerNS);
470: monthElem.setText(sched.getMonth());
471: schedElem.addContent(monthElem);
472:
473: Element dayOfWeekElem = new Element(DAY_OF_WEEK, schedulerNS);
474: dayOfWeekElem.setText(sched.getDayOfWeek());
475: schedElem.addContent(dayOfWeekElem);
476:
477: if (sched.getHolidaySchedule() != null
478: && sched.getHolidaySchedule().length() > 0) {
479: Element holidaySchedElem = new Element(HOLIDAY_SCHEDULE,
480: schedulerNS);
481: holidaySchedElem.setText(sched.getHolidaySchedule());
482: schedElem.addContent(holidaySchedElem);
483: }
484:
485: if (sched.getSuccessNotificationService() != null
486: && sched.getSuccessNotificationEndpoint() != null) {
487: Element successEP = new Element(SUCCESS_NOTIFICATION_EP,
488: schedulerNS);
489: schedElem.addContent(successEP);
490: Element serviceName = new Element(SERVICE_NAME, schedulerNS);
491: serviceName.setText(getPrefixedNameFromQName(sched
492: .getSuccessNotificationService(), nsMap));
493: successEP.addContent(serviceName);
494: Element endpointName = new Element(ENDPOINT_NAME,
495: schedulerNS);
496: endpointName
497: .setText(sched.getSuccessNotificationEndpoint());
498: successEP.addContent(endpointName);
499: }
500:
501: if (sched.getFailureNotificationService() != null
502: && sched.getFailureNotificationEndpoint() != null) {
503: Element failureEP = new Element(FAILURE_NOTIFICATION_EP,
504: schedulerNS);
505: schedElem.addContent(failureEP);
506: Element serviceName = new Element(SERVICE_NAME, schedulerNS);
507: serviceName.setText(getPrefixedNameFromQName(sched
508: .getFailureNotificationService(), nsMap));
509: failureEP.addContent(serviceName);
510: Element endpointName = new Element(ENDPOINT_NAME,
511: schedulerNS);
512: endpointName
513: .setText(sched.getFailureNotificationEndpoint());
514: failureEP.addContent(endpointName);
515: }
516:
517: return schedElem;
518: }
519: }
|