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.deploy.ContextEnvironment; 28 import org.apache.catalina.deploy.ContextResource; 29 import org.apache.catalina.deploy.ContextResourceLink; 30 import org.apache.catalina.deploy.NamingResources; 31 import org.apache.tomcat.util.modeler.BaseModelMBean; 32 import org.apache.tomcat.util.modeler.ManagedBean; 33 import org.apache.tomcat.util.modeler.Registry; 34 35 42 43 public class NamingResourcesMBean extends BaseModelMBean { 44 45 46 48 49 58 public NamingResourcesMBean() 59 throws MBeanException , RuntimeOperationsException { 60 61 super(); 62 63 } 64 65 66 68 71 protected Registry registry = MBeanUtils.createRegistry(); 72 73 74 77 protected ManagedBean managed = 78 registry.findManagedBean("NamingResources"); 79 80 82 83 87 public String [] getEnvironments() { 88 ContextEnvironment[] envs = 89 ((NamingResources)this.resource).findEnvironments(); 90 ArrayList results = new ArrayList (); 91 for (int i = 0; i < envs.length; i++) { 92 try { 93 ObjectName oname = 94 MBeanUtils.createObjectName(managed.getDomain(), envs[i]); 95 results.add(oname.toString()); 96 } catch (MalformedObjectNameException e) { 97 IllegalArgumentException iae = new IllegalArgumentException 98 ("Cannot create object name for environment " + envs[i]); 99 iae.initCause(e); 100 throw iae; 101 } 102 } 103 return ((String []) results.toArray(new String [results.size()])); 104 105 } 106 107 108 112 public String [] getResources() { 113 114 ContextResource[] resources = 115 ((NamingResources)this.resource).findResources(); 116 ArrayList results = new ArrayList (); 117 for (int i = 0; i < resources.length; i++) { 118 try { 119 ObjectName oname = 120 MBeanUtils.createObjectName(managed.getDomain(), resources[i]); 121 results.add(oname.toString()); 122 } catch (MalformedObjectNameException e) { 123 IllegalArgumentException iae = new IllegalArgumentException 124 ("Cannot create object name for resource " + resources[i]); 125 iae.initCause(e); 126 throw iae; 127 } 128 } 129 return ((String []) results.toArray(new String [results.size()])); 130 131 } 132 133 134 138 public String [] getResourceLinks() { 139 140 ContextResourceLink[] resourceLinks = 141 ((NamingResources)this.resource).findResourceLinks(); 142 ArrayList results = new ArrayList (); 143 for (int i = 0; i < resourceLinks.length; i++) { 144 try { 145 ObjectName oname = 146 MBeanUtils.createObjectName(managed.getDomain(), resourceLinks[i]); 147 results.add(oname.toString()); 148 } catch (MalformedObjectNameException e) { 149 IllegalArgumentException iae = new IllegalArgumentException 150 ("Cannot create object name for resource " + resourceLinks[i]); 151 iae.initCause(e); 152 throw iae; 153 } 154 } 155 return ((String []) results.toArray(new String [results.size()])); 156 157 } 158 159 161 162 169 public String addEnvironment(String envName, String type, String value) 170 throws MalformedObjectNameException { 171 172 NamingResources nresources = (NamingResources) this.resource; 173 if (nresources == null) { 174 return null; 175 } 176 ContextEnvironment env = nresources.findEnvironment(envName); 177 if (env != null) { 178 throw new IllegalArgumentException 179 ("Invalid environment name - already exists '" + envName + "'"); 180 } 181 env = new ContextEnvironment(); 182 env.setName(envName); 183 env.setType(type); 184 env.setValue(value); 185 nresources.addEnvironment(env); 186 187 ManagedBean managed = registry.findManagedBean("ContextEnvironment"); 189 ObjectName oname = 190 MBeanUtils.createObjectName(managed.getDomain(), env); 191 return (oname.toString()); 192 193 } 194 195 196 202 public String addResource(String resourceName, String type) 203 throws MalformedObjectNameException { 204 205 NamingResources nresources = (NamingResources) this.resource; 206 if (nresources == null) { 207 return null; 208 } 209 ContextResource resource = nresources.findResource(resourceName); 210 if (resource != null) { 211 throw new IllegalArgumentException 212 ("Invalid resource name - already exists'" + resourceName + "'"); 213 } 214 resource = new ContextResource(); 215 resource.setName(resourceName); 216 resource.setType(type); 217 nresources.addResource(resource); 218 219 ManagedBean managed = registry.findManagedBean("ContextResource"); 221 ObjectName oname = 222 MBeanUtils.createObjectName(managed.getDomain(), resource); 223 return (oname.toString()); 224 } 225 226 227 233 public String addResourceLink(String resourceLinkName, String type) 234 throws MalformedObjectNameException { 235 236 NamingResources nresources = (NamingResources) this.resource; 237 if (nresources == null) { 238 return null; 239 } 240 ContextResourceLink resourceLink = 241 nresources.findResourceLink(resourceLinkName); 242 if (resourceLink != null) { 243 throw new IllegalArgumentException 244 ("Invalid resource link name - already exists'" + 245 resourceLinkName + "'"); 246 } 247 resourceLink = new ContextResourceLink(); 248 resourceLink.setName(resourceLinkName); 249 resourceLink.setType(type); 250 nresources.addResourceLink(resourceLink); 251 252 ManagedBean managed = registry.findManagedBean("ContextResourceLink"); 254 ObjectName oname = 255 MBeanUtils.createObjectName(managed.getDomain(), resourceLink); 256 return (oname.toString()); 257 } 258 259 260 265 public void removeEnvironment(String envName) { 266 267 NamingResources nresources = (NamingResources) this.resource; 268 if (nresources == null) { 269 return; 270 } 271 ContextEnvironment env = nresources.findEnvironment(envName); 272 if (env == null) { 273 throw new IllegalArgumentException 274 ("Invalid environment name '" + envName + "'"); 275 } 276 nresources.removeEnvironment(envName); 277 278 } 279 280 281 286 public void removeResource(String resourceName) { 287 288 resourceName = ObjectName.unquote(resourceName); 289 NamingResources nresources = (NamingResources) this.resource; 290 if (nresources == null) { 291 return; 292 } 293 ContextResource resource = nresources.findResource(resourceName); 294 if (resource == null) { 295 throw new IllegalArgumentException 296 ("Invalid resource name '" + resourceName + "'"); 297 } 298 nresources.removeResource(resourceName); 299 300 } 301 302 303 308 public void removeResourceLink(String resourceLinkName) { 309 310 resourceLinkName = ObjectName.unquote(resourceLinkName); 311 NamingResources nresources = (NamingResources) this.resource; 312 if (nresources == null) { 313 return; 314 } 315 ContextResourceLink resourceLink = 316 nresources.findResourceLink(resourceLinkName); 317 if (resourceLink == null) { 318 throw new IllegalArgumentException 319 ("Invalid resource Link name '" + resourceLinkName + "'"); 320 } 321 nresources.removeResourceLink(resourceLinkName); 322 } 323 324 } 325 | Popular Tags |