1 23 24 package com.sun.enterprise.admin.server.core.mbean.config; 25 26 import com.sun.enterprise.config.ConfigException; 28 import com.sun.enterprise.config.serverbeans.ServerXPathHelper; 29 import com.sun.enterprise.config.serverbeans.ServerTags; 30 32 import javax.management.Attribute ; 34 import javax.management.AttributeNotFoundException ; 35 36 import com.sun.enterprise.admin.AdminContext; 38 import com.sun.enterprise.admin.common.ObjectNames; 39 import com.sun.enterprise.admin.common.EntityStatus; 40 import com.sun.enterprise.admin.common.constant.ConfigAttributeName; 41 import com.sun.enterprise.admin.common.exception.J2EEApplicationException; 42 import com.sun.enterprise.admin.common.exception.MBeanConfigException; 43 44 import com.sun.enterprise.instance.InstanceEnvironment; 46 import com.sun.enterprise.admin.event.AdminEvent; 47 import com.sun.enterprise.admin.event.AdminEventCache; 48 import com.sun.enterprise.admin.event.AdminEventResult; 49 import com.sun.enterprise.admin.event.ApplicationDeployEvent; 50 import com.sun.enterprise.admin.event.ModuleDeployEvent; 51 import com.sun.enterprise.admin.event.BaseDeployEvent; 52 import com.sun.enterprise.admin.event.AdminEventMulticaster; 53 import com.sun.enterprise.admin.server.core.channel.RMIClient; 54 import com.sun.enterprise.admin.server.core.channel.AdminChannel; 55 import com.sun.enterprise.admin.common.Status; 56 import com.sun.enterprise.admin.common.MBeanServerFactory; 57 import javax.management.MBeanServer ; 58 import javax.management.ObjectName ; 59 import com.sun.enterprise.server.ApplicationServer; 60 import com.sun.enterprise.server.ServerContext; 61 import com.sun.enterprise.Switch; 62 63 64 79 80 public class ManagedJ2EEApplication extends ConfigMBeanBase implements ConfigAttributeName.J2EEApplication 81 { 82 private static final String [][] MAPLIST = 83 { 84 {kName , ATTRIBUTE + ServerTags.NAME}, 85 {kLocation , ATTRIBUTE + ServerTags.LOCATION}, 86 {kVirtualServers , ATTRIBUTE + ServerTags.VIRTUAL_SERVERS}, 87 {kDescription , ATTRIBUTE + PSEUDO_ATTR_DESCRIPTION}, 89 }; 90 91 private static final String [] ATTRIBUTES = 92 { 93 kName + ", String, R" , 94 kLocation + ", String, RW" , 95 kVirtualServers + ", String, RW" , 96 kDescription + ", String, RW" , 98 }; 99 100 101 private static final String [] OPERATIONS = 102 { 103 "getModules(), INFO", 104 "getEjbModules(), INFO", 105 "getWebModules(), INFO", 106 "getStatus(), INFO", 107 "enable(), ACTION", 108 "disable(), ACTION", 109 "start(), ACTION", 110 "stop(), ACTION", 111 "getState(), INFO" 112 }; 113 114 117 public ManagedJ2EEApplication() throws MBeanConfigException 118 { 119 this.setDescriptions(MAPLIST, ATTRIBUTES, OPERATIONS); 120 } 121 122 123 public ManagedJ2EEApplication(String instanceName, String appName) 124 throws MBeanConfigException 125 { 126 this(instanceName, appName, null); 127 } 128 129 public ManagedJ2EEApplication(String instanceName, String appName, 130 AdminContext adminContext) 131 throws MBeanConfigException 132 { 133 this(); setAdminContext(adminContext); 135 initialize(ObjectNames.kApplication, new String []{instanceName, appName}); 136 } 137 138 public String [] getModules() throws J2EEApplicationException 139 { 140 return getModulesByType(ModulesXMLHelper.MODULE_TYPE_ALL); 141 } 142 143 public String [] getEjbModules() throws J2EEApplicationException 144 { 145 return getModulesByType(ModulesXMLHelper.MODULE_TYPE_EJB); 146 } 147 148 public String [] getWebModules() throws J2EEApplicationException 149 { 150 return getModulesByType(ModulesXMLHelper.MODULE_TYPE_WEB); 151 } 152 153 private String [] getModulesByType(int moduleTypes) throws J2EEApplicationException 154 { 155 try 156 { 157 String location = (String )this.getAttribute(kLocation); 158 return ModulesXMLHelper.getModulesFromApplicationLocation(location, moduleTypes); 159 } 160 catch (Exception e) 161 { 162 sLogger.throwing(getClass().getName(), "getModulesByType", e); 163 throw new J2EEApplicationException(e.getMessage()); 164 } 165 } 166 167 168 173 public EntityStatus getStatus() throws J2EEApplicationException 174 { 175 EntityStatus status = null; 176 177 try 178 { 179 boolean isAppEnabled = true; status = new EntityStatus(); 182 if (isAppEnabled) 183 { 184 status.setEnabled(); 185 } 186 else 187 { 188 status.setDisabled(); 189 } 190 } 191 catch (Exception e) 192 { 193 sLogger.throwing(getClass().getName(), "getStatus", e); 194 throw new J2EEApplicationException(e.getMessage()); 195 } 196 return status; 197 } 198 199 208 public void disable() throws J2EEApplicationException 209 { 210 return; 222 } 223 224 229 public void stop() throws J2EEApplicationException { 230 try { 231 String appName = (String )this.getAttribute(kName); 232 multicastAdminEvent(appName, BaseDeployEvent.DISABLE); 233 } catch (Exception e) { 234 sLogger.throwing(getClass().getName(), "stop", e); 235 throw new J2EEApplicationException(e.getMessage()); 236 } 237 } 238 239 242 243 public Integer getState() throws J2EEApplicationException { 244 try { 245 ServerContext serverContext = ApplicationServer.getServerContext(); 246 String namePattern = ( 247 serverContext.getDefaultDomainName() + ":" + 248 "j2eeType=J2EEApplication," + 249 "name=" + ((String )this.getAttribute(kName)) + "," + 250 "J2EEServer=" + serverContext.getInstanceName() + "," + 251 "*"); 252 Integer intObj = (Integer ) 253 Switch.getSwitch().getManagementObjectManager().getState(namePattern); 254 return intObj; 255 } catch (Exception e) { 256 sLogger.throwing(getClass().getName(), "getState", e); 257 throw new J2EEApplicationException(e.getMessage()); 258 } 259 } 260 261 280 281 282 291 public void enable() throws J2EEApplicationException 292 { 293 return; 305 } 306 307 308 313 public void start() throws J2EEApplicationException { 314 try { 315 String appName = (String )this.getAttribute(kName); 316 multicastAdminEvent(appName, BaseDeployEvent.ENABLE); 317 } catch (Exception e) { 318 sLogger.throwing(getClass().getName(), "start", e); 319 throw new J2EEApplicationException(e.getMessage()); 320 } 321 } 322 323 public static final void main(String [] args) throws Exception 324 { 325 System.setProperty("com.sun.aas.instanceRoot", "e:\\tmp"); 326 ManagedJ2EEApplication appMBean = 327 new ManagedJ2EEApplication("adminapp", "admserv"); 328 EntityStatus status = appMBean.getStatus(); 329 sLogger.info("======== Status = " + status.getStatusString()); 330 if (status.isDisabled()) 331 { 332 sLogger.info("======== Enabling app"); 333 appMBean.enable(); 334 status = appMBean.getStatus(); 335 sLogger.info("======== Status = " + status.getStatusString()); 336 } 337 else 338 { 339 sLogger.info("======== Disabling app"); 340 appMBean.disable(); 341 status = appMBean.getStatus(); 342 sLogger.info("======== Status = " + status.getStatusString()); 343 } 344 } 345 346 350 private void multicastAdminEvent(String entityName, String actionCode) 351 throws J2EEApplicationException { 352 String instanceName = super.getServerInstanceName(); 353 InstanceEnvironment instEnv = new InstanceEnvironment(instanceName); 354 try { 355 instEnv.applyServerXmlChanges(false); 356 } catch (Exception e) { 357 sLogger.throwing(getClass().getName(), "getAttr", e); 358 throw new J2EEApplicationException(e.getMessage()); 359 } 360 AdminEvent event = new ApplicationDeployEvent(instanceName, entityName, actionCode); 361 RMIClient serverInstancePinger = AdminChannel.getRMIClient(instanceName); 363 if (serverInstancePinger.getInstanceStatusCode() != Status.kInstanceRunningCode) { 364 return; 365 } 366 AdminEventResult multicastResult = AdminEventMulticaster.multicastEvent(event); 367 if (!AdminEventResult.SUCCESS.equals(multicastResult.getResultCode())) { 368 AdminEventCache cache = AdminEventCache.getInstance(instanceName); 369 cache.setRestartNeeded(true); 370 } 371 } 372 } 373 | Popular Tags |