KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > stringifier > NotificationStringifier


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.util.jmx.stringifier;
24
25 import java.util.Date JavaDoc;
26 import javax.management.Notification JavaDoc;
27 import javax.management.MBeanServerNotification JavaDoc;
28
29 import com.sun.appserv.management.util.stringifier.Stringifier;
30 import com.sun.appserv.management.util.stringifier.SmartStringifier;
31 import com.sun.appserv.management.util.misc.StringUtil;
32
33 public class NotificationStringifier implements Stringifier
34 {
35     public static final NotificationStringifier DEFAULT = new NotificationStringifier();
36     
37     protected Options mOptions;
38     
39     public final static class Options
40     {
41         // don't make 'final' fields; allow changes after instantiation
42
public boolean mIncludeObjectName;
43         public boolean mIncludeTimeStamp;
44         public boolean mIncludeType;
45         public boolean mIncludeSequenceNumber;
46         public boolean mIncludeUserData;
47         public String JavaDoc mDelim;
48         
49             public
50         Options()
51         {
52             mIncludeObjectName = true;
53             mIncludeTimeStamp = true;
54             mIncludeType = true;
55             mIncludeSequenceNumber = true;
56             mIncludeUserData = false;
57             mDelim = ", ";
58         }
59         
60     }
61     
62     
63         public
64     NotificationStringifier( )
65     {
66         mOptions = new Options();
67     }
68     
69         public
70     NotificationStringifier( Options options )
71     {
72         mOptions = options;
73     }
74         
75         protected void
76     append( StringBuffer JavaDoc b, Object JavaDoc o)
77     {
78         if ( b.length() != 0 )
79         {
80             b.append( mOptions.mDelim );
81         }
82         
83         b.append( SmartStringifier.toString( o ) );
84     }
85     
86         public String JavaDoc
87     stringify( Object JavaDoc o )
88     {
89         final Notification JavaDoc notif = (Notification JavaDoc)o;
90         
91         return( _stringify( notif ).toString() );
92     }
93     
94         public static String JavaDoc
95     toString( Object JavaDoc o )
96     {
97         return( DEFAULT.stringify( o ) );
98     }
99     
100         protected StringBuffer JavaDoc
101     _stringify( Notification JavaDoc notif )
102     {
103         final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
104         
105         if ( mOptions.mIncludeSequenceNumber )
106         {
107             append( b, "#" + notif.getSequenceNumber() );
108         }
109
110         if ( mOptions.mIncludeTimeStamp )
111         {
112             append( b, new Date JavaDoc( notif.getTimeStamp() ) );
113         }
114
115         if ( mOptions.mIncludeObjectName )
116         {
117             append( b, StringUtil.quote( notif.getSource() ) );
118         }
119
120         if ( mOptions.mIncludeType )
121         {
122             append( b, notif.getType() );
123         }
124
125         if ( mOptions.mIncludeUserData )
126         {
127             append( b, StringUtil.quote( notif.getUserData() ) );
128         }
129         
130         if ( notif instanceof MBeanServerNotification JavaDoc )
131         {
132             // this should really be done in a MBeanServerNotificationStringifier!
133
final MBeanServerNotification JavaDoc n = (MBeanServerNotification JavaDoc)notif;
134             
135             append( b, StringUtil.quote( n.getMBeanName() ) );
136         }
137         
138         return( b );
139     }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Popular Tags