KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > system > MQAddressList


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.connectors.system;
25
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 import com.sun.enterprise.util.SystemPropertyConstants;
33 import com.sun.enterprise.server.ServerContext;
34 import com.sun.enterprise.server.ApplicationServer;
35 import com.sun.logging.LogDomains;
36 import com.sun.enterprise.config.ConfigContext;
37 import com.sun.enterprise.config.ConfigException;
38 import com.sun.enterprise.config.serverbeans.*;
39 import com.sun.enterprise.connectors.ConnectorRuntimeException;
40 import com.sun.enterprise.connectors.util.JmsRaUtil;
41
42 /**
43  * Defines an MQ addressList.
44  *
45  * @author Binod P.G
46  */

47 public class MQAddressList {
48
49     static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
50     private static String JavaDoc myName =
51                System.getProperty(SystemPropertyConstants.SERVER_NAME);
52
53     private List JavaDoc<MQUrl> urlList = new ArrayList JavaDoc<MQUrl>();
54
55     private JmsService jmsService = null;
56     private AppserverClusterViewFromCacheRepository rep = null;
57     private static String JavaDoc nodeAgentHost = null;
58     private String JavaDoc targetName = null;
59
60     /**
61      * Create an empty address list
62      */

63     public MQAddressList() throws ConfigException {
64         this(null);
65     }
66
67     /**
68      * Use the provided <code>JmsService</code> to create an addresslist
69      */

70     public MQAddressList(JmsService service) throws ConfigException {
71         //use the server instance this is being run in as the target
72
this(service, getServerName());
73     }
74     
75     /**
76      * Creates an instance from jmsService and resolves
77      * values using the provided target name
78      * @param targetName Represents the target for which the addresslist
79      * needs to be created
80      * @param service <code>JmsService</code> instance.
81      */

82     public MQAddressList(JmsService service, String JavaDoc targetName) throws ConfigException {
83         logFine(" init" + service + "target " + targetName);
84         this.jmsService = service;
85         this.targetName = targetName;
86         ServerContext context = ApplicationServer.getServerContext();
87         Server server = context.getConfigBean();
88         String JavaDoc domainurl = context.getServerConfigURL();
89         rep = new AppserverClusterViewFromCacheRepository(domainurl);
90         try {
91             nodeAgentHost = rep.getNodeAgentHostName(server);
92             logFine("na host" + nodeAgentHost);
93         } catch (Exception JavaDoc e) {
94             logger.log(Level.FINE,"Exception while attempting to get nodeagentHost", e.getMessage());
95             logger.log(Level.FINER, e.getMessage(), e);
96         }
97     }
98
99     /**
100      * Sets up the addresslist.
101      */

102     public void setup() throws ConfigException {
103         try {
104             if (isClustered() && (!this.jmsService.getType().equals(ActiveJmsResourceAdapter.REMOTE)) ) {
105                 //setup for LOCAL/EMBEDDED clusters.
106
logFine("setting up for cluster " + this.targetName);
107                 setupForCluster();
108             } else {
109                 logFine("setting up for SI/DAS " + this.targetName);
110                 if (isAConfig(targetName) || isDAS(targetName)) {
111                     logFine("performing default setup for DAS/remote clusters");
112                     defaultSetup();
113                 } else {
114                     logFine("configuring for Standalone server instance");
115                     //resolve and add.
116
setupForStandaloneServerInstance();
117                 }
118             }
119         } catch (ConnectorRuntimeException ce) {
120             throw new ConfigException(ce);
121         }
122     }
123
124     private boolean isDAS(String JavaDoc targetName) throws ConfigException {
125         if (isAConfig(targetName)) {
126             return false;
127         }
128         return ServerHelper.isDAS(getAdminConfigContext(), targetName);
129     }
130     
131     private boolean isAConfig(String JavaDoc targetName) throws ConfigException {
132         return ServerHelper.isAConfig(getAdminConfigContext(), targetName);
133     }
134     
135
136     /**
137      * Gets the admin config context associated with this server instance
138      * Usage Notice: Use this only for operations that are performed in DAS
139      * and requires the admin config context
140      */

141     private ConfigContext getAdminConfigContext() {
142         return com.sun.enterprise.admin.server.core.AdminService.
143                    getAdminService().getAdminContext().getAdminConfigContext();
144     }
145
146     /**
147      * Setup addresslist for Standalone server instance in EE
148      */

149     private void setupForStandaloneServerInstance() throws ConfigException {
150         if (jmsService.getType().equals(ActiveJmsResourceAdapter.REMOTE)) {
151             logFine("REMOTE Standalone server instance and hence use default setup");
152             defaultSetup();
153         } else {
154             //For LOCAL or EMBEDDED standalone server instances, we need to resolve
155
//the JMSHost
156
logFine("LOCAL/EMBEDDED Standalone server instance");
157             JmsHost host = getResolvedJmsHostForStandaloneServerInstance(this.targetName);
158             MQUrl url = createUrl(host);
159             urlList.add(url);
160         }
161     }
162
163     /**
164      * Default setup concatanates all JMSHosts in a JMSService to create the address list
165      */

166     private void defaultSetup() throws ConfigException {
167         logFine("performing defaultsetup");
168         JmsHost[] hosts = jmsService.getJmsHost();
169         for (int i=0; i < hosts.length; i++) {
170             MQUrl url = createUrl(hosts[i]);
171             urlList.add(url);
172         }
173     }
174
175     /**
176      * Setup the address list after calculating the JMS hosts
177      * belonging to the local appserver cluster members.
178      * For LOCAL/EMBEDDED clusters the MQ broker corresponding
179      * to "this" server instance needs to be placed ahead
180      * of the other brokers of the other siblings in the AS
181      * cluter to enable sticky connection balancing by MQ.
182      */

183     private void setupForCluster() throws ConfigException {
184         java.util.Map JavaDoc<String JavaDoc,JmsHost> hostMap =
185             rep.getResolvedLocalJmsHostsInMyCluster(true);
186
187         //First add my jms host.
188
JmsHost jmsHost = hostMap.get(myName);
189         MQUrl myUrl = createUrl(jmsHost, nodeAgentHost);
190         urlList.add(myUrl);
191         hostMap.remove(myName);
192         
193         // Add all buddies to URL.
194
for (JmsHost host : hostMap.values() ) {
195             MQUrl url = createUrl(host, nodeAgentHost);
196             urlList.add(url);
197         }
198     }
199     
200
201     /**
202      * Creates a String representation of address list from
203      * array list. In short, it is a comma separated list.
204      * Actual syntax of an MQ url is inside MQUrl class.
205      *
206      * @returns AddressList String
207      * @see MQUrl
208      */

209     public String JavaDoc toString() {
210         String JavaDoc s = "";
211     
212         Iterator JavaDoc it = urlList.iterator();
213     if (it.hasNext()) {
214             s = it.next().toString();
215     }
216     
217         while (it.hasNext()) {
218             s = s + "," + it.next().toString();
219         }
220
221         logFine("toString returns :: " + s);
222         return s;
223     }
224
225     /**
226      * Creates an instance of MQUrl from JmsHost element in
227      * the dtd and add it to the addresslist.
228      *
229      * @param host An instance of <code>JmsHost</code> object.
230      */

231     public void addMQUrl(JmsHost host) {
232         MQUrl url = createUrl(host);
233         urlList.add(url);
234     }
235
236     /**
237      * Deletes the url represented by the JmsHost from the AddressList.
238      *
239      * @param host An instance of <code>JmsHost</code> object.
240      */

241     public void removeMQUrl(JmsHost host) {
242         MQUrl url = createUrl(host);
243         urlList.remove(url);
244     }
245
246     /**
247      * Updates the information about the <code>JmsHost</code>
248      * in the address list.
249      *
250      * @param host An instance of <code>JmsHost</code> object.
251      */

252     public void updateMQUrl(JmsHost host) {
253         MQUrl url = createUrl(host);
254         urlList.remove(url);
255         urlList.add(url);
256     }
257
258     private MQUrl createUrl(JmsHost host) {
259         return createUrl(host, this.jmsService);
260     }
261     
262     private MQUrl createUrl(JmsHost host, String JavaDoc overridedHostName) {
263         return createUrl(host, this.jmsService, overridedHostName);
264     }
265     
266     public static MQUrl createUrl(JmsHost host, JmsService js) {
267         return createUrl(host, js, null);
268     }
269     
270     public static MQUrl createUrl(JmsHost host, JmsService js, String JavaDoc overridedHostName) {
271         try {
272         String JavaDoc name = host.getName();
273         String JavaDoc hostName = host.getHost();
274         // For LOCAL/EMBEDDED Clustered instances and
275
// standalone server instances, use
276
// their nodeagent's hostname as the jms host name.
277
ServerContext serverContext = ApplicationServer.getServerContext();
278         Server server = serverContext.getConfigBean();
279         if (overridedHostName != null && !overridedHostName.trim().equals("")) {
280            hostName = overridedHostName;
281         }
282
283         String JavaDoc port = host.getPort();
284         MQUrl url = new MQUrl(name);
285         url.setHost(hostName);
286         url.setPort(port);
287         if (js != null) {
288             String JavaDoc scheme = js.getMqScheme();
289             if (scheme != null && !scheme.trim().equals("")) {
290                 url.setScheme(scheme);
291             }
292
293             String JavaDoc service = js.getMqService();
294             if (service != null && !service.trim().equals("")) {
295                 url.setService(service);
296             }
297         }
298         return url;
299         } catch (ConfigException ce) {
300             ce.printStackTrace();
301         }
302         return null;
303     }
304     
305      //Used to get resolved local JmsHost for a standalone server instance
306
private JmsHost getResolvedJmsHostForStandaloneServerInstance(
307                                          String JavaDoc serverName) throws ConfigException {
308         logFine(" getresolved " + serverName);
309        ConfigContext con = getAdminConfigContext();
310        Server serverInstance = ServerHelper.getServerByName(con, serverName);
311        logFine("serverinstace " + serverInstance);
312        JmsHost jmsHost = getResolvedJmsHost(serverInstance);
313        return jmsHost;
314     }
315 /*
316     //Used to get all LOCAL JMS Hosts in a cluster.
317     private Map<String, JmsHost> getResolvedLocalJmsHostsInCluster(final String clusterName) throws ConfigException {
318          final Map<String, JmsHost> map = new HashMap<String, JmsHost> ();
319          ConfigContext con = getAdminConfigContext();
320          final String myCluster = ClusterHelper.getClusterByName(con, clusterName).getName();
321          final Server[] buddies = ServerHelper.getServersInCluster(con, myCluster);
322          for (final Server as : buddies) {
323              final JmsHost copy = getResolvedJmsHost(as);
324              map.put(as.getName(), copy);
325          }
326          return map;
327     }
328 */

329     private JmsHost getResolvedJmsHost(Server as) throws ConfigException{
330         logFine("getResolvedJmsHost " + as);
331         return rep.getResolvedJmsHost(as);
332     }
333
334     private boolean isClustered() throws ConnectorRuntimeException {
335         return JmsRaUtil.isClustered();
336     }
337     
338     private static String JavaDoc getServerName() {
339         String JavaDoc serverName=System.getProperty(SystemPropertyConstants.SERVER_NAME);
340         return serverName;
341     }
342     
343     private void logFine(String JavaDoc s) {
344         if (logger.isLoggable(Level.FINE)) {
345             logger.log(Level.FINE, "MQAddressList :: " + s);
346         }
347     }
348
349     public int getSize() {
350         if (this.urlList != null) {
351             return this.urlList.size();
352         } else {
353             return 0;
354         }
355     }
356 }
357
Popular Tags