KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > NotificationWrapperSupport


1 /*
2  * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This package and its source code is available at www.jboss.org
19 **/

20 package org.jboss.jmx.adaptor.snmp.agent;
21
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.management.Notification JavaDoc;
27
28 /**
29  * <tt>NotificationWrapperSupport</tt> provides a base
30  * NotificationWrapper implementation
31  *
32  * @version $Revision: 44604 $
33  *
34  * @author <a HREF="mailto:spol@intracom.gr">Spyros Pollatos</a>
35  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
36 **/

37 public class NotificationWrapperSupport
38    implements NotificationWrapper
39 {
40     /** Holds the notification payload keyed on the attibute name */
41     protected Map JavaDoc payload = new HashMap JavaDoc();
42     
43     /** Provides uptime */
44     protected Clock clock;
45     
46     /** Provides trap count */
47     protected Counter trapCount;
48
49    /**
50     * CTOR
51    **/

52    public NotificationWrapperSupport()
53    {
54       // empty
55
}
56    
57    /**
58     * Loads the hashmap with the DCAs of interest. Note that the keys are used
59     * as attribute tags in the mapping resource file
60    **/

61    public void set(Clock uptime, Counter count)
62    {
63       this.clock = uptime;
64       this.trapCount = count;
65         
66       this.payload.put(STARTTIME_TAG,
67                        new Date JavaDoc(this.clock.instantiationTime()));
68       
69       this.payload.put(UPTIME_TAG, // anonymous class
70
new DynamicContentAccessor() {
71               public Object JavaDoc get()
72               {
73                  return new Long JavaDoc(NotificationWrapperSupport.this.clock.uptime());
74               }
75            });
76
77       this.payload.put(TRAPCOUNT_TAG, // anonymous class
78
new DynamicContentAccessor() {
79               public Object JavaDoc get()
80               {
81                  return new Long JavaDoc(NotificationWrapperSupport.this.trapCount.peek());
82               }
83            });
84    }
85
86    /**
87     * Set the notification to be used as the data source. Load the hashmap
88     * with all of the notification contents. Note that the keys are used
89     * as attribute tags in the mapping resource file
90     *
91     * @param n the notification to be used as data source at subsequent calls
92     * of get()
93    **/

94    public void prime(Notification JavaDoc n)
95    {
96       // Get fixed event payload and general info
97
this.payload.put(MESSAGE_TAG, n.getMessage());
98       this.payload.put(SEQNO_TAG, new Long JavaDoc(n.getSequenceNumber()));
99       this.payload.put(TSTAMP_TAG, new Long JavaDoc(n.getTimeStamp()));
100       this.payload.put(TYPE_TAG, n.getType());
101       this.payload.put(ALL_TAG, n.toString());
102       this.payload.put(CLASS_TAG, n.getClass().getName());
103
104       // Check if event contains anything in the user field. If there is, an
105
// attempt is made to interpret it as a hash map and copy it. Note
106
// that previous content may be overwritten if the same keys as above
107
// are used
108
Object JavaDoc userData = n.getUserData();
109       if (userData instanceof HashMap JavaDoc) {
110          // Copy all of the user data in the payload
111
this.payload.putAll((HashMap JavaDoc)userData);
112       }
113    } // prime
114

115    /**
116     * Implements the communication protocol between the caller and the data
117     * source (notification and agent) based on tags. Implementations are
118     * expected to map the provided attribute name to some aspect of the
119     * notification payload. The later is defined by method prime.
120     *
121     * @param tagName the tag of the attribute the value of which is required
122    **/

123    public Object JavaDoc get(String JavaDoc tagName)
124       throws MappingFailedException
125    {
126       Object JavaDoc o = this.payload.get(tagName);
127        
128       if (o == null)
129          throw new MappingFailedException("Tag \"" + tagName + "\" not found");
130        
131       // Check whether value returned is a dynamic content accessor. If not
132
// return as is. If yes invoke the accessor and return that value
133
if (o instanceof DynamicContentAccessor) {
134          DynamicContentAccessor d = (DynamicContentAccessor)o;
135
136          return d.get();
137       }
138       else {
139          return o;
140       }
141    } //get
142

143 } // NotificationWrapperSupport
144
Popular Tags