KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.omg.CORBA.Any JavaDoc;
27 import org.omg.CORBA.ORB JavaDoc;
28 import org.omg.IOP.Codec JavaDoc;
29 import org.omg.IOP.Encoding JavaDoc;
30 import org.omg.IOP.TaggedComponent JavaDoc;
31 import org.omg.PortableInterceptor.IORInfo JavaDoc;
32
33 import com.sun.corba.ee.spi.legacy.connection.ORBSocketFactory;
34 import com.sun.corba.ee.spi.legacy.interceptor.IORInfoExt;
35 import com.sun.corba.ee.impl.interceptors.IORInfoImpl;
36
37 import java.util.logging.*;
38 import com.sun.logging.*;
39
40 import java.net.InetAddress JavaDoc;
41 import java.net.SocketException JavaDoc;
42 import java.net.NetworkInterface JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Enumeration JavaDoc;
45
46 public class IORAddrAnyInterceptor extends org.omg.CORBA.LocalObject JavaDoc
47                     implements org.omg.PortableInterceptor.IORInterceptor JavaDoc{
48     
49     public static final String JavaDoc baseMsg = IORAddrAnyInterceptor.class.getName();
50     private static Logger _logger=null;
51     static {
52        _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER);
53     }
54     
55     private Codec JavaDoc codec;
56     
57     
58     /** Creates a new instance of IORAddrAnyInterceptor */
59     public IORAddrAnyInterceptor(Codec JavaDoc c) {
60         codec = c;
61     }
62     
63     /**
64      * Get all the InetAddresses on the machine
65      */

66     private static ArrayList JavaDoc getAllInetAddresses() {
67         ArrayList JavaDoc result = new ArrayList JavaDoc();
68         try {
69             Enumeration JavaDoc e = NetworkInterface.getNetworkInterfaces();
70             while (e.hasMoreElements()) {
71                 NetworkInterface JavaDoc ni = (NetworkInterface JavaDoc)e.nextElement();
72                 Enumeration JavaDoc ee = ni.getInetAddresses();
73                 while (ee.hasMoreElements()) {
74                     InetAddress JavaDoc addr = (InetAddress JavaDoc)ee.nextElement();
75                     result.add(addr);
76                 }
77             }
78         } catch (SocketException JavaDoc se) {
79             _logger.log(Level.WARNING,"Exception getting all Network Interfaces",se);
80             return result;
81         }
82         return result;
83     }
84     
85     /**
86      * Provides an opportunity to destroy this interceptor.
87      * The destroy method is called during <code>ORB.destroy</code>. When an
88      * application calls <code>ORB.destroy</code>, the ORB:
89      * <ol>
90      * <li>waits for all requests in progress to complete</li>
91      * <li>calls the <code>Interceptor.destroy</code> operation for each
92      * interceptor</li>
93      * <li>completes destruction of the ORB</li>
94      * </ol>
95      * Method invocations from within <code>Interceptor.destroy</code> on
96      * object references for objects implemented on the ORB being destroyed
97      * result in undefined behavior. However, method invocations on objects
98      * implemented on an ORB other than the one being destroyed are
99      * permitted. (This means that the ORB being destroyed is still capable
100      * of acting as a client, but not as a server.)
101      */

102     public void destroy() {
103     }
104     
105     /**
106      * A server side ORB calls the <code>establish_components</code>
107      * operation on all registered <code>IORInterceptor</code> instances
108      * when it is assembling the list of components that will be included
109      * in the profile or profiles of an object reference. This operation
110      * is not necessarily called for each individual object reference.
111      * For example, the POA specifies policies at POA granularity and
112      * therefore, this operation might be called once per POA rather than
113      * once per object. In any case, <code>establish_components</code> is
114      * guaranteed to be called at least once for each distinct set of
115      * server policies.
116      * <p>
117      * An implementation of <code>establish_components</code> must not
118      * throw exceptions. If it does, the ORB shall ignore the exception
119      * and proceed to call the next IOR Interceptor's
120      * <code>establish_components</code> operation.
121      *
122      * @param info The <code>IORInfo</code> instance used by the ORB
123      * service to query applicable policies and add components to be
124      * included in the generated IORs.
125      */

126     public void establish_components(org.omg.PortableInterceptor.IORInfo JavaDoc iorInfo) {
127         try {
128             IORInfoExt iorInfoExt = (IORInfoExt) iorInfo;
129             int port = iorInfoExt.getServerPort(ORBSocketFactory.IIOP_CLEAR_TEXT);
130
131             ArrayList JavaDoc allInetAddress = getAllInetAddresses();
132             addAddressComponents(iorInfo, allInetAddress, port);
133             com.sun.corba.ee.internal.corba.ORB orb =
134                     (com.sun.corba.ee.internal.corba.ORB)((IORInfoImpl)iorInfo).getORB();
135             Object JavaDoc[] userPorts = orb.getUserSpecifiedListenPorts().toArray();
136             if (userPorts.length > 0) {
137                 for (int i = 0; i < userPorts.length; i++) {
138                     com.sun.corba.ee.internal.corba.ORB.UserSpecifiedListenPort p =
139                         ((com.sun.corba.ee.internal.corba.ORB.UserSpecifiedListenPort)userPorts[i]);
140             // if (p.getType().equals(ORBSocketFactory.IIOP_CLEAR_TEXT)) {
141
addAddressComponents(iorInfo, allInetAddress, p.getPort());
142             // }
143
}
144             }
145         } catch (Exception JavaDoc e) {
146             _logger.log(Level.WARNING,"Exception in " + baseMsg, e);
147         }
148     }
149     
150     /**
151      * Returns the name of the interceptor.
152      * <p>
153      * Each Interceptor may have a name that may be used administratively
154      * to order the lists of Interceptors. Only one Interceptor of a given
155      * name can be registered with the ORB for each Interceptor type. An
156      * Interceptor may be anonymous, i.e., have an empty string as the name
157      * attribute. Any number of anonymous Interceptors may be registered with
158      * the ORB.
159      *
160      * @return the name of the interceptor.
161      */

162     public String JavaDoc name() {
163         return baseMsg;
164     }
165
166     protected short intToShort( int value )
167     {
168     if (value > 32767)
169         return (short)(value - 65536) ;
170     return (short)value ;
171     }
172     
173     private void addAddressComponents(org.omg.PortableInterceptor.IORInfo JavaDoc iorInfo,
174                     ArrayList JavaDoc allInetAddress, int port) {
175         try {
176             for (int i = 0; i < allInetAddress.size(); i++) {
177                 String JavaDoc address = ((InetAddress JavaDoc)allInetAddress.get(i)).getHostAddress();
178                 AlternateIIOPAddressComponent iiopAddress =
179                     new AlternateIIOPAddressComponent(address, intToShort(port));
180                 Any JavaDoc any = ORB.init().create_any();
181                 AlternateIIOPAddressComponentHelper.insert(any, iiopAddress);
182                 byte[] data = codec.encode_value(any);
183                 TaggedComponent JavaDoc taggedComponent =
184                     new TaggedComponent JavaDoc( org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value,
185                     //AlternateIIOPAddressComponent.TAG_ALTERNATE_IIOP_ADDRESS_ID,
186
data);
187                 iorInfo.add_ior_component(taggedComponent);
188             }
189         } catch (Exception JavaDoc e) {
190             _logger.log(Level.WARNING,"Exception in " + baseMsg, e);
191         }
192     }
193     
194 }
195
Popular Tags