1 19 20 package org.netbeans.modules.j2ee.sun.bridge.apis; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.List ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.io.IOException ; 29 30 import javax.management.remote.JMXConnector ; 31 import javax.management.Notification ; 32 import javax.management.MBeanServerConnection ; 33 34 import javax.enterprise.deploy.spi.DeploymentManager ; 35 import javax.enterprise.deploy.spi.Target ; 36 37 import com.sun.appserv.management.config.StandaloneServerConfig; 38 import com.sun.appserv.management.config.DomainConfig; 39 import com.sun.appserv.management.DomainRoot; 40 import com.sun.appserv.management.base.AMX; 41 import com.sun.appserv.management.base.QueryMgr; 42 import com.sun.appserv.management.client.AppserverConnectionSource; 43 import com.sun.appserv.management.client.ProxyFactory; 44 import com.sun.appserv.management.config.RARModuleConfig; 45 import com.sun.appserv.management.config.ResourceConfig; 46 import com.sun.appserv.management.config.ServerConfig; 47 import com.sun.appserv.management.j2ee.J2EEManagedObject; 48 import com.sun.appserv.management.j2ee.J2EEDomain; 49 import com.sun.appserv.management.j2ee.ResourceAdapterModule; 50 51 import org.netbeans.modules.j2ee.sun.util.NodeTypes; 52 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 53 import org.netbeans.modules.j2ee.sun.appsrvapi.PortDetector; 54 55 import com.sun.enterprise.deployment.backend.DeploymentStatus; 56 import com.sun.enterprise.deployment.client.DeploymentFacility; 57 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier; 58 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory; 59 import com.sun.enterprise.deployment.client.JESProgressObject; 60 import java.util.Properties ; 61 import java.util.Vector ; 62 import org.netbeans.modules.j2ee.sun.ide.controllers.ControllerUtil; 63 64 67 public abstract class AppserverMgmtControllerBase 68 implements javax.management.NotificationListener { 69 70 private AMX amxObj; 71 private ProxyFactory amxProxyFactory; 72 private MBeanServerConnection mbeanServerConn; 73 private DeploymentManager deployMgr; 74 private static Logger logger; 75 76 protected AppserverConnectionSource appMgmtConnection; 77 protected static final String DAS_SERVER_NAME = "server"; 78 protected static final String BACKEND_COM_SUN_APPSERV_MBEAN_DOMAIN_NAME = 79 "com.sun.appserv"; 80 81 static { 82 logger = Logger.getLogger("org.netbeans.modules.j2ee.sun"); 83 } 84 85 86 93 public AppserverMgmtControllerBase(final DeploymentManager deploymentMgr, 94 final AppserverConnectionSource connection) { 95 this.deployMgr = deploymentMgr; 96 this.appMgmtConnection = connection; 97 initializeAMXConnectorWithInterceptor(); 98 } 99 100 101 109 public AppserverMgmtControllerBase(final AMX amxObject, 110 final DeploymentManager deploymentMgr, 111 final AppserverConnectionSource connection) { 112 this.deployMgr = deploymentMgr; 113 this.appMgmtConnection = connection; 114 initializeAMXConnectorWithInterceptor(); 115 this.amxObj = amxObject; 116 } 117 118 119 120 128 public AppserverMgmtControllerBase(final AMX amxObject, 129 final AppserverConnectionSource connection) { 130 appMgmtConnection = connection; 131 setupProxyFactory(); 132 this.amxObj = amxObject; 133 } 134 135 136 140 141 public AppserverConnectionSource getAppserverConnectionSource(){ 142 return appMgmtConnection; 143 } 144 148 private void initializeAMXConnectorWithInterceptor() { 149 try { 150 ControllerUtil.checkIfServerInDebugMode(getDeploymentManager()); 151 JMXConnector jmxConn = 152 appMgmtConnection.getJMXConnector(false); 153 jmxConn.addConnectionNotificationListener(this,null,null); 154 setupProxyFactory(); 156 } catch (RuntimeException rex) { 157 getLogger().log(Level.FINE, rex.getMessage(), rex); 158 } catch (Exception ex) { 159 getLogger().log(Level.FINE, ex.getMessage(), ex); 160 } 161 } 162 163 164 167 private void setupProxyFactory() { 168 try { 169 ControllerUtil.checkIfServerInDebugMode(getDeploymentManager()); 170 this.amxProxyFactory = 171 ProxyFactory.getInstance(getMBeanServerConnection()); 172 } catch (RuntimeException rex) { 173 getLogger().log(Level.FINE, rex.getMessage(), rex); 174 } catch (Exception ex) { 175 getLogger().log(Level.FINE, ex.getMessage(), ex); 176 } 177 } 178 179 180 184 public void handleNotification(final Notification notification, 185 final Object handback) { 186 try { 187 JMXConnector jmxConn = appMgmtConnection.getJMXConnector(false); 188 if(jmxConn != null) { 189 jmxConn.close(); 190 } 191 } catch(IOException io) { 192 getLogger().log(Level.FINE, io.getMessage(), io); 193 } 194 appMgmtConnection = null; 195 } 196 197 198 199 204 public MBeanServerConnection getMBeanServerConnection() { 205 try { 206 if(mbeanServerConn == null) { 207 testIfServerInDebugAndLogException(); 208 mbeanServerConn = 209 ControllerUtil.getMBeanServerConnWithInterceptor( 210 (SunDeploymentManagerInterface)deployMgr, 211 appMgmtConnection); 212 } 213 testIfServerInDebugAndLogException(); 214 } catch (RuntimeException rex) { 215 getLogger().log(Level.FINE, rex.getMessage(), rex); 216 } catch(Exception e) { 217 getLogger().log(Level.FINE, e.getMessage(), e); 218 } 219 return mbeanServerConn; 220 } 221 222 223 228 public DeploymentManager getDeploymentManager() { 229 return deployMgr; 230 } 231 232 233 238 public String getName() { 239 return getAMXObject().getName(); 240 } 241 242 243 248 public AMX getAMXObject() { 249 return amxObj; 250 } 251 252 253 258 final DomainRoot getDomainRoot() { 259 testIfServerInDebug(); 260 return amxProxyFactory.getDomainRoot(); 261 } 262 263 264 267 final DomainConfig getDomainConfig() { 268 testIfServerInDebug(); 270 return getDomainRoot().getDomainConfig(); 271 } 272 273 274 279 protected final J2EEDomain getJ2EEDomain() { 280 return getDomainRoot().getJ2EEDomain(); 281 } 282 283 284 289 final protected QueryMgr getQueryMgr() { 290 return getDomainRoot().getQueryMgr(); 291 } 292 293 294 302 final ProxyFactory getAMXProxyFactory() { 303 return this.amxProxyFactory; 304 } 305 306 307 315 public Map getJ2EEAndConfigProperties(String nodeType, 316 AMX j2eeMod, AMX configPeer, List propsToIgnore) { 317 try { 318 Map propsMap = new HashMap (); 319 if(configPeer == null && j2eeMod != null){ 320 configPeer = getConfigPeerByNodeTypeAndName(j2eeMod, nodeType); 321 } 322 if(configPeer != null) { 323 propsMap = getConfigPropertiesFromBackend(nodeType, 324 configPeer, propsToIgnore); 325 326 } 327 if(j2eeMod != null){ 328 Map j2eeProps = 329 getPropertiesFromBackend(nodeType, j2eeMod, propsToIgnore); 330 propsMap.putAll(j2eeProps); 331 } 332 if(propsMap == null || propsMap.size() == 0) { 333 getLogger().log(Level.FINE, "The props in getJ2EE is 0!"); 334 } 335 propsMap = ControllerUtil.modifyEnabledProperty(propsMap, configPeer); 336 return propsMap; 337 } catch (RuntimeException rex) { 338 getLogger().log(Level.FINE, rex.getMessage(), rex); 339 return new HashMap (); 340 } catch (Exception e) { 341 getLogger().log(Level.FINE, e.getMessage()); 342 return new HashMap (); 343 } 344 } 345 346 347 356 protected Map getPropertiesFromBackend(final String nodeType, 357 final AMX res, final List propsToIgnore) { 358 testIfServerInDebug(); 359 Class intrface = NodeTypes.getAMXInterface(nodeType); 360 return ControllerUtil.getAllAttributes(intrface, res, propsToIgnore, 361 getMBeanServerConnection(), nodeType); 362 } 363 364 365 374 protected Map getConfigPropertiesFromBackend(final String nodeType, 375 final AMX res, final List propsToIgnore) { 376 testIfServerInDebug(); 377 Class intrface = NodeTypes.getAMXConfigPeerInterface(nodeType); 378 return ControllerUtil.getAllAttributes(intrface, res, propsToIgnore, 379 getMBeanServerConnection(), nodeType); 380 } 381 382 383 392 protected AMX getConfigPeerByNodeTypeAndName(final AMX amx, 393 final String nodeType) { 394 testIfServerInDebug(); 395 J2EEManagedObject managedObj = (J2EEManagedObject) amx; 396 AMX configPeer = managedObj.getConfigPeer(); 397 if(configPeer != null) { 398 return configPeer; 399 } else { 400 final String j2eeType = 401 NodeTypes.getAMXConfigPeerJ2EETypeByNodeType(nodeType); 402 java.util.Set set = getQueryMgr().queryJ2EETypeSet(j2eeType); 403 for(Iterator itr = set.iterator(); itr.hasNext(); ) { 404 AMX config = (AMX) itr.next(); 405 if(config.getName().equals(amx.getName())) { 406 return config; 407 } 408 } 409 } 410 return null; 411 } 412 413 421 protected Map getLogPropertiesFromBackend(final AMX res, final Map propNames) { 422 testIfServerInDebug(); 423 return ControllerUtil.getLogAttributes(res, propNames, getMBeanServerConnection()); 424 } 425 426 438 public void undeploy() { 439 testIfServerInDebug(); 440 DeploymentFacility df = createDeploymentFacility(); 441 JESProgressObject progressObject = null; 442 Properties props = null; 443 444 if((getAMXObject() instanceof ResourceAdapterModule) || 445 (getAMXObject() instanceof RARModuleConfig)){ 446 props = new Properties (); 447 props.put("name", getName()); props.put("cascade", "true"); } 450 451 if (df.isConnected()) { 452 String [] instances = getRelevantTargets(); 453 Target [] targets = df.createTargets(instances); 454 progressObject = df.undeploy(targets, getName(), props); 455 df.waitFor(progressObject); 456 } 457 if (null != progressObject) { 459 DeploymentStatus ds = progressObject.getCompletedStatus(); 460 ds.toString(); 461 } 462 } 463 464 private String [] getRelevantTargets(){ 465 Map serverConfigs = ControllerUtil.getServerInstancesMap(getAMXObject()); 466 Vector instances = new Vector (); 467 for(Iterator itr = serverConfigs.values().iterator(); itr.hasNext(); ) { 468 ServerConfig config = (ServerConfig)itr.next(); 469 boolean contains = config.getDeployedItemRefConfigMap().containsKey(getName()); 470 if(contains){ 471 instances.add(config.getName()); 472 } 473 } 474 String [] targets = new String [instances.size()]; 475 for(int i=0; i<instances.size(); i++){ 476 targets[i] = instances.get(i).toString(); 477 } 478 return targets; 479 } 480 481 private DeploymentFacility createDeploymentFacility(){ 482 DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility(); 483 SunDeploymentManagerInterface sunDplmtMgr = (SunDeploymentManagerInterface)getDeploymentManager(); 484 ServerConnectionIdentifier conn = createServerConnectionIdentifier( 485 sunDplmtMgr.getHost(), sunDplmtMgr.getPort(), sunDplmtMgr.getUserName(), sunDplmtMgr.getPassword()); 486 df.connect(conn); 487 return df; 488 } 489 490 private ServerConnectionIdentifier createServerConnectionIdentifier( 491 String host, int port, String user, String password) { 492 ServerConnectionIdentifier conn = 493 new ServerConnectionIdentifier(); 494 conn.setHostName(host); 495 conn.setHostPort(port); 496 conn.setUserName(user); 497 conn.setPassword(password); 498 try{ 499 if (PortDetector.isSecurePort(host, port)) { 500 conn.setSecure(true); 501 } else { 502 conn.setSecure(false); 503 } 504 }catch(Exception ex){ 505 conn.setSecure(false); 506 } 507 return conn; 508 } 509 510 511 512 513 518 protected void removeResourceRef(ResourceConfig resConfig, String resName) { 519 testIfServerInDebug(); 520 521 Map serverConfigs = ControllerUtil.getStandaloneServerInstancesMap(resConfig); 522 for(Iterator itr = serverConfigs.values().iterator(); itr.hasNext(); ) { 523 StandaloneServerConfig config = (StandaloneServerConfig)itr.next(); 524 boolean contains = config.getResourceRefConfigMap().containsKey(resName); 525 if(contains) { 526 config.removeResourceRefConfig(resName); 527 } 528 } 529 } 530 531 536 protected final Logger getLogger() { 537 if ( logger == null ) { 538 logger = Logger.getLogger("org.netbeans.modules.j2ee.sun"); 539 } 540 return logger; 541 } 542 543 544 548 protected void testIfServerInDebug() { 549 try { 550 ControllerUtil.checkIfServerInDebugMode(getDeploymentManager()); 551 } catch (RuntimeException rex) { 552 getLogger().log(Level.FINE, rex.getMessage(), rex); 553 throw rex; 554 } catch(Exception e) { 555 getLogger().log(Level.FINE, e.getMessage(), e); 556 } 557 } 558 559 560 564 protected void testIfServerInDebugAndLogException() { 565 try { 566 ControllerUtil.checkIfServerInDebugMode(getDeploymentManager()); 567 } catch (RuntimeException rex) { 568 getLogger().log(Level.FINE, rex.getMessage(), rex); 569 } catch(Exception e) { 570 getLogger().log(Level.FINE, e.getMessage(), e); 571 } 572 } 573 574 public boolean isDeployMgrLocal(){ 575 return ((SunDeploymentManagerInterface)deployMgr).isLocal(); 576 } 577 578 579 } 580 | Popular Tags |