KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > selfmanagement > configuration > JavaBeanConfigurator


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  * JavaBeanConfigurator.java
26  *
27  */

28
29 package com.sun.enterprise.admin.selfmanagement.configuration;
30
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33 import java.security.AccessController JavaDoc;
34 import java.security.PrivilegedAction JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.logging.ErrorManager JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40 import java.util.logging.Level JavaDoc;
41
42 import com.sun.enterprise.config.serverbeans.ElementProperty;
43
44
45 /**
46  *
47  * @author Sun Micro Systems, Inc
48  */

49 public class JavaBeanConfigurator {
50     
51     private static final JavaBeanConfigurator singleton =
52             new JavaBeanConfigurator( );
53     
54     /** Creates a new instance of JavaBeanConfigurator */
55     private JavaBeanConfigurator() {
56     }
57     
58     public static JavaBeanConfigurator getInstance( ) {
59         return singleton;
60     }
61     
62     
63      /**
64      * A Utility method to instantiate a class and set the properties.
65      */

66     public Object JavaDoc configureBean( Object JavaDoc bean, ElementProperty[] properties ){
67          setProperties( bean, properties );
68          return bean;
69     }
70     
71         
72     /**
73      * Utility method to get the declared fields.
74      */

75     private final Method JavaDoc[] getDeclaredMethods(final Class JavaDoc clz) {
76         return (Method JavaDoc[]) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
77        public Object JavaDoc run() {
78                 return clz.getDeclaredMethods();
79             }
80         });
81     }
82     
83     /**
84      * Utility method to set properties to the instantiated Object.
85      * _REVISIT_: We can just do java.beans.statement possibly
86      */

87     private void setProperties( Object JavaDoc o, ElementProperty[] properties ) {
88         if( properties == null ) return;
89         Method JavaDoc[] methods = null;
90         try {
91             methods = getDeclaredMethods( o.getClass( ) );
92             for( int i = 0; i < properties.length; i++ ) {
93                 ElementProperty property = properties[i];
94                 String JavaDoc propertyName = property.getName( ).toLowerCase( );
95                 String JavaDoc propertyValue = property.getValue( );
96                 for( int j = 0; j < methods.length; j++ ) {
97                     String JavaDoc methodName = methods[j].getName().toLowerCase();
98                     if ( ( methodName.startsWith( "set" ) )
99                        && ( methodName.endsWith( propertyName ) ) )
100                     {
101                         Class JavaDoc[] parameterTypes = methods[j].getParameterTypes( );
102                         if( parameterTypes.length != 1 ) {
103                             new ErrorManager JavaDoc().error(
104                                 "Only one Parameter is allowed for the setter " +
105                                 " Method: " + methodName +
106                                 " has invalid signature", new Exception JavaDoc(),
107                                 ErrorManager.GENERIC_FAILURE );
108                         }
109                                                                                      
110                         String JavaDoc parameterType = parameterTypes[0].getName();
111                         Object JavaDoc[] parameters = new Object JavaDoc[1];
112                                                                                      
113                         if( parameterType.equals( "java.lang.String") ) {
114                             parameters[0] = propertyValue;
115                         } else if( parameterType.equals( "byte" ) ) {
116                             parameters[0] =
117                                 new Byte JavaDoc( propertyValue.getBytes()[0]);
118                         } else if( parameterType.equals( "int" ) ) {
119                             parameters[0] = new Integer JavaDoc(propertyValue);
120                         } else if( parameterType.equals( "float" ) ) {
121                             parameters[0] = new Float JavaDoc(propertyValue);
122                         } else if( parameterType.equals( "double") ) {
123                             parameters[0] = new Double JavaDoc(propertyValue);
124                         } else if( parameterType.equals( "char" ) ) {
125                             parameters[0] =
126                                 new Character JavaDoc(propertyValue.charAt(0));
127                         } else if( parameterType.equals("boolean") ) {
128                             parameters[0] = new Boolean JavaDoc(propertyValue);
129                         } else if( parameterType.equals("long") ) {
130                             parameters[0] = new Long JavaDoc(propertyValue);
131                         } else if( parameterType.equals("short") ) {
132                              parameters[0] = new Short JavaDoc(propertyValue);
133                         } else {
134                             new ErrorManager JavaDoc().error(
135                                 "Only the basic primitive types can be set " +
136                                 "as properties to NotificationListener and " +
137                                 " NotificationFilter ", new Exception JavaDoc(),
138                                 ErrorManager.GENERIC_FAILURE );
139                             continue;
140                         }
141                         methods[j].invoke( o, parameters );
142                     }
143                 }
144             }
145         } catch( Exception JavaDoc e ) {
146             new ErrorManager JavaDoc().error(
147                 "Error While Setting properties to Notification Listener or " +
148                 " Filter ", e, ErrorManager.GENERIC_FAILURE );
149         }
150     }
151 }
152
Popular Tags