1 17 18 package org.apache.catalina.mbeans; 19 20 import java.util.ArrayList ; 21 22 import javax.management.MBeanException ; 23 import javax.management.MalformedObjectNameException ; 24 import javax.management.ObjectName ; 25 import javax.management.RuntimeOperationsException ; 26 27 import org.apache.catalina.core.StandardContext; 28 import org.apache.catalina.deploy.ContextEnvironment; 29 import org.apache.catalina.deploy.ContextResource; 30 import org.apache.catalina.deploy.ContextResourceLink; 31 import org.apache.catalina.deploy.NamingResources; 32 import org.apache.tomcat.util.modeler.BaseModelMBean; 33 import org.apache.tomcat.util.modeler.ManagedBean; 34 import org.apache.tomcat.util.modeler.Registry; 35 36 43 44 public class StandardContextMBean extends BaseModelMBean { 45 46 47 49 50 59 public StandardContextMBean() 60 throws MBeanException , RuntimeOperationsException { 61 62 super(); 63 64 } 65 66 67 69 70 73 protected Registry registry = MBeanUtils.createRegistry(); 74 75 78 protected ManagedBean managed = 79 registry.findManagedBean("StandardContext"); 80 81 82 84 85 88 private NamingResources getNamingResources() { 89 90 return ((StandardContext)this.resource).getNamingResources(); 91 92 } 93 94 97 public void reload() { 98 99 ((StandardContext)this.resource).reload(); 100 101 } 102 103 104 108 public String [] getEnvironments() { 109 ContextEnvironment[] envs = getNamingResources().findEnvironments(); 110 ArrayList results = new ArrayList (); 111 for (int i = 0; i < envs.length; i++) { 112 try { 113 ObjectName oname = 114 MBeanUtils.createObjectName(managed.getDomain(), envs[i]); 115 results.add(oname.toString()); 116 } catch (MalformedObjectNameException e) { 117 IllegalArgumentException iae = new IllegalArgumentException 118 ("Cannot create object name for environment " + envs[i]); 119 iae.initCause(e); 120 throw iae; 121 } 122 } 123 return ((String []) results.toArray(new String [results.size()])); 124 125 } 126 127 128 132 public String [] getResources() { 133 134 ContextResource[] resources = getNamingResources().findResources(); 135 ArrayList results = new ArrayList (); 136 for (int i = 0; i < resources.length; i++) { 137 try { 138 ObjectName oname = 139 MBeanUtils.createObjectName(managed.getDomain(), resources[i]); 140 results.add(oname.toString()); 141 } catch (MalformedObjectNameException e) { 142 IllegalArgumentException iae = new IllegalArgumentException 143 ("Cannot create object name for resource " + resources[i]); 144 iae.initCause(e); 145 throw iae; 146 } 147 } 148 return ((String []) results.toArray(new String [results.size()])); 149 150 } 151 152 153 157 public String [] getResourceLinks() { 158 159 ContextResourceLink[] links = getNamingResources().findResourceLinks(); 160 ArrayList results = new ArrayList (); 161 for (int i = 0; i < links.length; i++) { 162 try { 163 ObjectName oname = 164 MBeanUtils.createObjectName(managed.getDomain(), links[i]); 165 results.add(oname.toString()); 166 } catch (MalformedObjectNameException e) { 167 IllegalArgumentException iae = new IllegalArgumentException 168 ("Cannot create object name for resource " + links[i]); 169 iae.initCause(e); 170 throw iae; 171 } 172 } 173 return ((String []) results.toArray(new String [results.size()])); 174 175 } 176 177 178 181 public javax.naming.directory.DirContext getStaticResources() { 182 183 return ((StandardContext)this.resource).getResources(); 184 185 } 186 187 188 191 public String [] getWelcomeFiles() { 192 193 return ((StandardContext)this.resource).findWelcomeFiles(); 194 195 } 196 197 198 200 201 206 public String addEnvironment(String envName, String type) 207 throws MalformedObjectNameException { 208 209 NamingResources nresources = getNamingResources(); 210 if (nresources == null) { 211 return null; 212 } 213 ContextEnvironment env = nresources.findEnvironment(envName); 214 if (env != null) { 215 throw new IllegalArgumentException 216 ("Invalid environment name - already exists '" + envName + "'"); 217 } 218 env = new ContextEnvironment(); 219 env.setName(envName); 220 env.setType(type); 221 nresources.addEnvironment(env); 222 223 ManagedBean managed = registry.findManagedBean("ContextEnvironment"); 225 ObjectName oname = 226 MBeanUtils.createObjectName(managed.getDomain(), env); 227 return (oname.toString()); 228 229 } 230 231 232 237 public String addResource(String resourceName, String type) 238 throws MalformedObjectNameException { 239 240 NamingResources nresources = getNamingResources(); 241 if (nresources == null) { 242 return null; 243 } 244 ContextResource resource = nresources.findResource(resourceName); 245 if (resource != null) { 246 throw new IllegalArgumentException 247 ("Invalid resource name - already exists'" + resourceName + "'"); 248 } 249 resource = new ContextResource(); 250 resource.setName(resourceName); 251 resource.setType(type); 252 nresources.addResource(resource); 253 254 ManagedBean managed = registry.findManagedBean("ContextResource"); 256 ObjectName oname = 257 MBeanUtils.createObjectName(managed.getDomain(), resource); 258 return (oname.toString()); 259 } 260 261 262 267 public String addResourceLink(String resourceLinkName, String global, 268 String name, String type) throws MalformedObjectNameException { 269 270 NamingResources nresources = getNamingResources(); 271 if (nresources == null) { 272 return null; 273 } 274 ContextResourceLink resourceLink = 275 nresources.findResourceLink(resourceLinkName); 276 if (resourceLink != null) { 277 throw new IllegalArgumentException 278 ("Invalid resource link name - already exists'" + 279 resourceLinkName + "'"); 280 } 281 resourceLink = new ContextResourceLink(); 282 resourceLink.setGlobal(global); 283 resourceLink.setName(resourceLinkName); 284 resourceLink.setType(type); 285 nresources.addResourceLink(resourceLink); 286 287 ManagedBean managed = registry.findManagedBean("ContextResourceLink"); 289 ObjectName oname = 290 MBeanUtils.createObjectName(managed.getDomain(), resourceLink); 291 return (oname.toString()); 292 } 293 294 295 300 public void removeEnvironment(String envName) { 301 302 NamingResources nresources = getNamingResources(); 303 if (nresources == null) { 304 return; 305 } 306 ContextEnvironment env = nresources.findEnvironment(envName); 307 if (env == null) { 308 throw new IllegalArgumentException 309 ("Invalid environment name '" + envName + "'"); 310 } 311 nresources.removeEnvironment(envName); 312 313 } 314 315 316 321 public void removeResource(String resourceName) { 322 323 resourceName = ObjectName.unquote(resourceName); 324 NamingResources nresources = getNamingResources(); 325 if (nresources == null) { 326 return; 327 } 328 ContextResource resource = nresources.findResource(resourceName); 329 if (resource == null) { 330 throw new IllegalArgumentException 331 ("Invalid resource name '" + resourceName + "'"); 332 } 333 nresources.removeResource(resourceName); 334 } 335 336 337 342 public void removeResourceLink(String resourceLinkName) { 343 344 resourceLinkName = ObjectName.unquote(resourceLinkName); 345 NamingResources nresources = getNamingResources(); 346 if (nresources == null) { 347 return; 348 } 349 ContextResourceLink resource = nresources.findResourceLink(resourceLinkName); 350 if (resource == null) { 351 throw new IllegalArgumentException 352 ("Invalid resource name '" + resourceLinkName + "'"); 353 } 354 nresources.removeResourceLink(resourceLinkName); 355 } 356 357 358 } 359 | Popular Tags |