KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > mbeans > ContextResourceMBean


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
18 package org.apache.catalina.mbeans;
19
20
21 import javax.management.Attribute JavaDoc;
22 import javax.management.AttributeNotFoundException JavaDoc;
23 import javax.management.InstanceNotFoundException JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.ReflectionException JavaDoc;
26 import javax.management.RuntimeOperationsException JavaDoc;
27 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
28
29 import org.apache.catalina.deploy.ContextResource;
30 import org.apache.catalina.deploy.NamingResources;
31 import org.apache.tomcat.util.modeler.BaseModelMBean;
32
33
34 /**
35  * <p>A <strong>ModelMBean</strong> implementation for the
36  * <code>org.apache.catalina.deploy.ContextResource</code> component.</p>
37  *
38  * @author Amy Roh
39  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
40  */

41
42 public class ContextResourceMBean extends BaseModelMBean {
43
44
45     // ----------------------------------------------------------- Constructors
46

47
48     /**
49      * Construct a <code>ModelMBean</code> with default
50      * <code>ModelMBeanInfo</code> information.
51      *
52      * @exception MBeanException if the initializer of an object
53      * throws an exception
54      * @exception RuntimeOperationsException if an IllegalArgumentException
55      * occurs
56      */

57     public ContextResourceMBean()
58         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
59
60         super();
61
62     }
63
64
65     // ----------------------------------------------------- Instance Variables
66

67
68     // ------------------------------------------------------------- Attributes
69

70
71     /**
72      * Obtain and return the value of a specific attribute of this MBean.
73      *
74      * @param name Name of the requested attribute
75      *
76      * @exception AttributeNotFoundException if this attribute is not
77      * supported by this MBean
78      * @exception MBeanException if the initializer of an object
79      * throws an exception
80      * @exception ReflectionException if a Java reflection exception
81      * occurs when invoking the getter
82      */

83     public Object JavaDoc getAttribute(String JavaDoc name)
84         throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
85         ReflectionException JavaDoc {
86  
87         // Validate the input parameters
88
if (name == null)
89             throw new RuntimeOperationsException JavaDoc
90                 (new IllegalArgumentException JavaDoc("Attribute name is null"),
91                  "Attribute name is null");
92
93         ContextResource cr = null;
94         try {
95             cr = (ContextResource) getManagedResource();
96         } catch (InstanceNotFoundException JavaDoc e) {
97             throw new MBeanException JavaDoc(e);
98         } catch (InvalidTargetObjectTypeException JavaDoc e) {
99              throw new MBeanException JavaDoc(e);
100         }
101         
102         String JavaDoc value = null;
103         if ("auth".equals(name)) {
104             return (cr.getAuth());
105         } else if ("description".equals(name)) {
106             return (cr.getDescription());
107         } else if ("name".equals(name)) {
108             return (cr.getName());
109         } else if ("scope".equals(name)) {
110             return (cr.getScope());
111         } else if ("type".equals(name)) {
112             return (cr.getType());
113         } else {
114             value = (String JavaDoc) cr.getProperty(name);
115             if (value == null) {
116                 throw new AttributeNotFoundException JavaDoc
117                     ("Cannot find attribute "+name);
118             }
119         }
120         
121         return value;
122         
123     }
124
125     
126     /**
127      * Set the value of a specific attribute of this MBean.
128      *
129      * @param attribute The identification of the attribute to be set
130      * and the new value
131      *
132      * @exception AttributeNotFoundException if this attribute is not
133      * supported by this MBean
134      * @exception MBeanException if the initializer of an object
135      * throws an exception
136      * @exception ReflectionException if a Java reflection exception
137      * occurs when invoking the getter
138      */

139      public void setAttribute(Attribute JavaDoc attribute)
140         throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
141         ReflectionException JavaDoc {
142
143         // Validate the input parameters
144
if (attribute == null)
145             throw new RuntimeOperationsException JavaDoc
146                 (new IllegalArgumentException JavaDoc("Attribute is null"),
147                  "Attribute is null");
148         String JavaDoc name = attribute.getName();
149         Object JavaDoc value = attribute.getValue();
150         if (name == null)
151             throw new RuntimeOperationsException JavaDoc
152                 (new IllegalArgumentException JavaDoc("Attribute name is null"),
153                  "Attribute name is null");
154         
155         ContextResource cr = null;
156         try {
157             cr = (ContextResource) getManagedResource();
158         } catch (InstanceNotFoundException JavaDoc e) {
159             throw new MBeanException JavaDoc(e);
160         } catch (InvalidTargetObjectTypeException JavaDoc e) {
161              throw new MBeanException JavaDoc(e);
162         }
163         
164         if ("auth".equals(name)) {
165             cr.setAuth((String JavaDoc)value);
166         } else if ("description".equals(name)) {
167             cr.setDescription((String JavaDoc)value);
168         } else if ("name".equals(name)) {
169             cr.setName((String JavaDoc)value);
170         } else if ("scope".equals(name)) {
171             cr.setScope((String JavaDoc)value);
172         } else if ("type".equals(name)) {
173             cr.setType((String JavaDoc)value);
174         } else {
175             cr.setProperty(name, ""+value);
176         }
177         
178         // cannot use side-efects. It's removed and added back each time
179
// there is a modification in a resource.
180
NamingResources nr = cr.getNamingResources();
181         nr.removeResource(cr.getName());
182         nr.addResource(cr);
183     }
184     
185 }
186
Popular Tags