KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import com.sun.logging.LogDomains;
34
35 import org.omg.CORBA.Any JavaDoc;
36 import org.omg.IOP.Codec JavaDoc;
37 import org.omg.IOP.CodecFactory JavaDoc;
38 import org.omg.IOP.CodecFactoryHelper JavaDoc;
39 import org.omg.IOP.Encoding JavaDoc;
40 import org.omg.IOP.ENCODING_CDR_ENCAPS JavaDoc;
41 import org.omg.IOP.TaggedComponent JavaDoc;
42 import org.omg.PortableInterceptor.IORInfo JavaDoc;
43 import org.omg.PortableInterceptor.IORInterceptor JavaDoc;
44 import org.omg.PortableInterceptor.ObjectReferenceTemplate JavaDoc;
45 import org.omg.PortableInterceptor.ORBInitializer JavaDoc;
46 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
47 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName JavaDoc;
48
49 // BEGIN imports for cluster info.
50
import com.sun.enterprise.admin.util.JMXConnectorConfig;
51 import com.sun.enterprise.config.serverbeans.IiopListener;
52 import com.sun.enterprise.config.ConfigException;
53 import com.sun.enterprise.config.ConfigContext;
54 import com.sun.enterprise.server.ApplicationServer;
55 import com.sun.enterprise.config.serverbeans.Cluster;
56 import com.sun.enterprise.config.serverbeans.ClusterHelper;
57 import com.sun.enterprise.config.serverbeans.ServerRef;
58 import com.sun.enterprise.config.serverbeans.Server;
59 import com.sun.enterprise.config.serverbeans.Config;
60 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper;
61 import com.sun.enterprise.config.serverbeans.ServerHelper;
62 import com.sun.enterprise.config.serverbeans.IiopService;
63 import com.sun.enterprise.config.serverbeans.PropertyResolver;
64 // END imports for cluster info.
65

66 import com.sun.enterprise.iiop.AlternateIIOPAddressComponent;
67 import com.sun.enterprise.iiop.AlternateIIOPAddressComponentHelper;
68
69 import com.sun.corba.ee.spi.legacy.interceptor.ORBInitInfoExt ;
70 import com.sun.corba.ee.spi.orb.ORB;
71
72 import com.sun.corba.ee.spi.folb.ClusterInstanceInfo;
73 import com.sun.corba.ee.spi.folb.SocketInfo;
74
75 /**
76  * @author Harold Carr
77  */

78 public class FailoverIORInterceptor
79     extends org.omg.CORBA.LocalObject JavaDoc
80     implements ORBInitializer JavaDoc, IORInterceptor JavaDoc
81 {
82     private static Logger JavaDoc _logger = null;
83     static {
84        _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER);
85     }
86
87     private ORB orb ;
88
89     private static final String JavaDoc baseMsg
90     = FailoverIORInterceptor.class.getName();
91     private static final String JavaDoc ORB_LISTENTER = "orb-listener";
92
93     private List JavaDoc currentAddressList;
94     private List JavaDoc previousAddressList;
95     private List JavaDoc randomizedAddressList;
96     private List JavaDoc randomizedAndMarshaledAddressList;
97
98     //
99
// Interceptor
100
//
101

102     public String JavaDoc name() { return baseMsg; }
103     public void destroy() { }
104
105     //
106
// ORBInitializer
107
//
108

109     public FailoverIORInterceptor()
110     {
111     }
112
113     public void pre_init(ORBInitInfo JavaDoc info) { }
114
115     public void post_init(ORBInitInfo JavaDoc info)
116     {
117     ORB orb = ((ORBInitInfoExt)info).getORB() ;
118     try {
119         info.add_ior_interceptor(new FailoverIORInterceptor(orb));
120         _logger.log(Level.FINE, baseMsg + " registered.");
121     } catch (DuplicateName JavaDoc ex) {
122             _logger.log(Level.WARNING, "DuplicateName from " + baseMsg , ex);
123         /*
124         RuntimeException rte =
125         new RuntimeException("DuplicateName from " + baseMsg , ex);
126         rte.initCause(ex);
127         throw rte;
128         */

129     }
130     }
131
132     //
133
// IORInterceptor
134
//
135

136     public FailoverIORInterceptor( ORB orb )
137     {
138     this.orb = orb ;
139     }
140
141     public void establish_components(IORInfo JavaDoc iorInfo)
142     {
143     try {
144         _logger.log(Level.FINE,
145             baseMsg + ".establish_components->:");
146
147         if (ApplicationServer.getServerContext() == null) {
148         _logger.log(Level.FINE,
149                 baseMsg + ".establish_components:"
150                 + " not adding AlternateIIOPAddressComponent"
151                 + " because: no server context");
152         return;
153         }
154
155         ConfigContext configCtx =
156         ApplicationServer.getServerContext().getConfigContext();
157         String JavaDoc instanceName =
158         ApplicationServer.getServerContext().getInstanceName();
159         Cluster cluster = null;
160         try {
161         cluster =
162             ClusterHelper
163                 .getClusterForInstance(configCtx, instanceName);
164         } catch (ConfigException e) {
165         _logger.log(Level.FINE,
166                 baseMsg + ".establish_components:"
167                 + " not adding AlternateIIOPAddressComponent"
168                 + " because: "
169                 + e);
170         return;
171         }
172         ServerRef[] servers = cluster.getServerRef();
173         IiopListener[][] listeners = getIIOPEndPointsForCurrentCluster(
174                 configCtx, instanceName, servers);
175         currentAddressList = new ArrayList JavaDoc();
176         for (int i = 0; i < servers.length; i++) {
177         String JavaDoc serverName = servers[i].getRef();
178         String JavaDoc hostName =
179             getHostNameForServerInstance(configCtx, serverName);
180         if (hostName==null) {
181             hostName = listeners[i][0].getAddress();
182         }
183         for (int j = 0; j < listeners[i].length; j++) {
184             String JavaDoc id = listeners[i][j].getId();
185             String JavaDoc port =
186             getResolvedPort(configCtx,listeners[i][j], serverName);
187             // REVISIT: Make question an official API.
188
if (! id.startsWith(ORB_LISTENTER)) {
189             if (_logger.isLoggable(Level.FINE)) {
190                 _logger.log(Level.FINE,
191                     baseMsg + ".establish_components:"
192                     + " ignoring SSL ports: "
193                     + id + " " + hostName + " " + port);
194             }
195             continue;
196             }
197
198             if (_logger.isLoggable(Level.FINE)) {
199             _logger.log(Level.FINE,
200                     baseMsg + ".establish_components:"
201                     + " adding AlternateIIOPAddressComponent"
202                     + " for listener id: "
203                     + id
204                     + " address/port: "
205                     + hostName
206                     + "/"
207                     + port);
208             }
209
210             AlternateIIOPAddressComponent address =
211             new AlternateIIOPAddressComponent(
212                             hostName,
213                 Integer.decode(port).shortValue());
214
215             currentAddressList.add(address);
216         }
217         }
218
219         if (previousAddressList == null) {
220
221         //
222
// Save the previous list for subsequent comparisons.
223
//
224

225         previousAddressList = new ArrayList JavaDoc();
226         // List does not define .clone and Object.clone is protected.
227
Iterator JavaDoc i = currentAddressList.iterator();
228         while (i.hasNext()) { previousAddressList.add(i.next()); }
229
230         //
231
// Randomize the list for this ORB.
232
// Save it in unmarshaled form for logging.
233
//
234

235         java.util.Collections.shuffle(currentAddressList);
236         randomizedAddressList = currentAddressList;
237
238         if (_logger.isLoggable(Level.FINE)) {
239             _logger.log(Level.FINE, baseMsg
240                 + " first call - saving randomized alternate address list: "
241                 + randomizedAddressList);
242         }
243
244         //
245
// Save a marshaled version for subsequent calls.
246
//
247

248         randomizedAndMarshaledAddressList = new ArrayList JavaDoc();
249         i = randomizedAddressList.iterator();
250         while (i.hasNext()) {
251             AlternateIIOPAddressComponent address =
252             (AlternateIIOPAddressComponent) i.next();
253             Any JavaDoc any = orb.create_any();
254             AlternateIIOPAddressComponentHelper.insert(any, address);
255             byte[] data = getCodec(orb).encode_value(any);
256             TaggedComponent JavaDoc tc =
257             new TaggedComponent JavaDoc(org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value,
258                         //AlternateIIOPAddressComponent.TAG_ALTERNATE_IIOP_ADDRESS_ID,
259
data);
260             randomizedAndMarshaledAddressList.add(tc);
261         }
262         } else {
263         if (! currentAddressList.equals(previousAddressList)) {
264             throw new RuntimeException JavaDoc(
265                 "Current address list: "
266             + currentAddressList
267             + " different from previous list: "
268             + previousAddressList);
269         }
270         }
271
272         Iterator JavaDoc i = randomizedAndMarshaledAddressList.iterator();
273         while (i.hasNext()) {
274         iorInfo.add_ior_component((TaggedComponent JavaDoc) i.next());
275         }
276
277         if (_logger.isLoggable(Level.FINE)) {
278         _logger.log(Level.FINE, baseMsg
279                 + " adding randomized alternate addresses: "
280                 + randomizedAddressList);
281         }
282     } catch (Throwable JavaDoc e) {
283             _logger.log(Level.WARNING,
284             "Problem in " + baseMsg + ".establish_components",
285             e);
286         /*
287         RuntimeException rte =
288         new RuntimeException("Problem in " + baseMsg + ".establish_components");
289         rte.initCause(e);
290         throw rte;
291         */

292     } finally {
293         _logger.log(Level.FINE, baseMsg + ".establish_components<-:");
294     }
295     }
296
297     public void components_established( IORInfo JavaDoc iorInfo )
298     {
299     }
300
301     public void adapter_manager_state_changed( int managerId, short state )
302     {
303     }
304
305     public void adapter_state_changed( ObjectReferenceTemplate JavaDoc[] templates,
306                        short state )
307     {
308     }
309
310     ////////////////////////////////////////////////////
311
//
312
// Implementation.
313
//
314

315     public static List JavaDoc<ClusterInstanceInfo> getClusterInstanceInfo()
316     {
317     List JavaDoc<ClusterInstanceInfo> result =
318         new LinkedList JavaDoc<ClusterInstanceInfo>();
319
320     try {
321         _logger.log(Level.FINE,
322             baseMsg + ".getClusterForInstanceInfo->:");
323
324         if (ApplicationServer.getServerContext() == null) {
325         _logger.log(Level.FINE,
326                 baseMsg + ".getClusterForInstanceInfo:"
327                 + " not adding addresses "
328                 + " because: no server context");
329         return null;
330         }
331
332         ConfigContext configCtx =
333         ApplicationServer.getServerContext().getConfigContext();
334         String JavaDoc instanceName =
335         ApplicationServer.getServerContext().getInstanceName();
336         Cluster cluster = null;
337         try {
338         cluster =
339             ClusterHelper
340                 .getClusterForInstance(configCtx, instanceName);
341         } catch (ConfigException e) {
342         _logger.log(Level.FINE,
343                 baseMsg + ".getClusterForInstance: "
344                 + e);
345         return null;
346         }
347         ServerRef[] servers = cluster.getServerRef();
348         IiopListener[][] listeners = getIIOPEndPointsForCurrentCluster(
349                 configCtx, instanceName, servers);
350         for (int i = 0; i < servers.length; i++) {
351         String JavaDoc serverName = servers[i].getRef();
352         String JavaDoc hostName =
353             getHostNameForServerInstance(configCtx, serverName);
354         if (hostName==null) {
355             hostName = listeners[i][0].getAddress();
356         }
357         List JavaDoc<SocketInfo> listOfSocketInfo =
358             new LinkedList JavaDoc<SocketInfo>();
359         for (int j = 0; j < listeners[i].length; j++) {
360             String JavaDoc id = listeners[i][j].getId();
361             String JavaDoc port =
362             getResolvedPort(configCtx,listeners[i][j], serverName);
363             if (_logger.isLoggable(Level.FINE)) {
364             _logger.log(Level.FINE,
365                     baseMsg + ".getClusterForInstance:"
366                     + " adding address for "
367                     + serverName + "/" + id
368                     + "/" + hostName + "/" + port);
369             }
370             listOfSocketInfo.add(
371                 new SocketInfo(id, hostName, Integer.valueOf(port)));
372         }
373         // REVISIT - make orbutil utility
374
// and use in IiopFolbGmsClient and here.
375
SocketInfo[] arrayOfSocketInfo =
376             new SocketInfo[listOfSocketInfo.size()];
377         int x = 0;
378         for (SocketInfo si : listOfSocketInfo) {
379             arrayOfSocketInfo[x++] = si;
380         }
381         // REVISIT - default 100 weight
382
// Not used by JNLP but should put in the actual weight.
383
ClusterInstanceInfo clusterInstanceInfo =
384             new ClusterInstanceInfo(serverName, 100, arrayOfSocketInfo);
385         result.add(clusterInstanceInfo);
386         if (_logger.isLoggable(Level.FINE)) {
387             _logger.log(Level.FINE,
388                 baseMsg + ".getClusterForInstance: " +
389                 ASORBUtilities.toString(clusterInstanceInfo));
390         }
391         }
392         return result;
393     } catch (ConfigException e) {
394         return null;
395     } finally {
396         _logger.log(Level.FINE,
397             baseMsg + ".getClusterForInstanceInfo<-: " + result);
398     }
399     }
400
401     /**
402       * The following returns the IIOP listener(s) for the all
403       * server belonging to the current cluster.
404       *
405       * @author satish.viswanatham@sun.com
406       *
407       * @param configCtx Current server's config context
408       * @param instanceName instance name of the current server
409       *
410       * @return IiopListener[i][j] Two dimension array of iiop listener info
411       * i = index of server
412       * j = index of iiop listner (j <= 3)
413       * @throws ConfigException In any configuration error cases,
414       * ConfigException is thrown
415       */

416     private static IiopListener[][] getIIOPEndPointsForCurrentCluster(
417         ConfigContext configCtx, String JavaDoc instanceName, ServerRef[] servers)
418     throws ConfigException
419     {
420     // For each server instance in a cluster, there are 3 iiop listeners:
421
// one for non ssl, one for ssl and third for ssl mutual auth
422

423     IiopListener[][] listeners = new IiopListener[servers.length][3];
424     for (int i = 0; i < servers.length; i++) {
425         Server server =
426         ServerHelper.getServerByName(configCtx, servers[i].getRef());
427         String JavaDoc configRef = server.getConfigRef();
428         Config config =
429         ConfigAPIHelper.getConfigByName(configCtx, configRef);
430         IiopService iiopService = config.getIiopService();
431         listeners[i] = iiopService.getIiopListener();
432     }
433     return listeners;
434     }
435
436     /**
437      * Returns ip address from node agent refered from instance
438      * or null if Exception
439      *
440      * @author sridatta.viswanath@sun.com
441      */

442     private static String JavaDoc getHostNameForServerInstance(ConfigContext ctx,
443                                String JavaDoc serverName)
444     {
445         try {
446             JMXConnectorConfig info =
447         ServerHelper.getJMXConnectorInfo(ctx, serverName);
448             _logger.log(Level.FINE,
449             baseMsg + ".getHostNameForServerInstance: " +
450             "found info: " + info.toString());
451         String JavaDoc host = info.getHost();
452             _logger.log(Level.FINE,
453             baseMsg + ".getHostNameForServerInstance: " +
454             "found host: " + host);
455             return host;
456         } catch (Throwable JavaDoc e){
457             _logger.log(Level.FINE,
458             baseMsg + ".getHostNameForServerInstance: " +
459             "gotException: " + e + " " + e.getMessage() +
460             "; returning null");
461             return null;
462         }
463     }
464
465     /**
466      * Gets the correct resolved value for the specific instance
467      * Without this routine, config context resolves the value
468      * to the current running instance
469      *
470      * @author sridatta.viswanath@sun.com
471      */

472     private static String JavaDoc getResolvedPort(ConfigContext ctx,
473                       IiopListener l,
474                       String JavaDoc server)
475     throws ConfigException
476     {
477     String JavaDoc rawPort = l.getRawAttributeValue("port");
478     PropertyResolver pr = new PropertyResolver(ctx, server);
479     return pr.resolve(rawPort);
480     }
481
482     static Codec JavaDoc getCodec(ORB orb)
483     {
484     Codec JavaDoc result = null;
485     try {
486         CodecFactory JavaDoc codecFactory =
487         CodecFactoryHelper.narrow(
488                     orb.resolve_initial_references("CodecFactory"));
489         result = codecFactory.create_codec(
490                 new Encoding JavaDoc((short)ENCODING_CDR_ENCAPS.value,
491                  (byte)1, (byte)2));
492     } catch (Throwable JavaDoc e) {
493             _logger.log(Level.WARNING,
494             "Problem in " + baseMsg + ".getCodec",
495             e);
496     }
497     return result;
498     }
499 }
500
501 // End of file.
502
Popular Tags