0001: /*
0002: * BEGIN_HEADER - DO NOT EDIT
0003: *
0004: * The contents of this file are subject to the terms
0005: * of the Common Development and Distribution License
0006: * (the "License"). You may not use this file except
0007: * in compliance with the License.
0008: *
0009: * You can obtain a copy of the license at
0010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0011: * See the License for the specific language governing
0012: * permissions and limitations under the License.
0013: *
0014: * When distributing Covered Code, include this CDDL
0015: * HEADER in each file and include the License file at
0016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0017: * If applicable add the following below this CDDL HEADER,
0018: * with the fields enclosed by brackets "[]" replaced with
0019: * your own identifying information: Portions Copyright
0020: * [year] [name of copyright owner]
0021: */
0022:
0023: /*
0024: * @(#)MessageProcessor.java
0025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
0026: *
0027: * END_HEADER - DO NOT EDIT
0028: */
0029: package com.sun.jbi.binding.file;
0030:
0031: import com.sun.jbi.binding.file.framework.Command;
0032: import com.sun.jbi.binding.file.util.ConfigData;
0033: import com.sun.jbi.binding.file.util.FileBindingUtil;
0034: import com.sun.jbi.binding.file.util.StringTranslator;
0035:
0036: import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelper;
0037: import org.w3c.dom.Element;
0038:
0039: import java.io.File;
0040: import java.io.FileOutputStream;
0041: import java.io.PrintWriter;
0042:
0043: import java.net.URI;
0044:
0045: import java.text.DateFormat;
0046: import java.text.SimpleDateFormat;
0047:
0048: import java.util.Calendar;
0049: import java.util.Date;
0050: import java.util.GregorianCalendar;
0051: import java.util.logging.Logger;
0052:
0053: import javax.jbi.messaging.DeliveryChannel;
0054: import javax.jbi.messaging.ExchangeStatus;
0055: import javax.jbi.messaging.Fault;
0056: import javax.jbi.messaging.InOnly;
0057: import javax.jbi.messaging.InOut;
0058: import javax.jbi.messaging.InOptionalOut;
0059: import javax.jbi.messaging.MessageExchange;
0060: import javax.jbi.messaging.MessagingException;
0061: import javax.jbi.messaging.NormalizedMessage;
0062: import javax.jbi.messaging.RobustInOnly;
0063:
0064: import javax.xml.namespace.QName;
0065: import javax.xml.transform.Transformer;
0066: import javax.xml.transform.TransformerFactory;
0067: import javax.xml.transform.stream.StreamResult;
0068:
0069: import javax.xml.transform.stream.StreamSource;
0070:
0071: import javax.xml.transform.Source;
0072:
0073: import javax.xml.transform.dom.DOMSource;
0074: import org.w3c.dom.Document;
0075:
0076: /**
0077: * Message Processor which parses the message received from NMR.
0078: *
0079: * @author Sun Microsystems, Inc.
0080: */
0081: public class MessageProcessor implements Command, FileBindingResources {
0082: /**
0083: * In Only MEP.
0084: */
0085: public static final String IN_ONLY = "http://www.w3.org/ns/wsdl/in-only";
0086:
0087: /**
0088: * In Out MEP.
0089: */
0090: public static final String IN_OUT = "http://www.w3.org/ns/wsdl/in-out";
0091:
0092: /**
0093: * In Optional Out MEP.
0094: */
0095: public static final String IN_OPTIONAL_OUT = "http://www.w3.org/ns/wsdl/in-opt-out";
0096:
0097: /**
0098: * Robust In Only MEP.
0099: */
0100: public static final String ROBUST_IN_ONLY = "http://www.w3.org/ns/wsdl/robust-in-only";
0101:
0102: /**
0103: * Out Only MEP.
0104: */
0105: public static final String OUT_ONLY = "http://www.w3.org/ns/wsdl/out-only";
0106:
0107: /**
0108: * Out In MEP.
0109: */
0110: public static final String OUT_IN = "http://www.w3.org/ns/wsdl/out-in";
0111:
0112: /**
0113: * Out Optional In MEP.
0114: */
0115: public static final String OUT_OPTIONAL_IN = "http://www.w3.org/ns/wsdl/out-opt-in";
0116:
0117: /**
0118: * Robust Out Only MEP.
0119: */
0120: public static final String ROBUST_OUT_ONLY = "http://www.w3.org/ns/wsdl/robust-out-only";
0121:
0122: /**
0123: * Out-only MEP.
0124: */
0125: private static final String OUTONLY = "provider";
0126:
0127: /**
0128: * String representing property name in exchange
0129: */
0130: private static final String FILENAME_PROPERTY = "FILENAME";
0131:
0132: /**
0133: * String representing property name in exchange
0134: */
0135: private static final String FILEEXTENSION_PROPERTY = "FILEEXTENSION";
0136:
0137: /**
0138: * String representing default prefix
0139: */
0140: private static final String DEFAULT_PREFIX = "out";
0141:
0142: /**
0143: * String representing default extension
0144: */
0145: private static final String DEFAULT_EXTENSION = "xml";
0146:
0147: /**
0148: * Channel for this binding.
0149: */
0150: private DeliveryChannel mChannel;
0151:
0152: /**
0153: * Registry for storing active deployments.
0154: */
0155: private DeploymentRegistry mRegistry;
0156:
0157: /**
0158: * Endpoint Bean for the exhange being processed
0159: */
0160: private EndpointBean mEndpointBean;
0161:
0162: /**
0163: * Excpetion.
0164: */
0165: private Exception mException;
0166:
0167: /**
0168: * Logger object.
0169: */
0170: private Logger mLog;
0171:
0172: /**
0173: * Message exchange object.
0174: */
0175: private MessageExchange mExchange;
0176:
0177: /**
0178: * Name of file to be written out
0179: */
0180: private String mFileName;
0181:
0182: /**
0183: *
0184: */
0185:
0186: /**
0187: *
0188: */
0189: private String mOperation;
0190:
0191: private boolean mInput = true;
0192:
0193: private boolean mFault = false;
0194:
0195: /**
0196: * Helper for i18n.
0197: */
0198: private StringTranslator mTranslator;
0199:
0200: /**
0201: * Creates a new MessageProcessor object.
0202: *
0203: * @param chnl binding channel.
0204: * @param exchange message exchange.
0205: */
0206: public MessageProcessor(DeliveryChannel chnl,
0207: MessageExchange exchange) {
0208: mChannel = chnl;
0209: mRegistry = DeploymentRegistry.getInstance();
0210: mExchange = exchange;
0211: mLog = FileBindingContext.getInstance().getLogger();
0212: mTranslator = new StringTranslator();
0213: }
0214:
0215: /**
0216: * Util method to return current date.
0217: *
0218: * @return date
0219: */
0220: public String getDate() {
0221: Calendar cal = new GregorianCalendar();
0222: Date dt = cal.getTime();
0223: DateFormat format = new SimpleDateFormat("ddHHmmssSS");
0224: String sdate = format.format(dt);
0225:
0226: return sdate;
0227: }
0228:
0229: /**
0230: * Command pattern for executing in a thread pool.
0231: */
0232: public void execute() {
0233: URI pattern = mExchange.getPattern();
0234: String pat = pattern.toString().trim();
0235: mOperation = mExchange.getOperation().getLocalPart();
0236: setBean();
0237:
0238: if (mEndpointBean == null) {
0239: mLog.severe(mTranslator.getString(
0240: FBC_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
0241: .getExchangeId()));
0242:
0243: return;
0244: }
0245:
0246: int role = mEndpointBean.getRole();
0247:
0248: if (mExchange instanceof InOut) {
0249: mLog.info(mTranslator.getString(FBC_RECEIVED_INOUT,
0250: mExchange.getExchangeId()));
0251:
0252: if (role == ConfigData.CONSUMER) {
0253: processInOut((InOut) mExchange);
0254: } else if (role == ConfigData.PROVIDER) {
0255: mLog.info(mTranslator.getString(FBC_RECEIVED_OUTIN,
0256: mExchange.getExchangeId()));
0257: }
0258: } else if (mExchange instanceof InOnly) {
0259: mLog.info(mTranslator.getString(FBC_RECEIVED_INONLY,
0260: mExchange.getExchangeId()));
0261:
0262: if (role == ConfigData.CONSUMER) {
0263: processInOnly((InOnly) mExchange);
0264: } else if (role == ConfigData.PROVIDER) {
0265: processOutOnly((InOnly) mExchange);
0266: }
0267: } else if (mExchange instanceof RobustInOnly) {
0268: mLog.info(mTranslator.getString(FBC_RECEIVED_ROBUSTINONLY,
0269: mExchange.getExchangeId()));
0270:
0271: if (role == ConfigData.CONSUMER) {
0272: processRobustInOnly((RobustInOnly) mExchange);
0273: } else if (role == ConfigData.PROVIDER) {
0274: processRobustOutOnly((RobustInOnly) mExchange);
0275: }
0276: } else if (mExchange instanceof InOptionalOut) {
0277: mLog.info(mTranslator.getString(FBC_RECEIVED_INOPTIONALOUT,
0278: mExchange.getExchangeId()));
0279: } else {
0280: mLog.info(mTranslator.getString(
0281: FBC_RECEIVED_UNSUPPORTED_MEP, mExchange
0282: .getExchangeId()));
0283: }
0284: }
0285:
0286: /**
0287: * Processes inonly message.
0288: *
0289: * @param inonly inony message.
0290: */
0291: public void processInOnly(InOnly inonly) {
0292: try {
0293: if (canTerminate()) {
0294: ;
0295: } else {
0296: mLog.severe(mTranslator
0297: .getString(FBC_INVALID_MESSAGE_STATE));
0298: }
0299: } catch (Exception me) {
0300: mLog.severe(mTranslator
0301: .getString(FBC_PROCESS_INONLY_FAILED));
0302: mLog.severe(me.getMessage());
0303: }
0304: move();
0305: }
0306:
0307: /**
0308: * Processes in-out message.
0309: *
0310: * @param inout inout message.
0311: */
0312: public void processInOut(InOut inout) {
0313: /* This must be a response, cannot be a request , so set status
0314: * and send
0315: */
0316: try {
0317:
0318: if (canTerminate()) {
0319: move();
0320: return;
0321: }
0322:
0323: NormalizedMessage outmsg = inout.getOutMessage();
0324: Fault fault = inout.getFault();
0325: updateFileName();
0326:
0327: if (outmsg != null) {
0328: mInput = false;
0329: if (writeOutMessage(outmsg)) {
0330: inout.setStatus(ExchangeStatus.DONE);
0331: } else {
0332: inout.setStatus(ExchangeStatus.ERROR);
0333: }
0334: } else if (fault != null) {
0335: mFault = true;
0336: if (writeFaultMessage(fault)) {
0337: inout.setStatus(ExchangeStatus.DONE);
0338: } else {
0339: inout.setStatus(ExchangeStatus.ERROR);
0340: }
0341: } else {
0342: mLog.severe(mTranslator
0343: .getString(FBC_INVALID_MESSAGE_STATE));
0344: move();
0345: return;
0346: }
0347:
0348: mChannel.send(inout);
0349: } catch (MessagingException me) {
0350: mLog
0351: .severe(mTranslator
0352: .getString(FBC_PROCESS_INOUT_FAILED));
0353: mLog.severe(me.getMessage());
0354: me.printStackTrace();
0355: }
0356: move();
0357: }
0358:
0359: /**
0360: * Processes out only message.
0361: *
0362: * @param outonly outonly message.
0363: */
0364: public void processOutOnly(InOnly outonly) {
0365: try {
0366: if (canTerminate()) {
0367: return;
0368: }
0369:
0370: QName operation = outonly.getOperation();
0371:
0372: String mep = mEndpointBean.getMEP(operation.getLocalPart());
0373:
0374: if ((mep == null)) {
0375: mLog.severe(mTranslator
0376: .getString(FBC_INCONSISTENT_OPERATION)
0377: + "MEP "
0378: + mep
0379: + " Operation : "
0380: + operation.toString());
0381: outonly.setError(new Exception(mTranslator
0382: .getString(FBC_INCONSISTENT_OPERATION)));
0383: } else {
0384: NormalizedMessage outmsg = outonly.getInMessage();
0385: updateFileName();
0386: mInput = true;
0387: if (writeOutMessage(outmsg)) {
0388: outonly.setStatus(ExchangeStatus.DONE);
0389: } else {
0390: outonly.setStatus(ExchangeStatus.ERROR);
0391: }
0392: }
0393:
0394: mChannel.send(outonly);
0395: } catch (MessagingException me) {
0396: mLog.severe(mTranslator
0397: .getString(FBC_PROCESS_OUTONLY_FAILED));
0398: mLog.severe(me.getMessage());
0399: } catch (RuntimeException re) {
0400: mLog.severe(mTranslator
0401: .getString(FBC_PROCESS_OUTONLY_FAILED));
0402: mLog.severe(re.getMessage());
0403: }
0404: }
0405:
0406: /**
0407: * Processes robust inonly messages.
0408: *
0409: * @param robustinonly robustinonly exchange.
0410: */
0411: public void processRobustInOnly(RobustInOnly robustinonly) {
0412: /* This must be a response, cannot be a request , so set status
0413: * and send
0414: */
0415: try {
0416:
0417: if (canTerminate()) {
0418: move();
0419: return;
0420: }
0421:
0422: updateFileName();
0423:
0424: Fault fault = robustinonly.getFault();
0425:
0426: if (fault != null) {
0427: mFault = true;
0428: if (writeFaultMessage(fault)) {
0429: robustinonly.setStatus(ExchangeStatus.DONE);
0430: } else {
0431: robustinonly.setStatus(ExchangeStatus.ERROR);
0432: }
0433:
0434: mChannel.send(robustinonly);
0435: }
0436: } catch (MessagingException me) {
0437: mLog.severe(mTranslator
0438: .getString(FBC_PROCESS_ROBUSTINONLY_FAILED));
0439: mLog.severe(me.getMessage());
0440: }
0441: move();
0442: }
0443:
0444: /**
0445: * Writes a error message into file.
0446: *
0447: * @param ex exception message.
0448: *
0449: * @return true if successfull, false otherwise.
0450: */
0451: public boolean writeErrorMessage(Exception ex) {
0452: StringBuffer sb = new StringBuffer();
0453: StackTraceElement[] stckTrElem = null;
0454: FileOutputStream out = null;
0455:
0456: try {
0457: out = new FileOutputStream(mFileName);
0458:
0459: if (ex == null) {
0460: sb.append("Unknown error occurred");
0461: } else {
0462: sb.append(ex.getMessage());
0463: stckTrElem = ex.getStackTrace();
0464:
0465: if (stckTrElem != null) {
0466: for (int i = 0; i < stckTrElem.length; i++) {
0467: String stckTrace = stckTrElem[i].toString();
0468: sb.append(stckTrace);
0469: sb.append("\n");
0470: }
0471: }
0472: }
0473:
0474: out.write(sb.toString().getBytes());
0475: out.close();
0476:
0477: return true;
0478: } catch (Exception e) {
0479: mLog.info(mTranslator.getString(FBC_FILE_WRITE_FAILED,
0480: mFileName));
0481: e.printStackTrace();
0482: mException = e;
0483:
0484: return false;
0485: } finally {
0486: try {
0487: if (out != null) {
0488: out.close();
0489: }
0490: } catch (Exception e1) {
0491: mLog.severe(mTranslator.getString(FBC_CLOSE_FAILED, e1
0492: .getMessage()));
0493: }
0494: }
0495: }
0496:
0497: /**
0498: * Writes a fault message into file.
0499: *
0500: * @param msg fault message.
0501: *
0502: * @return true if successfull, false otherwise.
0503: */
0504: public boolean writeFaultMessage(Fault msg) {
0505: if (msg == null) {
0506: return false;
0507: }
0508:
0509: javax.xml.transform.Source doc = msg.getContent();
0510:
0511: if (doc == null) {
0512: mLog.severe(mTranslator.getString(FBC_CONTENT_NULL));
0513:
0514: return false;
0515: }
0516:
0517: return writeMessage(doc);
0518: }
0519:
0520: /**
0521: * Writes a sucess response message into file.
0522: *
0523: * @param doc normalized message.
0524: *
0525: * @return true if successfull, false otherwise.
0526: */
0527: public boolean writeMessage(javax.xml.transform.Source doc) {
0528: Element ele = null;
0529: FileOutputStream out = null;
0530: PrintWriter pw = null;
0531: File f = new File(mFileName);
0532:
0533: try {
0534: out = new FileOutputStream(f);
0535:
0536: Source unwrapsource = doc;
0537: if (mEndpointBean.getDeploymentType().equals("WSDL11")) {
0538: QName qname = new QName(mEndpointBean
0539: .getValue(ConfigData.SERVICE_NAMESPACE),
0540: mEndpointBean
0541: .getValue(ConfigData.SERVICE_LOCALNAME));
0542: String epname = mEndpointBean
0543: .getValue(ConfigData.ENDPOINTNAME);
0544: Wsdl11WrapperHelper helper = new Wsdl11WrapperHelper(
0545: mEndpointBean.getWsdlDefinition());
0546: Document wrapdoc = null;
0547: if (mFault) {
0548: wrapdoc = helper.unwrapFault(doc, qname, epname,
0549: mOperation);
0550: } else {
0551: wrapdoc = helper.unwrapMessage(doc, qname, epname,
0552: mOperation, mInput);
0553: }
0554: unwrapsource = new DOMSource(wrapdoc);
0555: }
0556:
0557: TransformerFactory tFactory = TransformerFactory
0558: .newInstance();
0559: Transformer trans = tFactory.newTransformer();
0560: trans
0561: .setOutputProperty(
0562: javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION,
0563: "yes");
0564:
0565: StreamResult result = new StreamResult(out);
0566: trans.transform(unwrapsource, result);
0567:
0568: out.close();
0569:
0570: return true;
0571: } catch (Exception e) {
0572: mLog.severe(mTranslator.getString(FBC_FILE_WRITE_FAILED,
0573: mFileName));
0574: mLog.severe(e.getMessage());
0575: mException = e;
0576: mFileName = getBaseName(mFileName) + "."
0577: + ConfigData.ERROR_EXTENSION;
0578: writeErrorMessage(e);
0579:
0580: try {
0581: if (out != null) {
0582: out.close();
0583: }
0584:
0585: f.delete();
0586: } catch (Exception e1) {
0587: mLog.severe(mTranslator.getString(FBC_CLOSE_FAILED, e1
0588: .getMessage()));
0589: }
0590:
0591: return false;
0592: } finally {
0593: try {
0594: if (out != null) {
0595: out.close();
0596: }
0597: } catch (Exception e1) {
0598: mLog.severe(mTranslator.getString(FBC_CLOSE_FAILED, e1
0599: .getMessage()));
0600: }
0601: }
0602: }
0603:
0604: /**
0605: * Writes an output message into file.
0606: *
0607: * @param msg normalized message.
0608: *
0609: * @return true if successfull, false otherwise.
0610: */
0611: public boolean writeOutMessage(NormalizedMessage msg) {
0612: if (msg == null) {
0613: return false;
0614: }
0615:
0616: javax.xml.transform.Source doc = msg.getContent();
0617:
0618: if (doc == null) {
0619: mLog.severe(mTranslator.getString(FBC_CONTENT_NULL));
0620:
0621: return false;
0622: }
0623:
0624: return writeMessage(doc);
0625: }
0626:
0627: /**
0628: * Util method to get base name of file.
0629: *
0630: * @param fname file name
0631: *
0632: * @return base name
0633: */
0634: private String getBaseName(String fname) {
0635: int k = fname.lastIndexOf('.');
0636:
0637: if (k == -1) {
0638: return fname;
0639: }
0640:
0641: if (k == 0) {
0642: return "";
0643: }
0644:
0645: return fname.substring(0, k);
0646: }
0647:
0648: /**
0649: * Finds a bean object from the message exchange parameters.
0650: */
0651: private void setBean() {
0652: String sname = mExchange.getEndpoint().getServiceName()
0653: .toString();
0654: String epname = mExchange.getEndpoint().getEndpointName();
0655: QName intername = mExchange.getInterfaceName();
0656:
0657: if ((sname == null) || (epname == null)) {
0658: mLog.severe(mTranslator.getString(
0659: FBC_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
0660: .getExchangeId()));
0661:
0662: return;
0663: }
0664:
0665: // try for a provider endpoint first
0666: if (mExchange.getRole().equals(MessageExchange.Role.PROVIDER)) {
0667: mEndpointBean = mRegistry.findEndpoint(sname + epname);
0668: } else {
0669: mEndpointBean = mRegistry.findEndpoint(sname);
0670:
0671: /**
0672: * There is a possiblity that the message was routed initially
0673: * with the interface name and not the service name
0674: * So we have to try an endpoint with interface name now
0675: */
0676:
0677: if (mEndpointBean == null) {
0678: if (intername != null) {
0679: mEndpointBean = mRegistry
0680: .findEndpointByInterface(intername
0681: .toString().trim());
0682: }
0683: }
0684: }
0685:
0686: if (mEndpointBean == null) {
0687: mLog
0688: .severe(mTranslator.getString(
0689: FBC_START_DEPLOYMENT_FAILED_BEANNULL, sname
0690: + " " + epname + " "
0691: + mExchange.getExchangeId()));
0692: }
0693: }
0694:
0695: /**
0696: * Util method to check for termination of exhcange.
0697: *
0698: * @return true of exchange can be terminated false othrewise.
0699: */
0700: private boolean canTerminate() {
0701: Exception exc;
0702: String sname = mExchange.getEndpoint().getServiceName()
0703: .toString();
0704: String epname = mExchange.getEndpoint().getEndpointName();
0705:
0706: if (mEndpointBean == null) {
0707: mLog.severe(mTranslator.getString(
0708: FBC_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
0709: .getExchangeId()));
0710:
0711: return true;
0712: }
0713:
0714: if (mExchange.getStatus() == ExchangeStatus.DONE) {
0715: mLog.info(mTranslator.getString(FBC_EXCHANGE_DONE,
0716: mExchange.getExchangeId()));
0717:
0718: if (!mExchange.getPattern().toString().trim()
0719: .equals(OUT_IN)) {
0720: updateFileName();
0721: writeSuccessMessage();
0722: }
0723:
0724: return true;
0725: }
0726:
0727: if (mExchange.getStatus() == ExchangeStatus.ERROR) {
0728: mLog.info(mTranslator.getString(FBC_EXCHANGE_ERROR,
0729: mExchange.getExchangeId()));
0730: exc = mExchange.getError();
0731: updateFileName();
0732:
0733: if (writeErrorMessage(exc)) {
0734: mLog.info(mTranslator
0735: .getString(FBC_WRITE_ERROR_SUCCESS));
0736: } else {
0737: mLog
0738: .info(mTranslator
0739: .getString(FBC_WRITE_ERROR_FAILED));
0740: }
0741:
0742: return true;
0743: }
0744:
0745: return false;
0746: }
0747:
0748: /**
0749: *
0750: */
0751: private void move() {
0752: String trk = null;
0753: String fn = null;
0754: String ext = null;
0755: QName operation = null;
0756:
0757: try {
0758: trk = (String) mExchange
0759: .getProperty(ConfigData.TRACKINGID_PROPERTY);
0760: ext = (String) mExchange
0761: .getProperty(ConfigData.EXTENSION_PROPERTY);
0762: fn = (String) mExchange
0763: .getProperty(ConfigData.FILENAME_PROPERTY);
0764: operation = mExchange.getOperation();
0765: } catch (Exception e) {
0766: mLog.severe(mTranslator.getString(FBC_MOVE_FAILED, e
0767: .getMessage()));
0768: }
0769:
0770: if (trk == null) {
0771: mLog.severe(mTranslator.getString(FBC_MOVE_FAILED,
0772: mExchange.getExchangeId()));
0773: } else {
0774: String filepath = FileBindingUtil.getFilePath(trk);
0775: String destfolder = (String) mEndpointBean
0776: .getValue(ConfigData.PROCESSEDDIR);
0777: String inputdir = (String) mEndpointBean
0778: .getValue(ConfigData.INPUTDIR);
0779: String filename = null;
0780: if ((ext == null) || (ext.trim().equals(""))) {
0781: filename = fn;
0782: } else {
0783: filename = fn + "." + ext;
0784: }
0785: String movefilename = null;
0786: try {
0787: if (operation.getLocalPart().equals(
0788: mEndpointBean.getDefaultOperation()
0789: .getLocalPart())) {
0790: // cud be in inputdir or in folder under the operation
0791: File f = new File(inputdir + File.separator
0792: + filename);
0793:
0794: if (f.exists()) {
0795: movefilename = inputdir + File.separator
0796: + filename;
0797: } else {
0798: movefilename = inputdir + File.separator
0799: + operation.getLocalPart()
0800: + File.separator + filename;
0801: }
0802: } else {
0803: movefilename = inputdir + File.separator
0804: + operation.getLocalPart() + File.separator
0805: + filename;
0806: }
0807: String movedfilename = FileBindingUtil.moveFile(trk,
0808: destfolder, movefilename);
0809: if (movedfilename == null) {
0810: mLog.severe(mTranslator.getString(FBC_MOVE_FAILED,
0811: filename, destfolder));
0812: } else {
0813: /*
0814: * update the input normalized message with the new location
0815: * of the file so that the provider will
0816: * still be able to access this.
0817: * The spec does not explicitely say that a message life cycle
0818: * ends when a staus is received. The NM object can still be
0819: * used by components.
0820: */
0821: try {
0822: URI pattern = mExchange.getPattern();
0823: String pat = pattern.toString().trim();
0824: NormalizedMessage inmessage = null;
0825: if (mExchange instanceof InOut) {
0826: inmessage = ((InOut) mExchange)
0827: .getInMessage();
0828: } else if (mExchange instanceof InOnly) {
0829: inmessage = ((InOnly) mExchange)
0830: .getInMessage();
0831: } else if (mExchange instanceof RobustInOnly) {
0832: inmessage = ((RobustInOnly) mExchange)
0833: .getInMessage();
0834: }
0835:
0836: if (inmessage != null) {
0837: Source src = inmessage.getContent();
0838: src = new StreamSource(new File(
0839: movedfilename));
0840: }
0841: } catch (Exception moveE) {
0842: mLog
0843: .severe(mTranslator.getString(
0844: FBC_RESET_SOURCE_FAILED,
0845: movedfilename));
0846: }
0847: }
0848:
0849: } catch (Exception fe) {
0850: mLog.severe(mTranslator.getString(FBC_MOVE_FAILED,
0851: filename, destfolder));
0852: }
0853:
0854: FileBindingUtil.removeEntry(trk);
0855: }
0856: }
0857:
0858: /**
0859: * Processes robust out only messages.
0860: *
0861: * @param robustoutonly robust out only message.
0862: */
0863: private void processRobustOutOnly(RobustInOnly robustoutonly) {
0864: /* This must be a request , cannot be a request , so set status
0865: * and send
0866: */
0867: try {
0868: if (canTerminate()) {
0869: return;
0870: }
0871:
0872: QName operation = robustoutonly.getOperation();
0873: String mep = mEndpointBean.getMEP(operation.getLocalPart());
0874:
0875: if ((mep == null)) {
0876: mLog.severe(mTranslator
0877: .getString(FBC_INCONSISTENT_OPERATION)
0878: + "MEP "
0879: + mep
0880: + " Operation : "
0881: + operation.toString());
0882:
0883: robustoutonly.setError(new Exception(mTranslator
0884: .getString(FBC_INCONSISTENT_OPERATION)));
0885: } else {
0886: NormalizedMessage outmsg = robustoutonly.getInMessage();
0887: updateFileName();
0888: mInput = true;
0889: if (writeOutMessage(outmsg)) {
0890: robustoutonly.setStatus(ExchangeStatus.DONE);
0891: } else {
0892: /* Have to set some error here
0893: */
0894: robustoutonly.setError(mException);
0895: }
0896: }
0897:
0898: mChannel.send(robustoutonly);
0899: } catch (MessagingException me) {
0900: mLog.severe(mTranslator
0901: .getString(FBC_PROCESS_ROBUSTOUTONLY_FAILED));
0902: me.printStackTrace();
0903: mLog.severe(me.getMessage());
0904: } catch (RuntimeException re) {
0905: mLog.severe(mTranslator
0906: .getString(FBC_PROCESS_ROBUSTOUTONLY_FAILED));
0907: re.printStackTrace();
0908: mLog.severe(re.getMessage());
0909: } catch (Throwable th) {
0910: th.printStackTrace();
0911: }
0912: }
0913:
0914: /**
0915: * Updates the file name according to the state.
0916: */
0917: private void updateFileName() {
0918: String prefix = null;
0919: String folder = null;
0920: String filename = null;
0921: String extension = null;
0922: String fileextension = null;
0923: String trkid = null;
0924: String prefixsep = null;
0925: String newext = null;
0926:
0927: try {
0928: prefix = mEndpointBean.getPrefix(mOperation);
0929: folder = mEndpointBean.getValue(ConfigData.OUTPUTDIR);
0930: extension = mEndpointBean.getExtension(mOperation);
0931:
0932: if (null == prefix) {
0933: prefixsep = DEFAULT_PREFIX + ConfigData.SEPARATOR;
0934: } else if (prefix.trim().equals("")) {
0935: prefixsep = "";
0936: } else {
0937: prefixsep = prefix + ConfigData.SEPARATOR;
0938: }
0939:
0940: if (mEndpointBean.getRole() == ConfigData.CONSUMER) {
0941: filename = (String) mExchange
0942: .getProperty(FILENAME_PROPERTY);
0943: fileextension = (String) mExchange
0944: .getProperty(FILEEXTENSION_PROPERTY);
0945: trkid = (String) mExchange
0946: .getProperty(ConfigData.TRACKINGID_PROPERTY);
0947: }
0948:
0949: if (mEndpointBean.getRole() == ConfigData.PROVIDER) {
0950: filename = OUTONLY;
0951: trkid = FileBindingUtil.getTrackingId();
0952: }
0953:
0954: if (null == extension) {
0955: if (fileextension != null) {
0956: newext = "." + fileextension;
0957: } else {
0958: newext = "." + DEFAULT_EXTENSION;
0959: }
0960: } else if (extension.trim().equals("")) {
0961: newext = "";
0962: } else {
0963: newext = "." + extension;
0964: }
0965:
0966: if (mExchange.getStatus() == ExchangeStatus.ERROR) {
0967: mFileName = folder + File.separatorChar + prefixsep
0968: + filename + ConfigData.SEPARATOR + trkid + "."
0969: + ConfigData.ERROR_EXTENSION;
0970: } else {
0971: mFileName = folder + File.separatorChar + prefixsep
0972: + filename + ConfigData.SEPARATOR + trkid
0973: + newext;
0974: }
0975: } catch (Exception e) {
0976: e.printStackTrace();
0977: mFileName = folder + File.separatorChar + trkid + "."
0978: + ConfigData.ERROR_EXTENSION;
0979: }
0980: }
0981:
0982: /**
0983: * Writes out a success message to file.
0984: */
0985: private void writeSuccessMessage() {
0986: FileOutputStream out = null;
0987: String fname;
0988: fname = getBaseName(mFileName) + "."
0989: + ConfigData.DONE_EXTENSION;
0990:
0991: try {
0992: out = new FileOutputStream(fname);
0993: out.write(mTranslator.getString(FBC_DONE_MESSAGE)
0994: .getBytes());
0995: out.close();
0996: } catch (Exception e) {
0997: mLog.severe(mTranslator.getString(FBC_FILE_WRITE_FAILED,
0998: fname));
0999: mLog.severe(e.getMessage());
1000: mException = e;
1001: } finally {
1002: try {
1003: if (out != null) {
1004: out.close();
1005: }
1006: } catch (Exception e1) {
1007: mLog.severe(mTranslator.getString(FBC_CLOSE_FAILED, e1
1008: .getMessage()));
1009: }
1010: }
1011: }
1012: }
|