KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix.bus.ws.rm;
2
3 import java.io.IOException JavaDoc;
4 import java.util.logging.Level JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import javax.xml.datatype.Duration JavaDoc;
8
9 import org.objectweb.celtix.bus.configuration.wsrm.DestinationPolicyType;
10 import org.objectweb.celtix.bus.ws.addressing.VersionTransformer;
11 import org.objectweb.celtix.common.logging.LogUtils;
12 import org.objectweb.celtix.ws.addressing.AddressingProperties;
13 import org.objectweb.celtix.ws.addressing.v200408.AttributedURI;
14 import org.objectweb.celtix.ws.rm.AcceptType;
15 import org.objectweb.celtix.ws.rm.CreateSequenceResponseType;
16 import org.objectweb.celtix.ws.rm.CreateSequenceType;
17 import org.objectweb.celtix.ws.rm.Expires;
18 import org.objectweb.celtix.ws.rm.Identifier;
19 import org.objectweb.celtix.ws.rm.OfferType;
20 import org.objectweb.celtix.ws.rm.wsdl.SequenceFault;
21
22 public class RMServant {
23
24     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(RMServant.class);
25
26     // REVISIT assumption there is only a single outstanding unattached Identifier
27
private Identifier unattachedIdentifier;
28     
29     public RMServant() {
30     }
31     
32     /**
33      * Called on the RM handler upon inbound processing a CreateSequence request.
34      *
35      * @param
36      * @return the CreateSequenceResponse.
37      */

38     public CreateSequenceResponseType createSequence(RMDestination destination,
39                                                      CreateSequenceType cs,
40                                                      AddressingProperties maps)
41         throws SequenceFault {
42         
43         CreateSequenceResponseType csr = RMUtils.getWSRMFactory().createCreateSequenceResponseType();
44         csr.setIdentifier(destination.generateSequenceIdentifier());
45
46         DestinationPolicyType dp = destination.getDestinationPolicies();
47         Duration JavaDoc supportedDuration = dp.getSequenceExpiration();
48         if (null == supportedDuration) {
49             supportedDuration = SourceSequence.PT0S;
50         }
51         Expires ex = cs.getExpires();
52         LOG.info("supported duration: " + supportedDuration);
53
54         if (null != ex || supportedDuration.isShorterThan(SourceSequence.PT0S)) {
55             Duration JavaDoc effectiveDuration = supportedDuration;
56             if (null != ex && supportedDuration.isLongerThan(ex.getValue())) {
57                 effectiveDuration = supportedDuration;
58             }
59             ex = RMUtils.getWSRMFactory().createExpires();
60             ex.setValue(effectiveDuration);
61             csr.setExpires(ex);
62         }
63         
64         OfferType offer = cs.getOffer();
65         if (null != offer) {
66             AcceptType accept = RMUtils.getWSRMFactory().createAcceptType();
67             if (dp.isAcceptOffers()) {
68                 RMSource source = destination.getHandler().getSource();
69                 LOG.fine("Accepting inbound sequence offer");
70                 AttributedURI to = VersionTransformer.convert(maps.getTo());
71                 accept.setAcksTo(RMUtils.createReference(to.getValue()));
72                 SourceSequence seq = new SourceSequence(offer.getIdentifier(),
73                                                                     null,
74                                                                     csr.getIdentifier());
75                 seq.setExpires(offer.getExpires());
76                 seq.setTarget(VersionTransformer.convert(cs.getAcksTo()));
77                 source.addSequence(seq);
78                 source.setCurrent(csr.getIdentifier(), seq);
79                 LOG.fine("Making offered sequence the current sequence for responses to "
80                          + csr.getIdentifier().getValue());
81             } else {
82                 LOG.fine("Refusing inbound sequence offer");
83                 accept.setAcksTo(RMUtils.createReference(Names.WSA_NONE_ADDRESS));
84             }
85             csr.setAccept(accept);
86         }
87
88         DestinationSequence seq = new DestinationSequence(csr.getIdentifier(), cs.getAcksTo(), destination);
89         seq.setCorrelationID(maps.getMessageID().getValue());
90         destination.addSequence(seq);
91         
92         return csr;
93     }
94     
95     public void createSequenceResponse(RMSource source,
96                                        CreateSequenceResponseType csr,
97                                        Identifier offeredId) {
98         // moved from RMProxy.createSequence
99
// csr.getIdentifier() is the Identifier for the newly created sequence
100
SourceSequence seq = new SourceSequence(csr.getIdentifier());
101         seq.setExpires(csr.getExpires());
102         source.addSequence(seq);
103         
104         // the incoming sequence ID is either used as the requestor sequence
105
// (signalled by null) or associated with a corresponding sequence
106
// identifier
107
source.setCurrent(clearUnattachedIdentifier(), seq);
108
109         // if a sequence was offered and accepted, then we can add this to
110
// to the local destination sequence list, otherwise we have to wait for
111
// and incoming CreateSequence request
112
if (null != offeredId) {
113             assert null != csr.getAccept();
114             RMDestination dest = source.getHandler().getDestination();
115             String JavaDoc address = csr.getAccept().getAcksTo().getAddress().getValue();
116             if (!RMUtils.getAddressingConstants().getNoneURI().equals(address)) {
117                 DestinationSequence ds =
118                     new DestinationSequence(offeredId, csr.getAccept().getAcksTo(), dest);
119                 dest.addSequence(ds);
120             }
121         }
122     }
123
124     /**
125      * Checks if the terminated sequence was created in response to a createSequence
126      * request that included an offer for an inbound sequence which was accepted.
127      * the offering identifier is equal to the identifier of the sequence now terminated,
128      * and request termination of that sequence in turn.
129      *
130      * @param destination
131      * @param sid
132      * @throws SequenceFault
133      */

134     public void terminateSequence(RMDestination destination, Identifier sid)
135         throws SequenceFault {
136         // check if the terminated sequence was created in response to a a createSequence
137
// request
138

139         DestinationSequence terminatedSeq = destination.getSequence(sid);
140         if (null == terminatedSeq) {
141             LOG.severe("No such sequence.");
142             return;
143         }
144
145         destination.removeSequence(terminatedSeq);
146         
147         // the following may be necessary if the last message for this sequence was a oneway
148
// request and hence there was no response to which a last message could have been added
149

150         for (SourceSequence outboundSeq : destination.getHandler().getSource().getAllSequences()) {
151             if (outboundSeq.offeredBy(sid) && !outboundSeq.isLastMessage()) {
152                 
153                 // send an out of band message with an empty body and a
154
// sequence header containing a lastMessage element.
155

156                 RMProxy proxy = destination.getHandler().getProxy();
157                 try {
158                     proxy.lastMessage(outboundSeq);
159                 } catch (IOException JavaDoc ex) {
160                     LOG.log(Level.SEVERE, "Could not terminate correlated sequence.", ex);
161                 }
162                 
163                 
164                 break;
165             }
166         }
167         
168         
169     }
170     
171     protected Identifier clearUnattachedIdentifier() {
172         Identifier ret = unattachedIdentifier;
173         unattachedIdentifier = null;
174         return ret;
175     }
176     
177     protected void setUnattachedIdentifier(Identifier i) {
178         unattachedIdentifier = i;
179     }
180 }
181
Popular Tags