1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.catalina.mbeans;
18
19 import javax.management.Attribute;
20 import javax.management.AttributeNotFoundException;
21 import javax.management.MBeanException;
22 import javax.management.ReflectionException;
23 import javax.management.RuntimeOperationsException;
24
25 import org.apache.tomcat.util.descriptor.web.ContextResource;
26 import org.apache.tomcat.util.descriptor.web.NamingResources;
27 import org.apache.tomcat.util.res.StringManager;
28
29 /**
30 * <p>A <strong>ModelMBean</strong> implementation for the
31 * <code>org.apache.tomcat.util.descriptor.web.ContextResource</code> component.</p>
32 *
33 * @author Amy Roh
34 */
35 public class ContextResourceMBean extends BaseCatalinaMBean<ContextResource> {
36
37 private static final StringManager sm = StringManager.getManager(ContextResourceMBean.class);
38
39 /**
40 * Obtain and return the value of a specific attribute of this MBean.
41 *
42 * @param name Name of the requested attribute
43 *
44 * @exception AttributeNotFoundException if this attribute is not
45 * supported by this MBean
46 * @exception MBeanException if the initializer of an object
47 * throws an exception
48 * @exception ReflectionException if a Java reflection exception
49 * occurs when invoking the getter
50 */
51 @Override
52 public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException,
53 ReflectionException {
54
55 // Validate the input parameters
56 if (name == null) {
57 throw new RuntimeOperationsException(
58 new IllegalArgumentException(sm.getString("mBean.nullName")),
59 sm.getString("mBean.nullName"));
60 }
61
62 ContextResource cr = doGetManagedResource();
63
64 String value = null;
65 if ("auth".equals(name)) {
66 return cr.getAuth();
67 } else if ("description".equals(name)) {
68 return cr.getDescription();
69 } else if ("name".equals(name)) {
70 return cr.getName();
71 } else if ("scope".equals(name)) {
72 return cr.getScope();
73 } else if ("type".equals(name)) {
74 return cr.getType();
75 } else {
76 value = (String) cr.getProperty(name);
77 if (value == null) {
78 throw new AttributeNotFoundException(sm.getString("mBean.attributeNotFound", name));
79 }
80 }
81
82 return value;
83 }
84
85
86 /**
87 * Set the value of a specific attribute of this MBean.
88 *
89 * @param attribute The identification of the attribute to be set
90 * and the new value
91 *
92 * @exception AttributeNotFoundException if this attribute is not
93 * supported by this MBean
94 * @exception MBeanException if the initializer of an object
95 * throws an exception
96 * @exception ReflectionException if a Java reflection exception
97 * occurs when invoking the getter
98 */
99 @Override
100 public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException,
101 ReflectionException {
102
103 // Validate the input parameters
104 if (attribute == null) {
105 throw new RuntimeOperationsException(
106 new IllegalArgumentException(sm.getString("mBean.nullAttribute")),
107 sm.getString("mBean.nullAttribute"));
108 }
109 String name = attribute.getName();
110 Object value = attribute.getValue();
111 if (name == null) {
112 throw new RuntimeOperationsException(
113 new IllegalArgumentException(sm.getString("mBean.nullName")),
114 sm.getString("mBean.nullName"));
115 }
116
117 ContextResource cr = doGetManagedResource();
118
119 if ("auth".equals(name)) {
120 cr.setAuth((String)value);
121 } else if ("description".equals(name)) {
122 cr.setDescription((String)value);
123 } else if ("name".equals(name)) {
124 cr.setName((String)value);
125 } else if ("scope".equals(name)) {
126 cr.setScope((String)value);
127 } else if ("type".equals(name)) {
128 cr.setType((String)value);
129 } else {
130 cr.setProperty(name, "" + value);
131 }
132
133 // cannot use side-effects. It's removed and added back each time
134 // there is a modification in a resource.
135 NamingResources nr = cr.getNamingResources();
136 nr.removeResource(cr.getName());
137 nr.addResource(cr);
138 }
139 }
140