001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.mfwk;
006:
007: import com.sun.cmm.cim.OperationalStatus;
008: import com.sun.mfwk.instrum.me.CIM_ManagedElementInstrum;
009: import com.sun.mfwk.instrum.me.CMM_ServiceInstrum;
010: import com.sun.mfwk.instrum.me.MfManagedElementInstrumException;
011: import com.sun.mfwk.instrum.me.settings.CMM_PSServiceSettingInstrum;
012: import com.sun.mfwk.instrum.me.statistics.CMM_PSServiceStatsInstrum;
013: import com.sun.mfwk.instrum.me.statistics.MfTransactionInstrum;
014: import com.sun.mfwk.instrum.me.statistics.MfTransactionInstrumConstants;
015: import com.sun.mfwk.instrum.relations.MfRelationInstrumException;
016: import com.sun.mfwk.instrum.server.MfManagedElementInfo;
017: import com.sun.mfwk.instrum.server.MfManagedElementServer;
018: import com.sun.mfwk.instrum.server.MfManagedElementServerException;
019: import com.sun.mfwk.instrum.server.MfManagedElementType;
020: import com.sun.mfwk.instrum.server.MfRelationType;
021: import com.sun.portal.log.common.PortalLogger;
022: import java.util.Arrays;
023: import java.util.HashSet;
024: import java.util.logging.Level;
025: import java.util.logging.LogRecord;
026: import java.util.logging.Logger;
027: import javax.management.AttributeNotFoundException;
028: import javax.management.MBeanException;
029: import javax.management.ReflectionException;
030:
031: public class MfwkPSServiceAdaptor {
032: private static final Logger logger = PortalLogger
033: .getLogger(MfwkPSServiceAdaptor.class);
034:
035: private static LogRecord getLogRecord(Level level, String message,
036: Object[] parameters, Throwable t) {
037: LogRecord result = new LogRecord(level, message);
038: result.setLoggerName(logger.getName());
039: result.setParameters(parameters);
040: result.setThrown(t);
041: return result;
042: }
043:
044: private String name;
045: private String settingName;
046: private String statsName;
047: private int lowerBound;
048: private int upperBound;
049:
050: public MfwkPSServiceAdaptor(String name, String settingName,
051: String statsName, int lowerBound, int upperBound) {
052: setName(name);
053: setSettingName(settingName);
054: setStatsName(statsName);
055: setLowerBound(lowerBound);
056: setUpperBound(upperBound);
057: }
058:
059: private boolean gearedUp = false;
060: private MfManagedElementInfo info;
061: private MfManagedElementInfo settingInfo;
062: private MfManagedElementInfo statsInfo;
063: private MfwkPSRelationAdaptor relation;
064: private MfwkPSRelationAdaptor settingRelation;
065: private MfwkPSRelationAdaptor statsRelation;
066: private ThreadLocal transactionInstrum = new ThreadLocal();
067:
068: public boolean isGearedUp() {
069: return gearedUp;
070: }
071:
072: public void setGearedUp(boolean gearedUp) {
073: this .gearedUp = gearedUp;
074: }
075:
076: public String getName() {
077: return name;
078: }
079:
080: public void setName(String name) {
081: this .name = name;
082: }
083:
084: public String getSettingName() {
085: return settingName;
086: }
087:
088: public void setSettingName(String settingName) {
089: this .settingName = settingName;
090: }
091:
092: public String getStatsName() {
093: return statsName;
094: }
095:
096: public void setStatsName(String statsName) {
097: this .statsName = statsName;
098: }
099:
100: public int getLowerBound() {
101: return lowerBound;
102: }
103:
104: public void setLowerBound(int lowerBound) {
105: this .lowerBound = lowerBound;
106: }
107:
108: public int getUpperBound() {
109: return upperBound;
110: }
111:
112: public void setUpperBound(int upperBound) {
113: this .upperBound = upperBound;
114: }
115:
116: public MfManagedElementInfo getInfo() {
117: return info;
118: }
119:
120: public void setInfo(MfManagedElementInfo info) {
121: this .info = info;
122: }
123:
124: public MfManagedElementInfo getSettingInfo() {
125: return settingInfo;
126: }
127:
128: public void setSettingInfo(MfManagedElementInfo settingInfo) {
129: this .settingInfo = settingInfo;
130: }
131:
132: public MfManagedElementInfo getStatsInfo() {
133: return statsInfo;
134: }
135:
136: public void setStatsInfo(MfManagedElementInfo statsInfo) {
137: this .statsInfo = statsInfo;
138: }
139:
140: public MfwkPSRelationAdaptor getRelation() {
141: return relation;
142: }
143:
144: public void setRelation(MfwkPSRelationAdaptor relation) {
145: this .relation = relation;
146: }
147:
148: public MfwkPSRelationAdaptor getSettingRelation() {
149: return settingRelation;
150: }
151:
152: public void setSettingRelation(MfwkPSRelationAdaptor settingRelation) {
153: this .settingRelation = settingRelation;
154: }
155:
156: public MfwkPSRelationAdaptor getStatsRelation() {
157: return statsRelation;
158: }
159:
160: public void setStatsRelation(MfwkPSRelationAdaptor statsRelation) {
161: this .statsRelation = statsRelation;
162: }
163:
164: public MfTransactionInstrum getTransactionInstrum() {
165: return (MfTransactionInstrum) transactionInstrum.get();
166: }
167:
168: public void setTransactionInstrum(
169: MfTransactionInstrum transactionInstrum) {
170: this .transactionInstrum.set(transactionInstrum);
171: }
172:
173: public void gearUp(MfManagedElementServer mfmeServer,
174: MfRelationType relationType,
175: CIM_ManagedElementInstrum sourceManagedElementInstrum)
176: throws MfManagedElementInstrumException,
177: MfManagedElementServerException, MfRelationInstrumException {
178: setInfo(mfmeServer.makeManagedElementInfo());
179: getInfo().setName(getName());
180: getInfo().setType(MfManagedElementType.CMM_SERVICE);
181: setRelation(new MfwkPSRelationAdaptor(relationType,
182: sourceManagedElementInstrum));
183: getRelation().gearUp(mfmeServer, getInfo());
184: OperationalStatus[] operationalStatus = new OperationalStatus[] { OperationalStatus.OK };
185: getInstrum().setOperationalStatus(
186: new HashSet(Arrays.asList(operationalStatus)));
187:
188: setSettingInfo(mfmeServer.makeManagedElementInfo());
189: getSettingInfo().setName(getSettingName());
190: getSettingInfo().setType(
191: MfManagedElementType.CMM_PS_SERVICE_SETTING);
192: setSettingRelation(new MfwkPSRelationAdaptor(
193: MfRelationType.CMM_SCOPED_SETTING, getInstrum()));
194: getSettingRelation().gearUp(mfmeServer, getSettingInfo());
195: getSettingInstrum().setResidentTimeRollingAvgLowerBound(
196: getLowerBound());
197: getSettingInstrum().setResidentTimeRollingAvgUpperBound(
198: getUpperBound());
199: getSettingInstrum().setServiceTimeRollingAvgLowerBound(
200: getLowerBound());
201: getSettingInstrum().setServiceTimeRollingAvgUpperBound(
202: getUpperBound());
203:
204: setStatsInfo(mfmeServer.makeManagedElementInfo());
205: getStatsInfo().setName(getStatsName());
206: getStatsInfo().setType(
207: MfManagedElementType.CMM_PS_SERVICE_STATS);
208: setStatsRelation(new MfwkPSRelationAdaptor(
209: MfRelationType.CMM_ELEMENT_STATISTICAL_DATA,
210: getInstrum()));
211: getStatsRelation().gearUp(mfmeServer, getStatsInfo());
212: getStatsInstrum().setResidentTimeRollingAvg(0); // set to bad value, calculate appropriately
213: getStatsInstrum().setServiceTimeRollingAvg(0); // set to bad value, calculate appropriately
214: getStatsInstrum().setInRequestsInBytes(0); // play safe, we really don't care about this attribute
215: getStatsInstrum().setOutRequestsInBytes(0); // play safe, we really don't care about this attribute
216: getStatsInstrum().setSampleInterval(0); // play safe, we really don't care about this attribute
217:
218: setGearedUp(true);
219: }
220:
221: public CMM_ServiceInstrum getInstrum()
222: throws MfRelationInstrumException {
223: return (CMM_ServiceInstrum) getRelation().getInstrum()
224: .getDestination();
225: }
226:
227: public CMM_PSServiceSettingInstrum getSettingInstrum()
228: throws MfRelationInstrumException {
229: return (CMM_PSServiceSettingInstrum) getSettingRelation()
230: .getInstrum().getDestination();
231: }
232:
233: public CMM_PSServiceStatsInstrum getStatsInstrum()
234: throws MfRelationInstrumException {
235: return (CMM_PSServiceStatsInstrum) getStatsRelation()
236: .getInstrum().getDestination();
237: }
238:
239: public void startTransaction() {
240: if (!isGearedUp()) {
241: return;
242: }
243:
244: startTransaction(null);
245: }
246:
247: public void startTransaction(MfTransactionInstrum parent) {
248: if (!isGearedUp()) {
249: return;
250: }
251:
252: try {
253: if (!getInstrum().isMonitoringEnabled()) {
254: return;
255: }
256:
257: if (getTransactionInstrum() == null) {
258: if (parent == null) {
259: setTransactionInstrum(getStatsInstrum()
260: .getTransaction());
261: } else {
262: setTransactionInstrum(getStatsInstrum()
263: .getTransaction(parent));
264: }
265: }
266:
267: if (getTransactionInstrum().start() == MfTransactionInstrumConstants.NOT_OK) {
268: if (logger.isLoggable(Level.SEVERE)) {
269: logger.log(getLogRecord(Level.SEVERE,
270: "PSDT_CSPDMF0002",
271: new Object[] { getTransactionInstrum()
272: .getErrorMessage(
273: getTransactionInstrum()
274: .getErrorCode()) },
275: null));
276: }
277: }
278: } catch (MfRelationInstrumException e) {
279: if (logger.isLoggable(Level.SEVERE)) {
280: logger.log(getLogRecord(Level.SEVERE,
281: "PSDT_CSPDMF0001", new Object[] { e
282: .getLocalizedMessage() }, e));
283: }
284: } catch (MfManagedElementInstrumException e) {
285: if (logger.isLoggable(Level.SEVERE)) {
286: logger.log(getLogRecord(Level.SEVERE,
287: "PSDT_CSPDMF0001", new Object[] { e
288: .getLocalizedMessage() }, e));
289: }
290: }
291: }
292:
293: public void stopTransaction() {
294: if (!isGearedUp()) {
295: return;
296: }
297:
298: if (getTransactionInstrum() == null) {
299: // Monitoring is possibly disabled.
300: return;
301: }
302:
303: int status = getTransactionInstrum().stop(
304: MfTransactionInstrumConstants.STATUS_GOOD);
305: if (status == MfTransactionInstrumConstants.NOT_OK) {
306: if (logger.isLoggable(Level.SEVERE)) {
307: logger.log(getLogRecord(Level.SEVERE,
308: "PSDT_CSPDMF0003",
309: new Object[] { getTransactionInstrum()
310: .getErrorMessage(
311: getTransactionInstrum()
312: .getErrorCode()) },
313: null));
314: }
315: } else {
316: try {
317: MfwkPSUtils.updateRollingAverageTimes(
318: getSettingInstrum(), getStatsInstrum());
319: } catch (MfRelationInstrumException e) {
320: if (logger.isLoggable(Level.SEVERE)) {
321: logger.log(getLogRecord(Level.SEVERE,
322: "PSDT_CSPDMF0001", new Object[] { e
323: .getLocalizedMessage() }, e));
324: }
325: } catch (AttributeNotFoundException e) {
326: if (logger.isLoggable(Level.SEVERE)) {
327: logger.log(getLogRecord(Level.SEVERE,
328: "PSDT_CSPDMF0001", new Object[] { e
329: .getLocalizedMessage() }, e));
330: }
331: } catch (MBeanException e) {
332: if (logger.isLoggable(Level.SEVERE)) {
333: logger.log(getLogRecord(Level.SEVERE,
334: "PSDT_CSPDMF0001", new Object[] { e
335: .getLocalizedMessage() }, e));
336: }
337: } catch (MfManagedElementInstrumException e) {
338: if (logger.isLoggable(Level.SEVERE)) {
339: logger.log(getLogRecord(Level.SEVERE,
340: "PSDT_CSPDMF0001", new Object[] { e
341: .getLocalizedMessage() }, e));
342: }
343: } catch (ReflectionException e) {
344: if (logger.isLoggable(Level.SEVERE)) {
345: logger.log(getLogRecord(Level.SEVERE,
346: "PSDT_CSPDMF0001", new Object[] { e
347: .getLocalizedMessage() }, e));
348: }
349: }
350: }
351:
352: // Get NPE to help debugging
353: setTransactionInstrum(null);
354: }
355: }
|