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: JobNotification.java 7088 2007-04-25 20:06:32Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.messages;
025:
026: import java.util.HashMap;
027:
028: import javax.xml.namespace.QName;
029: import javax.xml.transform.Source;
030: import javax.xml.transform.dom.DOMSource;
031:
032: import org.jdom.Document;
033: import org.jdom.Element;
034: import org.jdom.Namespace;
035: import org.jdom.input.DOMBuilder;
036: import org.jdom.output.DOMOutputter;
037:
038: public class JobNotification extends SchedulerMessage {
039:
040: private QName jobEndpointServiceName;
041: private String jobEndpointEPName;
042: private boolean success;
043: private String error;
044:
045: /**
046: * @return the error
047: */
048: public String getError() {
049: return error;
050: }
051:
052: /**
053: * @param error the error to set
054: */
055: public void setError(String error) {
056: this .error = error;
057: }
058:
059: /**
060: * @return the jobEndpointEPName
061: */
062: public String getJobEndpointEPName() {
063: return jobEndpointEPName;
064: }
065:
066: /**
067: * @param jobEndpointEPName the jobEndpointEPName to set
068: */
069: public void setJobEndpointEPName(String jobEndpointEPName) {
070: this .jobEndpointEPName = jobEndpointEPName;
071: }
072:
073: /**
074: * @return the jobEndpointServiceName
075: */
076: public QName getJobEndpointServiceName() {
077: return jobEndpointServiceName;
078: }
079:
080: /**
081: * @param jobEndpointServiceName the jobEndpointServiceName to set
082: */
083: public void setJobEndpointServiceName(QName jobEndpointServiceName) {
084: this .jobEndpointServiceName = jobEndpointServiceName;
085: }
086:
087: /**
088: * @return the success
089: */
090: public boolean isSuccess() {
091: return success;
092: }
093:
094: /**
095: * @param success the success to set
096: */
097: public void setSuccess(boolean success) {
098: this .success = success;
099: }
100:
101: /* (non-Javadoc)
102: * @see com.bostechcorp.cbesb.runtime.component.scheduler.messages.SchedulerMessage#getMessageType()
103: */
104: @Override
105: public QName getMessageType() {
106: return SchedulerMessageFactory.TYPE_JOB_STATUS;
107: }
108:
109: public static SchedulerMessage createFromSource(Source src)
110: throws Exception {
111: DOMBuilder domBuilder = new DOMBuilder();
112: Document jdomDoc = domBuilder.build(getDocFromSource(src));
113: Element rootElem = jdomDoc.getRootElement();
114:
115: HashMap<String, Namespace> nsMap = getNamespaceMap(rootElem);
116:
117: if (rootElem.getNamespace().equals(schedulerNS)
118: && rootElem.getName().equals(JOB_STATUS)) {
119: JobNotification message = new JobNotification();
120:
121: Element jobEP = rootElem
122: .getChild(JOB_ENDPOINT, schedulerNS);
123: if (jobEP != null) {
124: Element serviceName = jobEP.getChild(SERVICE_NAME,
125: schedulerNS);
126: if (serviceName != null) {
127: QName serviceNameQName = getQNameFromPrefixedName(
128: serviceName.getValue(), nsMap);
129: message.setJobEndpointServiceName(serviceNameQName);
130: }
131: Element endpointName = jobEP.getChild(ENDPOINT_NAME,
132: schedulerNS);
133: if (endpointName != null) {
134: String epName = endpointName.getValue();
135: message.setJobEndpointEPName(epName);
136: }
137:
138: } else {
139: throw new Exception("Missing required element: "
140: + JOB_ENDPOINT);
141: }
142:
143: Element successElem = rootElem.getChild(SUCCESS,
144: schedulerNS);
145: if (successElem != null) {
146: message.success = Boolean.parseBoolean(successElem
147: .getText());
148: } else {
149: throw new Exception("Missing required element: "
150: + SUCCESS);
151: }
152:
153: Element errorElem = rootElem.getChild(ERROR, schedulerNS);
154: if (errorElem != null) {
155: message.error = errorElem.getText();
156: }
157:
158: return message;
159: } else {
160: throw new Exception("XML message is not {" + SCHEDULER_NS
161: + "}" + JOB_STATUS);
162: }
163: }
164:
165: /* (non-Javadoc)
166: * @see com.bostechcorp.cbesb.runtime.component.scheduler.messages.SchedulerMessage#toSource()
167: */
168: @Override
169: public Source toSource() throws Exception {
170: HashMap<String, Namespace> nsMap = new HashMap<String, Namespace>();
171:
172: Element rootElem = new Element(JOB_STATUS, schedulerNS);
173: Document jdomDoc = new Document(rootElem);
174:
175: Element jobEP = new Element(JOB_ENDPOINT, schedulerNS);
176: rootElem.addContent(jobEP);
177: Element serviceName = new Element(SERVICE_NAME, schedulerNS);
178: serviceName.setText(getPrefixedNameFromQName(
179: getJobEndpointServiceName(), nsMap));
180: jobEP.addContent(serviceName);
181: Element endpointName = new Element(ENDPOINT_NAME, schedulerNS);
182: endpointName.setText(getJobEndpointEPName());
183: jobEP.addContent(endpointName);
184:
185: Element successElem = new Element(SUCCESS, schedulerNS);
186: rootElem.addContent(successElem);
187: successElem.setText(Boolean.toString(success));
188:
189: Element errorElem = new Element(ERROR, schedulerNS);
190: rootElem.addContent(errorElem);
191: errorElem.setText(error);
192:
193: addNamespaceDeclsToRoot(rootElem, nsMap);
194: DOMOutputter domOut = new DOMOutputter();
195: org.w3c.dom.Document domDoc = domOut.output(jdomDoc);
196: return new DOMSource(domDoc);
197:
198: }
199:
200: }
|