KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > ws > rm > RMProxy


1 package org.objectweb.celtix.bus.ws.rm;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.xml.ws.handler.MessageContext;
10
11 import org.objectweb.celtix.bindings.AbstractClientBinding;
12 import org.objectweb.celtix.bindings.BindingContextUtils;
13 import org.objectweb.celtix.bindings.DataBindingCallback;
14 import org.objectweb.celtix.bindings.Request;
15 import org.objectweb.celtix.bindings.Response;
16 import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
17 import org.objectweb.celtix.bus.ws.addressing.VersionTransformer;
18 import org.objectweb.celtix.common.i18n.Message;
19 import org.objectweb.celtix.common.logging.LogUtils;
20 import org.objectweb.celtix.transports.Transport;
21 import org.objectweb.celtix.ws.addressing.AddressingProperties;
22 import org.objectweb.celtix.ws.addressing.RelatesToType;
23 import org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType;
24 import org.objectweb.celtix.ws.rm.CreateSequenceResponseType;
25 import org.objectweb.celtix.ws.rm.Identifier;
26 import org.objectweb.celtix.ws.rm.OfferType;
27 import org.objectweb.celtix.ws.rm.persistence.RMDestinationSequence;
28 import org.objectweb.celtix.ws.rm.wsdl.SequenceFault;
29
30 public class RMProxy {
31
32     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(RMProxy.class);
33     private RMHandler handler;
34     // REVISIT assumption there is only a single outstanding offer
35
private Identifier offeredIdentifier;
36
37     public RMProxy(RMHandler h) {
38         handler = h;
39     }
40
41     public void createSequence(RMSource source,
42                                org.objectweb.celtix.ws.addressing.EndpointReferenceType to,
43                                EndpointReferenceType acksTo,
44                                RelatesToType relatesTo)
45         throws IOException JavaDoc, SequenceFault {
46         CreateSequenceRequest request =
47             new CreateSequenceRequest(handler.getBinding(),
48                                       getTransport(),
49                                       source,
50                                       to,
51                                       acksTo,
52                                       relatesTo);
53         setOfferedIdentifier(request.getIncludedOffer());
54
55         send(request, CreateSequenceRequest.createDataBindingCallback());
56     }
57     
58     public void createSequenceResponse(AddressingProperties inMAPs,
59                                        CreateSequenceResponseType csr)
60         throws IOException JavaDoc, SequenceFault {
61         CreateSequenceResponse request =
62             new CreateSequenceResponse(handler.getBinding(),
63                                        getTransport(),
64                                        inMAPs,
65                                        csr);
66
67         send(request, CreateSequenceResponse.createDataBindingCallback());
68     }
69
70     public void terminateSequence(SourceSequence seq) throws IOException JavaDoc {
71         if (canSend(seq.getTarget())) {
72             TerminateSequenceRequest request =
73                 new TerminateSequenceRequest(handler.getBinding(),
74                                              getTransport(),
75                                              seq);
76             // required?
77
handler.getSource().removeSequence(seq);
78
79             send(request, TerminateSequenceRequest.createDataBindingCallback());
80         }
81     }
82     
83     /**
84      * Send a standalone message requesting acknowledgments for the
85      * given sequences.
86      *
87      * @param seqs the sequences for which acknowledgments are requested.
88      * @throws IOException
89      */

90     public void requestAcknowledgment(Collection JavaDoc<SourceSequence> seqs) throws IOException JavaDoc {
91         // it only makes sense to relate a group of sequnces in the same
92
// AckRequest if they all have the same AcksTo, hence we can safely
93
// take the AckTo from the first sequence in the collection
94
SourceSequence first = getFirstSequence(seqs);
95         if (canSend(first.getTarget())) {
96             SequenceInfoRequest request =
97                 new SequenceInfoRequest(handler.getBinding(),
98                                         getTransport(),
99                                         first.getTarget());
100             request.requestAcknowledgement(seqs);
101             send(request, null);
102         }
103     }
104     
105     /**
106      * Send a standalone LastMessage message for the given sequence.
107      *
108      * @param seq the sequence for which the last message is to be sent.
109      * @throws IOException
110      */

111     
112     public void lastMessage(SourceSequence seq) throws IOException JavaDoc {
113         LOG.fine("sending standalone last message");
114         if (canSend(seq.getTarget())) {
115             SequenceInfoRequest request =
116                 new SequenceInfoRequest(handler.getBinding(),
117                                         getTransport(),
118                                         seq.getTarget());
119             request.lastMessage(seq);
120             send(request, null);
121         }
122     }
123     
124     /**
125      * Send a standalone SequenceAcknowledgement message for the given sequence.
126      *
127      * @param seq the sequence for which an acknowledgment is to be sent.
128      * @throws IOException
129      */

130     public void acknowledge(RMDestinationSequence seq) throws IOException JavaDoc {
131         // required?
132
if (Names.WSA_ANONYMOUS_ADDRESS.equals(seq.getAcksTo().getAddress().getValue())) {
133             LOG.log(Level.WARNING, "STANDALONE_ANON_ACKS_NOT_SUPPORTED");
134             return;
135         }
136         LOG.fine("sending standalone sequence acknowledgment");
137         if (canSend(seq.getAcksTo())) {
138             SequenceInfoRequest request =
139                 new SequenceInfoRequest(handler.getBinding(),
140                                         handler.getTransport(),
141                                         seq.getAcksTo());
142             request.acknowledge(seq);
143             send(request, null);
144         }
145     }
146         
147     protected Identifier getOfferedIdentifier() {
148         return offeredIdentifier;
149     }
150     
151     protected void setOfferedIdentifier(OfferType offer) {
152         if (offer != null) {
153             offeredIdentifier = offer.getIdentifier();
154         }
155     }
156     
157     private void send(Request request, DataBindingCallback callback)
158         throws IOException JavaDoc {
159         
160         boolean isOneway = request.isOneway();
161         if (handler.getBinding() != null) {
162             handler.getBinding().send(request, callback);
163             if (!(handler.getClientBinding() == null || isOneway)) {
164                 Response response =
165                     ((AbstractClientBinding)handler.getClientBinding())
166                         .getResponseCorrelator().getResponse(request);
167                 response.setHandlerInvoker(request.getHandlerInvoker());
168                 MessageContext responseContext = response.getBindingMessageContext();
169                 DataBindingCallback responseCallback =
170                     BindingContextUtils.retrieveDataBindingCallback(responseContext);
171                 response.processLogical(responseCallback);
172             }
173         } else {
174             AddressingProperties maps =
175                 ContextUtils.retrieveMAPs(request.getObjectMessageContext(), true, true);
176             String JavaDoc action = maps.getAction() != null
177                             ? maps.getAction().getValue()
178                             : "empty";
179             Message msg = new Message("NO_BINDING_FOR_OUT_OF_BAND_MSG", LOG, action);
180             LOG.severe(msg.toString());
181         }
182     }
183     
184     /**
185      * A outgoing out-of-band protocol message cannot be sent if from the server
186      * side if the target (e.g. the AcksTo address) is anonymous.
187      *
188      * @param to the target EPR
189      * @return true if the message may be sent
190      */

191     protected boolean canSend(EndpointReferenceType to) {
192         return !(handler.getClientBinding() == null
193                  && ContextUtils.isGenericAddress(VersionTransformer.convert(to)));
194     }
195     
196     /**
197      * A outgoing out-of-band protocol message cannot be sent if from the server
198      * side if the target (e.g. the AcksTo address) is anonymous.
199      *
200      * @param to the target EPR
201      * @return true if the message may be sent
202      */

203     protected boolean canSend(org.objectweb.celtix.ws.addressing.EndpointReferenceType to) {
204         boolean ret = false;
205         if (handler.getClientBinding() == null) {
206             ret = !ContextUtils.isGenericAddress(to);
207         } else {
208             try {
209                 ret = ((AbstractClientBinding)handler.getClientBinding()).getTransport() != null;
210             } catch (IOException JavaDoc ioe) {
211                 // ignore
212
}
213         }
214         return ret;
215     }
216     
217     /**
218      * This is required as the resource injected transport may be shutdown already
219      * (e.g. for LastMessage or TerminateSequence messages originating from
220      * BusLifeCycleListener.preShutdown()).
221      *
222      * @return
223      */

224     protected Transport getTransport() {
225         Transport ret = null;
226         if (handler.getClientBinding() == null) {
227             ret = handler.getTransport();
228         } else {
229             try {
230                 ret = ((AbstractClientBinding)handler.getClientBinding()).getTransport();
231             } catch (IOException JavaDoc ioe) {
232                 // ignore
233
}
234         }
235         return ret;
236     }
237     
238     
239     private SourceSequence getFirstSequence(Collection JavaDoc<SourceSequence> seqs) {
240         Iterator JavaDoc<SourceSequence> i = seqs.iterator();
241         return i.hasNext() ? i.next() : null;
242     }
243 }
244
Popular Tags