1 /*------------------------------------------------------------------------------
  2 Name:      ClientErrorHandler.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.client;
  7 
  8 import java.util.logging.Level;
  9 import java.util.logging.Logger;
 10 
 11 import org.xmlBlaster.util.Global;
 12 import org.xmlBlaster.util.XmlBlasterException;
 13 import org.xmlBlaster.util.def.MethodName;
 14 import org.xmlBlaster.util.error.I_MsgErrorHandler;
 15 import org.xmlBlaster.util.error.I_MsgErrorInfo;
 16 import org.xmlBlaster.util.queuemsg.MsgQueueEntry;
 17 
 18 /**
 19  * The default error recovery implementation for messages which are lost
 20  * in time and universe.
 21  * @author xmlBlaster@marcelruff.info
 22  * @author michele@laghi.eu
 23  */
 24 public final class ClientErrorHandler implements I_MsgErrorHandler
 25 {
 26    private final String ME;
 27    //private final Global glob;
 28    private static Logger log = Logger.getLogger(ClientErrorHandler.class.getName());
 29    //private final I_XmlBlasterAccess xmlBlasterAccess;
 30 
 31    /**
 32     */
 33    public ClientErrorHandler(Global glob, I_XmlBlasterAccess xmlBlasterAccess) {
 34       this.ME = xmlBlasterAccess.getId();
 35       //this.glob = glob;
 36       //this.xmlBlasterAccess = xmlBlasterAccess;
 37    }
 38 
 39    /**
 40     * Handle errors in async mode, we have nobody we can throw an exception to
 41     * so we handle everything here. 
 42     */
 43    public void handleError(I_MsgErrorInfo msgErrorInfo) {
 44       if (msgErrorInfo == null) return;
 45       if (log.isLoggable(Level.FINER)) log.finer("Entering handleError for " + msgErrorInfo.getMsgQueueEntries().length + " messages");
 46 
 47       boolean shutdown = false;
 48       XmlBlasterException ex = msgErrorInfo.getXmlBlasterException();
 49       //user.security.authorization.notAuthorized
 50       //if (!ex.isCommunication()) { // verify if this makes sense!
 51       //if (ex.isInternal() || ex.isResource()) { // verify if this makes sense!
 52       if (ex.isInternal()) { // verify if this makes sense!
 53          shutdown = true;
 54       }
 55 
 56       String prefix = "";
 57       MsgQueueEntry[] entries = msgErrorInfo.getMsgQueueEntries();
 58       for (int i=0; i<entries.length; i++) {
 59          if (entries[i].getMethodName() == MethodName.CONNECT) {
 60             prefix = "Connection failed: ";
 61             shutdown = true;
 62          }
 63          else {
 64             log.warning(ME+": Default error handling: Message '" + entries[i].getEmbeddedType() + "' '" +
 65                        entries[i].getLogId() + "' is lost: " + msgErrorInfo.getXmlBlasterException().getMessage() +
 66                        ". You can add your own client side error handler with I_XmlBlasterAccess.setClientErrorHandler() if desired or intercept the message with xmlBlasterAccess.registerPostSendListener().");
 67          }
 68       }
 69 
 70       if (shutdown) {
 71          log.severe(prefix + msgErrorInfo.getXmlBlasterException().getMessage());
 72          if (msgErrorInfo.getDispatchManager() != null) {
 73             msgErrorInfo.getDispatchManager().toDead(msgErrorInfo.getXmlBlasterException());
 74             //if (xmlBlasterAccess.getQueue() != null)
 75             //   xmlBlasterAccess.getQueue().clear();
 76             msgErrorInfo.getDispatchManager().shutdown();
 77             return;
 78          }
 79       }
 80       
 81       log.severe(ex.getMessage());
 82       Thread.dumpStack();
 83       //if (xmlBlasterAccess.getQueue() != null)
 84       //   xmlBlasterAccess.getQueue().clear();
 85    }
 86 
 87    /**
 88     * @exception XmlBlasterException is thrown if we are in sync mode and we have no COMMUNICATION problem,
 89     * the client shall handle it himself
 90     */
 91    public String handleErrorSync(I_MsgErrorInfo msgErrorInfo) throws XmlBlasterException {
 92       if (msgErrorInfo.getXmlBlasterException().isCommunication()) {
 93          handleError(msgErrorInfo);
 94          return "";
 95       }
 96       throw msgErrorInfo.getXmlBlasterException(); // Throw back to client
 97    }
 98 
 99    public void shutdown() {
100    }
101 }


syntax highlighted by Code2HTML, v. 0.9.1