1 23 24 package com.sun.enterprise.connectors.system; 25 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.util.Iterator ; 29 import java.util.ArrayList ; 30 import java.util.List ; 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 47 public class MQAddressList { 48 49 static Logger logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 50 private static String myName = 51 System.getProperty(SystemPropertyConstants.SERVER_NAME); 52 53 private List <MQUrl> urlList = new ArrayList <MQUrl>(); 54 55 private JmsService jmsService = null; 56 private AppserverClusterViewFromCacheRepository rep = null; 57 private static String nodeAgentHost = null; 58 private String targetName = null; 59 60 63 public MQAddressList() throws ConfigException { 64 this(null); 65 } 66 67 70 public MQAddressList(JmsService service) throws ConfigException { 71 this(service, getServerName()); 73 } 74 75 82 public MQAddressList(JmsService service, String 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 domainurl = context.getServerConfigURL(); 89 rep = new AppserverClusterViewFromCacheRepository(domainurl); 90 try { 91 nodeAgentHost = rep.getNodeAgentHostName(server); 92 logFine("na host" + nodeAgentHost); 93 } catch (Exception 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 102 public void setup() throws ConfigException { 103 try { 104 if (isClustered() && (!this.jmsService.getType().equals(ActiveJmsResourceAdapter.REMOTE)) ) { 105 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 setupForStandaloneServerInstance(); 117 } 118 } 119 } catch (ConnectorRuntimeException ce) { 120 throw new ConfigException(ce); 121 } 122 } 123 124 private boolean isDAS(String targetName) throws ConfigException { 125 if (isAConfig(targetName)) { 126 return false; 127 } 128 return ServerHelper.isDAS(getAdminConfigContext(), targetName); 129 } 130 131 private boolean isAConfig(String targetName) throws ConfigException { 132 return ServerHelper.isAConfig(getAdminConfigContext(), targetName); 133 } 134 135 136 141 private ConfigContext getAdminConfigContext() { 142 return com.sun.enterprise.admin.server.core.AdminService. 143 getAdminService().getAdminContext().getAdminConfigContext(); 144 } 145 146 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 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 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 183 private void setupForCluster() throws ConfigException { 184 java.util.Map <String ,JmsHost> hostMap = 185 rep.getResolvedLocalJmsHostsInMyCluster(true); 186 187 JmsHost jmsHost = hostMap.get(myName); 189 MQUrl myUrl = createUrl(jmsHost, nodeAgentHost); 190 urlList.add(myUrl); 191 hostMap.remove(myName); 192 193 for (JmsHost host : hostMap.values() ) { 195 MQUrl url = createUrl(host, nodeAgentHost); 196 urlList.add(url); 197 } 198 } 199 200 201 209 public String toString() { 210 String s = ""; 211 212 Iterator 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 231 public void addMQUrl(JmsHost host) { 232 MQUrl url = createUrl(host); 233 urlList.add(url); 234 } 235 236 241 public void removeMQUrl(JmsHost host) { 242 MQUrl url = createUrl(host); 243 urlList.remove(url); 244 } 245 246 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 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 overridedHostName) { 271 try { 272 String name = host.getName(); 273 String hostName = host.getHost(); 274 ServerContext serverContext = ApplicationServer.getServerContext(); 278 Server server = serverContext.getConfigBean(); 279 if (overridedHostName != null && !overridedHostName.trim().equals("")) { 280 hostName = overridedHostName; 281 } 282 283 String port = host.getPort(); 284 MQUrl url = new MQUrl(name); 285 url.setHost(hostName); 286 url.setPort(port); 287 if (js != null) { 288 String scheme = js.getMqScheme(); 289 if (scheme != null && !scheme.trim().equals("")) { 290 url.setScheme(scheme); 291 } 292 293 String 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 private JmsHost getResolvedJmsHostForStandaloneServerInstance( 307 String 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 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 getServerName() { 339 String serverName=System.getProperty(SystemPropertyConstants.SERVER_NAME); 340 return serverName; 341 } 342 343 private void logFine(String 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 |