KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > config > ConfigBeanHelper


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  * $Id: ConfigBeanHelper.java,v 1.4 2006/03/12 01:26:56 jluehe Exp $
26  * @author: alexkrav
27  *
28  * $Log: ConfigBeanHelper.java,v $
29  * Revision 1.4 2006/03/12 01:26:56 jluehe
30  * Renamed AS's org.apache.commons.* to com.sun.org.apache.commons.*, to avoid collisions with org.apache.commons.* packages bundled by webapps.
31  *
32  * Tests run: QL, Servlet TCK
33  *
34  * Revision 1.3 2005/12/25 03:47:29 tcfujii
35  * Updated copyright text and year.
36  *
37  * Revision 1.2 2005/06/27 21:19:40 tcfujii
38  * Issue number: CDDL header updates.
39  *
40  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
41  * GlassFish first drop
42  *
43  * Revision 1.11 2005/05/07 04:35:26 ruyakr
44  * Engineer: Rob Ruyak
45  *
46  * Fixed warning messages displayed from jdk1.5 because of introduction of varargs in certain apis such as ja
47  * va.lang.Class.
48  *
49  *
50  * This is my last checkin here at Sun! Thanks for all the good times!
51  *
52  * Revision 1.10 2004/11/14 07:04:17 tcfujii
53  * Updated copyright text and/or year.
54  *
55  * Revision 1.9 2004/11/10 21:17:01 kmeduri
56  * This is needed for fixing 6177312.
57  * Add the following method
58  * static public boolean checkIfAttributesAndPropertiesAreResolvable(ConfigBean element, String instanceName)
59  * This method tests if all attributes and properties of the tested config element are fully resolvable.
60  *
61  * Author: Alexandre Kravtchenko
62  * Reviewer: Sreenivas Munnangi
63  * Tests Ran: PE/EE Quicklook tests
64  * Approved by: Bugswat
65  *
66  * Revision 1.8 2004/02/20 03:56:07 qouyang
67  *
68  *
69  * First pass at code merge.
70  *
71  * Details for the merge will be published at:
72  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
73  *
74  * Revision 1.7.4.2 2004/02/02 07:25:14 tcfujii
75  * Copyright updates notices; reviewer: Tony Ng
76  *
77  * Revision 1.7.4.1 2003/12/23 01:51:43 kravtch
78  * Bug #4959186
79  * Reviewer: Sridatta
80  * Checked in PE8FCS_BRANCH
81  * (1) admin/admin-core/admin-cli/admin-gui/appserv-core/assembly-tool: switch to new domain name "ias:" -> "com.sun.appserv"
82  * (2) admin-core and admin-cli: switch to "dashed" attribute names
83  * (3) admin-core: support both "dashed"/"underscored" names for get/setAttributes
84  * (4) admin-gui: hook for reverse converting attribute names (temporary hack);
85  *
86  * Revision 1.7 2003/06/25 20:03:37 kravtch
87  * 1. java file headers modified
88  * 2. properties handling api is added
89  * 3. fixed bug for xpathes containing special symbols;
90  * 4. new testcases added for jdbc-resource
91  * 5. introspector modified by not including base classes operations;
92  *
93  *
94 */

95
96 package com.sun.enterprise.admin.config;
97
98 import java.util.Enumeration JavaDoc;
99 import java.util.Hashtable JavaDoc;
100 import java.text.CharacterIterator JavaDoc;
101 import java.text.StringCharacterIterator JavaDoc;
102
103 import java.lang.reflect.Method JavaDoc;
104 import java.lang.reflect.Modifier JavaDoc;
105
106 //JMX imports
107
import javax.management.*;
108 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
109 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
110 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
111 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
112 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
113 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
114
115 import com.sun.org.apache.commons.modeler.AttributeInfo;
116
117 //naming
118
import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo;
119
120 //config
121
import com.sun.enterprise.config.ConfigException;
122 import com.sun.enterprise.config.ConfigBean;
123 import com.sun.enterprise.config.ConfigBeansFactory;
124 import com.sun.enterprise.config.ConfigContext;
125 import com.sun.enterprise.config.serverbeans.ElementProperty;
126 import com.sun.enterprise.config.serverbeans.PropertyResolver;
127 /****************************************************************************************************************
128  */

129 public class ConfigBeanHelper
130 {
131    private static final String JavaDoc XPATH_SEPARATOR = "/";
132    public final String JavaDoc PROPERTY_NAME_PREFIX = "property.";
133     
134     private ConfigBean m_baseConfigBean;
135     private BaseConfigMBean m_mbean;
136     public ConfigBeanHelper(BaseConfigMBean mbean, ConfigBean cb)
137     {
138         m_baseConfigBean = cb;
139         m_mbean = mbean;
140 // if(!descriptor.isConfigBeanRetrospected())
141
// {
142
// }
143
}
144
145 /* public static ConfigBeanHelper getConfigBeanHelper(BaseConfigMBean mbean)
146     {
147         return new ConfigBeanHelper((MBeanDescriptor)mbean.getMBeanInfo(), mbean.getManagedConfigBean());
148     }
149 */

150     
151     /****************************************************************************************************************/
152     public String JavaDoc getAttributeValue(String JavaDoc name) throws MBeanException,AttributeNotFoundException
153     {
154 //FIXME change to CoinfigMBeanBase implementation
155
if(m_baseConfigBean==null)
156             return null;
157         if(name.startsWith(PROPERTY_NAME_PREFIX))
158         {
159             // get property value
160
return (String JavaDoc)getPropertyElementValue(name.substring(PROPERTY_NAME_PREFIX.length()));
161         }
162         else
163             return m_baseConfigBean.getAttributeValue(name);
164     }
165     public String JavaDoc setAttributeValue(String JavaDoc name, String JavaDoc value) throws MBeanException,AttributeNotFoundException
166     {
167 //FIXME change to CoinfigMBeanBase implementation
168
if(m_baseConfigBean==null)
169             return null;
170         if(name.startsWith(PROPERTY_NAME_PREFIX))
171         {
172             // set property value
173
setPropertyElementValue(new Attribute(name.substring(PROPERTY_NAME_PREFIX.length()), value), false);
174         }
175         else
176            m_baseConfigBean.setAttributeValue(name, value);
177         return value;
178     }
179     
180     
181     
182     /**
183      * call app server logging
184      */

185     protected static boolean isDebugEnabled() {
186         return true;
187     }
188     
189     protected static void debug(String JavaDoc s) {
190         //TODO: change this to app server logging
191
System.out.println(s);
192     }
193     protected static void info(String JavaDoc s) {
194         //TODO: change this to app server logging
195
System.out.println(s);
196     }
197     protected static void error(String JavaDoc s) {
198         //TODO: change this to app server logging
199
System.out.println(s);
200     }
201     
202
203
204     /****************************************************************************************************************
205      * Gets MBean's property value.
206      * @param externalName the MBean's property name.
207      * @return The value of the property retrieved.
208      * @throws MBeanException exception
209      * @throws AttributeNotFoundException exception
210      */

211     public Object JavaDoc getPropertyElementValue(String JavaDoc propertyName) throws MBeanException,AttributeNotFoundException
212     {
213
214         Class JavaDoc cl = m_baseConfigBean.getClass();
215         ElementProperty prop;
216         try
217         {
218            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
219            prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object JavaDoc[]{propertyName});
220         }
221         catch (Exception JavaDoc e)
222         {
223             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.getattribute.undefined_properties_in_base_element"+ propertyName );
224             throw new MBeanException(new MBeanConfigException( msg ));
225         }
226         if(prop==null) {
227             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.getattribute_properties_not_found_in_base_element"+ propertyName );
228             throw new MBeanException(new MBeanConfigException( msg ));
229         }
230         return prop.getValue();
231     }
232     /****************************************************************************************************************
233      * Sets MBean's property value.
234      * @param attr The identification of the property to be set and the value it is to be set to.
235      * @throws MBeanException exception
236      * @throws AttributeNotFoundException exception
237      */

238     public void setPropertyElementValue(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException
239     {
240         String JavaDoc propertyName = attr.getName();
241         String JavaDoc value = (String JavaDoc)attr.getValue();
242         
243         Class JavaDoc cl = m_baseConfigBean.getClass();
244         ElementProperty prop;
245         try
246         {
247            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
248            prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object JavaDoc[]{propertyName});
249         }
250         catch (Exception JavaDoc e)
251         {
252             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setattribute_undefined_properties_in_base_element"+ propertyName );
253             throw new MBeanException(new MBeanConfigException( msg ));
254         }
255         if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals("")))
256         {
257             prop = new ElementProperty();
258             prop.setName(propertyName);
259             prop.setValue(value);
260             try
261             {
262                 Method JavaDoc method = cl.getDeclaredMethod("addElementProperty", new Class JavaDoc[]{prop.getClass()});
263                 method.invoke(m_baseConfigBean, new Object JavaDoc[]{prop});
264             }
265             catch (Exception JavaDoc e)
266             {
267                 String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setproperty_invoke_error"+propertyName );
268                 throw new MBeanException(new MBeanConfigException( msg ));
269             }
270         }
271         else
272         {
273             if(value==null || (!bAllowsEmptyValue && value.equals("")))
274             {
275                 try
276                 {
277                     Method JavaDoc method = cl.getDeclaredMethod("removeElementProperty", new Class JavaDoc[]{prop.getClass()});
278                     method.invoke(m_baseConfigBean, new Object JavaDoc[]{prop});
279                 }
280                 catch (Exception JavaDoc e)
281                 {
282                     String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setproperty_could_not_remove_propery"+ propertyName );
283                     throw new MBeanException(new MBeanConfigException( msg ));
284                 }
285             }
286             else
287                 prop.setValue(value);
288         }
289         
290 /* try
291         {
292             m_configContext.flush();
293         }
294         catch (ConfigException e)
295         {
296             throw new MBeanException(new MBeanConfigException(e.getMessage()));
297         }
298 */

299     }
300
301     static private ElementProperty[] getElementProperties(ConfigBean element)
302     {
303
304         Class JavaDoc cl = element.getClass();
305         ElementProperty[] props = null;
306         try
307         {
308            Method JavaDoc method = cl.getDeclaredMethod("getElementProperty");
309            props = (ElementProperty[])method.invoke(element);
310         }
311         catch (Exception JavaDoc e)
312         {
313         }
314         return props;
315     }
316
317     /**
318      * this method tests if all attributes and properties of the tested config element are fully resolvable
319      * (not consist of unresolvable ${...} - varibles inside
320      * @param element The testing config element bean
321      * @param appserver instance name
322      * @throws ConfigException
323      **/

324     static public boolean checkIfAttributesAndPropertiesAreResolvable(ConfigBean element, String JavaDoc instanceName)
325                               throws ConfigException
326     {
327         PropertyResolver resolver = new PropertyResolver(element.getConfigContext(), instanceName);
328         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
329         String JavaDoc[] attrNames = element.getAttributeNames();
330         //first - check attributes;
331
if(attrNames!=null)
332         {
333             for(int i=0; i<attrNames.length; i++)
334             {
335                String JavaDoc value = element.getAttributeValue(attrNames[i]);
336                if(value!=null && !resolver.isResolvable(value, true))
337                {
338                    return false;
339                }
340             }
341         }
342         //then - properties;
343
ElementProperty[] props = getElementProperties(element);
344         if(props!=null)
345         {
346             for(int i=0; i<props.length; i++)
347             {
348                String JavaDoc value = props[i].getValue();
349                if(value!=null && !resolver.isResolvable(value, true))
350                {
351                    return false;
352                }
353             }
354         }
355            
356         return true;
357     }
358 }
359
Popular Tags