001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.messaging;
022
023 import java.util.concurrent.ExecutionException;
024 import java.util.concurrent.TimeoutException;
025
026 import org.granite.client.messaging.events.AbstractResponseEvent;
027 import org.granite.client.messaging.events.CancelledEvent;
028 import org.granite.client.messaging.events.Event;
029 import org.granite.client.messaging.events.FailureEvent;
030 import org.granite.client.messaging.events.FaultEvent;
031 import org.granite.client.messaging.events.ResultEvent;
032 import org.granite.client.messaging.events.TimeoutEvent;
033 import org.granite.client.messaging.messages.ResponseMessage;
034 import org.granite.logging.Logger;
035
036 /**
037 * @author Franck WOLFF
038 */
039 public final class ResponseListenerDispatcher {
040
041 private static final Logger log = Logger.getLogger(ResponseListenerDispatcher.class);
042
043 private ResponseListenerDispatcher() {
044 throw new RuntimeException("Not instanciable");
045 }
046
047 public static void dispatch(ResponseListener listener, Event event) {
048 if (listener == null || event == null)
049 throw new NullPointerException("listener and event cannot be null");
050
051 boolean unknownEventType = false;
052
053 try {
054 switch (event.getType()) {
055 case RESULT:
056 listener.onResult((ResultEvent)event);
057 break;
058 case FAULT:
059 listener.onFault((FaultEvent)event);
060 break;
061 case FAILURE:
062 listener.onFailure((FailureEvent)event);
063 break;
064 case TIMEOUT:
065 listener.onTimeout((TimeoutEvent)event);
066 break;
067 case CANCELLED:
068 listener.onCancelled((CancelledEvent)event);
069 break;
070 default:
071 unknownEventType = true;
072 break;
073 }
074 }
075 catch (Exception e) {
076 log.error(e, "ResponseListener %s threw an exception for event %s", listener, event);
077 }
078
079 if (unknownEventType) {
080 RuntimeException e = new RuntimeException("Unknown event type: " + event);
081 log.error(e, "");
082 throw e;
083 }
084 }
085
086 public static ResponseMessage getResponseMessage(Event event) throws InterruptedException, ExecutionException, TimeoutException {
087 if (event == null)
088 throw new NullPointerException("event cannot be null");
089
090 switch (event.getType()) {
091 case RESULT: case FAULT:
092 return ((AbstractResponseEvent<?>)event).getResponse();
093 case FAILURE:
094 throw new ExecutionException(((FailureEvent)event).getCause());
095 case TIMEOUT:
096 throw new TimeoutException(((TimeoutEvent)event).toString());
097 case CANCELLED:
098 throw new InterruptedException(((CancelledEvent)event).toString());
099 default: {
100 RuntimeException e = new RuntimeException("Unknown event type: " + event);
101 log.error(e, "");
102 throw e;
103 }
104 }
105 }
106 }