1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.extensions.wsrm;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.addressing.AddressingBuilder;
import javax.xml.ws.addressing.AddressingProperties;
import javax.xml.ws.addressing.JAXWSAConstants;
import org.jboss.logging.Logger;
import org.jboss.ws.core.jaxws.client.ClientImpl;
import org.jboss.ws.core.utils.UUIDGenerator;
import org.jboss.ws.extensions.addressing.AddressingClientUtil;
import org.jboss.ws.extensions.wsrm.config.RMConfig;
import org.jboss.ws.extensions.wsrm.api.RMException;
import org.jboss.ws.extensions.wsrm.spi.RMConstants;
import org.jboss.ws.extensions.wsrm.spi.RMProvider;
import org.jboss.ws.extensions.wsrm.spi.protocol.RMIncompleteSequenceBehavior;
import org.jboss.ws.extensions.wsrm.transport.RMUnassignedMessageListener;
/**
* Client side implementation of the RM sequence
*
* @author richard.opalka@jboss.com
*
* @since Oct 25, 2007
*/
@SuppressWarnings("unchecked")
public final class RMClientSequence implements RMSequence, RMUnassignedMessageListener
{
private static final Logger logger = Logger.getLogger(RMClientSequence.class);
private static final String PATH_PREFIX = "/temporary_listen_address/";
private static final RMConstants wsrmConstants = RMProvider.get().getConstants();
private final RMConfig wsrmConfig;
private final boolean addressableClient;
private final Set<Long> acknowledgedOutboundMessages = new TreeSet<Long>();
private final Set<Long> receivedInboundMessages = new TreeSet<Long>();
private RMIncompleteSequenceBehavior behavior = RMIncompleteSequenceBehavior.NO_DISCARD;
private String incomingSequenceId;
private String outgoingSequenceId;
private long duration = -1;
private long creationTime;
private URI backPort;
private ClientImpl client;
private boolean isFinal;
private AtomicBoolean inboundMessageAckRequested = new AtomicBoolean();
private AtomicLong messageNumber = new AtomicLong();
private AtomicInteger countOfUnassignedMessagesAvailable = new AtomicInteger();
private static final String ANONYMOUS_URI = AddressingBuilder.getAddressingBuilder().newAddressingConstants().getAnonymousURI();
public RMClientSequence(RMConfig wsrmConfig)
{
super();
if (wsrmConfig == null)
throw new RMException("WS-RM configuration missing");
this.wsrmConfig = wsrmConfig;
this.addressableClient = wsrmConfig.getBackPortsServer() != null;
if (this.addressableClient)
{
try
{
String host = wsrmConfig.getBackPortsServer().getHost();
if (host == null)
{
host = InetAddress.getLocalHost().getCanonicalHostName();
logger.debug("Backports server configuration omits host configuration - using autodetected " + host);
}
String port = wsrmConfig.getBackPortsServer().getPort();
String path = PATH_PREFIX + UUIDGenerator.generateRandomUUIDString();
this.backPort = new URI("http://" + host + ":" + port + path);
}
catch (URISyntaxException use)
{
logger.warn(use);
throw new RMException(use.getMessage(), use);
}
catch (UnknownHostException uhe)
{
logger.warn(uhe);
throw new RMException(uhe.getMessage(), uhe);
}
}
}
public void unassignedMessageReceived()
{
// we can't use objectLock in the method - possible deadlock
this.countOfUnassignedMessagesAvailable.addAndGet(1);
logger.debug("Expected sequence expiration in " + ((System.currentTimeMillis() - this.creationTime) / 1000) + "seconds");
logger.debug("Unassigned message available in callback handler");
}
public final RMConfig getRMConfig()
{
return this.wsrmConfig;
}
public final Set<Long> getReceivedInboundMessages()
{
return this.receivedInboundMessages;
}
public final BindingProvider getBindingProvider()
{
return (BindingProvider)this.client;
}
public final void setFinal()
{
this.isFinal = true;
logger.debug("Sequence " + this.outgoingSequenceId + " state changed to final");
}
public final void ackRequested(boolean requested)
{
this.inboundMessageAckRequested.set(requested);
logger.debug("Inbound Sequence: " + this.incomingSequenceId + ", ack requested. Messages in the queue: " + this.receivedInboundMessages);
}
public final boolean isAckRequested()
{
return this.inboundMessageAckRequested.get();
}
public final void addReceivedInboundMessage(long messageId)
{
this.receivedInboundMessages.add(messageId);
logger.debug("Inbound Sequence: " + this.incomingSequenceId + ", received message no. " + messageId);
}
public final void addReceivedOutboundMessage(long messageId)
{
this.acknowledgedOutboundMessages.add(messageId);
logger.debug("Outbound Sequence: " + this.outgoingSequenceId + ", message no. " + messageId + " acknowledged by server");
}
public final void setOutboundId(String outboundId)
{
this.outgoingSequenceId = outboundId;
}
public final void setInboundId(String inboundId)
{
this.incomingSequenceId = inboundId;
}
public final void setClient(ClientImpl client)
{
this.client = client;
}
public final void setDuration(long duration)
{
if (duration > 0)
{
this.creationTime = System.currentTimeMillis();
this.duration = duration;
}
}
public final long getDuration()
{
return this.duration;
}
public final URI getBackPort()
{
// no need for synchronization
return (this.addressableClient) ? this.backPort : null;
}
public final String getAcksTo()
{
return (this.addressableClient) ? this.backPort.toString() : ANONYMOUS_URI;
}
public final long newMessageNumber()
{
// no need for synchronization
return this.messageNumber.incrementAndGet();
}
public final long getLastMessageNumber()
{
// no need for synchronization
return this.messageNumber.get();
}
public final void close() throws RMException
{
sendCloseMessage();
sendTerminateMessage();
}
private void sendMessage(String action, QName operationQName, List protocolMessages) throws RMException
{
try
{
// set up addressing properties
String address = client.getEndpointMetaData().getEndpointAddress();
AddressingProperties props = null;
if (this.client.getWSRMSequence().getBackPort() != null)
{
props = AddressingClientUtil.createDefaultProps(action, address);
props.setReplyTo(AddressingBuilder.getAddressingBuilder().newEndpointReference(this.client.getWSRMSequence().getBackPort()));
}
else
{
props = AddressingClientUtil.createAnonymousProps(action, address);
}
// prepare WS-RM request context
Map requestContext = client.getBindingProvider().getRequestContext();
Map rmRequestContext = (Map)requestContext.get(RMConstant.REQUEST_CONTEXT);
if (rmRequestContext == null)
{
rmRequestContext = new HashMap();
}
rmRequestContext.put(RMConstant.PROTOCOL_MESSAGES, protocolMessages);
rmRequestContext.put(RMConstant.SEQUENCE_REFERENCE, this);
// set up method invocation context
requestContext.put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_OUTBOUND, props);
requestContext.put(RMConstant.REQUEST_CONTEXT, rmRequestContext);
// call stub method
this.client.invoke(operationQName, new Object[] {}, client.getBindingProvider().getResponseContext());
}
catch (Exception e)
{
throw new RMException("Unable to terminate WSRM sequence", e);
}
}
public final void sendCloseMessage()
{
if (RMProvider.get().getMessageFactory().newCloseSequence() != null) // e.g. WS-RM 1.0 doesn't support this protocol message
{
while (this.isAckRequested())
{
logger.debug("Waiting till all inbound sequence acknowledgements will be sent");
sendSequenceAcknowledgementMessage();
}
Map<String, Object> wsrmReqCtx = new HashMap<String, Object>();
wsrmReqCtx.put(RMConstant.ONE_WAY_OPERATION, false);
this.getBindingProvider().getRequestContext().put(RMConstant.REQUEST_CONTEXT, wsrmReqCtx);
List msgs = new LinkedList();
msgs.add(wsrmConstants.getCloseSequenceQName());
sendMessage(RMAddressingConstants.CLOSE_SEQUENCE_WSA_ACTION, wsrmConstants.getCloseSequenceQName(), msgs);
}
}
public final void sendTerminateMessage()
{
List msgs = new LinkedList();
msgs.add(wsrmConstants.getTerminateSequenceQName());
if (this.getInboundId() != null)
{
msgs.add(wsrmConstants.getSequenceAcknowledgementQName());
}
Map<String, Object> wsrmReqCtx = new HashMap<String, Object>();
boolean oneWayOperation = RMProvider.get().getMessageFactory().newTerminateSequenceResponse() == null;
wsrmReqCtx.put(RMConstant.ONE_WAY_OPERATION, oneWayOperation);
this.getBindingProvider().getRequestContext().put(RMConstant.REQUEST_CONTEXT, wsrmReqCtx);
sendMessage(RMAddressingConstants.TERMINATE_SEQUENCE_WSA_ACTION, wsrmConstants.getTerminateSequenceQName(), msgs);
}
public final void sendSequenceAcknowledgementMessage()
{
Map<String, Object> wsrmReqCtx = new HashMap<String, Object>();
wsrmReqCtx.put(RMConstant.ONE_WAY_OPERATION, true);
this.getBindingProvider().getRequestContext().put(RMConstant.REQUEST_CONTEXT, wsrmReqCtx);
ackRequested(false);
List msgs = new LinkedList();
msgs.add(wsrmConstants.getSequenceAcknowledgementQName());
sendMessage(RMAddressingConstants.SEQUENCE_ACKNOWLEDGEMENT_WSA_ACTION, wsrmConstants.getSequenceAcknowledgementQName(), msgs);
}
public final void setBehavior(RMIncompleteSequenceBehavior behavior)
{
if (behavior != null)
{
this.behavior = behavior;
}
}
public final String getOutboundId()
{
return outgoingSequenceId;
}
public final String getInboundId()
{
return incomingSequenceId;
}
}
|