001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring;
006:
007: import com.sun.jdmk.comm.AuthInfo;
008: import com.sun.jdmk.comm.HtmlAdaptorServer;
009: import com.sun.portal.log.common.PortalLogger;
010:
011: import javax.management.*;
012: import javax.management.remote.JMXConnectorServer;
013: import java.io.IOException;
014: import java.lang.reflect.InvocationTargetException;
015: import java.lang.reflect.Method;
016: import java.util.ArrayList;
017: import java.util.List;
018: import java.util.Set;
019: import java.util.logging.Logger;
020: import java.util.logging.Level;
021: import java.util.logging.LogRecord;
022:
023: public class SubsystemHelper {
024: private static final Logger logger = PortalLogger
025: .getLogger(SubsystemHelper.class);
026:
027: private static LogRecord getLogRecord(Level level, String message,
028: Object[] parameters, Throwable t) {
029: LogRecord result = new LogRecord(level, message);
030: result.setLoggerName(logger.getName());
031: result.setParameters(parameters);
032: result.setThrown(t);
033: return result;
034: }
035:
036: public static MBeanServer getJavaPlatformMBeanServer() {
037: MBeanServer mBeanServer = null;
038:
039: try {
040: Class managementFactory = Class
041: .forName("java.lang.management.ManagementFactory");
042: Method getPlatformMBeanServerMethod = managementFactory
043: .getMethod("getPlatformMBeanServer", null);
044: mBeanServer = (MBeanServer) getPlatformMBeanServerMethod
045: .invoke(managementFactory, null);
046: } catch (ClassNotFoundException e) {
047: if (logger.isLoggable(Level.SEVERE)) {
048: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
049: new Object[] { e.getLocalizedMessage() }, e));
050: }
051: } catch (NoSuchMethodException e) {
052: if (logger.isLoggable(Level.SEVERE)) {
053: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
054: new Object[] { e.getLocalizedMessage() }, e));
055: }
056: } catch (IllegalAccessException e) {
057: if (logger.isLoggable(Level.SEVERE)) {
058: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
059: new Object[] { e.getLocalizedMessage() }, e));
060: }
061: } catch (InvocationTargetException e) {
062: if (logger.isLoggable(Level.SEVERE)) {
063: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
064: new Object[] { e.getLocalizedMessage() }, e));
065: }
066: }
067:
068: return mBeanServer;
069: }
070:
071: public static ObjectName getMonitoringObjectName(String domain,
072: String typePropertyValue) throws MonitoringException {
073: try {
074: return new ObjectName(domain, JMXHelper.TYPE,
075: typePropertyValue);
076: } catch (MalformedObjectNameException mone) {
077: throw new MonitoringException(mone);
078: }
079: }
080:
081: public static ObjectName getConnectorServerObjectName(
082: MonitoringContext monitoringContext)
083: throws MonitoringException {
084: return getMonitoringObjectName(monitoringContext
085: .getNamingDomain(), monitoringContext
086: .getNamingTypeConnectorServer());
087: }
088:
089: public static ObjectName getHtmlAdaptorObjectName(
090: MonitoringContext monitoringContext)
091: throws MonitoringException {
092: return getMonitoringObjectName(monitoringContext
093: .getNamingDomain(), monitoringContext
094: .getNamingTypeHtmlAdaptorServer());
095: }
096:
097: public static ObjectName getMonitoringSubsystemObjectName(
098: MonitoringContext monitoringContext)
099: throws MonitoringException {
100: return getMonitoringObjectName(monitoringContext
101: .getNamingDomain(), monitoringContext
102: .getNamingTypeMonitoringConfiguration());
103: }
104:
105: public static void gearUpHtmlAdaptor(
106: MonitoringContext monitoringContext, MBeanServer mBeanServer)
107: throws MonitoringException {
108: String htmlAdaptorPort = monitoringContext.getHtmlAdaptorPort();
109: if (!htmlAdaptorPort
110: .equals(MonitoringContext.HTML_ADAPTOR_PORT)) {
111: AuthInfo[] aAuthInfo = new AuthInfo[] { new AuthInfo(
112: monitoringContext.getUserName(), monitoringContext
113: .getUserPassword()) };
114: HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer(
115: Integer.parseInt(htmlAdaptorPort), aAuthInfo);
116:
117: try {
118: mBeanServer.registerMBean(htmlAdaptorServer,
119: getHtmlAdaptorObjectName(monitoringContext));
120: } catch (InstanceAlreadyExistsException iaee) {
121: throw new MonitoringException(iaee);
122: } catch (MBeanRegistrationException mbre) {
123: throw new MonitoringException(mbre);
124: } catch (NotCompliantMBeanException ncmbe) {
125: throw new MonitoringException(ncmbe);
126: }
127:
128: htmlAdaptorServer.start();
129: }
130: }
131:
132: public static void windDownHtmlAdaptor(
133: MonitoringContext monitoringContext, MBeanServer mBeanServer)
134: throws MonitoringException {
135: ObjectName on = getHtmlAdaptorObjectName(monitoringContext);
136:
137: Set onNames = mBeanServer.queryNames(on, null);
138: if (onNames != null) {
139: if (!onNames.isEmpty()) {
140: try {
141: mBeanServer.invoke(on, "stop", null, null);
142: } catch (InstanceNotFoundException infe) {
143: throw new MonitoringException(infe);
144: } catch (MBeanException mbe) {
145: throw new MonitoringException(mbe);
146: } catch (ReflectionException re) {
147: throw new MonitoringException(re);
148: }
149:
150: try {
151: mBeanServer.unregisterMBean(on);
152: } catch (InstanceNotFoundException infe) {
153: throw new MonitoringException(infe);
154: } catch (MBeanRegistrationException mbre) {
155: throw new MonitoringException(mbre);
156: }
157: }
158: }
159: }
160:
161: public static void gearUpConnectorServer(
162: MonitoringContext monitoringContext, MBeanServer mBeanServer)
163: throws MonitoringException {
164: JMXConnectorServer jmxConnectorServer = ConnectorFactory
165: .getJMXConnectorServer(monitoringContext
166: .getConnectorContext(), monitoringContext
167: .getSaslContext(), monitoringContext
168: .getSslContext());
169:
170: try {
171: mBeanServer.registerMBean(jmxConnectorServer,
172: getConnectorServerObjectName(monitoringContext));
173: } catch (InstanceAlreadyExistsException iaee) {
174: throw new MonitoringException(iaee);
175: } catch (MBeanRegistrationException mbre) {
176: throw new MonitoringException(mbre);
177: } catch (NotCompliantMBeanException ncmbe) {
178: throw new MonitoringException(ncmbe);
179: }
180:
181: try {
182: jmxConnectorServer.start();
183: } catch (IOException ioe) {
184: throw new MonitoringException(ioe);
185: }
186: }
187:
188: public static void windDownConnectorServer(
189: MonitoringContext monitoringContext, MBeanServer mBeanServer)
190: throws MonitoringException {
191: ObjectName on = getConnectorServerObjectName(monitoringContext);
192:
193: Set onNames = mBeanServer.queryNames(on, null);
194: if (onNames != null) {
195: if (!onNames.isEmpty()) {
196: try {
197: mBeanServer.invoke(on, "stop", null, null);
198: } catch (InstanceNotFoundException infe) {
199: throw new MonitoringException(infe);
200: } catch (MBeanException mbe) {
201: throw new MonitoringException(mbe);
202: } catch (ReflectionException re) {
203: throw new MonitoringException(re);
204: }
205:
206: try {
207: mBeanServer
208: .unregisterMBean(getConnectorServerObjectName(monitoringContext));
209: } catch (InstanceNotFoundException infe) {
210: throw new MonitoringException(infe);
211: } catch (MBeanRegistrationException mbre) {
212: throw new MonitoringException(mbre);
213: }
214: }
215: }
216: }
217:
218: public static void gearUpMonitoringSubsystem(
219: SubsystemImpl subsystemImpl,
220: MonitoringContext monitoringContext)
221: throws MonitoringException {
222: MBeanSubsystem mBeanSubsystem = new MBeanSubsystem(
223: subsystemImpl, monitoringContext);
224: ObjectName on = SubsystemHelper
225: .getMonitoringSubsystemObjectName(monitoringContext);
226: subsystemImpl.registerMBean(mBeanSubsystem, on.toString());
227: }
228:
229: public static List getImmortalMBeanObjectNames(
230: MonitoringContext monitoringContext)
231: throws MonitoringException {
232: List immortalMBeanObjectNames = new ArrayList();
233: immortalMBeanObjectNames.add(SubsystemHelper
234: .getMonitoringSubsystemObjectName(monitoringContext));
235: immortalMBeanObjectNames.add(SubsystemHelper
236: .getConnectorServerObjectName(monitoringContext));
237: immortalMBeanObjectNames.add(SubsystemHelper
238: .getHtmlAdaptorObjectName(monitoringContext));
239: return immortalMBeanObjectNames;
240: }
241: }
|