KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > ConfigBeanIntrospector


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 package com.sun.enterprise.admin.server.core.mbean.config;
25
26 //JDK imports
27
import java.beans.*;
28 import java.lang.reflect.*;
29
30 //Logging imports
31
import java.util.logging.Logger JavaDoc;
32 import com.sun.logging.LogDomains;
33
34 //Our imports
35
import com.sun.enterprise.config.*;
36 import com.sun.enterprise.config.serverbeans.*;
37 import com.sun.enterprise.admin.util.ArgChecker;
38
39 public final class ConfigBeanIntrospector
40 {
41     private static final Logger JavaDoc sLogger =
42         LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
43
44     public static Object JavaDoc instantiate(Class JavaDoc bean, Object JavaDoc[] params)
45         throws Exception JavaDoc
46     {
47         ArgChecker.checkValid(bean, "bean"); //NOI18N
48
ArgChecker.checkValid(params, "params"); //NOI18N
49

50         Class JavaDoc[] paramTypes = new Class JavaDoc[params.length];
51         for (int i = 0; i < params.length; i++)
52         {
53             paramTypes[i] = params[i].getClass();
54         }
55         Constructor ctor = bean.getConstructor(paramTypes);
56         Object JavaDoc inst = ctor.newInstance(params);
57         return inst;
58     }
59
60     public static boolean isAttributeSupported(Class JavaDoc beanClass,
61                                                String JavaDoc attributeName)
62     {
63         boolean isSupported = false;
64         try
65         {
66             PropertyDescriptor pd = getPropertyDescriptor(beanClass,
67                                                           attributeName);
68             isSupported = (pd != null);
69         }
70         catch (Exception JavaDoc e)
71         {
72             sLogger.throwing(ConfigBeanIntrospector.class.getName(),
73                     "isAttributeSupported", e);
74             isSupported = false;
75         }
76         return isSupported;
77     }
78
79     public static boolean isAttributeReadable(Class JavaDoc beanClass,
80                                               String JavaDoc attributeName)
81     {
82         boolean isReadable = false;
83         try
84         {
85             Method m = getGetter(beanClass, attributeName);
86             isReadable = (m != null);
87         }
88         catch (Exception JavaDoc e)
89         {
90             sLogger.throwing(ConfigBeanIntrospector.class.getName(),
91                     "isAttributeReadable", e);
92             isReadable = false;
93         }
94         return isReadable;
95     }
96
97     public static boolean isAttributeWritable(Class JavaDoc beanClass,
98                                               String JavaDoc attributeName)
99     {
100         boolean isWritable = false;
101         try
102         {
103             Method m = getSetter(beanClass, attributeName);
104             isWritable = (m != null);
105         }
106         catch (Exception JavaDoc e)
107         {
108             sLogger.throwing(ConfigBeanIntrospector.class.getName(),
109                     "isAttributeWritable", e);
110             isWritable = false;
111         }
112         return isWritable;
113     }
114
115     public static Object JavaDoc invokeGetter(Object JavaDoc target, String JavaDoc attributeName)
116         throws Exception JavaDoc
117     {
118         ArgChecker.checkValid(target, "target"); //NOI18N
119
ArgChecker.checkValid(attributeName, "attributeName"); //NOI18N
120

121         Object JavaDoc ret = null;
122         Class JavaDoc beanClass = target.getClass();
123         Method getter = getGetter(beanClass, attributeName);
124         if (getter != null)
125         {
126             ret = getter.invoke(target, null);
127         }
128         return ret;
129     }
130
131     public static void invokeSetter(Object JavaDoc target,
132                                     String JavaDoc attributeName,
133                                     Object JavaDoc value)
134         throws Exception JavaDoc
135     {
136         ArgChecker.checkValid(target, "target"); //NOI18N
137
ArgChecker.checkValid(attributeName, "attributeName"); //NOI18N
138

139         Class JavaDoc beanClass = target.getClass();
140         Method setter = getSetter(beanClass, attributeName);
141         if (setter != null)
142         {
143             setter.invoke(target, new Object JavaDoc[] {value});
144         }
145     }
146
147     private static Method getGetter(Class JavaDoc beanClass, String JavaDoc attributeName)
148         throws Exception JavaDoc
149     {
150         Method m = null;
151         PropertyDescriptor pd = getPropertyDescriptor(beanClass, attributeName);
152         if (pd != null)
153         {
154             m = pd.getReadMethod();
155         }
156         return m;
157     }
158
159     private static Method getSetter(Class JavaDoc beanClass, String JavaDoc attributeName)
160         throws Exception JavaDoc
161     {
162         Method m = null;
163         PropertyDescriptor pd = getPropertyDescriptor(beanClass, attributeName);
164         if (pd != null)
165         {
166             m = pd.getWriteMethod();
167         }
168         return m;
169     }
170
171     private static PropertyDescriptor getPropertyDescriptor(Class JavaDoc beanClass,
172                                             String JavaDoc attributeName)
173         throws Exception JavaDoc
174     {
175         PropertyDescriptor descriptor = null;
176         BeanInfo javaBeanInfo = Introspector.getBeanInfo(beanClass);
177         PropertyDescriptor[] pds = javaBeanInfo.getPropertyDescriptors();
178         for (int i = 0; i < pds.length; i++)
179         {
180             PropertyDescriptor pd = pds[i];
181             if (pd.getName().equals(attributeName))
182             {
183                 descriptor = pd;
184                 break;
185             }
186         }
187         return descriptor;
188     }
189
190     public static void main(String JavaDoc[] args) throws Exception JavaDoc
191     {
192         ConfigContext ctx = ConfigFactory.createConfigContext(
193                 "/u/ramakant/server.xml", true);
194         Object JavaDoc obj = null;
195 // Server baseBean = (Server) ctx.getRootConfigBean();
196
Config config = (Config)ConfigBeansFactory.getConfigBeanByXPath(ctx, ServerXPathHelper.XPATH_CONFIG);
197             
198         IiopService iiopService = config.getIiopService();
199         obj = iiopService.getIiopListenerById("orb-listener-1");
200
201         //Getters
202
Object JavaDoc value = ConfigBeanIntrospector.invokeGetter(obj, "id");
203         sLogger.info(value.toString());
204         try
205         {
206             ConfigBeanIntrospector.invokeGetter(obj, "abcd");
207         }
208         catch (Exception JavaDoc e)
209         {
210             sLogger.info("OK " + e.getMessage());
211         }
212         //Setters
213
ConfigBeanIntrospector.invokeSetter(obj, "id", "surya10");
214         ConfigBeanIntrospector.invokeSetter(obj, "port",
215                 new Integer JavaDoc(8888).toString());
216
217         ctx.flush(true);
218     }
219 }
220
Popular Tags