1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.logging.Logger; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Hashtable ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 45 public class WebModule extends J2EEModule 46 implements WebModuleMBean 47 { 48 49 private static final String [] eventTypes = {NotificationConstants.OBJECT_CREATED, 50 NotificationConstants.OBJECT_DELETED}; 51 52 private static Logger log = Logger.getLogger(WebModule.class); 54 55 58 private List servletNames = new ArrayList (); 59 60 private String jbossWebDD; 61 62 65 private static final Map fakeJ2EEApps = new HashMap (); 66 67 69 79 public static ObjectName create(MBeanServer mbeanServer, 80 String earName, 81 String warName, 82 URL pURL, 83 ObjectName webContainerName) 84 { 85 String webXml = null; 86 String jbossWebXml = null; 87 ObjectName jsr77ParentName = null; 88 ObjectName lCreated = null; 89 ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer); 90 ObjectName jsr77Name = null; 91 try 92 { 93 Hashtable props = j2eeServerName.getKeyPropertyList(); 95 String j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" + 96 props.get("name"); 97 98 99 if (earName == null) 100 { 101 jsr77ParentName = j2eeServerName; 103 } 104 else 105 { 106 ObjectName lApplicationQuery = new ObjectName (J2EEDomain.getDomainName() + ":" + 108 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," + 109 "name=" + earName + "," + 110 j2eeServer + "," + 111 "*"); 112 Set lApplications = mbeanServer.queryNames(lApplicationQuery, null); 113 114 if (lApplications.isEmpty()) 115 { 116 lCreated = J2EEApplication.create(mbeanServer, 117 earName, 118 null); 119 jsr77ParentName = lCreated; 120 } else if (lApplications.size() == 1) 122 { 123 jsr77ParentName = (ObjectName ) lApplications.iterator().next(); 124 } } 126 127 webXml = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.WEB); 129 jbossWebXml = J2EEDeployedObject.getDeploymentDescriptor(pURL, 131 J2EEDeployedObject.JBOSS_WEB); 132 } 133 catch (Exception e) 134 { 135 log.error("Could not create JSR-77 WebModule: " + warName, e); 136 return null; 137 } 138 139 try 140 { 141 String [] jvms = (String []) mbeanServer.getAttribute(j2eeServerName, 143 "javaVMs"); 144 145 WebModule webModule = new WebModule(warName, jsr77ParentName, jvms, webXml, 146 webContainerName, jbossWebXml); 147 jsr77Name = webModule.getObjectName(); 148 mbeanServer.registerMBean(webModule, jsr77Name); 149 if (lCreated != null) 151 { 152 fakeJ2EEApps.put(jsr77Name, lCreated); 153 } log.debug("Created JSR-77 WebModule: " + jsr77Name); 155 } 156 catch (Exception e) 157 { 158 log.debug("Could not create JSR-77 WebModule: " + warName, e); 159 return null; 160 } 161 return jsr77Name; 162 } 163 164 170 public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name) 171 { 172 try 173 { 174 mbeanServer.unregisterMBean(jsr77Name); 175 log.debug("Remove JSR-77 WebModule: " + jsr77Name); 176 ObjectName jsr77ParentName = (ObjectName ) fakeJ2EEApps.get(jsr77Name); 177 if (jsr77ParentName != null) 178 { 179 log.debug("Remove fake JSR-77 parent Application: " + jsr77ParentName); 180 J2EEApplication.destroy(mbeanServer, jsr77ParentName); 181 } 182 } 183 catch (Exception e) 184 { 185 log.debug("Could not destroy JSR-77 WebModule: " + jsr77Name, e); 186 } 187 } 188 189 191 201 public WebModule(String warName, ObjectName j2eeAppName, String [] jvms, 202 String webDD, ObjectName webContainerName, String jbossWebDD) 203 throws MalformedObjectNameException , 204 InvalidParentException 205 { 206 super(J2EETypeConstants.WebModule, warName, j2eeAppName, jvms, webDD); 207 this.jbossWebDD = (jbossWebDD == null ? "" : jbossWebDD); 208 } 209 210 212 217 public String [] getservlets() 218 { 219 String [] servlets = new String [servletNames.size()]; 220 servletNames.toArray(servlets); 221 return servlets; 222 } 223 224 227 public String getservlet(int pIndex) 228 { 229 if (pIndex >= 0 && pIndex < servletNames.size()) 230 { 231 return (String ) servletNames.get(pIndex); 232 } 233 else 234 { 235 return null; 236 } 237 } 238 239 242 public String getjbossWebDeploymentDescriptor() 243 { 244 return jbossWebDD; 245 } 246 247 249 public void addChild(ObjectName pChild) 250 { 251 String lType = J2EEManagedObject.getType(pChild); 252 if (J2EETypeConstants.Servlet.equals(lType) 253 ) 254 { 255 servletNames.add(pChild.getCanonicalName()); 256 } 257 } 258 259 public void removeChild(ObjectName pChild) 260 { 261 String lType = J2EEManagedObject.getType(pChild); 262 if (J2EETypeConstants.Servlet.equals(lType)) 263 { 264 servletNames.remove(pChild.getCanonicalName()); 265 } 266 } 267 268 270 public String [] getEventTypes() 271 { 272 return eventTypes; 273 } 274 275 public String getEventType(int index) 276 { 277 String type = null; 278 if (index >= 0 && index < eventTypes.length) 279 { 280 type = eventTypes[index]; 281 } 282 return type; 283 } 284 285 public void postCreation() 286 { 287 sendNotification(NotificationConstants.OBJECT_CREATED, "Web module created"); 288 } 289 290 public void preDestruction() 291 { 292 sendNotification(NotificationConstants.OBJECT_DELETED, "Web module destroyed"); 293 } 294 295 297 public String toString() 298 { 299 return "WebModule[ " + super.toString() + 300 ", Servlets: " + servletNames + 301 ", JBoss-Web-DD: " + jbossWebDD + 302 " ]"; 303 } 304 305 307 309 313 protected Hashtable getParentKeys(ObjectName jsr77ParentName) 314 { 315 Hashtable parentKeys = new Hashtable (); 316 Hashtable parentProps = jsr77ParentName.getKeyPropertyList(); 317 String parentName = (String ) parentProps.get("name"); 318 String j2eeType = (String ) parentProps.get(J2EEManagedObject.TYPE); 319 320 if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false) 322 { 323 parentKeys.put(J2EETypeConstants.J2EEServer, parentName); 325 parentKeys.put(J2EETypeConstants.J2EEApplication, "null"); 326 } 327 else 328 { 329 parentKeys.put(J2EETypeConstants.J2EEApplication, parentName); 331 String j2eeServerName = (String ) parentProps.get(J2EETypeConstants.J2EEServer); 332 parentKeys.put(J2EETypeConstants.J2EEServer, j2eeServerName); 333 } 334 335 return parentKeys; 336 } 337 338 340 342 } 343 344 | Popular Tags |