1 23 24 package com.sun.enterprise.connectors.system; 25 26 import java.lang.reflect.Method ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.resource.spi.ResourceAdapter ; 32 import com.sun.enterprise.Switch; 33 import com.sun.enterprise.admin.target.Target; 34 import com.sun.enterprise.admin.target.TargetBuilder; 35 import com.sun.enterprise.admin.target.TargetType; 36 import com.sun.enterprise.config.ConfigException; 37 import com.sun.enterprise.config.serverbeans.AppserverClusterViewFromCacheRepository; 38 import com.sun.enterprise.config.serverbeans.Config; 39 import com.sun.enterprise.config.serverbeans.JmsHost; 40 import com.sun.enterprise.config.serverbeans.JmsService; 41 import com.sun.enterprise.config.serverbeans.Server; 42 import com.sun.enterprise.connectors.ConnectorRegistry; 43 import com.sun.enterprise.connectors.ConnectorRuntime; 44 import com.sun.enterprise.connectors.ConnectorRuntimeException; 45 import com.sun.enterprise.server.ApplicationServer; 46 import com.sun.enterprise.util.i18n.StringManager; 47 import com.sun.enterprise.server.ManagerFactory; 48 import com.sun.logging.LogDomains; 49 import com.sun.enterprise.config.serverbeans.ServerHelper; 50 import com.sun.enterprise.config.ConfigContext; 51 52 57 61 public class MQJMXConnectorHelper { 62 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 63 private static final String JMXSERVICEURLLIST = "JMXServiceURLList"; 64 private static final String JMXCONNECTORENV = "JMXConnectorEnv"; 65 66 private static StringManager sm = StringManager.getManager( 67 MQJMXConnectorHelper.class); 68 69 70 public static MQJMXConnectorInfo[] getMQJMXConnectorInfo(final String target) 71 throws ConnectorRuntimeException { 72 try { 73 final Target tgt = getTarget(target); 74 final Config config = tgt.getConfigs()[0]; 75 final JmsService jmsService = config.getJmsService(); 76 77 ActiveJmsResourceAdapter air = getMQAdapter(); 78 final Class mqRAClassName = air.getResourceAdapter().getClass(); 79 80 MQJMXConnectorInfo mqjmxForServer = (MQJMXConnectorInfo) 81 java.security.AccessController.doPrivileged 82 (new java.security.PrivilegedExceptionAction () { 83 public java.lang.Object run() throws Exception { 84 if(!isClustered(tgt)) { 85 _logger.log(Level.FINE, "Getting JMX connector for" + 86 " standalone target " + target); 87 return _getMQJMXConnectorInfo(tgt.getName(), 88 jmsService, mqRAClassName); 89 } else { 90 _logger.log(Level.FINE, "Getting JMX connector for" + 91 " cluster target " + target); 92 return _getMQJMXConnectorInfoForCluster(tgt.getName(), 93 jmsService, mqRAClassName); 94 } 95 } 96 }); 97 98 return new MQJMXConnectorInfo[]{mqjmxForServer}; 99 } catch (Exception e) { 100 ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage()); 102 cre.initCause(e); 103 throw cre; 104 } 105 } 106 107 111 private static ActiveJmsResourceAdapter getMQAdapter() throws Exception { 112 final String mqraModuleName = ConnectorRuntime.DEFAULT_JMS_ADAPTER; 114 115 ActiveJmsResourceAdapter air = (ActiveJmsResourceAdapter) 116 java.security.AccessController.doPrivileged 117 (new java.security.PrivilegedExceptionAction () { 118 public java.lang.Object run() throws Exception { 119 ManagerFactory.getSAConnectorModulesManager(). 120 loadOneSystemApp(mqraModuleName, true); 121 return (ActiveJmsResourceAdapter) ConnectorRegistry.getInstance(). 122 getActiveResourceAdapter(mqraModuleName); 123 } 124 }); 125 return air; 126 } 127 128 133 private static MQJMXConnectorInfo _getMQJMXConnectorInfoForCluster( 134 String target, JmsService jmsService, Class mqRAClassName) 135 throws ConnectorRuntimeException { 136 ResourceAdapter raInstance = null; 138 try { 140 MQAddressList list = null; 141 142 if (jmsService.getType().equalsIgnoreCase(ActiveJmsResourceAdapter.REMOTE)) { 143 list = getDefaultAddressList(jmsService); 144 } else { 145 String domainurl = 146 ApplicationServer.getServerContext().getServerConfigURL(); 147 AppserverClusterViewFromCacheRepository rep 148 = new AppserverClusterViewFromCacheRepository(domainurl); 149 150 java.util.Map <String ,JmsHost> hostMap = 151 rep.getResolvedLocalJmsHostsInCluster(target); 152 153 if ( hostMap.size() == 0 ) { 154 String msg = sm.getString("mqjmx.no_jms_hosts"); 155 throw new ConnectorRuntimeException(msg); 156 } 157 158 list = new MQAddressList(); 159 for (JmsHost host : hostMap.values()) { 160 list.addMQUrl(host); 161 } 162 } 163 164 String connectionUrl = list.toString(); 165 raInstance = getConfiguredRA(mqRAClassName, connectionUrl); 166 } catch (Exception e) { 167 e.printStackTrace(); 168 ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage()); 169 cre.initCause(e); 170 throw cre; 171 } 172 173 try { 174 String jmxServiceURL = null, jmxServiceURLList = null; 175 Map <String , ?> jmxConnectorEnv = null; 176 Method [] methds = raInstance.getClass().getMethods(); 177 for (int i = 0; i < methds.length; i++) { 178 Method m = methds[i]; 179 if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){ 180 jmxServiceURLList = (String )m.invoke(raInstance, new Object []{}); 181 if (jmxServiceURLList != null && !jmxServiceURLList.trim().equals("")){ 182 jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList); 183 } 184 } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){ 185 jmxConnectorEnv = (Map <String ,?>)m.invoke(raInstance, new Object []{}); 186 } 187 } 188 MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(target, 189 ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) , 190 jmsService.getType(), jmxServiceURL, jmxConnectorEnv); 191 return mqInfo; 192 } catch (Exception e) { 193 e.printStackTrace(); 194 ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage()); 195 cre.initCause(e); 196 throw cre; 197 } 198 } 199 200 205 private static MQJMXConnectorInfo _getMQJMXConnectorInfo( 206 String targetName, JmsService jmsService, Class mqRAClassName) 207 throws ConnectorRuntimeException { 208 try { 209 211 String connectionURL = null; 212 213 boolean isDAS = isDAS(targetName); 214 215 if (isDAS) { 216 logFine(" _getMQJMXConnectorInfo - in DAS"); 217 _logger.log(Level.FINE,"In DAS"); 218 connectionURL = getDefaultAddressList(jmsService).toString(); 219 } else { 220 _logger.log(Level.FINE,"not in DAS"); 222 logFine(" _getMQJMXConnectorInfo - NOT in DAS"); 223 String domainurl = 224 ApplicationServer.getServerContext().getServerConfigURL(); 225 JmsService serverJmsService= getJmsServiceOfStandaloneServerInstance(targetName); 226 MQAddressList mqadList = new MQAddressList(serverJmsService, targetName); 227 mqadList.setup(); 228 connectionURL = mqadList.toString(); 229 } 230 logFine(" _getMQJMXConnectorInfo - connection URL " + connectionURL); 231 232 ResourceAdapter raInstance = getConfiguredRA(mqRAClassName, connectionURL); 233 String jmxServiceURL = null, jmxServiceURLList = null; 234 Map <String , ?> jmxConnectorEnv = null; 235 Method [] methds = raInstance.getClass().getMethods(); 236 for (int i = 0; i < methds.length; i++) { 237 Method m = methds[i]; 238 if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){ 239 jmxServiceURLList = (String )m.invoke(raInstance, new Object []{}); 240 } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){ 241 jmxConnectorEnv = (Map <String ,?>)m.invoke(raInstance, new Object []{}); 242 } 243 } 244 logFine(" _getMQJMXConnectorInfo - jmxServiceURLList " + jmxServiceURLList); 245 logFine(" _getMQJMXConnectorInfo - jmxConnectorEnv " + jmxConnectorEnv); 246 jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList); 247 248 MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(targetName, 249 ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) , 250 jmsService.getType(), jmxServiceURL, jmxConnectorEnv); 251 return mqInfo; 252 } catch (Exception e) { 253 e.printStackTrace(); 254 ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage()); 255 cre.initCause(e); 256 throw cre; 257 } 258 } 259 260 private static boolean isDAS(String targetName) throws ConfigException { 261 ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext(); 262 if (isAConfig(targetName)) { 263 return false; 264 } 265 return ServerHelper.isDAS(con, targetName); 266 } 267 268 private static boolean isAConfig(String targetName) throws ConfigException { 269 ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext(); 270 return ServerHelper.isAConfig(con, targetName); 271 } 272 273 274 private static JmsService getJmsServiceOfStandaloneServerInstance(String targetName) throws ConfigException { 275 logFine("getJMSServiceOfSI LL " + targetName); 276 ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext(); 277 278 Config cfg = null; 279 if (isAConfig(targetName)) { 280 cfg = ServerHelper.getConfigByName(con, targetName); 281 } else { 282 cfg = ServerHelper.getConfigForServer(con, targetName); 283 } 284 285 logFine("cfg " + cfg); 286 JmsService jmsService = cfg.getJmsService(); 287 logFine("jmsservice " + jmsService); 288 return jmsService; 289 } 290 291 295 private static ResourceAdapter getConfiguredRA(Class mqRAclassname, 296 String connectionURL) throws Exception { 297 ResourceAdapter raInstance = (ResourceAdapter ) mqRAclassname.newInstance(); 298 Method setConnectionURL = mqRAclassname.getMethod( 299 "set" + ActiveJmsResourceAdapter.CONNECTION_URL, 300 new Class [] { String .class}); 301 setConnectionURL.invoke(raInstance, new Object [] {connectionURL}); 302 logFine(" getConfiguredRA - set connectionURL as " + connectionURL); 303 return raInstance; 304 } 305 306 private static MQAddressList getDefaultAddressList(JmsService jmsService) 307 throws ConfigException { 308 MQAddressList list = new MQAddressList(jmsService); 309 list.setup(); 310 return list; 311 } 312 313 private static String getFirstJMXServiceURL(String jmxServiceURLList) { 314 if ((jmxServiceURLList == null) || ("".equals(jmxServiceURLList))) { 317 return jmxServiceURLList; 318 } else { 319 StringTokenizer tokenizer = new StringTokenizer (jmxServiceURLList, " "); 320 return tokenizer.nextToken(); 321 } 322 } 323 324 private static Target getTarget(String target) throws ConfigException { 325 final TargetType[] vaildTargetTypes = new TargetType[] { 326 TargetType.CONFIG, 327 TargetType.SERVER, 328 TargetType.DOMAIN, 329 TargetType.CLUSTER, 330 TargetType.STANDALONE_SERVER, 331 TargetType.UNCLUSTERED_SERVER, 332 TargetType.STANDALONE_CLUSTER, 333 TargetType.DAS }; 334 final Target tgt = TargetBuilder.INSTANCE.createTarget( 335 vaildTargetTypes, target, 336 com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext()); 337 assert tgt != null; 338 return tgt; 339 } 340 341 private static boolean isClustered(Target tgt) throws ConfigException { 342 return tgt.getType() == TargetType.CLUSTER; 343 } 344 345 private static void logFine(String s) { 346 if (_logger.isLoggable(Level.FINE)) { 347 _logger.log(Level.FINE, "MQJMXConnectorHelper :: " + s); 348 } 349 } 350 351 380 } 381 | Popular Tags |