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.Context; 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 DefaultContextMBean extends BaseModelMBean { 45 46 47 49 50 59 public DefaultContextMBean() 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("DefaultContext"); 80 81 82 84 85 88 private NamingResources getNamingResources() { 89 90 return ((Context)this.resource).getNamingResources(); 91 92 } 93 94 95 99 public String [] getEnvironments() { 100 ContextEnvironment[] envs = getNamingResources().findEnvironments(); 101 ArrayList results = new ArrayList (); 102 for (int i = 0; i < envs.length; i++) { 103 try { 104 ObjectName oname = 105 MBeanUtils.createObjectName(managed.getDomain(), envs[i]); 106 results.add(oname.toString()); 107 } catch (MalformedObjectNameException e) { 108 IllegalArgumentException iae = new IllegalArgumentException 109 ("Cannot create object name for environment " + envs[i]); 110 iae.initCause(e); 111 throw iae; 112 } 113 } 114 return ((String []) results.toArray(new String [results.size()])); 115 116 } 117 118 119 123 public String [] getResources() { 124 125 ContextResource[] resources = getNamingResources().findResources(); 126 ArrayList results = new ArrayList (); 127 for (int i = 0; i < resources.length; i++) { 128 try { 129 ObjectName oname = 130 MBeanUtils.createObjectName(managed.getDomain(), resources[i]); 131 results.add(oname.toString()); 132 } catch (MalformedObjectNameException e) { 133 IllegalArgumentException iae = new IllegalArgumentException 134 ("Cannot create object name for resource " + resources[i]); 135 iae.initCause(e); 136 throw iae; 137 } 138 } 139 return ((String []) results.toArray(new String [results.size()])); 140 141 } 142 143 144 148 public String [] getResourceLinks() { 149 150 ContextResourceLink[] links = getNamingResources().findResourceLinks(); 151 ArrayList results = new ArrayList (); 152 for (int i = 0; i < links.length; i++) { 153 try { 154 ObjectName oname = 155 MBeanUtils.createObjectName(managed.getDomain(), links[i]); 156 results.add(oname.toString()); 157 } catch (MalformedObjectNameException e) { 158 IllegalArgumentException iae = new IllegalArgumentException 159 ("Cannot create object name for resource " + links[i]); 160 iae.initCause(e); 161 throw iae; 162 } 163 } 164 return ((String []) results.toArray(new String [results.size()])); 165 166 } 167 168 170 171 176 public String addEnvironment(String envName, String type) 177 throws MalformedObjectNameException { 178 179 NamingResources nresources = getNamingResources(); 180 if (nresources == null) { 181 return null; 182 } 183 ContextEnvironment env = nresources.findEnvironment(envName); 184 if (env != null) { 185 throw new IllegalArgumentException 186 ("Invalid environment name - already exists '" + envName + "'"); 187 } 188 env = new ContextEnvironment(); 189 env.setName(envName); 190 env.setType(type); 191 nresources.addEnvironment(env); 192 193 ManagedBean managed = registry.findManagedBean("ContextEnvironment"); 195 ObjectName oname = 196 MBeanUtils.createObjectName(managed.getDomain(), env); 197 return (oname.toString()); 198 199 } 200 201 202 207 public String addResource(String resourceName, String type) 208 throws MalformedObjectNameException { 209 210 NamingResources nresources = getNamingResources(); 211 if (nresources == null) { 212 return null; 213 } 214 ContextResource resource = nresources.findResource(resourceName); 215 if (resource != null) { 216 throw new IllegalArgumentException 217 ("Invalid resource name - already exists'" + resourceName + "'"); 218 } 219 resource = new ContextResource(); 220 resource.setName(resourceName); 221 resource.setType(type); 222 nresources.addResource(resource); 223 224 ManagedBean managed = registry.findManagedBean("ContextResource"); 226 ObjectName oname = 227 MBeanUtils.createObjectName(managed.getDomain(), resource); 228 229 return (oname.toString()); 230 } 231 232 233 238 public String addResourceLink(String resourceLinkName, String global, 239 String name, String type) throws MalformedObjectNameException { 240 241 NamingResources nresources = getNamingResources(); 242 if (nresources == null) { 243 return null; 244 } 245 ContextResourceLink resourceLink = 246 nresources.findResourceLink(resourceLinkName); 247 if (resourceLink != null) { 248 throw new IllegalArgumentException 249 ("Invalid resource link name - already exists'" + 250 resourceLinkName + "'"); 251 } 252 resourceLink = new ContextResourceLink(); 253 resourceLink.setGlobal(global); 254 resourceLink.setName(resourceLinkName); 255 resourceLink.setType(type); 256 nresources.addResourceLink(resourceLink); 257 258 ManagedBean managed = registry.findManagedBean("ContextResourceLink"); 260 ObjectName oname = 261 MBeanUtils.createObjectName(managed.getDomain(), resourceLink); 262 return (oname.toString()); 263 } 264 265 266 271 public void removeEnvironment(String envName) { 272 273 NamingResources nresources = getNamingResources(); 274 if (nresources == null) { 275 return; 276 } 277 ContextEnvironment env = nresources.findEnvironment(envName); 278 if (env == null) { 279 throw new IllegalArgumentException 280 ("Invalid environment name '" + envName + "'"); 281 } 282 nresources.removeEnvironment(envName); 283 284 } 285 286 287 292 public void removeResource(String resourceName) { 293 294 resourceName = ObjectName.unquote(resourceName); 295 NamingResources nresources = getNamingResources(); 296 if (nresources == null) { 297 return; 298 } 299 ContextResource resource = nresources.findResource(resourceName); 300 if (resource == null) { 301 throw new IllegalArgumentException 302 ("Invalid resource name '" + resourceName + "'"); 303 } 304 nresources.removeResource(resourceName); 305 } 306 307 308 313 public void removeResourceLink(String resourceLinkName) { 314 315 resourceLinkName = ObjectName.unquote(resourceLinkName); 316 NamingResources nresources = getNamingResources(); 317 if (nresources == null) { 318 return; 319 } 320 ContextResourceLink resource = nresources.findResourceLink(resourceLinkName); 321 if (resource == null) { 322 throw new IllegalArgumentException 323 ("Invalid resource name '" + resourceLinkName + "'"); 324 } 325 nresources.removeResourceLink(resourceLinkName); 326 } 327 328 329 } 330 | Popular Tags |