KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CORBA.COMM_FAILURE JavaDoc;
33 import org.omg.CORBA.CompletionStatus JavaDoc;
34
35 import com.sun.corba.ee.spi.transport.SocketInfo;
36 import com.sun.corba.ee.spi.legacy.connection.GetEndPointInfoAgainException;
37 import com.sun.corba.ee.spi.legacy.connection.ORBSocketFactory;
38 import com.sun.corba.ee.spi.ior.IOR;
39 import com.sun.corba.ee.spi.ior.iiop.IIOPProfileTemplate;
40 import com.sun.corba.ee.spi.ior.iiop.IIOPAddress;
41 import com.sun.corba.ee.impl.legacy.connection.EndPointInfoImpl;
42
43 import java.util.logging.*;
44 import com.sun.logging.*;
45
46 import org.omg.IOP.Codec JavaDoc;
47 import org.omg.IOP.CodecFactory JavaDoc;
48 import org.omg.IOP.CodecFactoryHelper JavaDoc;
49 import org.omg.IOP.Encoding JavaDoc;
50
51 import java.util.Hashtable JavaDoc;
52 import org.omg.CORBA.Any JavaDoc;
53 import org.omg.IOP.TaggedComponent JavaDoc;
54
55 /**
56  *
57  *
58  */

59 public class SocketFactory extends com.sun.corba.ee.impl.legacy.connection.DefaultSocketFactory {
60
61     private static Logger _logger=null;
62     static {
63        _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER);
64     }
65     
66     private Hashtable JavaDoc endpointTable = new Hashtable JavaDoc();
67     private Codec JavaDoc codec;
68
69     /** Creates a new instance of SocketFactory */
70     public SocketFactory() {
71         super();
72     }
73
74     public SocketInfo getEndPointInfo(ORB JavaDoc orb, IOR ior,
75                     SocketInfo endPointInfo) {
76         try {
77             IIOPProfileTemplate temp = (IIOPProfileTemplate)ior.
78                          getProfile().getTaggedProfileTemplate();
79             IIOPAddress primary = temp.getPrimaryAddress() ;
80             String JavaDoc host = primary.getHost().toLowerCase();
81             int port = primary.getPort();
82       
83         if(_logger.isLoggable(Level.FINE)) {
84         _logger.log(Level.FINE,"ENDPOINT INFO:host=" +host + ", port=" + port);
85         }
86             
87             // If there is already a cached EndPointImpl for the primary host and
88
// port return that.
89
if ((endPointInfo = (EndPointInfoImpl)endpointTable.get(host + port)) != null)
90                 return endPointInfo;
91             
92             TaggedComponent JavaDoc alternateAddressTaggedComponents[] =
93                        ior.getProfile().getTaggedProfileTemplate().
94             getIOPComponents((com.sun.corba.ee.spi.orb.ORB)orb,
95                                org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value
96                            //AlternateIIOPAddressComponent.TAG_ALTERNATE_IIOP_ADDRESS_ID
97
);
98             if (alternateAddressTaggedComponents.length > 0) {
99                 getCodec(orb);
100                 for (int i = 0; i < alternateAddressTaggedComponents.length; i++) {
101                     byte[] data = alternateAddressTaggedComponents[i].component_data;
102                     Any JavaDoc any = null;
103                     try {
104                         any = codec.decode_value(data,
105                                  AlternateIIOPAddressComponentHelper.type());
106                     } catch (org.omg.IOP.CodecPackage.TypeMismatch JavaDoc e) {
107                         if (_logger.isLoggable(Level.FINE)){
108                             _logger.log(Level.FINE,"Exception codec TypeMismatch",e);
109             }
110                         throw new RuntimeException JavaDoc(e.toString());
111                     } catch (org.omg.IOP.CodecPackage.FormatMismatch JavaDoc e) {
112                         if (_logger.isLoggable(Level.FINE) ){
113                             _logger.log(Level.FINE,"Exception codec FormatMismatch",e);
114             }
115                         throw new RuntimeException JavaDoc(e.toString());
116                     }
117                     AlternateIIOPAddressComponent iiopAddress =
118                         AlternateIIOPAddressComponentHelper.extract(any);
119                     // if any host:port has an EndPointImpl in the endpointTable
120
// we should make sure we set the primary host:port with
121
// that EndPointImpl in the endpointTable and return that.
122
// Subsequently we will not even get here, since for the
123
// primary host:port the cached value will be returned
124
if ((endPointInfo = (EndPointInfoImpl)endpointTable.
125                             get(iiopAddress.host + iiopAddress.port)) != null) {
126                                 /*
127                        if (!type.equals(ORBSocketFactory.IIOP_CLEAR_TEXT)) {
128                            // Use this host address to make the connection, since a connection
129                            // has already been made using this address, just use the secure port.
130                            // It is assumed that the primary host address in the IOR is also
131                            // one of the taggeded alternate addresses, which is a fair assumption.
132                            // Hence the remote machine should be reacheable thru the cached
133                            // host address and new secure port being specified
134                            endPointInfo = new EndPointInfoImpl(type, iiopAddress.host, port);
135                            endpointTable.put(host + port, endPointInfo);
136                            return endPointInfo;
137                        } else { */

138                            endpointTable.put(host + port, endPointInfo);
139                            return endPointInfo;
140                        //}
141
}
142                 }
143                 // In case nothing is found use primary host:port
144
endPointInfo = new EndPointInfoImpl(ORBSocketFactory.IIOP_CLEAR_TEXT, port, host);
145                 endpointTable.put(host + port, endPointInfo);
146            } else {
147                 
148                 endPointInfo = new EndPointInfoImpl(ORBSocketFactory.IIOP_CLEAR_TEXT, port, host);
149                 endpointTable.put(host + port, endPointInfo);
150             }
151             return endPointInfo;
152         } catch ( Exception JavaDoc ex ) {
153             if (_logger.isLoggable(Level.FINE)){
154                 _logger.log(Level.FINE,"Exception getting End point info",ex);
155         }
156             throw new RuntimeException JavaDoc(ex.getMessage());
157         }
158     }
159     
160     private Codec JavaDoc getCodec(ORB JavaDoc orb)
161     {
162         if (codec == null) {
163             synchronized (this) {
164                 CodecFactory JavaDoc codecFactory = null;
165                 try {
166                     codecFactory = CodecFactoryHelper.narrow(
167                             orb.resolve_initial_references("CodecFactory"));
168                 } catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e) {
169                     System.out.println("Getting org.omg.CORBA.ORBPackage.InvalidName exception");
170                 }
171                 Encoding JavaDoc encoding = new Encoding JavaDoc((short)0, (byte)1, (byte)2);
172                 try {
173                     codec = codecFactory.create_codec(encoding);
174                 } catch (org.omg.IOP.CodecFactoryPackage.UnknownEncoding JavaDoc e) {
175                     System.out.println("Getting org.omg.IOP.CodecFactoryPackage.UnknownEncoding exception");
176                 }
177             }
178         }
179         return codec;
180     }
181
182     private int shortToInt( short value )
183     {
184         if (value < 0)
185             return value + 65536 ;
186         return value ;
187     }
188         
189 }
190
Popular Tags