001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.engines.call;
009:
010: //base classes
011: import java.io.IOException;
012: import java.math.BigDecimal;
013: import java.net.URL;
014: import java.net.URLConnection;
015: import java.util.ArrayList;
016: import java.util.Iterator; //import javax.xml.soap.MessageFactory;
017: //import javax.xml.soap.Name;
018: //import javax.xml.soap.SOAPBody;
019: //import javax.xml.soap.SOAPBodyElement;
020: //import javax.xml.soap.SOAPConnection;
021: //import javax.xml.soap.SOAPConnectionFactory;
022: //import javax.xml.soap.SOAPElement;
023: //import javax.xml.soap.SOAPException;
024: //import javax.xml.soap.SOAPFactory;
025: //import javax.xml.soap.SOAPMessage;
026:
027: //project specific classes
028: import org.jfolder.common.UnexpectedSystemException;
029: import org.jfolder.common.entity.InitializeSystemEntityTagContext;
030: import org.jfolder.common.entity.SystemEntityTag;
031: import org.jfolder.common.entity.SystemEntityTagHelper;
032: import org.jfolder.common.files.VirtualFileSystemDirectory;
033: import org.jfolder.common.files.VirtualFileSystemFile;
034: import org.jfolder.common.files.VirtualFileSystemHolder;
035: import org.jfolder.common.tagging.ConceptTag;
036: import org.jfolder.common.tagging.ConceptTagCharacteristic;
037: import org.jfolder.common.tagging.ConceptTagCharacteristicHolder;
038: import org.jfolder.common.tagging.RootConceptTagHolder;
039: import org.jfolder.common.utils.misc.MiscHelper;
040:
041: //other classes
042:
043: public class GovernorCallHelper {
044:
045: private GovernorCallHelper() {
046: }
047:
048: public final static RootConceptTagHolder createGovernorRequest(
049: String inServiceType, String inCallName) {
050:
051: RootConceptTagHolder outValue = getGovernorCall();
052:
053: GovernorCall gs = (GovernorCall) outValue
054: .getFirstChildConceptTag();
055:
056: gs.setCallAsRequest();
057: gs.setServiceType(inServiceType);
058: gs.setCallName(inCallName);
059:
060: return outValue;
061: }
062:
063: public final static RootConceptTagHolder createGovernorResponse(
064: GovernorCall inGc) {
065:
066: RootConceptTagHolder outValue = getGovernorCall();
067:
068: GovernorCall gs = (GovernorCall) outValue
069: .getFirstChildConceptTag();
070:
071: gs.setCallAsResponse();
072: gs.setServiceType(inGc.getServiceType());
073: gs.setCallName(inGc.getCallName());
074:
075: return outValue;
076: }
077:
078: private final static RootConceptTagHolder getGovernorCall() {
079:
080: RootConceptTagHolder outValue = SystemEntityTagHelper
081: .getGovernorCall();
082:
083: ConceptTagCharacteristicHolder ctcharh = outValue
084: .getFirstChildCharacteristicHolder();
085: ConceptTag ct = ctcharh.getConceptTag();
086:
087: InitializeSystemEntityTagContext isetc = new InitializeSystemEntityTagContext(
088: ctcharh);
089:
090: ct.initialize(isetc);
091:
092: return outValue;
093: }
094:
095: //
096: /*
097: public final static GovernorCall callGovernorService(
098: RootConceptTagHolder inRcth, URL inTarget) {
099:
100: try {
101:
102: GovernorCall outValue = null;
103:
104: //
105: ArrayList gcRequests = new ArrayList();
106: gcRequests.add(inRcth);
107:
108: //
109: SOAPConnectionFactory soapConnFac =
110: SOAPConnectionFactory.newInstance();
111: SOAPConnection soapConn = soapConnFac.createConnection();
112:
113: //
114: SOAPMessage request = prepareMessage(gcRequests);
115: SOAPMessage response = soapConn.call(request, inTarget);
116:
117: ArrayList gcResponses = extractGovernorCalls(response);
118: //MiscHelper.writeMessage(response, "This is the SOAP Response");
119:
120: RootConceptTagHolder rcth =
121: (RootConceptTagHolder)gcResponses.get(0);
122:
123:
124: outValue = (GovernorCall)rcth.getFirstChildConceptTag();
125: //MiscHelper.println("RECEIVED RESPONSE call type = "
126: // + outValue.isCallResponse());
127: //MiscHelper.println("RECEIVED RESPONSE service type = "
128: // + outValue.getServiceType());
129: //MiscHelper.println("RECEIVED RESPONSE call name = "
130: // + outValue.getCallName());
131:
132: MiscHelper.writeMessage(
133: GovernorCallHelper.class, rcth.getXMLRepresentation());
134:
135: return outValue;
136:
137: ////////////////////////////////////////////////////
138: ////////////////////////////////////////////////////
139: //URLConnection urlConn = inTarget.openConnection();
140: //
141: ////
142: //urlConn.setDoInput(true);
143: //urlConn.setDoOutput(true);
144: //urlConn.setUseCaches(false);
145: //urlConn.setRequestProperty(
146: // "Content-Type", "application/x-www-form-urlencoded");
147: //
148: ////
149: //OutputStream os = urlConn.getOutputStream();
150: //OutputStreamWriter osw = new OutputStreamWriter(os);
151: ////
152: //String output = inRcth.getXMLRepresentation();
153: //osw.write(output, 0, output.length());
154: ////
155: //osw.flush();
156: //os.flush();
157: ////
158: //osw.close();
159: //os.close();
160: //
161: ////
162: //StringBuffer response = new StringBuffer();
163: ////
164: //InputStream is = urlConn.getInputStream();
165: //InputStreamReader isr = new InputStreamReader(is);
166: //
167: //int nextChar = 0;
168: //while ((nextChar = isr.read()) != -1) {
169: // char nextPart = (char)nextChar;
170: // response.append(nextPart);
171: //}
172: //
173: //isr.close();
174: //
175: //MiscHelper.writeMessage(this, response.toString());
176:
177: ////////////////////////////////
178: //
179: //ConceptTagPreferences ctp = new GovernorCallTagPreferences();
180: //RootConceptTagHolder rcth = getRootHolder(is);
181: //ConceptTagCharacteristicHolder ctcharh =
182: // rcth.getCharacteristicHolder();
183: //ConceptTagCharacteristic ctchar =
184: // ctcharh.getCharacteristic(ctp.getRootConstraint());
185: //outValue = (GovernorCall)ctchar.getValueAsConceptTag(0);
186: //
187: //is.close();
188:
189: //
190: }
191: catch (SOAPException se) {
192: throw new UnexpectedSystemException(se);
193: }
194: catch (IOException ioe) {
195: throw new UnexpectedSystemException(ioe);
196: }
197: }
198:
199: public final static SOAPMessage prepareMessage(ArrayList inRequests)
200: throws SOAPException {
201:
202: SOAPMessage outValue = null;
203:
204: //
205: MessageFactory mf = MessageFactory.newInstance();
206: SOAPFactory soapFactory = SOAPFactory.newInstance();
207: //
208: outValue = mf.createMessage();
209: outValue.getSOAPHeader().detachNode();
210: //
211: SOAPBody body = outValue.getSOAPBody();
212: Name rootName = soapFactory.createName(GovernorCall.GOVERNOR_CALL);
213: Name docsName = soapFactory.createName(GovernorCall.GOVERNOR_DOCS);
214:
215: //outValue = mf.createMessage();
216: //
217: for (int i = 0; i < inRequests.size(); i++) {
218:
219: SOAPBodyElement rootElement = body.addBodyElement(rootName);
220: RootConceptTagHolder nextGcRequest =
221: (RootConceptTagHolder)inRequests.get(i);
222: rootElement.addTextNode(nextGcRequest.getXMLRepresentation());
223: //
224: SOAPBodyElement docsElement = body.addBodyElement(rootName);
225: nextGcRequest.exportDocuments(soapFactory, outValue, docsElement);
226: }
227:
228: //
229: //application/octet-stream
230:
231: return outValue;
232: }
233:
234: public final static ArrayList extractGovernorCalls(SOAPMessage inMessage)
235: throws SOAPException, IOException {
236:
237: ArrayList outValue = new ArrayList();
238:
239: SOAPBody body = inMessage.getSOAPBody();
240: SOAPFactory soapFactory = SOAPFactory.newInstance();
241: Name rootName = soapFactory.createName(GovernorCall.GOVERNOR_CALL);
242: Name docsName = soapFactory.createName(GovernorCall.GOVERNOR_DOCS);
243: Iterator childRootIter = body.getChildElements(rootName);
244: Iterator childDocsIter = body.getChildElements(docsName);
245:
246: while (childRootIter.hasNext()) {
247:
248: SOAPElement nextSe = (SOAPElement)childRootIter.next();
249: RootConceptTagHolder nextRequestRcth =
250: SystemEntityTagHelper.getRootHolder(nextSe.getValue());
251: outValue.add(nextRequestRcth);
252: //
253: SOAPElement docsElement = (SOAPElement)childDocsIter.next();
254: nextRequestRcth.importDocuments(
255: soapFactory, inMessage, docsElement);
256: }
257:
258: return outValue;
259: }
260: */
261: public static ConceptTagCharacteristic getLocalDummyCharacteristic() {
262:
263: ConceptTagCharacteristic outValue = null;
264:
265: RootConceptTagHolder dummyRcth = GovernorCallHelper
266: .createGovernorRequest("dummy", "dummy");
267: GovernorCall dummyGc = (GovernorCall) dummyRcth
268: .getFirstChildConceptTag();
269: outValue = dummyGc.addPrepareArgumentAsSEntity();
270:
271: return outValue;
272: }
273: }
|