KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintStream JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
32
33 import com.sun.enterprise.config.ConfigException;
34 import com.sun.enterprise.config.ConfigContext;
35 import com.sun.enterprise.server.ApplicationServer;
36 import com.sun.enterprise.server.ServerContext;
37 import com.sun.enterprise.config.serverbeans.Cluster;
38 import com.sun.enterprise.config.serverbeans.ClusterHelper;
39 import com.sun.logging.LogDomains;
40
41 import com.sun.corba.ee.spi.folb.ClusterInstanceInfo;
42 import com.sun.corba.ee.spi.folb.GroupInfoService;
43 import com.sun.corba.ee.spi.folb.GroupInfoServiceObserver;
44 import com.sun.corba.ee.spi.folb.SocketInfo;
45 import com.sun.corba.ee.spi.orb.ORB;
46
47 import com.sun.corba.ee.spi.orbutil.misc.ORBClassLoader;
48 import com.sun.corba.ee.impl.orbutil.ORBConstants;
49
50 /**
51  * @author Harold Carr
52  */

53 public class ASORBUtilities
54 {
55     private static Logger JavaDoc _logger = null;
56     static {
57        _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER);
58     }
59
60     private static final String JavaDoc GMS_CLASS =
61     "com.sun.enterprise.ee.ejb.iiop.IiopFolbGmsClient";
62
63     private static boolean gisInitialized = false;
64
65     public static synchronized void initGIS(org.omg.CORBA.ORB JavaDoc orb)
66     {
67     GroupInfoService gis = null;
68     try {
69             if(_logger.isLoggable(Level.FINE)) {
70         _logger.log(Level.FINE, "initGIS->: " + gisInitialized);
71         }
72         if (gisInitialized) {
73         return;
74         }
75         if (isGMSAvailableAndClusterHeartbeatEnabled()) {
76         gis = initGISUsesGMS();
77         } else {
78         gis = initGISUsesAdmin();
79         }
80         if (gis == null) {
81         return;
82         }
83         try {
84         ((ORB)orb).register_initial_reference(
85                     ORBConstants.FOLB_SERVER_GROUP_INFO_SERVICE,
86             (org.omg.CORBA.Object JavaDoc) gis);
87         if(_logger.isLoggable(Level.FINE)) {
88             _logger.log(Level.FINE,
89                 ".initGIS: naming registration complete: "
90                 + gis);
91         }
92         gisInitialized = true;
93
94         if (_logger.isLoggable(Level.FINE)) {
95             gis = (GroupInfoService)
96             ((ORB)orb).resolve_initial_references(
97                             ORBConstants.FOLB_SERVER_GROUP_INFO_SERVICE);
98             List JavaDoc<ClusterInstanceInfo> lcii =
99             gis.getClusterInstanceInfo(null);
100             _logger.log(Level.FINE,
101                 "Results from getClusterInstanceInfo:");
102             if (lcii != null) {
103             for (ClusterInstanceInfo cii : lcii) {
104                 _logger.log(Level.INFO, toString(cii));
105             }
106             }
107         }
108
109         } catch (InvalidName JavaDoc e) {
110         if(_logger.isLoggable(Level.FINE)) {
111             _logger.log(Level.SEVERE,
112                 ".initGIS: registering GIS failed: " + e);
113         }
114         }
115     } finally {
116             if(_logger.isLoggable(Level.FINE)) {
117         _logger.log(Level.FINE,
118                 "initGIS<-: " + gisInitialized + " " + gis);
119         }
120     }
121     }
122
123     private static GroupInfoService initGISUsesAdmin()
124     {
125     GroupInfoService result = null;
126     try {
127             if(_logger.isLoggable(Level.FINE)) {
128         _logger.log(Level.FINE, "initGISUsesAdmin->:");
129         }
130         result = new GroupInfoServiceImplForJNLP();
131         return result;
132     } finally {
133             if(_logger.isLoggable(Level.FINE)) {
134         _logger.log(Level.FINE, "initGISUsesAdmin<-: " + result);
135         }
136     }
137     }
138
139     private static GroupInfoService initGISUsesGMS()
140     {
141     GroupInfoService result = null;
142     try {
143             if(_logger.isLoggable(Level.FINE)) {
144         _logger.log(Level.FINE, "initGISUsesGMS->:");
145         }
146
147         Class JavaDoc clazz = loadClass(GMS_CLASS);
148
149         if (clazz == null) {
150         _logger.log(Level.SEVERE,
151                 ".initGISUsesGMS: GMS initialization failure: class not found: " + GMS_CLASS);
152         }
153
154         result = (GroupInfoService) newInstance(clazz);
155
156         if (result == null) {
157         _logger.log(Level.SEVERE, ".initGISUsesGMS: GMS initialization failure: cannot instantiate: " + GMS_CLASS);
158         }
159
160         return result;
161
162     } finally {
163             if(_logger.isLoggable(Level.FINE)) {
164         _logger.log(Level.FINE, "initGISUsesGMS<-: " + result);
165         }
166     }
167     }
168
169     public static boolean isGMSAvailableAndClusterHeartbeatEnabled()
170     {
171     Cluster cluster = null;
172     boolean result =
173         (loadClass("com.sun.enterprise.ee.cms.core.GMSFactory") != null)
174         && (cluster = getCluster()) != null
175         && isClusterHeartbeatEnabled(cluster);
176     return result;
177     }
178
179     public static Class JavaDoc loadClass(String JavaDoc classname)
180     {
181     Class JavaDoc result = null;
182     try {
183             if(_logger.isLoggable(Level.FINE)) {
184         _logger.log(Level.FINE, ".loadClass->: " + classname);
185         }
186         result = ORBClassLoader.loadClass(classname);
187     } catch (ClassNotFoundException JavaDoc e) {
188             if(_logger.isLoggable(Level.FINE)) {
189         _logger.log(Level.FINE, ".loadClass: " + classname + " " + e);
190         }
191     } finally {
192             if(_logger.isLoggable(Level.FINE)) {
193         _logger.log(Level.FINE,
194                 ".loadClass<-: " + classname + " " + result);
195         }
196     }
197     return result;
198     }
199
200     public static Object JavaDoc newInstance(Class JavaDoc clazz)
201     {
202     Object JavaDoc result = null;
203     try {
204             if(_logger.isLoggable(Level.FINE)) {
205         _logger.log(Level.FINE, ".newInstance->: " + clazz);
206         }
207         result = clazz.newInstance();
208     } catch (InstantiationException JavaDoc e) {
209         _logger.log(Level.WARNING, ".newInstance: " + clazz + " " + e);
210     } catch (IllegalAccessException JavaDoc e) {
211         _logger.log(Level.WARNING, ".newInstance: " + clazz + " " + e);
212     } finally {
213             if(_logger.isLoggable(Level.FINE)) {
214         _logger.log(Level.FINE, ".newInstance<-: " + clazz + " " + result);
215         }
216     }
217     return result;
218     }
219
220     public static Cluster getCluster()
221     {
222     ConfigContext configCtx = null;
223     String JavaDoc instanceName = null;
224     Cluster result = null;
225     try {
226             if(_logger.isLoggable(Level.FINE)) {
227         _logger.log(Level.FINE, ".getCluster->:");
228         }
229         ServerContext serverContext = ApplicationServer.getServerContext();
230         if (serverContext == null) {
231         if(_logger.isLoggable(Level.FINE)) {
232             _logger.log(Level.FINE, ".getCluster: No ServerContext");
233         }
234         return null;
235         }
236         configCtx = serverContext.getConfigContext();
237         instanceName = serverContext.getInstanceName();
238         result =
239             ClusterHelper.getClusterForInstance(configCtx, instanceName);
240     } catch (ConfigException e) {
241             if(_logger.isLoggable(Level.FINE)) {
242         _logger.log(Level.FINE,
243                 ".getCluster: ConfigContext: " + configCtx.toString()
244                 + " ; instanceName: " + instanceName
245                 + " ; exception: " + e);
246         }
247     } finally {
248             if(_logger.isLoggable(Level.FINE)) {
249         _logger.log(Level.FINE, ".getCluster<-: " + result);
250         }
251     }
252     return result;
253     }
254     
255     public static boolean isClusterHeartbeatEnabled(Cluster cluster)
256     {
257     boolean result = false;
258     try {
259             if(_logger.isLoggable(Level.FINE)) {
260         _logger.log(Level.FINE,
261                 ".isClusterHeartbeatEnabled->: " + cluster);
262         }
263         result = cluster.isHeartbeatEnabled();
264     } finally {
265             if(_logger.isLoggable(Level.FINE)) {
266         _logger.log(Level.FINE,
267                 ".isClusterHeartbeatEnabled<-: " + cluster
268                 + " " + result);
269         }
270     }
271     return result;
272     }
273
274     public static boolean member(ClusterInstanceInfo item,
275                  List JavaDoc<ClusterInstanceInfo> list)
276     {
277     for (ClusterInstanceInfo element : list) {
278         if (equals(item, element)) {
279         return true;
280         }
281     }
282     return false;
283     }
284
285     public static boolean equals(ClusterInstanceInfo c1,
286                  ClusterInstanceInfo c2)
287     {
288     if (c1 == c2) {
289         return true;
290     }
291     if (c1.weight != c2.weight) {
292         return false;
293     }
294     if (c1.endpoints.length != c2.endpoints.length) {
295         return false;
296     }
297     if (! c1.name.equals(c2.name)) {
298         return false;
299     }
300     for (int i = 0; i < c1.endpoints.length; ++i) {
301         if (c1.endpoints[i].port != c2.endpoints[i].port) {
302         return false;
303         }
304         if (! c1.endpoints[i].type.equals(c2.endpoints[i].type)) {
305         return false;
306         }
307         if (! c1.endpoints[i].host.equals(c2.endpoints[i].host)) {
308         return false;
309         }
310     }
311     return true;
312     }
313
314     public static String JavaDoc toString(ClusterInstanceInfo cii)
315     {
316     return
317         "[ClusterInstanceInfo "
318         + cii.name
319         + " "
320         + cii.weight
321         + " "
322         + toString(cii.endpoints)
323         + "]";
324
325     }
326
327     public static String JavaDoc toString(SocketInfo[] socketInfo)
328     {
329     String JavaDoc result = "";
330     for (int i = 0; i < socketInfo.length; ++i) {
331         result += toString(socketInfo[i]) + " ";
332     }
333     return result;
334     }
335
336     public static String JavaDoc toString(SocketInfo socketInfo)
337     {
338     return
339         "[SocketInfo "
340         + socketInfo.type
341         + " "
342         + socketInfo.host
343         + " "
344         + socketInfo.port
345         + "]";
346     }
347
348     public static void forceStackTrace(String JavaDoc msg)
349     {
350     forceStackTrace(msg, System.out);
351     }
352
353     public static void forceStackTrace(String JavaDoc msg, PrintStream JavaDoc out)
354     {
355     try {
356         _logger.log(Level.INFO, msg + "->:");
357         throw new Exception JavaDoc("FORCED STACKTRACE");
358     } catch (Exception JavaDoc e) {
359         e.printStackTrace(out);
360     } finally {
361         _logger.log(Level.INFO, msg + "<-:");
362     }
363     }
364 }
365
366 class GroupInfoServiceImplForJNLP
367     extends org.omg.CORBA.LocalObject JavaDoc
368     implements GroupInfoService
369 {
370     public boolean addObserver(GroupInfoServiceObserver x)
371     {
372     throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
373     }
374     public void notifyObservers()
375     {
376     throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
377     }
378     public List JavaDoc<ClusterInstanceInfo> getClusterInstanceInfo(
379         String JavaDoc[] adapterName)
380     {
381     if (adapterName != null) {
382         throw new RuntimeException JavaDoc("Argument should be null");
383     }
384     return FailoverIORInterceptor.getClusterInstanceInfo();
385     }
386     public boolean shouldAddAddressesToNonReferenceFactory(String JavaDoc[] x)
387     {
388     throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
389     }
390     public boolean shouldAddMembershipLabel (String JavaDoc[] adapterName)
391     {
392     throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
393     }
394 }
395
396 // End of file.
397
Popular Tags