KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > DelegateBase


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/mbeanapi-impl/src/java/com/sun/enterprise/management/support/DelegateBase.java,v 1.3 2005/12/25 03:40:40 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:40:40 $
28  */

29
30 package com.sun.enterprise.management.support;
31
32 import java.util.Set JavaDoc;
33
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.Attribute JavaDoc;
37 import javax.management.AttributeList JavaDoc;
38 import javax.management.InstanceNotFoundException JavaDoc;
39 import javax.management.AttributeNotFoundException JavaDoc;
40 import javax.management.InvalidAttributeValueException JavaDoc;
41 import javax.management.ReflectionException JavaDoc;
42 import javax.management.MBeanException JavaDoc;
43 import javax.management.MBeanParameterInfo JavaDoc;
44 import javax.management.MBeanOperationInfo JavaDoc;
45
46 import com.sun.appserv.management.base.AMXDebug;
47
48 import com.sun.appserv.management.util.jmx.JMXUtil;
49 import com.sun.appserv.management.util.misc.ArrayConversion;
50 import com.sun.appserv.management.util.misc.Output;
51
52 /**
53     Delegate base class which most Delegates will want to extend.
54  */

55 public abstract class DelegateBase implements Delegate
56 {
57     private Set JavaDoc mAttributeNames;
58     private DelegateOwner mOwner;
59     private Output mDebug;
60     private final String JavaDoc mID;
61     
62     /**
63         An operation has not been implemented. Deal with appropriately.
64      */

65         protected void
66     unimplementedOperation( String JavaDoc operation )
67     {
68         debug( "unimplemented operation: " + operation );
69         throw new UnsupportedOperationException JavaDoc( operation );
70     }
71     
72         public
73     DelegateBase( final String JavaDoc id, DelegateOwner owner )
74     {
75         mID = id;
76         mAttributeNames = null;
77         mOwner = owner;
78         mDebug = null;
79     }
80     
81         public void
82     setDebugOutput( final Output debugOutput )
83     {
84         mDebug = debugOutput;
85     }
86     
87         protected final void
88     debug(final Object JavaDoc o)
89     {
90         if ( mDebug != null )
91         {
92             mDebug.println( o );
93         }
94     }
95     
96     
97     public final String JavaDoc getID() { return mID; }
98     
99         public void
100     setOwner( final DelegateOwner owner )
101     {
102         mOwner = owner;
103     }
104     
105         public DelegateOwner
106     getOwner()
107     {
108         return( mOwner );
109     }
110
111     /**
112         Default behavior is to loop over each Attribute; subclass
113         may wish to maintain atomicity by implementing directly.
114      */

115         public AttributeList JavaDoc
116     getAttributes( final String JavaDoc[] attrNames )
117     {
118         final AttributeList JavaDoc attrs = new AttributeList JavaDoc();
119         
120         for( int i = 0; i < attrNames.length; ++i )
121         {
122             try
123             {
124                 final String JavaDoc attrName = attrNames[ i ];
125                 
126                 final Attribute JavaDoc attr =
127                     new Attribute JavaDoc( attrName, getAttribute( attrName ) );
128                 attrs.add( attr );
129             }
130             catch( Exception JavaDoc e )
131             {
132                 // ignore
133
}
134         }
135         
136         return( attrs );
137     }
138     
139     
140     /**
141         Default behavior is too loop over each Attribute; subclass
142         may wish to maintain atomicity by implementing directly.
143      */

144         public AttributeList JavaDoc
145     setAttributes( final AttributeList JavaDoc attrs )
146     {
147         final int numAttrs = attrs.size();
148         final AttributeList JavaDoc successList = new AttributeList JavaDoc();
149         
150         for( int i = 0; i < numAttrs; ++i )
151         {
152             final Attribute JavaDoc attr = (Attribute JavaDoc)attrs.get( i );
153             try
154             {
155                 setAttribute( attr );
156                 
157                 successList.add( attr );
158             }
159             catch( AttributeNotFoundException JavaDoc e )
160             {
161                 // ignore, as per spec
162
}
163             catch( InvalidAttributeValueException JavaDoc e )
164             {
165                 // ignore, as per spec
166
}
167         }
168         return( successList );
169     }
170     
171     
172     
173     /**
174         Do the classnames match the parameter infos?
175      */

176         private boolean
177     typesMatch(
178         final String JavaDoc[] types,
179         final MBeanParameterInfo JavaDoc[] paramInfos )
180     {
181         boolean matches = false;
182         final int numTypes = types == null ? 0 : types.length;
183         final int numParams = paramInfos == null ? 0 : paramInfos.length;
184         
185         if ( numTypes == numParams )
186         {
187             matches = true;
188             
189             for( int i = 0; i < numTypes; ++i )
190             {
191                 if ( ! types[ i ].equals( paramInfos[ i ].getType() ) )
192                 {
193                     matches = false;
194                     break;
195                 }
196             }
197         }
198         
199         return( matches );
200     }
201
202         public synchronized boolean
203     supportsAttribute( String JavaDoc attrName )
204     {
205         if ( mAttributeNames == null )
206         {
207             final String JavaDoc[] attrNames =
208                 JMXUtil.getAttributeNames( getMBeanInfo().getAttributes() );
209                 
210             mAttributeNames = ArrayConversion.arrayToSet( attrNames );
211         }
212         
213         return( mAttributeNames.contains( attrName ) );
214     }
215
216         public boolean
217     supportsOperation(
218         String JavaDoc operationName,
219         Object JavaDoc[] args,
220         String JavaDoc[] types )
221     {
222         boolean supports = false;
223         
224         final MBeanOperationInfo JavaDoc[] opInfos = getMBeanInfo().getOperations();
225         
226         for( int i = 0; i < opInfos.length; ++i )
227         {
228             final MBeanOperationInfo JavaDoc info = opInfos[ i ];
229             
230             if ( info.getName().equals( operationName ) )
231             {
232                 if ( typesMatch( types, info.getSignature() ) )
233                 {
234                     supports = true;
235                     break;
236                 }
237             }
238         }
239         
240         return( supports );
241     }
242     
243 }
244
245
246
247
248
249
250
251
252
Popular Tags