KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > transport > CorbaContactInfoListIteratorImpl


1 /*
2  * @(#)CorbaContactInfoListIteratorImpl.java 1.29 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.transport;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.omg.CORBA.COMM_FAILURE JavaDoc;
14 import org.omg.CORBA.CompletionStatus JavaDoc;
15 import org.omg.CORBA.INTERNAL JavaDoc;
16 import org.omg.CORBA.SystemException JavaDoc;
17
18 import com.sun.corba.se.pept.transport.ContactInfo ;
19 import com.sun.corba.se.pept.transport.ContactInfoList ;
20
21 import com.sun.corba.se.spi.ior.IOR ;
22 import com.sun.corba.se.spi.logging.CORBALogDomains;
23 import com.sun.corba.se.spi.orb.ORB ;
24 import com.sun.corba.se.spi.transport.CorbaContactInfo;
25 import com.sun.corba.se.spi.transport.CorbaContactInfoList;
26 import com.sun.corba.se.spi.transport.CorbaContactInfoListIterator;
27 import com.sun.corba.se.spi.transport.IIOPPrimaryToContactInfo;
28
29 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
30 import com.sun.corba.se.impl.protocol.CorbaInvocationInfo;
31
32 // REVISIT: create a unit test for this class.
33

34 public class CorbaContactInfoListIteratorImpl
35     implements
36     CorbaContactInfoListIterator
37 {
38     protected ORB orb;
39     protected CorbaContactInfoList contactInfoList;
40     protected CorbaContactInfo successContactInfo;
41     protected CorbaContactInfo failureContactInfo;
42     protected RuntimeException JavaDoc failureException;
43
44     // ITERATOR state
45
protected Iterator JavaDoc effectiveTargetIORIterator;
46     protected CorbaContactInfo previousContactInfo;
47     protected boolean isAddrDispositionRetry;
48     protected IIOPPrimaryToContactInfo primaryToContactInfo;
49     protected ContactInfo primaryContactInfo;
50     protected List JavaDoc listOfContactInfos;
51     // End ITERATOR state
52

53     public CorbaContactInfoListIteratorImpl(
54         ORB orb,
55     CorbaContactInfoList corbaContactInfoList,
56     ContactInfo primaryContactInfo,
57     List JavaDoc listOfContactInfos)
58     {
59     this.orb = orb;
60     this.contactInfoList = corbaContactInfoList;
61     this.primaryContactInfo = primaryContactInfo;
62     if (listOfContactInfos != null) {
63         // listOfContactInfos is null when used by the legacy
64
// socket factory. In that case this iterator is NOT used.
65
this.effectiveTargetIORIterator = listOfContactInfos.iterator();
66     }
67     // List is immutable so no need to synchronize access.
68
this.listOfContactInfos = listOfContactInfos;
69
70     this.previousContactInfo = null;
71     this.isAddrDispositionRetry = false;
72
73     this.successContactInfo = null;
74     this.failureContactInfo = null;
75     this.failureException = null;
76
77     primaryToContactInfo = orb.getORBData().getIIOPPrimaryToContactInfo();
78     }
79
80     ////////////////////////////////////////////////////
81
//
82
// java.util.Iterator
83
//
84

85     public boolean hasNext()
86     {
87     // REVISIT: Implement as internal closure iterator which would
88
// wraps sticky or default. Then hasNext and next just call
89
// the closure.
90

91     if (isAddrDispositionRetry) {
92         return true;
93     }
94
95     boolean result;
96
97     if (primaryToContactInfo != null) {
98         result = primaryToContactInfo.hasNext(primaryContactInfo,
99                           previousContactInfo,
100                           listOfContactInfos);
101     } else {
102         result = effectiveTargetIORIterator.hasNext();
103     }
104
105     return result;
106     }
107
108     public Object JavaDoc next()
109     {
110     if (isAddrDispositionRetry) {
111         isAddrDispositionRetry = false;
112         return previousContactInfo;
113     }
114
115     // We hold onto the last in case we get an addressing
116
// disposition retry. Then we use it again.
117

118     // We also hold onto it for the sticky manager.
119

120     if (primaryToContactInfo != null) {
121         previousContactInfo = (CorbaContactInfo)
122         primaryToContactInfo.next(primaryContactInfo,
123                       previousContactInfo,
124                       listOfContactInfos);
125     } else {
126         previousContactInfo = (CorbaContactInfo)
127         effectiveTargetIORIterator.next();
128     }
129
130     return previousContactInfo;
131     }
132
133     public void remove()
134     {
135     throw new UnsupportedOperationException JavaDoc();
136     }
137
138     ////////////////////////////////////////////////////
139
//
140
// com.sun.corba.se.pept.transport.ContactInfoListIterator
141
//
142

143     public ContactInfoList getContactInfoList()
144     {
145     return contactInfoList;
146     }
147
148     public void reportSuccess(ContactInfo contactInfo)
149     {
150     this.successContactInfo = (CorbaContactInfo)contactInfo;
151     }
152
153     public boolean reportException(ContactInfo contactInfo,
154                    RuntimeException JavaDoc ex)
155     {
156     this.failureContactInfo = (CorbaContactInfo)contactInfo;
157     this.failureException = ex;
158     if (ex instanceof COMM_FAILURE JavaDoc) {
159         SystemException JavaDoc se = (SystemException JavaDoc) ex;
160         if (se.completed == CompletionStatus.COMPLETED_NO) {
161         if (hasNext()) {
162             return true;
163         }
164         if (contactInfoList.getEffectiveTargetIOR() !=
165             contactInfoList.getTargetIOR())
166                 {
167             // retry from root ior
168
updateEffectiveTargetIOR(contactInfoList.getTargetIOR());
169             return true;
170         }
171         }
172     }
173     return false;
174     }
175
176     public RuntimeException JavaDoc getFailureException()
177     {
178     if (failureException == null) {
179         return
180         ORBUtilSystemException.get( orb,
181                         CORBALogDomains.RPC_TRANSPORT )
182             .invalidContactInfoListIteratorFailureException();
183     } else {
184         return failureException;
185     }
186     }
187
188     ////////////////////////////////////////////////////
189
//
190
// spi.CorbaContactInfoListIterator
191
//
192

193     public void reportAddrDispositionRetry(CorbaContactInfo contactInfo,
194                        short disposition)
195     {
196     previousContactInfo.setAddressingDisposition(disposition);
197     isAddrDispositionRetry = true;
198     }
199
200     public void reportRedirect(CorbaContactInfo contactInfo,
201                    IOR forwardedIOR)
202     {
203     updateEffectiveTargetIOR(forwardedIOR);
204     }
205
206     ////////////////////////////////////////////////////
207
//
208
// Implementation.
209
//
210

211     //
212
// REVISIT:
213
//
214
// The normal operation for a standard iterator is to throw
215
// ConcurrentModificationException whenever the underlying collection
216
// changes. This is implemented by keeping a modification counter (the
217
// timestamp may fail because the granularity is too coarse).
218
// Essentially what you need to do is whenever the iterator fails this
219
// way, go back to ContactInfoList and get a new iterator.
220
//
221
// Need to update CorbaClientRequestDispatchImpl to catch and use
222
// that exception.
223
//
224

225     public void updateEffectiveTargetIOR(IOR newIOR)
226     {
227     contactInfoList.setEffectiveTargetIOR(newIOR);
228     // If we report the exception in _request (i.e., beginRequest
229
// we cannot throw RemarshalException to the stub because _request
230
// does not declare that exception.
231
// To keep the two-level dispatching (first level chooses ContactInfo,
232
// second level is specific to that ContactInfo/EPT) we need to
233
// ensure that the request dispatchers get their iterator from the
234
// InvocationStack (i.e., ThreadLocal). That way if the list iterator
235
// needs a complete update it happens right here.
236
((CorbaInvocationInfo)orb.getInvocationInfo())
237         .setContactInfoListIterator(contactInfoList.iterator());
238     }
239 }
240
241 // End of file.
242
Popular Tags