KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.iiop;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 //import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS ;
34

35 import org.omg.CORBA.Any JavaDoc;
36
37 import com.sun.corba.ee.spi.ior.IOR;
38 import com.sun.corba.ee.spi.ior.iiop.AlternateIIOPAddressComponent;
39 //import com.sun.enterprise.iiop.AlternateIIOPAddressComponent;
40
import com.sun.corba.ee.spi.ior.TaggedComponent;
41 import com.sun.corba.ee.spi.ior.iiop.IIOPProfileTemplate ;
42 import com.sun.corba.ee.spi.ior.iiop.IIOPAddress ;
43 import com.sun.corba.ee.spi.transport.IORToSocketInfo;
44 import com.sun.corba.ee.spi.transport.SocketInfo;
45
46 import com.sun.enterprise.iiop.security.SecurityMechanismSelector;
47 import com.sun.enterprise.iiop.security.ConnectionContext;
48
49 import com.sun.logging.LogDomains;
50
51 /**
52  * This implements IORToSocketInfo for ORB.
53  * Part of logic is from previous version of IIOPSSLSocketFactory.
54  * @author Shing Wai Chan
55  */

56 public class IORToSocketInfoImpl implements IORToSocketInfo {
57     private static Logger JavaDoc _logger = null;
58     static {
59         _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER);
60     }
61
62     private final String JavaDoc baseMsg = IORToSocketInfoImpl.class.getName();
63
64     // Maps primary address to list of alternate addresses.
65
// Used to compare new IOR/primaries with ones already seen for
66
// that primary. We expect them to be equal.
67
private Map JavaDoc primaryToAddresses = new HashMap JavaDoc();
68
69     // Maps primary to randomized list of alternate addresses.
70
private Map JavaDoc primaryToRandomizedAddresses = new HashMap JavaDoc();
71
72     //----- implements com.sun.corba.ee.spi.transport.IORToSocketInfo -----
73

74     public List JavaDoc getSocketInfo(IOR ior, List JavaDoc previous)
75     {
76         try {
77         if (_logger.isLoggable(Level.FINE)) {
78         _logger.log(Level.FINE, baseMsg + ".getSocketInfo->:");
79         }
80             List JavaDoc result = new ArrayList JavaDoc();
81             SecurityMechanismSelector selector = new SecurityMechanismSelector();
82             IIOPProfileTemplate iiopProfileTemplate = (IIOPProfileTemplate)ior.
83                                  getProfile().getTaggedProfileTemplate();
84             IIOPAddress primary = iiopProfileTemplate.getPrimaryAddress() ;
85             String JavaDoc host = primary.getHost().toLowerCase();
86
87             String JavaDoc type = null;
88             int port = 0;
89             ConnectionContext ctx = new ConnectionContext();
90             SocketInfo socketInfo = selector.getSSLPort(ior, ctx);
91             selector.setClientConnectionContext(ctx);
92             if (socketInfo == null) {
93                 type = SocketInfo.IIOP_CLEAR_TEXT;
94                 port = primary.getPort();
95         if (_logger.isLoggable(Level.FINE)) {
96             _logger.log(Level.FINE, baseMsg
97                 + ".getSocketInfo: did not find SSL SocketInfo");
98         }
99             } else {
100                 type = socketInfo.getType();
101                 port = socketInfo.getPort();
102         if (_logger.isLoggable(Level.FINE)) {
103             _logger.log(Level.FINE, baseMsg
104                 + ".getSocketInfo: found SSL socketInfo");
105         }
106             }
107         
108         if (_logger.isLoggable(Level.FINE)) {
109         _logger.log(Level.FINE, baseMsg
110                 + ".getSocketInfo: Connection Context:" + ctx);
111         _logger.log(Level.FINE, baseMsg
112                 + ".getSocketInfo: ENDPOINT INFO:type=" + type + ",host=" +host + ", port=" + port);
113         }
114
115             // for SSL
116
if (socketInfo != null ) {
117                 result.add(socketInfo);
118         if (_logger.isLoggable(Level.FINE)) {
119             _logger.log(Level.FINE, baseMsg
120                 + ".getSocketInfo: returning SSL socketInfo:"
121                 + " " + socketInfo.getType()
122                 + " " + socketInfo.getHost()
123                 + " " + socketInfo.getPort());
124         }
125         // REVISIT: should call IIOPPrimaryToContactInfo.reset
126
// right here to invalidate sticky for this result.
127
// However, SSL and IIOP-FO is not a supported feature.
128
return result;
129             }
130
131         ////////////////////////////////////////////////////
132
//
133
// The remainder of this code is non-SSL.
134
// Author: Harold Carr
135
// Please contact author is changes needed.
136
//
137

138             // for non-SSL
139
if (_logger.isLoggable(Level.FINE)) {
140         _logger.log(Level.FINE, baseMsg
141                 + ".getSocketInfo: returning non SSL socketInfo");
142         }
143
144         if (! previous.isEmpty()) {
145         if (_logger.isLoggable(Level.FINE)) {
146             _logger.log(Level.FINE, baseMsg
147                 + ".getSocketInfo: returning previous socketInfo: "
148                 + previous);
149         }
150         return previous;
151         }
152
153         //
154
// Save and add primary address
155
//
156

157         SocketInfo primarySocketInfo =
158         createSocketInfo("primary", type, host, port);
159         result.add(primarySocketInfo);
160
161         //
162
// List alternate addresses.
163
//
164

165         Iterator JavaDoc iterator = iiopProfileTemplate.iteratorById(
166             org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value
167         );
168
169         while (iterator.hasNext()) {
170         AlternateIIOPAddressComponent alternate =
171             (AlternateIIOPAddressComponent) iterator.next();
172         
173         host = alternate.getAddress().getHost().toLowerCase();
174         port = alternate.getAddress().getPort();
175         
176         result.add(createSocketInfo(
177             "AlternateIIOPAddressComponent",
178             SocketInfo.IIOP_CLEAR_TEXT, host, port));
179         }
180
181         synchronized (this) {
182         List JavaDoc existing = (List JavaDoc)
183             primaryToAddresses.get(primarySocketInfo);
184         if ( existing == null ) {
185             // First time we've seen this primary.
186
// Save unrandomized list with primary at head.
187
primaryToAddresses.put(primarySocketInfo, result);
188             result.remove(0); // do not randomize primary
189
// Randomized the alternates.
190
java.util.Collections.shuffle(result);
191             result.add(0, primarySocketInfo); // put primary at head
192
// Save for subsequent use.
193
primaryToRandomizedAddresses.put(primarySocketInfo,result);
194             if (_logger.isLoggable(Level.FINE)) {
195             _logger.log(Level.FINE, baseMsg
196                     + ".getSocketInfo: initial randomized result: "
197                     + result);
198             }
199             return result;
200         } else {
201             if ( result.equals(existing) ) {
202             // The are the same so return the randomized version.
203
result = (List JavaDoc)
204                 primaryToRandomizedAddresses.get(primarySocketInfo);
205             if (_logger.isLoggable(Level.FINE)) {
206                 _logger.log(Level.FINE, baseMsg
207                     + ".getSocketInfo: existing randomized result: "
208                     + result);
209             }
210             return result;
211             } else {
212             // The lists should be the same.
213
// If not log a warning and return the
214
// non-randomized current list since it is different.
215
_logger.log(Level.FINE,
216                     baseMsg + ".getSocketInfo:"
217                     + " Address lists do not match: primary: "
218                     + primarySocketInfo
219                     + "; returning current: " + result
220                     + "; existing is: " + existing);
221             return result;
222             }
223         }
224         }
225         } catch ( Exception JavaDoc ex ) {
226         _logger.log(Level.WARNING, "Exception getting SocketInfo",ex);
227         RuntimeException JavaDoc rte = new RuntimeException JavaDoc(ex.getMessage());
228         rte.initCause(ex);
229             throw rte;
230         } finally {
231         if (_logger.isLoggable(Level.FINE)) {
232         _logger.log(Level.FINE, baseMsg + ".getSocketInfo<-:");
233         }
234     }
235     }
236
237     //----- END implements com.sun.corba.ee.spi.transport.IORToSocketInfo -----
238

239     public static SocketInfo createSocketInfo(String JavaDoc msg,
240                           final String JavaDoc type,
241                           final String JavaDoc host,
242                           final int port)
243     {
244     if (_logger.isLoggable(Level.FINE)) {
245         _logger.log(Level.FINE,
246             "Address from: "
247             + msg
248             + "; type/address/port: "
249             + type + "/" + host + "/" + port);
250     }
251
252         return new SocketInfo() {
253                 public String JavaDoc getType() {
254                     return type;
255                 }
256
257                 public String JavaDoc getHost() {
258                     return host;
259                 }
260
261                 public int getPort() {
262                     return port;
263                 }
264
265         public boolean equals(Object JavaDoc o) {
266             if (o == null) {
267             return false;
268             }
269             if (! (o instanceof SocketInfo)) {
270             return false;
271             }
272             SocketInfo other = (SocketInfo)o;
273             if (other.getPort() != port) {
274             return false;
275             }
276             if (! other.getHost().equals(host)) {
277             return false;
278             }
279             if (! other.getType().equals(type)) {
280             return false;
281             }
282             return true;
283         }
284
285         public int hashCode() {
286             return type.hashCode() ^ host.hashCode() ^ port;
287         }
288
289         public String JavaDoc toString() {
290             return "SocketInfo[" + type + " " + host + " " + port +"]";
291         }
292             };
293     }
294 }
295
296 // End of file.
297

298
Popular Tags