aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/com/sun/corba/se/impl/transport/CorbaContactInfoListIteratorImpl.java
blob: 599752c5c71904199c3522371b3ea60aabba22fc (plain)
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
/*
 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.corba.se.impl.transport;

import java.util.Iterator;
import java.util.List;

import org.omg.CORBA.COMM_FAILURE;
import org.omg.CORBA.CompletionStatus;
import org.omg.CORBA.INTERNAL;
import org.omg.CORBA.SystemException;

import com.sun.corba.se.pept.transport.ContactInfo ;
import com.sun.corba.se.pept.transport.ContactInfoList ;

import com.sun.corba.se.spi.ior.IOR ;
import com.sun.corba.se.spi.logging.CORBALogDomains;
import com.sun.corba.se.spi.orb.ORB ;
import com.sun.corba.se.spi.transport.CorbaContactInfo;
import com.sun.corba.se.spi.transport.CorbaContactInfoList;
import com.sun.corba.se.spi.transport.CorbaContactInfoListIterator;
import com.sun.corba.se.spi.transport.IIOPPrimaryToContactInfo;

import com.sun.corba.se.impl.logging.ORBUtilSystemException;
import com.sun.corba.se.impl.protocol.CorbaInvocationInfo;

// REVISIT: create a unit test for this class.

public class CorbaContactInfoListIteratorImpl
    implements
        CorbaContactInfoListIterator
{
    protected ORB orb;
    protected CorbaContactInfoList contactInfoList;
    protected CorbaContactInfo successContactInfo;
    protected CorbaContactInfo failureContactInfo;
    protected RuntimeException failureException;

    // ITERATOR state
    protected Iterator effectiveTargetIORIterator;
    protected CorbaContactInfo previousContactInfo;
    protected boolean isAddrDispositionRetry;
    protected IIOPPrimaryToContactInfo primaryToContactInfo;
    protected ContactInfo primaryContactInfo;
    protected List listOfContactInfos;
    // End ITERATOR state

    public CorbaContactInfoListIteratorImpl(
        ORB orb,
        CorbaContactInfoList corbaContactInfoList,
        ContactInfo primaryContactInfo,
        List listOfContactInfos)
    {
        this.orb = orb;
        this.contactInfoList = corbaContactInfoList;
        this.primaryContactInfo = primaryContactInfo;
        if (listOfContactInfos != null) {
            // listOfContactInfos is null when used by the legacy
            // socket factory.  In that case this iterator is NOT used.
            this.effectiveTargetIORIterator = listOfContactInfos.iterator();
        }
        // List is immutable so no need to synchronize access.
        this.listOfContactInfos = listOfContactInfos;

        this.previousContactInfo = null;
        this.isAddrDispositionRetry = false;

        this.successContactInfo = null;
        this.failureContactInfo = null;
        this.failureException = null;

        primaryToContactInfo = orb.getORBData().getIIOPPrimaryToContactInfo();
    }

    ////////////////////////////////////////////////////
    //
    // java.util.Iterator
    //

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

        if (isAddrDispositionRetry) {
            return true;
        }

        boolean result;

        if (primaryToContactInfo != null) {
            result = primaryToContactInfo.hasNext(primaryContactInfo,
                                                  previousContactInfo,
                                                  listOfContactInfos);
        } else {
            result = effectiveTargetIORIterator.hasNext();
        }

        return result;
    }

    public Object next()
    {
        if (isAddrDispositionRetry) {
            isAddrDispositionRetry = false;
            return previousContactInfo;
        }

        // We hold onto the last in case we get an addressing
        // disposition retry.  Then we use it again.

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

        if (primaryToContactInfo != null) {
            previousContactInfo = (CorbaContactInfo)
                primaryToContactInfo.next(primaryContactInfo,
                                          previousContactInfo,
                                          listOfContactInfos);
        } else {
            previousContactInfo = (CorbaContactInfo)
                effectiveTargetIORIterator.next();
        }

        return previousContactInfo;
    }

    public void remove()
    {
        throw new UnsupportedOperationException();
    }

    ////////////////////////////////////////////////////
    //
    // com.sun.corba.se.pept.transport.ContactInfoListIterator
    //

    public ContactInfoList getContactInfoList()
    {
        return contactInfoList;
    }

    public void reportSuccess(ContactInfo contactInfo)
    {
        this.successContactInfo = (CorbaContactInfo)contactInfo;
    }

    public boolean reportException(ContactInfo contactInfo,
                                   RuntimeException ex)
    {
        this.failureContactInfo = (CorbaContactInfo)contactInfo;
        this.failureException = ex;
        if (ex instanceof COMM_FAILURE) {
            SystemException se = (SystemException) ex;
            if (se.completed == CompletionStatus.COMPLETED_NO) {
                if (hasNext()) {
                    return true;
                }
                if (contactInfoList.getEffectiveTargetIOR() !=
                    contactInfoList.getTargetIOR())
                {
                    // retry from root ior
                    updateEffectiveTargetIOR(contactInfoList.getTargetIOR());
                    return true;
                }
            }
        }
        return false;
    }

    public RuntimeException getFailureException()
    {
        if (failureException == null) {
            return
                ORBUtilSystemException.get( orb,
                                            CORBALogDomains.RPC_TRANSPORT )
                    .invalidContactInfoListIteratorFailureException();
        } else {
            return failureException;
        }
    }

    ////////////////////////////////////////////////////
    //
    // spi.CorbaContactInfoListIterator
    //

    public void reportAddrDispositionRetry(CorbaContactInfo contactInfo,
                                           short disposition)
    {
        previousContactInfo.setAddressingDisposition(disposition);
        isAddrDispositionRetry = true;
    }

    public void reportRedirect(CorbaContactInfo contactInfo,
                               IOR forwardedIOR)
    {
        updateEffectiveTargetIOR(forwardedIOR);
    }

    ////////////////////////////////////////////////////
    //
    // Implementation.
    //

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

    public void updateEffectiveTargetIOR(IOR newIOR)
    {
        contactInfoList.setEffectiveTargetIOR(newIOR);
        // If we report the exception in _request (i.e., beginRequest
        // we cannot throw RemarshalException to the stub because _request
        // does not declare that exception.
        // To keep the two-level dispatching (first level chooses ContactInfo,
        // second level is specific to that ContactInfo/EPT) we need to
        // ensure that the request dispatchers get their iterator from the
        // InvocationStack (i.e., ThreadLocal). That way if the list iterator
        // needs a complete update it happens right here.
        ((CorbaInvocationInfo)orb.getInvocationInfo())
            .setContactInfoListIterator(contactInfoList.iterator());
    }
}

// End of file.