001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: /*
037: * CompositeDataUtils.java
038: */
039: package com.sun.jbi.jsf.util;
040:
041: import com.sun.jbi.ui.common.JBIStatisticsItemNames;
042: import com.sun.jbi.ui.common.JBITimeUtil;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Date;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Properties;
049: import java.util.Set;
050: import java.util.logging.Logger;
051: import javax.management.openmbean.CompositeData;
052: import javax.management.openmbean.CompositeType;
053: import javax.management.openmbean.TabularData;
054:
055: /**
056: * Utilities to format alert messages
057: *
058: * @author Sun Microsystems Inc.
059: */
060:
061: public final class CompositeDataUtils {
062:
063: /**
064: * @param aTableOfData Description of Parameter
065: * @param anInstanceName Description of Parameter
066: * @return The DataForInstance value
067: */
068: public static CompositeData getDataForInstance(
069: TabularData aTableOfData, String anInstanceName) {
070: CompositeData result = null;
071:
072: Collection values = null;
073: if ((null != anInstanceName) && (null != aTableOfData)) {
074: values = aTableOfData.values();
075: }
076: Iterator iterator = null;
077: if (null != values) {
078: iterator = values.iterator();
079: }
080: instanceLoop: while ((null != iterator) && (iterator.hasNext())) {
081: CompositeData compData = (CompositeData) iterator.next();
082:
083: String compDataInstanceName = (String) compData
084: .get(JBIStatisticsItemNames.INSTANCE_NAME);
085:
086: // If data is for a different instance, skip it
087: if (!anInstanceName.equals(compDataInstanceName)) {
088: continue instanceLoop;
089: } else {
090: result = compData;
091: break instanceLoop;
092: }
093: }
094: return result;
095: }
096:
097: /**
098: * prints the contents of a CompositeData
099: *
100: * @param aCompositeData to dump
101: */
102: public static void dump(CompositeData aCompositeData) {
103: CompositeType compType = aCompositeData.getCompositeType();
104:
105: Set compItemSet = compType.keySet();
106:
107: String compTypeName = aCompositeData.getCompositeType()
108: .getTypeName();
109: sLog.fine("addProvidingEndpointsGroup, compTypeName="
110: + compTypeName);
111: Iterator debugIt = compItemSet.iterator();
112: while (debugIt.hasNext()) {
113: Object next = debugIt.next();
114: sLog.fine("CompositeDataUtils.dump debugIt.next.class="
115: + next.getClass() + ", value=" + next);
116: }
117: }
118:
119: /**
120: * find endpoints on the specified instance for the specified component
121: *
122: * @param aNMRStatsTD contains all active endpoints and their
123: * component and instance
124: * @param anInstanceName to filter for
125: * @param aCompName to filter for
126: * @return list of endpoints on the specified instance for
127: * the specified component
128: */
129: public static List findComponentEndpoints(TabularData aNMRStatsTD,
130: String anInstanceName, String aCompName) {
131: List result = new ArrayList();
132:
133: sLog.fine("CompositeDataUtils.findComponentEndpoints("
134: + aNMRStatsTD + ", " + anInstanceName + ", "
135: + aCompName + ")");
136:
137: CompositeData compData = CompositeDataUtils.getDataForInstance(
138: aNMRStatsTD, anInstanceName);
139:
140: sLog
141: .fine("CompositeDataUtils.findComponentEndpoints(), compData="
142: + compData);
143:
144: Object activeEndpoints = compData
145: .get(JBIStatisticsItemNames.NMR_STATS_ACTIVE_ENDPOINTS);
146: if ((null != activeEndpoints)
147: && (activeEndpoints instanceof String[])) {
148: String clusterOrSaTargetName = ClusterUtilities
149: .getInstanceDomainCluster(anInstanceName);
150:
151: String[] activeEndpointsArray = (String[]) activeEndpoints;
152:
153: for (int i = 0; i < activeEndpointsArray.length; ++i) {
154: // get endpoint data
155: Properties statusProps = new Properties();
156: TabularData activeEndpointsTD = RuntimeMonitoringUtils
157: .getEndpointStats(activeEndpointsArray[i],
158: clusterOrSaTargetName, statusProps);
159: if (null != activeEndpointsTD) {
160: // filter for instance
161: CompositeData activeEndpointsCD = CompositeDataUtils
162: .getDataForInstance(activeEndpointsTD,
163: anInstanceName);
164:
165: // filter for component
166: String compName = (String) activeEndpointsCD
167: .get(JBIStatisticsItemNames.ENDPOINT_COMPONENT_NAME);
168: if (aCompName.equals(compName)) {
169: result.add(activeEndpointsCD);
170: }
171: }
172:
173: }
174:
175: }
176:
177: if (null != compData) {
178: sLog.fine("CompositeDataUtils.findComponentEndpoints nmr:");
179: dump(compData);
180: // TBD add to result...
181: }
182: sLog
183: .fine("CompositeDataUtils.findComponentEndpoints(), result="
184: + result);
185:
186: return result;
187: }
188:
189: /**
190: * Gets a String representation of a simple data item for the given <code>CompositeData</code>
191: * for the specified key.
192: *
193: * @param aCompData the composite data
194: * @param anItemKey the key to extract the object from the composite data
195: * @return the value of the specified item
196: */
197: public static String findCompositeItem(CompositeData aCompData,
198: String anItemKey) {
199: String result = null;
200:
201: CompositeType compType = aCompData.getCompositeType();
202:
203: Set compItemSet = compType.keySet();
204:
205: if (compItemSet.contains(anItemKey)) {
206: Object item = aCompData.get(anItemKey);
207:
208: if (null != item) {
209: result = item.toString();
210: }
211: }
212: return result;
213: }
214:
215: /**
216: * Gets a List data item for the given <code>CompositeData</code> for the
217: * specified key.
218: *
219: * @param aCompData the composite data
220: * @param anItemKey the key to extract the object from the composite data
221: * @return the value of the specified item as a List
222: */
223: public static CompositeData[] findCompositeArrayItem(
224: CompositeData aCompData, String anItemKey) {
225: CompositeData[] result = null;
226:
227: CompositeType compType = aCompData.getCompositeType();
228:
229: Set compItemSet = compType.keySet();
230:
231: if (compItemSet.contains(anItemKey)) {
232: Object item = aCompData.get(anItemKey);
233:
234: if ((null != item) && (item instanceof CompositeData[])) {
235: result = (CompositeData[]) item;
236: }
237: }
238: return result;
239: }
240:
241: /**
242: * Gets the response time from the given <code>CompositeData</code> for
243: * the specified key.
244: *
245: * @param aCompData the composite data
246: * @param anItemKey the key to extract the object from the composite data
247: * @return an <code>Object[]</code> containing the response time
248: */
249: public static Object[] findCompositeResponseTime(
250: CompositeData aCompData, String anItemKey) {
251: Object[] result = null;
252:
253: CompositeType compType = aCompData.getCompositeType();
254:
255: Set compItemSet = compType.keySet();
256:
257: if (compItemSet.contains(anItemKey)) {
258: Double responseTime = (Double) aCompData.get(anItemKey);
259:
260: if (null != responseTime) {
261: result = new Object[] { responseTime, };
262: }
263: }
264: return result;
265: }
266:
267: /**
268: * Gets the response Date from the given <code>CompositeData</code> for
269: * the specified key.
270: *
271: * @param aCompData the composite data
272: * @param anItemKey the key to extract the object from the composite data
273: * @return an <code>Date</code> containing the response time
274: */
275: public static Date findCompositeDate(CompositeData aCompData,
276: String anItemKey) {
277: Date result = null;
278:
279: CompositeType compType = aCompData.getCompositeType();
280:
281: Set compItemSet = compType.keySet();
282:
283: if (compItemSet.contains(anItemKey)) {
284: Object item = aCompData.get(anItemKey);
285:
286: if ((null != item) && (item instanceof Date)) {
287: result = (Date) item;
288: }
289: }
290: return result;
291: }
292:
293: /**
294: * Gets an elapsed time from the given <code>CompositeData</code> using
295: * the specified key.
296: *
297: * @param aCompData the composite data
298: * @param anItemKey the key to extract the object from the composite data
299: * @return an <code>Object[]</code> of time unit inserts (days,
300: * hours, minutes, and seconds)
301: */
302: public static Object[] findCompositeUpTime(CompositeData aCompData,
303: String anItemKey) {
304: Object[] result = null;
305:
306: CompositeType compType = aCompData.getCompositeType();
307:
308: Set compItemSet = compType.keySet();
309:
310: if (compItemSet.contains(anItemKey)) {
311: Long upTime = (Long) aCompData.get(anItemKey);
312:
313: if (null != upTime) {
314: JBITimeUtil elapsedTime = new JBITimeUtil(upTime
315: .longValue());
316:
317: result = new Object[] { elapsedTime.getDays(),
318: elapsedTime.getHours(),
319: elapsedTime.getMinutes(),
320: elapsedTime.getSeconds(), };
321: }
322: }
323: return result;
324: }
325:
326: /**
327: * Controls printing of diagnostic messages to the log
328: */
329: private static Logger sLog = JBILogger.getInstance();
330:
331: /**
332: * Utility class - constructor not used.
333: */
334: private CompositeDataUtils() {
335: }
336:
337: }
|