001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.mbeans;
018:
019: import javax.management.Attribute;
020: import javax.management.AttributeNotFoundException;
021: import javax.management.InstanceNotFoundException;
022: import javax.management.MBeanException;
023: import javax.management.ReflectionException;
024: import javax.management.RuntimeOperationsException;
025: import javax.management.modelmbean.InvalidTargetObjectTypeException;
026:
027: import org.apache.catalina.deploy.ContextResource;
028: import org.apache.catalina.deploy.NamingResources;
029: import org.apache.catalina.deploy.ResourceParams;
030: import org.apache.commons.modeler.BaseModelMBean;
031:
032: /**
033: * <p>A <strong>ModelMBean</strong> implementation for the
034: * <code>org.apache.catalina.deploy.ContextResource</code> component.</p>
035: *
036: * @author Amy Roh
037: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:44 $
038: */
039:
040: public class ContextResourceMBean extends BaseModelMBean {
041:
042: // ----------------------------------------------------------- Constructors
043:
044: /**
045: * Construct a <code>ModelMBean</code> with default
046: * <code>ModelMBeanInfo</code> information.
047: *
048: * @exception MBeanException if the initializer of an object
049: * throws an exception
050: * @exception RuntimeOperationsException if an IllegalArgumentException
051: * occurs
052: */
053: public ContextResourceMBean() throws MBeanException,
054: RuntimeOperationsException {
055:
056: super ();
057:
058: }
059:
060: // ----------------------------------------------------- Instance Variables
061:
062: // ------------------------------------------------------------- Attributes
063:
064: /**
065: * Obtain and return the value of a specific attribute of this MBean.
066: *
067: * @param name Name of the requested attribute
068: *
069: * @exception AttributeNotFoundException if this attribute is not
070: * supported by this MBean
071: * @exception MBeanException if the initializer of an object
072: * throws an exception
073: * @exception ReflectionException if a Java reflection exception
074: * occurs when invoking the getter
075: */
076: public Object getAttribute(String name)
077: throws AttributeNotFoundException, MBeanException,
078: ReflectionException {
079:
080: // Validate the input parameters
081: if (name == null)
082: throw new RuntimeOperationsException(
083: new IllegalArgumentException(
084: "Attribute name is null"),
085: "Attribute name is null");
086:
087: ContextResource cr = null;
088: try {
089: cr = (ContextResource) getManagedResource();
090: } catch (InstanceNotFoundException e) {
091: throw new MBeanException(e);
092: } catch (InvalidTargetObjectTypeException e) {
093: throw new MBeanException(e);
094: }
095:
096: String value = null;
097: if ("auth".equals(name)) {
098: return (cr.getAuth());
099: } else if ("description".equals(name)) {
100: return (cr.getDescription());
101: } else if ("name".equals(name)) {
102: return (cr.getName());
103: } else if ("scope".equals(name)) {
104: return (cr.getScope());
105: } else if ("type".equals(name)) {
106: return (cr.getType());
107: } else {
108: NamingResources nr = cr.getNamingResources();
109: if (nr == null) {
110: throw new AttributeNotFoundException(
111: "Cannot find naming resource " + cr.getName());
112: }
113: ResourceParams rp = nr.findResourceParams(cr.getName());
114: if (rp == null) {
115: throw new AttributeNotFoundException(
116: "Cannot find resource param " + cr.getName());
117: }
118: value = (String) rp.getParameters().get(name);
119: if (value == null) {
120: throw new AttributeNotFoundException(
121: "Cannot find attribute " + name + rp);
122: }
123: }
124:
125: return value;
126:
127: }
128:
129: /**
130: * Set the value of a specific attribute of this MBean.
131: *
132: * @param attribute The identification of the attribute to be set
133: * and the new value
134: *
135: * @exception AttributeNotFoundException if this attribute is not
136: * supported by this MBean
137: * @exception MBeanException if the initializer of an object
138: * throws an exception
139: * @exception ReflectionException if a Java reflection exception
140: * occurs when invoking the getter
141: */
142: public void setAttribute(Attribute attribute)
143: throws AttributeNotFoundException, MBeanException,
144: ReflectionException {
145:
146: // Validate the input parameters
147: if (attribute == null)
148: throw new RuntimeOperationsException(
149: new IllegalArgumentException("Attribute is null"),
150: "Attribute is null");
151: String name = attribute.getName();
152: Object value = attribute.getValue();
153: if (name == null)
154: throw new RuntimeOperationsException(
155: new IllegalArgumentException(
156: "Attribute name is null"),
157: "Attribute name is null");
158:
159: ContextResource cr = null;
160: try {
161: cr = (ContextResource) getManagedResource();
162: } catch (InstanceNotFoundException e) {
163: throw new MBeanException(e);
164: } catch (InvalidTargetObjectTypeException e) {
165: throw new MBeanException(e);
166: }
167:
168: if ("auth".equals(name)) {
169: cr.setAuth((String) value);
170: } else if ("description".equals(name)) {
171: cr.setDescription((String) value);
172: } else if ("name".equals(name)) {
173: cr.setName((String) value);
174: } else if ("scope".equals(name)) {
175: cr.setScope((String) value);
176: } else if ("type".equals(name)) {
177: cr.setType((String) value);
178: } else {
179: ResourceParams rp = cr.getNamingResources()
180: .findResourceParams(cr.getName());
181: if (rp != null) {
182: String valueStr = "" + value;
183: rp.addParameter(name, valueStr);
184: cr.getNamingResources().removeResourceParams(
185: cr.getName());
186: } else {
187: rp = new ResourceParams();
188: rp.setName(cr.getName());
189: String valueStr = "" + value;
190: rp.addParameter(name, valueStr);
191: }
192: cr.getNamingResources().addResourceParams(rp);
193: }
194:
195: // cannot use side-efects. It's removed and added back each time
196: // there is a modification in a resource.
197: NamingResources nr = cr.getNamingResources();
198: nr.removeResource(cr.getName());
199: nr.addResource(cr);
200: }
201:
202: }
|