KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > naming > S1ASCtxFactory


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.appserv.naming;
25
26 import javax.naming.spi.InitialContextFactory JavaDoc;
27 import javax.naming.*;
28
29 import java.util.Hashtable JavaDoc;
30
31 import org.omg.CORBA.ORB JavaDoc;
32
33 import com.sun.corba.ee.impl.orbutil.ORBConstants;
34 import com.sun.corba.ee.spi.folb.GroupInfoService;
35
36 import com.sun.jndi.cosnaming.CNCtxFactory;
37
38 import com.sun.enterprise.naming.SerialContext;
39 import com.sun.enterprise.util.ORBManager;
40
41 import java.util.logging.Logger JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import com.sun.logging.LogDomains;
44
45 /**
46  * Implements the JNDI SPI InitialContextFactory interface used to
47  * create the InitialContext objects.
48  *
49  * The static block creates the list of endpoints from the system property
50  * com.sun.appserv.iiop.endpoints.
51  * The list is randomised the very first time S1ASCtxFactory is initialized.
52  * When a call for a new InitialContext comes, the pointer in the list is moved
53  * one element ahead. Thus for loadbalancing purposes, there is always a different
54  * list created.
55  * failover is taken care of by the ORB infrastructure.
56  *
57  * @author Dhiru Pandey & Vijay Raghavan
58  * @author Sheetal Vartak
59  */

60
61 public class S1ASCtxFactory extends CNCtxFactory {
62
63     protected static Logger JavaDoc _logger = LogDomains.getLogger(
64                        LogDomains.JNDI_LOGGER);
65
66     private static RoundRobinPolicy rrPolicy = null;
67
68     private static final String JavaDoc IIOP_URL = "iiop:1.2@";
69
70     private static final String JavaDoc CORBALOC = "corbaloc:";
71
72     public static final String JavaDoc LOAD_BALANCING_PROPERTY =
73             "com.sun.appserv.iiop.loadbalancingpolicy";
74
75     public static final String JavaDoc IIOP_ENDPOINTS_PROPERTY =
76             "com.sun.appserv.iiop.endpoints";
77
78     private Hashtable JavaDoc defaultEnv;
79
80     private static GroupInfoService gis;
81
82     public static final String JavaDoc IC_BASED_WEIGHTED = "ic-based-weighted";
83
84     public static final String JavaDoc IC_BASED = "ic-based";
85
86     static {
87     String JavaDoc [] list = null;
88     String JavaDoc [] commaDelimitedValues = null;
89     String JavaDoc policy = null;
90       
91     // Get the load balancing policy
92
String JavaDoc propertyValue = System.getProperty(
93                           LOAD_BALANCING_PROPERTY);
94
95     if (propertyValue != null) {
96         commaDelimitedValues = propertyValue.split(",");
97         
98         
99         if (commaDelimitedValues != null) {
100         if (commaDelimitedValues[0].trim().equals(IC_BASED) ||
101             commaDelimitedValues[0].trim().equals(IC_BASED_WEIGHTED)) {
102             policy = commaDelimitedValues[0];
103         }
104         }
105         if(policy != null) {
106         System.setProperty(LOAD_BALANCING_PROPERTY, policy);
107         }
108     }
109     
110     propertyValue = System.getProperty(
111                        IIOP_ENDPOINTS_PROPERTY);
112     
113     if(propertyValue==null||propertyValue.length()==0){
114       //do not use the defaults here as then we are not giving a
115
//chance to the <policy>,host:port(,host:port)* type of policy
116
//specification
117
list = null;
118     } else {
119         list = propertyValue.split(",");
120     }
121             
122     //if the endpoints property was not specified, give a
123
//chance to the <policy>,host:port(,host:port)* type of policy
124
//specification
125
if(list == null ) {
126         if (commaDelimitedValues != null &&
127         commaDelimitedValues.length > 1) {
128             
129             list = new String JavaDoc[commaDelimitedValues.length-1];
130         for (int i=0; i<list.length; i++) {
131             list[i] = commaDelimitedValues[i+1];
132         }
133         }
134     }
135     
136     rrPolicy = new RoundRobinPolicy(list);
137     
138     try {
139         //bug 6311845
140
// remove the list_initial_services part once the bug gets fixed.
141
String JavaDoc[] services = (ORBManager.getORB()).list_initial_services();
142         //for (int i = 0; i < str.length; i++) {
143
for (String JavaDoc str : services) {
144         if (str.equals(ORBConstants.FOLB_CLIENT_GROUP_INFO_SERVICE)) {
145             //lookup the GroupInfoService and register with it
146
gis = (GroupInfoService) ((ORBManager.getORB()).resolve_initial_references(ORBConstants.FOLB_CLIENT_GROUP_INFO_SERVICE));
147             gis.addObserver(new GroupInfoServiceObserverImpl(gis));
148             break;
149         }
150         }
151
152         //log at fine if gis not present
153
if (gis == null) {
154         _logger.fine("GroupInfoService not available. This is PE");
155         }
156
157     } catch(Exception JavaDoc e) {
158         _logger.log(Level.WARNING, "groupinfoservice.lookup.problem", new Object JavaDoc[] {e.getMessage()});
159     }
160     }
161
162     public S1ASCtxFactory() {
163         defaultEnv = new Hashtable JavaDoc();
164     }
165
166     public S1ASCtxFactory(Hashtable JavaDoc env) {
167         defaultEnv = env;
168     }
169
170     public static RoundRobinPolicy getRRPolicy() {
171     return rrPolicy;
172     }
173
174     public Context getInitialContext(Hashtable JavaDoc env) throws NamingException {
175         
176     String JavaDoc policy = null;
177
178         if (SerialContext.getSticky() != null) {
179         Context ctx = SerialContext.getStickyContext();
180         return ctx;
181     }
182
183     if (env == null) {
184         env = defaultEnv;
185     }
186     
187         //user can specify the load balancing policy and endpoints
188
// via env. Hence the logic below.
189

190         String JavaDoc propertyValue = (String JavaDoc) env.get(LOAD_BALANCING_PROPERTY);
191     String JavaDoc[] commaDelimitedValues = null;
192     String JavaDoc host = null;
193     String JavaDoc port = null;
194
195     if (propertyValue != null) {
196         commaDelimitedValues = propertyValue.split(",");
197
198         if (commaDelimitedValues != null) {
199         if (commaDelimitedValues[0].trim().equals(IC_BASED) ||
200             commaDelimitedValues[0].trim().equals(IC_BASED_WEIGHTED)) {
201             policy = commaDelimitedValues[0];
202         }
203         }
204         if (policy != null) {
205         System.setProperty(LOAD_BALANCING_PROPERTY, policy);
206         }
207     }
208     
209     propertyValue = (String JavaDoc) env.get(IIOP_ENDPOINTS_PROPERTY);
210
211     String JavaDoc [] temp_list = (propertyValue == null ||
212               propertyValue.length() == 0)
213                       ? null
214                       : propertyValue.split(",");
215     if(temp_list == null || temp_list.length == 0) {
216         if (commaDelimitedValues != null) {
217             temp_list = new String JavaDoc[commaDelimitedValues.length - 1];
218       
219         for (int i=0; i<temp_list.length; i++) {
220             temp_list[i] = commaDelimitedValues[i+1];
221         }
222         }
223     }
224         //if endpoints property is not set by commandline or via env
225
// check for JNDI provider url
226
// else use ORB host:port value
227
if ((System.getProperty(IIOP_ENDPOINTS_PROPERTY) == null) &&
228         (temp_list == null ||
229          temp_list.length == 0)) {
230         if (env.get(ORBManager.JNDI_PROVIDER_URL_PROPERTY) != null) {
231         temp_list = rrPolicy.getEndpointForProviderURL(
232             (String JavaDoc)env.get(ORBManager.JNDI_PROVIDER_URL_PROPERTY));
233         }
234         if (temp_list == null || temp_list.length == 0) {
235         if (env.get(ORBManager.OMG_ORB_INIT_HOST_PROPERTY) != null &&
236             env.get(ORBManager.OMG_ORB_INIT_PORT_PROPERTY) != null) {
237             host = (String JavaDoc)env.get(
238             ORBManager.OMG_ORB_INIT_HOST_PROPERTY);
239             port = (String JavaDoc)env.get(
240             ORBManager.OMG_ORB_INIT_PORT_PROPERTY);
241         } else {
242             host = System.getProperty(
243             ORBManager.OMG_ORB_INIT_HOST_PROPERTY);
244             port = System.getProperty(
245             ORBManager.OMG_ORB_INIT_PORT_PROPERTY);
246         }
247         if (host != null &&
248             port != null) {
249             temp_list = rrPolicy.getAddressPortList(host, port);
250             _logger.log(Level.WARNING, "no.endpoints.selected",
251                 new Object JavaDoc[] {host, port});
252         } else {
253             _logger.log(Level.SEVERE, "no.endpoints");
254             throw new RuntimeException JavaDoc("Cannot Proceed. No Endpoints specified.");
255         }
256         }
257     }
258     
259     //add the list after randomising it to the circular list in rrPolicy
260
if (temp_list != null && temp_list.length > 0) {
261         rrPolicy.setClusterInstanceInfo(temp_list);
262     }
263     
264     //get next version of the randomized list using round robin algo
265
Object JavaDoc [] list = rrPolicy.getNextRotation();
266     
267     if (_logger.isLoggable(Level.FINE)) {
268         rrPolicy.print();
269     }
270     
271     String JavaDoc corbalocURL = "";
272     
273     //convert list into corbaloc url
274
for (int i = 0; i < list.length;i++) {
275         _logger.fine("list[i] ==> " + list[i]);
276         if (corbalocURL.equals("")) {
277             corbalocURL = IIOP_URL + ((String JavaDoc)list[i]).trim();
278         } else {
279             corbalocURL = corbalocURL + "," +
280             IIOP_URL + ((String JavaDoc)list[i]).trim();
281         }
282     }
283     _logger.fine("corbaloc url ==> " + corbalocURL);
284
285     env.put("com.sun.appserv.ee.iiop.endpointslist",
286         CORBALOC + corbalocURL);
287     env.put(ORBManager.JNDI_CORBA_ORB_PROPERTY, ORBManager.getORB());
288     
289     return new SerialContext(env);
290     }
291 }
292
Popular Tags