KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > base > MapCapableBase


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 package com.sun.appserv.management.base;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32
33 import com.sun.appserv.management.util.misc.MapUtil;
34 import com.sun.appserv.management.util.misc.TypeCast;
35 import com.sun.appserv.management.util.misc.ObjectUtil;
36 import com.sun.appserv.management.util.jmx.OpenMBeanUtil;
37
38
39 /**
40     Base impl class.
41  */

42
43 public abstract class MapCapableBase implements MapCapable
44 {
45     private Map JavaDoc<String JavaDoc,Serializable JavaDoc> mFields;
46     private boolean mWasMadeImmutable;
47     private String JavaDoc mClassName;
48     
49         public int
50     hashCode()
51     {
52         return ObjectUtil.hashCode( mFields, mClassName) ^
53                     (mWasMadeImmutable ? 1 : 0);
54     }
55     
56         protected
57     MapCapableBase( final String JavaDoc className )
58     {
59         mFields = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
60         mWasMadeImmutable = false;
61         mClassName = className;
62     }
63     
64         protected <T extends Serializable JavaDoc>
65     MapCapableBase(
66         final Map JavaDoc<String JavaDoc,T> m,
67         final String JavaDoc className)
68     {
69         this( className );
70         
71         mClassName = className;
72         
73         if ( m != null )
74         {
75             putAll( m );
76             
77             // if present, remove the type
78
getFields().remove( MAP_CAPABLE_CLASS_NAME_KEY );
79         }
80     }
81     
82         protected <T extends Serializable JavaDoc> void
83     checkValidType( final Map JavaDoc<String JavaDoc,T> m, final String JavaDoc requiredType )
84     {
85         final String JavaDoc type = (String JavaDoc)m.get( MAP_CAPABLE_CLASS_NAME_KEY );
86         
87         if ( ! requiredType.equals( type ) )
88         {
89             throw new IllegalArgumentException JavaDoc( "Illegal MAP_CAPABLE_CLASS_NAME_KEY: " + type );
90         }
91     }
92     
93     /**
94         Return true if internal state is valid, false otherwise.
95      */

96     protected abstract boolean validate();
97     
98     /**
99         Return the type name which will be paired with MAP_CAPABLE_CLASS_NAME_KEY.
100      */

101         public String JavaDoc
102     getMapClassName()
103     {
104         return( mClassName );
105     }
106     
107     
108         protected void
109     illegalObject( final Object JavaDoc o )
110     {
111         throw new IllegalArgumentException JavaDoc(
112                     "Object is of illegal class and/or non-Serializable class " +
113                     o.getClass().getName() );
114     }
115     
116         protected void
117     checkInJavaUtil( final Object JavaDoc o)
118     {
119         if ( ! o.getClass().getName().startsWith( "java.util." ) )
120         {
121             illegalObject( o );
122         }
123     }
124     
125     /**
126         We restrict the set of legal types to:
127         <ul>
128         <li>OpenTypes</li>
129         <li>Collection whose values meet these restrictions</li>
130         <li>arrays whose values meet these restrictions</li>
131         <li>Map whose keys and values meet these restrictions</li>
132         <li>Throwable which are part of java or javax</li>
133         <li>MapCapable whose Map meets these restrictions (assuming allowMapCapable is true)</li>
134         </ul>
135      */

136         protected final void
137     checkLegalObject(
138         final Object JavaDoc o,
139         final boolean allowMapCapable )
140     {
141         if ( o != null )
142         {
143             // we can't check for "instanceof Serializable" because items such as
144
// HashMap use a non-Serializable key set; it apparently serializes
145
// the key set when it itself is serialized.
146
if ( o instanceof Collection JavaDoc )
147             {
148                 checkInJavaUtil( o );
149
150                 final Collection JavaDoc<?> oc = TypeCast.asCollection( o );
151                 for( final Object JavaDoc next : oc )
152                 {
153                     checkLegalObject( next );
154                 }
155             }
156             else if ( o.getClass().getComponentType() != null )
157             {
158                 // it's an array
159
final Object JavaDoc[] ta = (Object JavaDoc[])o;
160                 for( int i = 0; i < ta.length; ++i )
161                 {
162                     checkLegalObject( ta[i] );
163                 }
164             }
165             else if ( o instanceof MapCapable )
166             {
167                 if ( allowMapCapable )
168                 {
169                     checkLegalObject( ((MapCapable)o).asMap() );
170                 }
171                 else
172                 {
173                     illegalObject( o );
174                 }
175             }
176             else if ( o instanceof Map JavaDoc )
177             {
178                 checkInJavaUtil( o );
179                 
180                 checkLegalObject( ((Map JavaDoc)o).keySet() );
181                 checkLegalObject( ((Map JavaDoc)o).values() );
182             }
183             else if ( o instanceof Throwable JavaDoc )
184             {
185                 final String JavaDoc classname = o.getClass().getName();
186                 
187                 if ( ! (classname.startsWith( "java." ) ||
188                         classname.startsWith( "javax." )) )
189                 {
190                     illegalObject( o );
191                 }
192                 checkLegalObject( asT( ((Throwable JavaDoc)o).getCause() ) );
193             }
194             else if ( OpenMBeanUtil.getSimpleType( o.getClass() ) == null )
195             {
196                 illegalObject( o );
197             }
198         }
199     }
200     
201         private Serializable JavaDoc
202     asT( final Object JavaDoc o )
203     {
204         return Serializable JavaDoc.class.cast( o );
205     }
206     
207         protected final void
208     checkLegalObject( final Object JavaDoc o )
209     {
210         checkLegalObject( o, true );
211     }
212     
213         protected boolean
214     validateNullOrOfType(
215         final String JavaDoc key,
216         final Class JavaDoc<?> theClass )
217     {
218         final Object JavaDoc o = getField( key );
219         
220         return( o == null || theClass.isAssignableFrom( o.getClass() ) );
221     }
222     
223     
224         protected final void
225     validateThrow()
226     {
227         if ( ! validate() )
228         {
229             throw new IllegalArgumentException JavaDoc( toString() );
230         }
231     }
232     
233         protected Serializable JavaDoc
234     asMapHook( final String JavaDoc key, final Serializable JavaDoc value)
235     {
236         Serializable JavaDoc result = value;
237         
238         if ( result instanceof MapCapable )
239         {
240             final Map JavaDoc<String JavaDoc,Serializable JavaDoc> m = ((MapCapable)result).asMap();
241             
242             TypeCast.checkSerializable( m );
243             
244             result = Serializable JavaDoc.class.cast( m );
245         }
246         
247         return result;
248     }
249
250         public final Map JavaDoc<String JavaDoc,Serializable JavaDoc>
251     asMap()
252     {
253         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> result = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
254         
255         // convert all MapCapable into Map
256
for( final String JavaDoc key : getFields().keySet() )
257         {
258             final Serializable JavaDoc value = getField( key );
259             
260             Serializable JavaDoc s = asMapHook( key, value );
261             result.put( key, s );
262         }
263         
264         result.put( MAP_CAPABLE_CLASS_NAME_KEY, getMapClassName() );
265         
266         checkLegalObject( result, false );
267         
268         return( result );
269     }
270     
271     /**
272         Make this Object immutable.
273      */

274         public void
275     makeImmutable()
276     {
277         if ( ! mWasMadeImmutable )
278         {
279             mFields = Collections.unmodifiableMap( mFields );
280             mWasMadeImmutable = true;
281         }
282     }
283     
284     
285     /**
286         Add a field to the object. The Object can choose to reject addition
287         of the field.
288      */

289         public void
290     putField(
291         final String JavaDoc key,
292         final Serializable JavaDoc value )
293     {
294         checkLegalObject( value );
295         
296         final Serializable JavaDoc newValue = putFieldHook( key, value );
297         getFields().put( key, newValue );
298     }
299     
300         protected Serializable JavaDoc
301     putFieldHook(
302         final String JavaDoc key,
303         final Serializable JavaDoc value )
304     {
305         return( value );
306     }
307     
308     /**
309         Add fields one-by-one so we can check compliance with OpenTypes
310      */

311         protected <T extends Serializable JavaDoc>void
312     putAll( final Map JavaDoc<String JavaDoc,T> m )
313     {
314         if ( m != null )
315         {
316             for( final String JavaDoc key : m.keySet() )
317             {
318                 putField( key, m.get( key ) );
319             }
320         }
321     }
322     
323     
324         private Map JavaDoc<String JavaDoc,Serializable JavaDoc>
325     getFields()
326     {
327         return( mFields );
328     }
329     
330         protected Serializable JavaDoc
331     getField( String JavaDoc key )
332     {
333         return( getFields().get( key ) );
334     }
335     
336         protected final Serializable JavaDoc
337     getObject( final String JavaDoc key )
338     {
339         return( getFields().get( key ) );
340     }
341     
342         protected final String JavaDoc
343     getString( final String JavaDoc key )
344     {
345         return( (String JavaDoc)getObject( key ) );
346     }
347     
348         protected final String JavaDoc[]
349     getStringArray( final String JavaDoc key )
350     {
351         return( (String JavaDoc[])getObject( key ) );
352     }
353     
354         protected final Boolean JavaDoc
355     getBoolean( final String JavaDoc key )
356     {
357         return( (Boolean JavaDoc)getObject( key ) );
358     }
359     
360         protected final Byte JavaDoc
361     getByte( final String JavaDoc key )
362     {
363         return( (Byte JavaDoc)getObject( key ) );
364     }
365     
366     
367         protected final boolean
368     getboolean( final String JavaDoc key )
369     {
370         final Boolean JavaDoc b = getBoolean( key );
371         
372         if ( b == null )
373         {
374             throw new IllegalArgumentException JavaDoc( key );
375         }
376         
377         return( b.booleanValue() );
378     }
379     
380         protected final Integer JavaDoc
381     getInteger( final String JavaDoc key )
382     {
383         return( (Integer JavaDoc)getObject( key ) );
384     }
385
386         protected final Map JavaDoc<String JavaDoc,Serializable JavaDoc>
387     getMap( final String JavaDoc key )
388     {
389         return (Map JavaDoc<String JavaDoc,Serializable JavaDoc>)getObject( key );
390     }
391
392         protected final File JavaDoc
393     getFile( final String JavaDoc key )
394     {
395         final String JavaDoc s = getString( key );
396         
397         return( s == null ? null : new File JavaDoc( s ) );
398     }
399     
400         public boolean
401     equals( final Object JavaDoc o )
402     {
403         if ( o == this )
404         {
405             return( true );
406         }
407         else if ( ! (o instanceof MapCapableBase) )
408         {
409             return( false );
410         }
411         
412         boolean equals = false;
413         
414         final MapCapableBase rhs = (MapCapableBase)o;
415         equals = MapUtil.mapsEqual( getFields(), rhs.getFields() );
416         
417         return( equals );
418     }
419     
420         public String JavaDoc
421     toString()
422     {
423         return( MapUtil.toString( getFields() ) );
424     }
425 }
426
427
428
429
430
431
432
433
434
Popular Tags