001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.mbeans;
019:
020: import javax.management.Attribute;
021: import javax.management.AttributeNotFoundException;
022: import javax.management.InstanceNotFoundException;
023: import javax.management.MBeanException;
024: import javax.management.ReflectionException;
025: import javax.management.RuntimeOperationsException;
026: import javax.management.modelmbean.InvalidTargetObjectTypeException;
027:
028: import org.apache.catalina.deploy.ContextResource;
029: import org.apache.catalina.deploy.NamingResources;
030: import org.apache.tomcat.util.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: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
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: value = (String) cr.getProperty(name);
109: if (value == null) {
110: throw new AttributeNotFoundException(
111: "Cannot find attribute " + name);
112: }
113: }
114:
115: return value;
116:
117: }
118:
119: /**
120: * Set the value of a specific attribute of this MBean.
121: *
122: * @param attribute The identification of the attribute to be set
123: * and the new value
124: *
125: * @exception AttributeNotFoundException if this attribute is not
126: * supported by this MBean
127: * @exception MBeanException if the initializer of an object
128: * throws an exception
129: * @exception ReflectionException if a Java reflection exception
130: * occurs when invoking the getter
131: */
132: public void setAttribute(Attribute attribute)
133: throws AttributeNotFoundException, MBeanException,
134: ReflectionException {
135:
136: // Validate the input parameters
137: if (attribute == null)
138: throw new RuntimeOperationsException(
139: new IllegalArgumentException("Attribute is null"),
140: "Attribute is null");
141: String name = attribute.getName();
142: Object value = attribute.getValue();
143: if (name == null)
144: throw new RuntimeOperationsException(
145: new IllegalArgumentException(
146: "Attribute name is null"),
147: "Attribute name is null");
148:
149: ContextResource cr = null;
150: try {
151: cr = (ContextResource) getManagedResource();
152: } catch (InstanceNotFoundException e) {
153: throw new MBeanException(e);
154: } catch (InvalidTargetObjectTypeException e) {
155: throw new MBeanException(e);
156: }
157:
158: if ("auth".equals(name)) {
159: cr.setAuth((String) value);
160: } else if ("description".equals(name)) {
161: cr.setDescription((String) value);
162: } else if ("name".equals(name)) {
163: cr.setName((String) value);
164: } else if ("scope".equals(name)) {
165: cr.setScope((String) value);
166: } else if ("type".equals(name)) {
167: cr.setType((String) value);
168: } else {
169: cr.setProperty(name, "" + value);
170: }
171:
172: // cannot use side-efects. It's removed and added back each time
173: // there is a modification in a resource.
174: NamingResources nr = cr.getNamingResources();
175: nr.removeResource(cr.getName());
176: nr.addResource(cr);
177: }
178:
179: }
|