Clover coverage report - Maven Clover report
Coverage timestamp: Sun Jun 1 2008 19:59:48 EST
file stats: LOC: 129   Methods: 6
NCLOC: 91   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BasicEnvelopeDispatcherManager.java 42.9% 75.7% 100% 70.2%
coverage coverage
 1    /**
 2    *
 3    * Copyright 2003-2005 Core Developers Network Ltd.
 4    *
 5    * Licensed under the Apache License, Version 2.0 (the "License");
 6    * you may not use this file except in compliance with the License.
 7    * You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    */
 17    package org.codehaus.wadi.group.impl;
 18   
 19    import java.util.ArrayList;
 20    import java.util.Collection;
 21    import java.util.IdentityHashMap;
 22    import java.util.Iterator;
 23   
 24    import org.apache.commons.logging.Log;
 25    import org.apache.commons.logging.LogFactory;
 26    import org.codehaus.wadi.group.Envelope;
 27    import org.codehaus.wadi.group.ServiceEndpoint;
 28   
 29    /**
 30    * @author <a href="mailto:jules@coredevelopers.net">Jules Gosnell</a>
 31    * @version $Revision: 1595 $
 32    */
 33    class BasicEnvelopeDispatcherManager implements EnvelopeDispatcherManager {
 34    protected static final Log _messageLog = LogFactory.getLog("org.codehaus.wadi.INBOUND_MESSAGES");
 35   
 36    private final AbstractDispatcher _dispatcher;
 37    private final Log _log = LogFactory.getLog(getClass());
 38    private final IdentityHashMap _msgDispatchers = new IdentityHashMap();
 39    private final ThreadPool _executor;
 40   
 41  8 public BasicEnvelopeDispatcherManager(AbstractDispatcher dispatcher, ThreadPool executor) {
 42  8 _dispatcher = dispatcher;
 43  8 _executor = executor;
 44    }
 45   
 46  3 public void register(ServiceEndpoint msgDispatcher) {
 47  3 synchronized (_msgDispatchers) {
 48  3 _msgDispatchers.put(msgDispatcher, new ServiceEndpointWrapper(msgDispatcher));
 49    }
 50    }
 51   
 52  3 public void unregister(ServiceEndpoint msgDispatcher, int nbAttemp, long delayMillis) {
 53  3 ServiceEndpointWrapper seWrapper;
 54  3 synchronized (_msgDispatchers) {
 55  3 seWrapper = (ServiceEndpointWrapper) _msgDispatchers.remove(msgDispatcher);
 56  3 if (null == seWrapper) {
 57  0 throw new IllegalArgumentException(msgDispatcher + " is unknown.");
 58    }
 59    }
 60   
 61    // this isn't failproof - if onMessage has not yet been called,
 62    // the counter may still read 0 - but it's the best we can do...
 63  3 for (int i= nbAttemp; seWrapper.getNumberOfCurrentDispatch()>0 && i > 0; i--) {
 64  0 try {
 65  0 Thread.sleep(delayMillis);
 66    } catch (InterruptedException e) {
 67    // ignore
 68    }
 69    }
 70    }
 71   
 72  3 public void onEnvelope(Envelope message) {
 73  3 Collection targetDispatchers;
 74  3 synchronized (_msgDispatchers) {
 75  3 targetDispatchers = new ArrayList(_msgDispatchers.values());
 76    }
 77   
 78  3 if (targetDispatchers.size() == 0) {
 79  0 if (_log.isWarnEnabled()) {
 80  0 _log.warn("spurious message received: " + message);
 81    }
 82  0 return;
 83    }
 84   
 85  3 try {
 86  3 for (Iterator iter = targetDispatchers.iterator(); iter.hasNext();) {
 87  3 ServiceEndpointWrapper dispatcher = (ServiceEndpointWrapper) iter.next();
 88  3 boolean dispatchMessage = dispatcher.testDispatchEnvelope(message);
 89  3 if (dispatchMessage) {
 90  3 if (_messageLog.isTraceEnabled()) {
 91  0 _messageLog.trace(message +
 92    " {"+ message.getReplyTo() +
 93    "->"+ message.getAddress() +
 94    "} - " +
 95    message.getTargetCorrelationId() +
 96    "/" +
 97    message.getSourceCorrelationId());
 98    }
 99  3 dispatcher.beforeDispatch();
 100  3 _executor.execute(new DispatchRunner(dispatcher, message));
 101  3 break;
 102    }
 103    }
 104    } catch (InterruptedException e) {
 105  0 _log.warn("bad message", e);
 106    }
 107    }
 108   
 109    class DispatchRunner implements Runnable {
 110    protected final ServiceEndpointWrapper _msgDispatcher;
 111    protected final Envelope _message;
 112   
 113  3 public DispatchRunner(ServiceEndpointWrapper msgDispatcher, Envelope message) {
 114  3 _msgDispatcher=msgDispatcher;
 115  3 _message=message;
 116    }
 117   
 118  3 public void run() {
 119  3 try {
 120  3 _dispatcher.hook();
 121  3 _msgDispatcher.dispatch(_message);
 122  3 _msgDispatcher.afterDispatch();
 123    } catch (Exception e) {
 124  0 _log.error("problem dispatching message", e);
 125    }
 126    }
 127    }
 128   
 129    }