KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > offline > 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 package com.sun.enterprise.management.offline;
26
27 import java.util.Set JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import java.io.IOException JavaDoc;
34
35
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.MBeanInfo JavaDoc;
38 import javax.management.MBeanAttributeInfo JavaDoc;
39 import javax.management.MBeanOperationInfo JavaDoc;
40 import javax.management.Attribute JavaDoc;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.AttributeNotFoundException JavaDoc;
43 import javax.management.InvalidAttributeValueException JavaDoc;
44
45 import com.sun.appserv.management.base.AMXDebug;
46
47 import com.sun.appserv.management.util.misc.GSetUtil;
48 import com.sun.appserv.management.util.misc.StringUtil;
49 import com.sun.appserv.management.util.misc.ListUtil;
50
51 import com.sun.enterprise.management.support.oldconfig.OldProps;
52
53 import com.sun.enterprise.config.ConfigContext;
54 import com.sun.enterprise.config.ConfigBean;
55 import com.sun.enterprise.config.ConfigException;
56
57
58 import com.sun.enterprise.config.util.ConfigXPathHelper;
59
60 //import com.sun.enterprise.config.serverbeans.*;
61

62 abstract class ConfigBeanHelper
63
64 {
65     private final ConfigBean mConfigBean;
66     private final String JavaDoc mXPath;
67     private final ConfigContext mConfigContext;
68     
69     private final String JavaDoc mType;
70     private final String JavaDoc mName; // may be null!
71

72     static private final String JavaDoc NAME_ATTR = "Name";
73     static private final String JavaDoc VALUE_ATTR = "Value";
74     static private final String JavaDoc DESCRIPTION_ATTR = "Description";
75     
76     static protected final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
77     
78     ConfigBeanHelper(
79         final ConfigContext configContext,
80         final ConfigBean configBean )
81     {
82         if ( configBean == null )
83         {
84             throw new IllegalArgumentException JavaDoc( "null configBean" );
85         }
86         if ( configContext == null )
87         {
88             throw new IllegalArgumentException JavaDoc( "null configContext" );
89         }
90         
91         // why does configBean.getConfigContext() return null?
92
mConfigContext = configContext;
93         
94         mConfigBean = configBean;
95         mXPath = mConfigBean.getXPath();
96         assert( mXPath != null );
97         
98         mType = _getType( mXPath );
99         mName = _getName();
100         
101     }
102     
103        protected void
104     debug( Object JavaDoc o )
105     {
106         sdebug( o );
107         
108         final String JavaDoc xpath = "" + mXPath;
109         AMXDebug.getInstance().getOutput( "ConfigBeanHelper" ).println( xpath + ": " + o );
110     }
111     
112         protected void
113     sdebug( Object JavaDoc o )
114     {
115         System.out.println( "" + o );
116     }
117     
118     
119         public ConfigBean
120     getConfigBean()
121     {
122         return mConfigBean;
123     }
124     
125         public ConfigContext
126     getConfigContext()
127     {
128         return mConfigContext;
129     }
130
131         public Object JavaDoc
132     getAttribute( final String JavaDoc attrName )
133         throws AttributeNotFoundException JavaDoc
134     {
135         Object JavaDoc result = null;
136         
137         if ( DESCRIPTION_ATTR.equals( attrName ) )
138         {
139             result = getDescription();
140         }
141         else
142         {
143             try
144             {
145                 result = mConfigBean.getAttributeValue( attrName );
146             }
147             catch( RuntimeException JavaDoc e )
148             {
149                 debug( "ATTR FAILED: " + attrName );
150                 throw e;
151             }
152         }
153         return result;
154     }
155     
156         public String JavaDoc
157     getXPath()
158     {
159         return mXPath;
160     }
161     
162         public String JavaDoc
163     getParentXPath()
164     {
165         return ConfigXPathHelper.getParentXPath( getXPath() );
166     }
167     
168     /**
169         @return type, always non-null
170      */

171         public String JavaDoc
172     getType()
173     {
174         return mType;
175     }
176     
177         static String JavaDoc
178     _getType( final String JavaDoc xPath )
179     {
180         return ConfigXPathHelper.getLastNodeName( xPath );
181     }
182     
183     /**
184         @return name, possibly null if no name
185      */

186         public String JavaDoc
187     getName()
188     {
189         return mName;
190     }
191     
192     /**
193         @return String[2], where [0] is the type and [1] is the name
194      */

195         public String JavaDoc[]
196     getTypeAndName()
197         throws ConfigException
198     {
199         return new String JavaDoc[] { getType(), getName() };
200     }
201     
202         private String JavaDoc
203     _getName()
204     {
205         String JavaDoc name = null;
206         
207         final Set JavaDoc<String JavaDoc> attrNames = GSetUtil.newStringSet( getAttributeNames() );
208                     
209         for( final String JavaDoc nameKey : OldProps.getPossibleNameKeys() )
210         {
211             final String JavaDoc camelized = mConfigBean.camelize( nameKey );
212             
213             if ( attrNames.contains( camelized ) )
214             {
215                 try
216                 {
217                     name = (String JavaDoc)getAttribute( camelized );
218                     if ( name != null )
219                     {
220                         break;
221                     }
222                 }
223                 catch( AttributeNotFoundException JavaDoc e )
224                 {
225                     throw new RuntimeException JavaDoc( e );
226                 }
227             }
228         }
229         
230         return name;
231     }
232     
233     
234         public List JavaDoc<String JavaDoc[]>
235     getAllObjectNameProps( final Set JavaDoc<String JavaDoc> ignoreTypes )
236         throws ConfigException
237     {
238         final List JavaDoc<String JavaDoc[]> props = new ArrayList JavaDoc<String JavaDoc[]>();
239         
240         //debug( "\n--------------------------------------" );
241
// debug( xPath );
242
String JavaDoc[] pair = getTypeAndName();
243         props.add( pair );
244         
245         String JavaDoc curXPath = ConfigXPathHelper.getParentXPath( getXPath() );
246         String JavaDoc lastXPath = null;
247         while ( ! curXPath.equals( lastXPath ) )
248         {
249             final ConfigBeanHelper helper =
250                 ConfigBeanHelperFactory.getInstance( getConfigContext() ).getHelper( curXPath );
251             
252             pair = helper.getTypeAndName();
253             
254             if ( ! ignoreTypes.contains( pair[ 0 ] ) )
255             {
256                 props.add( pair );
257             }
258             
259             lastXPath = curXPath;
260             curXPath = helper.getParentXPath();
261         }
262         
263         return props;
264     }
265     
266
267         public void
268     setAttribute( final Attribute JavaDoc attr )
269         throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
270     {
271         final Object JavaDoc value = attr.getValue();
272         
273         setAttribute( attr.getName(), value );
274     }
275     
276         public void
277     setAttribute( final String JavaDoc name, final Object JavaDoc value )
278         throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
279     {
280         if ( DESCRIPTION_ATTR.equals( name ) )
281         {
282             setDescription( (String JavaDoc)value );
283         }
284         else
285         {
286             // Config API wants strings (only)
287
final String JavaDoc valueString = (value == null) ? null : ("" + value);
288             mConfigBean.setAttributeValue( name, valueString );
289         }
290     }
291     
292     
293         protected boolean
294     hasValue( final String JavaDoc valueName )
295     {
296         boolean hasValue = false;
297         
298         try
299         {
300             final Object JavaDoc value = mConfigBean.getValue( valueName );
301             // value may be null, but it still indicates that the value is possible
302
hasValue = true;
303             //sdebug( "HAS VALUE " + valueName + "=" + value );
304
}
305         catch( Exception JavaDoc e )
306         {
307         }
308         
309         return hasValue;
310     }
311
312
313         private boolean
314     hasDescription()
315     {
316         return hasValue( DESCRIPTION_ATTR );
317     }
318
319         public final String JavaDoc[]
320     getAttributeNames()
321     {
322         return GSetUtil.toStringArray( _getAttributeNames() );
323     }
324     
325         protected Set JavaDoc<String JavaDoc>
326     _getAttributeNames()
327     {
328         final Set JavaDoc<String JavaDoc> attrNames =
329             GSetUtil.newSet( mConfigBean.getAttributeNames() );
330         
331         // it does this sometimes!
332
assert( ! attrNames.contains( null ) );
333         attrNames.remove( null );
334             
335         if ( hasDescription() )
336         {
337             attrNames.add( DESCRIPTION_ATTR );
338         }
339         
340         return attrNames;
341     }
342
343     /**
344         Subclass may override this for special cases that are non-String.
345      */

346         protected Class JavaDoc
347     getAttributeClass( final String JavaDoc attrName )
348     {
349         return String JavaDoc.class;
350     }
351     
352     /**
353         Subclass may override this, or alternatly, just specify
354         the Class of the Attribute by overriding {@link #getAttributeClass}.
355      */

356         protected MBeanAttributeInfo JavaDoc
357     getMBeanAttributeInfo( final String JavaDoc attrName )
358     {
359         final String JavaDoc description = "";
360         final boolean isReadable = true;
361         final boolean isWriteable = true;
362         final boolean isIs = false;
363         
364         assert( attrName != null );
365         final MBeanAttributeInfo JavaDoc info = new MBeanAttributeInfo JavaDoc(
366             attrName,
367             getAttributeClass( attrName ).getName(),
368             description,
369             isReadable,
370             isWriteable,
371             isIs );
372         
373         return info;
374     }
375
376         public MBeanInfo JavaDoc
377     getMBeanInfo()
378     {
379         final List JavaDoc<String JavaDoc> attrNames = ListUtil.newListFromArray( getAttributeNames() );
380         
381         final MBeanOperationInfo JavaDoc[] operationInfos = new MBeanOperationInfo JavaDoc[0];
382         final MBeanAttributeInfo JavaDoc[] attributeInfos = new MBeanAttributeInfo JavaDoc[ attrNames.size() ];
383         
384         int i = 0;
385         for( final String JavaDoc name : attrNames )
386         {
387             assert( name != null );
388             attributeInfos[ i ] = getMBeanAttributeInfo( name );
389             assert attributeInfos[ i ].getName() != null;
390             
391             ++i;
392         }
393                     
394         final MBeanInfo JavaDoc info =
395             new MBeanInfo JavaDoc( this.getClass().getName(),
396                 "exposes Attributes from ConfigBean",
397                 attributeInfos,
398                 null,
399                 operationInfos,
400                 null );
401         
402         for( final MBeanAttributeInfo JavaDoc xxx : info.getAttributes() )
403         {
404             assert( xxx.getName() != null );
405         }
406
407         return info;
408     }
409     
410         public List JavaDoc<ConfigBeanHelper>
411     getAllChildren()
412     {
413         final List JavaDoc<ConfigBeanHelper> children = new ArrayList JavaDoc<ConfigBeanHelper>();
414         
415         final ConfigBean[] configBeans = mConfigBean.getAllChildBeans();
416         if ( configBeans != null )
417         {
418             for( final ConfigBean configBean : configBeans )
419             {
420                 if ( configBean != null )
421                 {
422                     final ConfigBeanHelper helper =
423                         ConfigBeanHelperFactory.getInstance( getConfigContext() ).getHelper( configBean );
424                     
425                     children.add( helper );
426                 }
427             }
428         }
429         
430         return children;
431     }
432     
433         public List JavaDoc<ConfigBeanHelper>
434     getAllChildrenOfType( final String JavaDoc desiredType )
435     {
436         final List JavaDoc<ConfigBeanHelper> children = getAllChildren();
437         final List JavaDoc<ConfigBeanHelper> propertyChildren = new ArrayList JavaDoc<ConfigBeanHelper>();
438         
439         for( final ConfigBeanHelper helper : children )
440         {
441             if ( desiredType.equals( helper.getType() ) )
442             {
443                 propertyChildren.add( helper );
444             }
445         }
446         
447         return propertyChildren;
448     }
449     
450     
451
452         public Map JavaDoc<String JavaDoc,ConfigBeanHelper>
453     getSpecialChildMap( final String JavaDoc type )
454     {
455         if ( ! ("element-property".equals( type ) ||
456             "system-property".equals( type )) ||
457             "jvm-option".equals( type ) )
458         {
459             throw new IllegalArgumentException JavaDoc( type );
460         }
461
462         final List JavaDoc<ConfigBeanHelper> children = getAllChildrenOfType( type );
463         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> m = new HashMap JavaDoc<String JavaDoc,ConfigBeanHelper>();
464         
465         for( final ConfigBeanHelper helper : children )
466         {
467             try
468             {
469                 final String JavaDoc name = (String JavaDoc)helper.getAttribute( NAME_ATTR );
470                 
471                 m.put( name, helper );
472             }
473             catch( Exception JavaDoc e )
474             {
475                 throw new RuntimeException JavaDoc(
476                     "FAILURE getting Name Attribute for property element", e );
477             }
478         }
479         
480         return m;
481     }
482     
483     
484     /**
485         @see com.sun.enterprise.management.config.OldPropertiesImpl
486      */

487         public AttributeList JavaDoc
488     getProperties()
489     {
490         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
491             getSpecialChildMap( "element-property" );
492         
493         final AttributeList JavaDoc attrs = new AttributeList JavaDoc();
494         
495         for( final String JavaDoc name : children.keySet() )
496         {
497             final ConfigBeanHelper helper = children.get( name );
498             
499             final String JavaDoc value = getValueAttributeValue( helper );
500             
501             attrs.add( new Attribute JavaDoc( name, value ) );
502         }
503         
504         return attrs;
505     }
506     
507       
508     /**
509         @see com.sun.enterprise.management.config.OldPropertiesImpl
510      */

511         public AttributeList JavaDoc
512     getSystemProperties()
513     {
514         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
515             getSpecialChildMap( "system-property" );
516         
517         final AttributeList JavaDoc attrs = new AttributeList JavaDoc();
518         for( final String JavaDoc name : children.keySet() )
519         {
520             final ConfigBeanHelper helper = children.get( name );
521             
522             final String JavaDoc value = getValueAttributeValue( helper );
523             
524             attrs.add( new Attribute JavaDoc( name, value ) );
525         }
526         
527         return attrs;
528     }
529     
530     /**
531      */

532         protected static String JavaDoc
533     getValueAttributeValue( final ConfigBeanHelper helper )
534     {
535         try
536         {
537             final String JavaDoc value = (String JavaDoc)helper.getAttribute( VALUE_ATTR );
538             return value;
539         }
540         catch( Exception JavaDoc e )
541         {
542             throw new RuntimeException JavaDoc(
543                 "FAILURE getting Value Attribute for property element ", e );
544         }
545     }
546     
547      /**
548         @see com.sun.enterprise.management.config.OldPropertiesImpl
549      */

550         public String JavaDoc
551     getPropertyValue( final String JavaDoc propertyName )
552     {
553         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
554             getSpecialChildMap( "element-property" );
555         if ( ! children.containsKey( propertyName ) )
556         {
557             final String JavaDoc msg = "No such property: " +
558                 StringUtil.quote( propertyName );
559                 
560             throw new IllegalArgumentException JavaDoc( msg );
561         }
562         
563         return getValueAttributeValue( children.get( propertyName ) );
564     }
565     
566      /**
567         @see com.sun.enterprise.management.config.OldPropertiesImpl
568      */

569         public String JavaDoc
570     getSystemPropertyValue( final String JavaDoc propertyName )
571     {
572         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
573             getSpecialChildMap( "system-property" );
574         if ( ! children.containsKey( propertyName ) )
575         {
576             throw new IllegalArgumentException JavaDoc( propertyName );
577         }
578         
579         return getValueAttributeValue( children.get( propertyName ) );
580     }
581     
582     
583         private static void
584     setValueAttributeValue(
585         final ConfigBeanHelper helper,
586         final String JavaDoc value )
587     {
588         try
589         {
590             helper.setAttribute( VALUE_ATTR, value );
591         }
592         catch( Exception JavaDoc e )
593         {
594             throw new RuntimeException JavaDoc(
595                 "FAILURE setting Value Attribute for property element ", e );
596         }
597     }
598     
599      /**
600         @see com.sun.enterprise.management.config.OldPropertiesImpl
601      */

602         public void
603     setProperty( final Attribute JavaDoc attr )
604     {
605         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
606             getSpecialChildMap( "element-property" );
607         
608         // this won't work for creating a new property!
609
final ConfigBeanHelper helper = children.get( attr.getName() );
610         if ( helper == null )
611         {
612             throw new IllegalArgumentException JavaDoc( attr.getName() );
613         }
614         
615         setValueAttributeValue( helper, (String JavaDoc)attr.getValue() );
616     }
617     
618      /**
619         @see com.sun.enterprise.management.config.OldPropertiesImpl
620      */

621         public void
622     setSystemProperty( final Attribute JavaDoc attr )
623     {
624         final Map JavaDoc<String JavaDoc,ConfigBeanHelper> children =
625             getSpecialChildMap( "system-property" );
626         
627         // this won't work for creating a new property!
628
final ConfigBeanHelper helper = children.get( attr.getName() );
629         if ( helper == null )
630         {
631             throw new IllegalArgumentException JavaDoc( attr.getName() );
632         }
633         
634         setValueAttributeValue( helper, (String JavaDoc)attr.getValue() );
635     }
636     
637         public String JavaDoc
638     getDescription()
639     {
640         String JavaDoc result = (String JavaDoc)getValue( DESCRIPTION_ATTR );
641         
642         return result;
643     }
644     
645         public void
646     setDescription( final String JavaDoc description )
647     {
648         mConfigBean.setValue( DESCRIPTION_ATTR, description );
649     }
650     
651     /**
652         When the ConfigBean is called for an array value that
653         can potentially exist, but doesn't, it tries to access an
654         array illegally. Not much we can do except return null.
655         <p>
656         Here's the stack trace:
657         
658         java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
659          at java.util.ArrayList.RangeCheck(ArrayList.java:547)
660          at java.util.ArrayList.get(ArrayList.java:322)
661          at org.netbeans.modules.schema2beans.BeanProp.getValue(BeanProp.java:474)
662          at org.netbeans.modules.schema2beans.BaseBean.getValue(BaseBean.java:353)
663          at com.sun.enterprise.config.ConfigBean.getValue(ConfigBean.java:383)
664      */

665         protected Object JavaDoc
666     getValue( final String JavaDoc valueName )
667     {
668         Object JavaDoc value = null;
669         
670         try
671         {
672             value = mConfigBean.getValue( valueName );
673         }
674         catch( Exception JavaDoc e )
675         {
676             debug( "ConfigBeanHelper.getValue: Exception accessing value: " + valueName );
677             //e.printStackTrace();
678
}
679         
680         return value;
681     }
682     
683         public abstract Object JavaDoc
684     handleInvoke(
685         String JavaDoc operationName,
686         Object JavaDoc[] args,
687         String JavaDoc[] types );
688         
689          public void
690     unsupportedOperation(
691         String JavaDoc operationName,
692         Object JavaDoc[] args,
693         String JavaDoc[] types )
694     {
695         throw new IllegalArgumentException JavaDoc(
696             "invoke() unknown operation " + operationName + "()" );
697     }
698 }
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
Popular Tags