KickJava   Java API By Example, From Geeks To Geeks.

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


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/ValueAccessorBase.java,v 1.3 2005/12/25 03:42:08 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:08 $
28  */

29  
30
31 package com.sun.enterprise.admin.dottedname.valueaccessor;
32
33 import java.lang.reflect.Array JavaDoc;
34
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.IntrospectionException JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
38 import javax.management.InstanceNotFoundException JavaDoc;
39 import javax.management.AttributeList JavaDoc;
40 import javax.management.MBeanInfo JavaDoc;
41 import javax.management.MBeanAttributeInfo JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43 import javax.management.Attribute JavaDoc;
44
45 import com.sun.enterprise.admin.util.ClassUtil;
46 import com.sun.enterprise.admin.util.Tokenizer;
47 import com.sun.enterprise.admin.util.TokenizerImpl;
48
49
50 abstract class ValueAccessorBase implements ValueAccessor
51 {
52     final MBeanServerConnection JavaDoc mConn;
53     
54     
55         MBeanServerConnection JavaDoc
56     getMBS()
57     {
58         return( mConn );
59     }
60
61         public
62     ValueAccessorBase( MBeanServerConnection JavaDoc conn )
63     {
64         mConn = conn;
65     }
66     
67     final char ARRAY_ELEMENT_SEPARATOR = ',';
68     final char ESCAPE_CHAR = '\\';
69     
70         String JavaDoc []
71     stringToStringArray( final String JavaDoc s )
72         throws com.sun.enterprise.admin.util.TokenizerException
73     {
74         final String JavaDoc delimiters = "" + ARRAY_ELEMENT_SEPARATOR;
75         final String JavaDoc escapableChars = "" + ARRAY_ELEMENT_SEPARATOR + ESCAPE_CHAR;
76         
77         final Tokenizer tok = new TokenizerImpl( s, delimiters, false, ESCAPE_CHAR, escapableChars );
78         
79         final String JavaDoc [] values = tok.getTokens();
80         
81         return( values );
82     }
83     
84         Object JavaDoc []
85     convert( final String JavaDoc [] stringValues, final Class JavaDoc elementClass )
86         throws Exception JavaDoc
87     {
88         if ( elementClass == String JavaDoc.class )
89         {
90             return( stringValues );
91         }
92         
93         if ( ClassUtil.classnameIsPrimitiveArray( elementClass.getName() ) )
94         {
95             throw new IllegalArgumentException JavaDoc( );
96         }
97         
98         final Object JavaDoc [] values = (Object JavaDoc [])Array.newInstance( elementClass, stringValues.length );
99         
100         for( int i = 0; i < values.length; ++i )
101         {
102             values[ i ] = ClassUtil.InstantiateFromString( elementClass, stringValues[ i ] );
103         }
104         
105         return( values );
106     }
107     
108         Object JavaDoc []
109     stringToArray( final String JavaDoc s, final Class JavaDoc elementClass )
110         throws Exception JavaDoc
111     {
112         final String JavaDoc [] stringValues = stringToStringArray( s );
113         
114         final Object JavaDoc [] values = convert( stringValues, elementClass );
115         
116         return( values );
117     }
118     
119         
120         Object JavaDoc
121     coerceToClass( final Class JavaDoc theClass, String JavaDoc value )
122         throws Exception JavaDoc
123     {
124         Object JavaDoc result = value;
125         
126         if ( theClass != String JavaDoc.class )
127         {
128             if ( ClassUtil.classIsArray( theClass ) )
129             {
130                 final String JavaDoc theClassName = theClass.getName();
131                 final String JavaDoc elementClassName = ClassUtil.getArrayMemberClassName( theClassName );
132                 
133                 // convert the string to an array of strings
134
result = stringToArray( value, ClassUtil.getClassFromName( elementClassName ) );
135             }
136             else
137             {
138                 boolean canCoerce = true;
139                 
140                 final Class JavaDoc resultClass = ClassUtil.PrimitiveClassToObjectClass( theClass );
141                 
142                 if ( resultClass == Boolean JavaDoc.class )
143                 {
144                     // insist on "true" or "false" (case insensitive)
145
canCoerce = value.equalsIgnoreCase( "true" ) || value.equalsIgnoreCase( "false" );
146                 }
147                 
148                 if ( canCoerce )
149                 {
150                     result = ClassUtil.InstantiateFromString( resultClass, value );
151                 }
152             }
153         }
154         return( result );
155     }
156
157     public abstract Attribute JavaDoc getValue( ObjectName JavaDoc objectName, String JavaDoc valueName ) throws Exception JavaDoc;
158     public abstract Attribute JavaDoc setValue( ObjectName JavaDoc objectName, Attribute JavaDoc attr ) throws Exception JavaDoc;
159 }
160
161
162
163
164
165
166
Popular Tags