1 16 17 18 package org.apache.webapp.admin.resources; 19 20 import java.util.Arrays ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Collections ; 24 25 import javax.management.Attribute ; 26 import javax.management.AttributeNotFoundException ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 import javax.management.ObjectInstance ; 30 31 32 40 41 public class ResourceUtils { 42 43 public final static String ENVIRONMENT_TYPE = ":type=Environment"; 44 public final static String RESOURCE_TYPE = ":type=Resource"; 45 public final static String RESOURCELINK_TYPE = ":type=ResourceLink"; 46 public final static String NAMINGRESOURCES_TYPE = ":type=NamingResources"; 47 public final static String GLOBAL_TYPE = ",resourcetype=Global"; 48 public final static String CONTEXT_TYPE = ",resourcetype=Context"; 49 public final static String SERVICE_DEFAULTCONTEXT_TYPE = 50 ",resourcetype=ServiceDefaultContext"; 51 public final static String HOST_DEFAULTCONTEXT_TYPE = 52 ",resourcetype=HostDefaultContext"; 53 54 public final static String USERDB_CLASS = "org.apache.catalina.UserDatabase"; 56 public final static String DATASOURCE_CLASS = "javax.sql.DataSource"; 57 public final static String MAILSESSION_CLASS = "javax.mail.Session"; 58 59 61 62 public static ObjectName getNamingResourceObjectName(String domain, 63 String resourcetype, String path, String host) throws Exception { 64 65 ObjectName oname = null; 66 if ((resourcetype==null) || (domain==null)) { 67 return null; 68 } 69 if (resourcetype.equals("Global")) { 71 oname = new ObjectName (domain + NAMINGRESOURCES_TYPE + 72 GLOBAL_TYPE); 73 } else if (resourcetype.equals("Context")) { 74 oname = new ObjectName (domain + NAMINGRESOURCES_TYPE + 75 CONTEXT_TYPE + 76 ",path=" + path + ",host=" + host); 77 } else if (resourcetype.equals("DefaultContext")) { 78 if (host.length() > 0) { 79 oname = new ObjectName (domain + NAMINGRESOURCES_TYPE + 80 HOST_DEFAULTCONTEXT_TYPE + 81 ",host=" + host); 82 } else { 83 oname = new ObjectName (domain + NAMINGRESOURCES_TYPE + 84 SERVICE_DEFAULTCONTEXT_TYPE); 85 } 86 } 87 88 return oname; 89 90 } 91 92 100 public static EnvEntriesForm getEnvEntriesForm(MBeanServer mserver, 101 String resourcetype, String path, String host, String domain) 102 throws Exception { 103 104 ObjectName ename = null; 105 StringBuffer sb = null; 106 if (resourcetype!=null) { 107 if (resourcetype.equals("Global")) { 108 ename = new ObjectName ( domain + ENVIRONMENT_TYPE + 109 GLOBAL_TYPE + ",*"); 110 } else if (resourcetype.equals("Context")) { 111 ename = new ObjectName ( domain + ENVIRONMENT_TYPE + 112 CONTEXT_TYPE + ",path=" + path + 113 ",host=" + host + ",*"); 114 } else if (resourcetype.equals("DefaultContext")) { 115 if (host.length() > 0) { 116 ename = new ObjectName ( domain + ENVIRONMENT_TYPE + 117 HOST_DEFAULTCONTEXT_TYPE + ",host=" + host + ",*"); 118 } else { 119 ename = new ObjectName ( domain + ENVIRONMENT_TYPE + 120 SERVICE_DEFAULTCONTEXT_TYPE + ",*"); 121 } 122 } 123 } 124 125 Iterator iterator = (mserver.queryMBeans(ename, null).iterator()); 126 127 ArrayList results = new ArrayList (); 128 while (iterator.hasNext()) { 129 ObjectInstance instance = (ObjectInstance ) iterator.next(); 130 results.add(instance.getObjectName().toString()); 131 } 132 133 Collections.sort(results); 134 135 EnvEntriesForm envEntriesForm = new EnvEntriesForm(); 136 envEntriesForm.setEnvEntries((String []) 137 results.toArray(new String [results.size()])); 138 139 if (resourcetype != null) { 140 envEntriesForm.setResourcetype(resourcetype); 141 } else { 142 envEntriesForm.setResourcetype(""); 143 } 144 if (path != null) { 145 envEntriesForm.setPath(path); 146 } else { 147 envEntriesForm.setPath(""); 148 } 149 if (host != null) { 150 envEntriesForm.setHost(host); 151 } else { 152 envEntriesForm.setHost(""); 153 } 154 if (domain != null) { 155 envEntriesForm.setDomain(domain); 156 } else { 157 envEntriesForm.setDomain(""); 158 } 159 160 return (envEntriesForm); 161 162 } 163 164 172 public static DataSourcesForm getDataSourcesForm(MBeanServer mserver, 173 String resourcetype, String path, String host, String domain) 174 throws Exception { 175 176 ObjectName rname = null; 177 if (resourcetype!=null) { 178 if (resourcetype.equals("Global")) { 179 rname = new ObjectName ( domain + RESOURCE_TYPE + GLOBAL_TYPE + 180 ",class=" + DATASOURCE_CLASS + ",*"); 181 } else if (resourcetype.equals("Context")) { 182 rname = new ObjectName ( domain + RESOURCE_TYPE + CONTEXT_TYPE + 183 ",path=" + path + ",host=" + host + ",class=" + 184 DATASOURCE_CLASS + ",*"); 185 } else if (resourcetype.equals("DefaultContext")) { 186 if (host.length() > 0) { 187 rname = new ObjectName ( domain + RESOURCE_TYPE + 188 HOST_DEFAULTCONTEXT_TYPE + ",host=" + host + 189 ",class=" + DATASOURCE_CLASS + ",*"); 190 } else { 191 rname = new ObjectName ( domain + RESOURCE_TYPE + 192 SERVICE_DEFAULTCONTEXT_TYPE + 193 ",class=" + DATASOURCE_CLASS + ",*"); 194 } 195 } 196 } 197 Iterator iterator = (mserver.queryMBeans(rname, null).iterator()); 198 199 ArrayList results = new ArrayList (); 200 while (iterator.hasNext()) { 201 202 ObjectInstance instance = (ObjectInstance ) iterator.next(); 203 ObjectName oname = instance.getObjectName(); 204 try { 205 mserver.getAttribute(oname, "driverClassName"); 207 results.add(oname.toString()); 208 } catch (AttributeNotFoundException ex) { 209 mserver.setAttribute(oname, 210 new Attribute ("driverClassName", "")); 211 } 212 } 213 214 Collections.sort(results); 215 DataSourcesForm dataSourcesForm = new DataSourcesForm(); 216 dataSourcesForm.setDataSources((String []) 217 results.toArray(new String [results.size()])); 218 219 if (resourcetype != null) { 220 dataSourcesForm.setResourcetype(resourcetype); 221 } else { 222 dataSourcesForm.setResourcetype(""); 223 } 224 if (path != null) { 225 dataSourcesForm.setPath(path); 226 } else { 227 dataSourcesForm.setPath(""); 228 } 229 if (host != null) { 230 dataSourcesForm.setHost(host); 231 } else { 232 dataSourcesForm.setHost(""); 233 } 234 if (domain != null) { 235 dataSourcesForm.setDomain(domain); 236 } else { 237 dataSourcesForm.setDomain(""); 238 } 239 return (dataSourcesForm); 240 241 } 242 243 251 public static MailSessionsForm getMailSessionsForm(MBeanServer mserver, 252 String resourcetype, String path, String host, String domain) 253 throws Exception { 254 255 ObjectName rname = null; 256 if (resourcetype!=null) { 257 if (resourcetype.equals("Global")) { 258 rname = new ObjectName ( domain + RESOURCE_TYPE + GLOBAL_TYPE + 259 ",class=" + MAILSESSION_CLASS + ",*"); 260 } else if (resourcetype.equals("Context")) { 261 rname = new ObjectName (domain + RESOURCE_TYPE + CONTEXT_TYPE + 262 ",path=" + path + ",host=" + host + ",class=" + 263 MAILSESSION_CLASS + ",*"); 264 } else if (resourcetype.equals("DefaultContext")) { 265 if (host.length() > 0) { 266 rname = new ObjectName (domain + RESOURCE_TYPE + 267 HOST_DEFAULTCONTEXT_TYPE + ",host=" + host + 268 ",class=" + MAILSESSION_CLASS + ",*"); 269 } else { 270 rname = new ObjectName (domain + RESOURCE_TYPE + 271 SERVICE_DEFAULTCONTEXT_TYPE + 272 ",class=" + MAILSESSION_CLASS + ",*"); 273 } 274 } 275 } 276 277 Iterator iterator = (mserver.queryMBeans(rname, null).iterator()); 278 279 ArrayList results = new ArrayList (); 280 while (iterator.hasNext()) { 281 282 ObjectInstance instance = (ObjectInstance ) iterator.next(); 283 results.add(instance.getObjectName().toString()); 284 } 285 286 Collections.sort(results); 287 MailSessionsForm mailSessionsForm = new MailSessionsForm(); 288 mailSessionsForm.setMailSessions((String []) 289 results.toArray(new String [results.size()])); 290 291 if (resourcetype != null) { 292 mailSessionsForm.setResourcetype(resourcetype); 293 } else { 294 mailSessionsForm.setResourcetype(""); 295 } 296 if (path != null) { 297 mailSessionsForm.setPath(path); 298 } else { 299 mailSessionsForm.setPath(""); 300 } 301 if (host != null) { 302 mailSessionsForm.setHost(host); 303 } else { 304 mailSessionsForm.setHost(""); 305 } 306 if (domain != null) { 307 mailSessionsForm.setDomain(domain); 308 } else { 309 mailSessionsForm.setDomain(""); 310 } 311 312 return (mailSessionsForm); 313 314 } 315 316 324 public static ResourceLinksForm getResourceLinksForm(MBeanServer mserver, 325 String resourcetype, String path, String host, String domain) 326 throws Exception { 327 328 ObjectName rname = null; 329 if (resourcetype!=null) { 330 if (resourcetype.equals("Global")) { 331 rname = new ObjectName ( domain + RESOURCELINK_TYPE + 332 GLOBAL_TYPE + ",*"); 333 } else if (resourcetype.equals("Context")) { 334 rname = new ObjectName ( domain + RESOURCELINK_TYPE + 335 CONTEXT_TYPE + ",path=" + path + 336 ",host=" + host + ",*"); 337 } else if (resourcetype.equals("DefaultContext")) { 338 if (host.length() > 0) { 339 rname = new ObjectName ( domain + RESOURCELINK_TYPE + 340 HOST_DEFAULTCONTEXT_TYPE + 341 ",host=" + host + ",*"); 342 } else { 343 rname = new ObjectName ( domain + RESOURCELINK_TYPE + 344 SERVICE_DEFAULTCONTEXT_TYPE + ",*"); 345 } 346 } 347 } 348 349 Iterator iterator = (mserver.queryMBeans(rname, null).iterator()); 350 351 ArrayList results = new ArrayList (); 352 while (iterator.hasNext()) { 353 ObjectInstance instance = (ObjectInstance ) iterator.next(); 354 results.add(instance.getObjectName().toString()); 355 } 356 357 Collections.sort(results); 358 ResourceLinksForm resourceLinksForm = new ResourceLinksForm(); 359 resourceLinksForm.setResourceLinks((String []) 360 results.toArray(new String [results.size()])); 361 362 if (resourcetype != null) { 363 resourceLinksForm.setResourcetype(resourcetype); 364 } else { 365 resourceLinksForm.setResourcetype(""); 366 } 367 if (path != null) { 368 resourceLinksForm.setPath(path); 369 } else { 370 resourceLinksForm.setPath(""); 371 } 372 if (host != null) { 373 resourceLinksForm.setHost(host); 374 } else { 375 resourceLinksForm.setHost(""); 376 } 377 if (domain != null) { 378 resourceLinksForm.setDomain(domain); 379 } else { 380 resourceLinksForm.setDomain(""); 381 } 382 383 return (resourceLinksForm); 384 385 } 386 387 395 public static UserDatabasesForm getUserDatabasesForm(MBeanServer mserver,String domain) 396 throws Exception { 397 398 ObjectName rname = new ObjectName ( domain + RESOURCE_TYPE + GLOBAL_TYPE + 399 ",class=" + USERDB_CLASS + ",*"); 400 401 Iterator iterator = (mserver.queryMBeans(rname, null).iterator()); 402 403 ArrayList results = new ArrayList (); 404 while (iterator.hasNext()) { 405 ObjectInstance instance = (ObjectInstance ) iterator.next(); 406 results.add(instance.getObjectName().toString()); 407 } 408 409 Collections.sort(results); 410 411 UserDatabasesForm userDatabasesForm = new UserDatabasesForm(); 412 userDatabasesForm.setUserDatabases((String []) 413 results.toArray(new String [results.size()])); 414 return (userDatabasesForm); 415 416 } 417 418 } 419 | Popular Tags |