KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > valueaccessor > PropertyValueAccessorBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeans/src/java/com/sun/enterprise/admin/dottedname/valueaccessor/PropertyValueAccessorBase.java,v 1.3 2005/12/25 03:42:07 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:07 $
28  */

29 package com.sun.enterprise.admin.dottedname.valueaccessor;
30
31
32 import com.sun.enterprise.admin.dottedname.DottedNameStrings;
33
34 import javax.management.Attribute JavaDoc;
35 import javax.management.AttributeList JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.MBeanServerConnection JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39 import javax.management.MBeanException JavaDoc;
40 import javax.management.InstanceNotFoundException JavaDoc;
41 import javax.management.AttributeNotFoundException JavaDoc;
42 import javax.management.RuntimeOperationsException JavaDoc;
43 import javax.management.JMException JavaDoc;
44
45 import javax.management.MBeanServerConnection JavaDoc;
46
47 public abstract class PropertyValueAccessorBase extends PrefixedValueAccessorBase
48 {
49     abstract String JavaDoc getGetPropertyMethodName();
50     abstract String JavaDoc getSetPropertyMethodName();
51     abstract String JavaDoc getGetPropertiesMethodName();
52     
53     
54     public PropertyValueAccessorBase(MBeanServerConnection JavaDoc conn, String JavaDoc prefix)
55     {
56         super( conn, prefix);
57     }
58   
59     public Attribute JavaDoc getValue( ObjectName JavaDoc objectName, String JavaDoc valueName )
60         throws java.io.IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc,
61         AttributeNotFoundException JavaDoc
62     {
63         Attribute JavaDoc result = null;
64         try
65         {
66             final Object JavaDoc value = getMBS().invoke( objectName,
67                 getGetPropertyMethodName(), new Object JavaDoc [] { valueName },
68                 new String JavaDoc [] { "java.lang.String" } );
69             result = new Attribute JavaDoc( valueName, value );
70         }
71         catch( MBeanException JavaDoc e )
72         {
73             // method doesn't exist
74
throw new AttributeNotFoundException JavaDoc( DottedNameStrings.getString(DottedNameStrings.ATTRIBUTE_NOT_FOUND_KEY, valueName ));
75         }
76         catch( ReflectionException JavaDoc e )
77         {
78             // method doesn't exist
79
throw new AttributeNotFoundException JavaDoc( DottedNameStrings.getString(DottedNameStrings.ATTRIBUTE_NOT_FOUND_KEY, valueName ));
80         }
81         return( result );
82     }
83     
84     public Attribute JavaDoc
85     setValue( final ObjectName JavaDoc objectName, final Attribute JavaDoc attr ) throws Exception JavaDoc
86     {
87     // NOTE: -Djmx.invoke.getters must be set for setProperty() to succeed
88
// as a method invocation (in the unit tests)
89

90     // note that if attr.getValue() is null, the property will be removed
91
getMBS().invoke( objectName,
92             getSetPropertyMethodName(), new Object JavaDoc [] { attr },
93             new String JavaDoc [] { "javax.management.Attribute" } );
94         
95         return( attr );
96     }
97     
98     
99     public String JavaDoc [] getAllPropertyNames(ObjectName JavaDoc objectName )
100         throws java.io.IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc
101     {
102         return getAllPropertyNames( objectName, false );
103     }
104     public String JavaDoc [] getAllPropertyNames(ObjectName JavaDoc objectName, boolean bIncludingPrefix )
105         throws java.io.IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc
106     {
107         String JavaDoc [] names = null;
108         
109         try
110         {
111             final AttributeList JavaDoc props = (AttributeList JavaDoc)getMBS().invoke( objectName,
112                     getGetPropertiesMethodName(), null,
113                     null );
114             names = new String JavaDoc [ props.size() ];
115             for( int i = 0; i < names.length; ++i )
116             {
117                 final Attribute JavaDoc attr = (Attribute JavaDoc)props.get( i );
118                 if(bIncludingPrefix)
119                     names[ i ] = getDottedNamePrefix() + attr.getName();
120                 else
121                     names[ i ] = attr.getName();
122             }
123         }
124         // getProperties() does not exist--do not log this--it will always happen
125
// on MBeans that don't have properties. Certain bugs in S1As interceptor
126
// cause the wrong types of exceptions to be thrown, so we have to catch
127
// several of them. The correct one is ReflectionException
128
catch( MBeanException JavaDoc e )
129         {
130             names = new String JavaDoc [ 0 ];
131         }
132         catch( RuntimeOperationsException JavaDoc e )
133         {
134             names = new String JavaDoc [ 0 ];
135         }
136         catch( ReflectionException JavaDoc e )
137         {
138             names = new String JavaDoc [ 0 ];
139         }
140         
141         return( names );
142     }
143     
144     
145 }
146
147
148
Popular Tags