001: /*
002: * Copyright (c) 2003, Intracom S.A. - www.intracom.com
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * This package and its source code is available at www.jboss.org
019: **/
020: package org.jboss.jmx.adaptor.snmp.agent;
021:
022: import java.util.Date;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.management.Notification;
027:
028: /**
029: * <tt>NotificationWrapperSupport</tt> provides a base
030: * NotificationWrapper implementation
031: *
032: * @version $Revision: 23902 $
033: *
034: * @author <a href="mailto:spol@intracom.gr">Spyros Pollatos</a>
035: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
036: **/
037: public class NotificationWrapperSupport implements NotificationWrapper {
038: /** Holds the notification payload keyed on the attibute name */
039: protected Map payload = new HashMap();
040:
041: /** Provides uptime */
042: protected Clock clock;
043:
044: /** Provides trap count */
045: protected Counter trapCount;
046:
047: /**
048: * CTOR
049: **/
050: public NotificationWrapperSupport() {
051: // empty
052: }
053:
054: /**
055: * Loads the hashmap with the DCAs of interest. Note that the keys are used
056: * as attribute tags in the mapping resource file
057: **/
058: public void set(Clock uptime, Counter count) {
059: this .clock = uptime;
060: this .trapCount = count;
061:
062: this .payload.put(STARTTIME_TAG, new Date(this .clock
063: .instantiationTime()));
064:
065: this .payload.put(UPTIME_TAG, // anonymous class
066: new DynamicContentAccessor() {
067: public Object get() {
068: return new Long(
069: NotificationWrapperSupport.this .clock
070: .uptime());
071: }
072: });
073:
074: this .payload.put(TRAPCOUNT_TAG, // anonymous class
075: new DynamicContentAccessor() {
076: public Object get() {
077: return new Long(
078: NotificationWrapperSupport.this .trapCount
079: .peek());
080: }
081: });
082: }
083:
084: /**
085: * Set the notification to be used as the data source. Load the hashmap
086: * with all of the notification contents. Note that the keys are used
087: * as attribute tags in the mapping resource file
088: *
089: * @param n the notification to be used as data source at subsequent calls
090: * of get()
091: **/
092: public void prime(Notification n) {
093: // Get fixed event payload and general info
094: this .payload.put(MESSAGE_TAG, n.getMessage());
095: this .payload.put(SEQNO_TAG, new Long(n.getSequenceNumber()));
096: this .payload.put(TSTAMP_TAG, new Long(n.getTimeStamp()));
097: this .payload.put(TYPE_TAG, n.getType());
098: this .payload.put(ALL_TAG, n.toString());
099: this .payload.put(CLASS_TAG, n.getClass().getName());
100:
101: // Check if event contains anything in the user field. If there is, an
102: // attempt is made to interpret it as a hash map and copy it. Note
103: // that previous content may be overwritten if the same keys as above
104: // are used
105: Object userData = n.getUserData();
106: if (userData instanceof HashMap) {
107: // Copy all of the user data in the payload
108: this .payload.putAll((HashMap) userData);
109: }
110: } // prime
111:
112: /**
113: * Implements the communication protocol between the caller and the data
114: * source (notification and agent) based on tags. Implementations are
115: * expected to map the provided attribute name to some aspect of the
116: * notification payload. The later is defined by method prime.
117: *
118: * @param tagName the tag of the attribute the value of which is required
119: **/
120: public Object get(String tagName) throws MappingFailedException {
121: Object o = this .payload.get(tagName);
122:
123: if (o == null)
124: throw new MappingFailedException("Tag \"" + tagName
125: + "\" not found");
126:
127: // Check whether value returned is a dynamic content accessor. If not
128: // return as is. If yes invoke the accessor and return that value
129: if (o instanceof DynamicContentAccessor) {
130: DynamicContentAccessor d = (DynamicContentAccessor) o;
131:
132: return d.get();
133: } else {
134: return o;
135: }
136: } //get
137:
138: } // NotificationWrapperSupport
|