KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > iiop > IIOPPrimaryToContactInfoImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.iiop;
25
26 // NOTE: This is the EXACT same file as in corba/folb unit test,
27
// except for parts commented out.
28
// REVISIT: Need to use from a direct copy of AS version.
29
// Just update Ant to build and use it AND provide dummy logging code.
30

31 //package corba.folb;
32

33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39
40 import com.sun.logging.LogDomains;
41
42 import com.sun.corba.ee.pept.transport.ContactInfo;
43 import com.sun.corba.ee.spi.ior.IOR;
44 import com.sun.corba.ee.spi.orb.ORB;
45 import com.sun.corba.ee.spi.transport.CorbaContactInfoList;
46 import com.sun.corba.ee.spi.transport.IIOPPrimaryToContactInfo;
47 import com.sun.corba.ee.spi.transport.SocketInfo;
48 import com.sun.corba.ee.impl.orbutil.ORBUtility;
49
50 /**
51  * This is the "sticky manager" - based on the 7.1 EE concept.
52  * @author Harold Carr
53  */

54 public class IIOPPrimaryToContactInfoImpl
55     implements IIOPPrimaryToContactInfo
56 {
57
58     // REVISIT - log messages must be internationalized.
59

60     private static Logger JavaDoc _logger = null;
61     static {
62        _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER);
63     }
64
65     /*
66     private static MyLogger _logger = new MyLogger();
67     */

68
69     public final String JavaDoc baseMsg = IIOPPrimaryToContactInfoImpl.class.getName();
70
71     private Map JavaDoc map;
72     private boolean debugChecked;
73     private boolean debug;
74
75     public IIOPPrimaryToContactInfoImpl()
76     {
77     map = new HashMap JavaDoc();
78     debugChecked = false;
79     debug = false;
80     }
81
82     public synchronized void reset(ContactInfo primary)
83     {
84     try {
85         if (debug) {
86         dprint(".reset: " + getKey(primary));
87         }
88         map.remove(getKey(primary));
89     } catch (Throwable JavaDoc t) {
90             _logger.log(Level.WARNING,
91             "Problem in " + baseMsg + ".reset",
92             t);
93         RuntimeException JavaDoc rte =
94         new RuntimeException JavaDoc(baseMsg + ".reset error");
95         rte.initCause(t);
96         throw rte;
97     }
98     }
99
100     public synchronized boolean hasNext(ContactInfo primary,
101                     ContactInfo previous,
102                     List JavaDoc contactInfos)
103     {
104     try {
105         if (! debugChecked) {
106         debugChecked = true;
107         debug = ((ORB)primary.getBroker()).transportDebugFlag
108                 || _logger.isLoggable(Level.FINE);
109         }
110
111         if (debug) {
112         dprint(".hasNext->: "
113                + formatKeyPreviousList(getKey(primary),
114                            previous,
115                            contactInfos));
116         }
117         boolean result;
118         if (previous == null) {
119         result = true;
120         } else {
121         int previousIndex = contactInfos.indexOf(previous);
122         int contactInfosSize = contactInfos.size();
123         if (debug) {
124             dprint(".hasNext: "
125                + previousIndex + " " + contactInfosSize);
126         }
127         if (previousIndex < 0) {
128             // This SHOULD not happen.
129
// It would only happen if the previous is NOT
130
// found in the current list of contactInfos.
131
RuntimeException JavaDoc rte = new RuntimeException JavaDoc(
132
133
134             "Problem in " + baseMsg + ".hasNext: previousIndex: "
135             + previousIndex);
136
137             _logger.log(Level.SEVERE,
138             "Problem in " + baseMsg + ".hasNext: previousIndex: "
139             + previousIndex, rte);
140             throw rte;
141         } else {
142             // Since this is a retry, ensure that there is a following
143
// ContactInfo for .next
144
result = (contactInfosSize - 1) > previousIndex;
145         }
146         }
147         if (debug) {
148         dprint(".hasNext<-: " + result);
149         }
150         return result;
151     } catch (Throwable JavaDoc t) {
152             _logger.log(Level.WARNING,
153             "Problem in " + baseMsg + ".hasNext",
154             t);
155         RuntimeException JavaDoc rte =
156         new RuntimeException JavaDoc(baseMsg + ".hasNext error");
157         rte.initCause(t);
158         throw rte;
159     }
160     }
161
162     public synchronized ContactInfo next(ContactInfo primary,
163                      ContactInfo previous,
164                      List JavaDoc contactInfos)
165     {
166     try {
167         String JavaDoc debugMsg = null;
168
169         if (debug) {
170         debugMsg = "";
171         dprint(".next->: "
172                + formatKeyPreviousList(getKey(primary),
173                            previous,
174                            contactInfos));
175         dprint(".next: map: " + formatMap(map));
176         }
177
178         Object JavaDoc result = null;
179
180         if (previous == null) {
181         // This is NOT a retry.
182
result = map.get(getKey(primary));
183         if (result == null) {
184             if (debug) {
185             debugMsg = ".next<-: initialize map: ";
186             }
187             // NOTE: do not map primary to primary.
188
// In case of local transport we NEVER use primary.
189
result = contactInfos.get(0);
190             map.put(getKey(primary), result);
191         } else {
192             if (debug) {
193             dprint(".next: primary mapped to: " + result);
194             }
195             int position = contactInfos.indexOf(result);
196             if (position == -1) {
197             // It is possible that communication to the key
198
// took place on SharedCDR, then a corbaloc to
199
// same location uses a SocketOrChannelContactInfo
200
// and vice versa.
201
if (debug) {
202                 dprint(".next: cannot find mapped entry in current list. Removing mapped entry and trying .next again.");
203             }
204             reset(primary);
205             return next(primary, previous, contactInfos);
206             }
207             // NOTE: This step is critical. You do NOT want to
208
// return contact info from the map. You want to find
209
// it, as a SocketInfo, in the current list, and then
210
// return that ContactInfo. Otherwise you will potentially
211
// return a ContactInfo pointing to an incorrect IOR.
212
result = contactInfos.get(position);
213             if (debug) {
214             debugMsg = ".next<-: mapped: ";
215             }
216         }
217         } else {
218         // This is a retry.
219
// If previous is last element then .next is not called
220
// because hasNext will return false.
221
result = contactInfos.get(contactInfos.indexOf(previous) + 1);
222         map.put(getKey(primary), result);
223
224         _logger.log(Level.INFO, "IIOP failover to: " + result);
225
226         if (debug) {
227             debugMsg = ".next<-: update map: "
228             + " " + contactInfos.indexOf(previous)
229             + " " + contactInfos.size() + " ";
230         }
231         }
232         if (debug) {
233         dprint(debugMsg + result);
234         }
235         return (ContactInfo) result;
236     } catch (Throwable JavaDoc t) {
237             _logger.log(Level.WARNING,
238             "Problem in " + baseMsg + ".next",
239             t);
240         RuntimeException JavaDoc rte =
241         new RuntimeException JavaDoc(baseMsg + ".next error");
242         rte.initCause(t);
243         throw rte;
244     }
245     }
246
247     private Object JavaDoc getKey(ContactInfo contactInfo)
248     {
249     if (((SocketInfo)contactInfo).getPort() == 0) {
250         // When CSIv2 is used the primary will have a zero port.
251
// Therefore type/host/port will NOT be unique.
252
// So use the entire IOR for the key in that case.
253
return ((CorbaContactInfoList)contactInfo.getContactInfoList())
254         .getEffectiveTargetIOR();
255     } else {
256         return contactInfo;
257     }
258     }
259
260     private String JavaDoc formatKeyPreviousList(Object JavaDoc key,
261                      ContactInfo previous, List JavaDoc list)
262     {
263     String JavaDoc result =
264           "\n key : " + key
265         + "\n previous: " + previous
266         + "\n list:";
267     Iterator JavaDoc i = list.iterator();
268     int count = 1;
269     while (i.hasNext()) {
270         result += "\n " + count++ + " " + i.next();
271     }
272     return result;
273     }
274
275     private String JavaDoc formatMap(Map JavaDoc map)
276     {
277     String JavaDoc result = "";
278     synchronized (map) {
279         Iterator JavaDoc i = map.entrySet().iterator();
280         if (! i.hasNext()) {
281         return "empty";
282         }
283         while (i.hasNext()) {
284         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
285         result +=
286               "\n key : " + entry.getKey()
287             + "\n value: " + entry.getValue()
288             + "\n";
289         }
290     }
291     return result;
292     }
293
294     private void dprint(String JavaDoc msg)
295     {
296     /*
297     ORBUtility.dprint("IIOPPrimaryToContactInfoImpl", msg);
298     */

299     _logger.log(Level.FINE, msg);
300     }
301 }
302
303 /*
304 class MyLogger
305 {
306     void log(Level level, String msg, Throwable t)
307     {
308     ORBUtility.dprint("IIOPPrimaryToContactInfoImpl.MyLogger.log", msg);
309     t.printStackTrace(System.out);
310     }
311 }
312 */

313
314 // End of file.
315
Popular Tags