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.portal.monitoring.client.SunClientFactory;
008: import com.sun.portal.monitoring.server.SunServerFactory;
009: import com.sun.portal.monitoring.utilities.PropertyHelper;
010: import com.sun.portal.log.common.PortalLogger;
011:
012: import javax.management.*;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.List;
016: import java.util.Set;
017: import java.util.Properties;
018: import java.util.Map;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024:
025: public class SubsystemImpl implements Subsystem, NotificationListener {
026: private static final Logger logger = PortalLogger
027: .getLogger(SubsystemImpl.class);
028:
029: private static LogRecord getLogRecord(Level level, String message,
030: Object[] parameters, Throwable t) {
031: LogRecord result = new LogRecord(level, message);
032: result.setLoggerName(logger.getName());
033: result.setParameters(parameters);
034: result.setThrown(t);
035: return result;
036: }
037:
038: private SubsystemImpl(
039: MonitoringConfiguration monitoringConfiguration) {
040: try {
041: monitoringConfiguration.setUp();
042: } catch (MonitoringException e) {
043: // Disable Monitoring for initialization is failing.
044: monitoringConfiguration.getMonitoringContext()
045: .setMonitoringDisable(Boolean.TRUE);
046: if (logger.isLoggable(Level.SEVERE)) {
047: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0002",
048: new Object[] { e.getLocalizedMessage() }, e));
049: }
050: }
051: setMonitoringContext(monitoringConfiguration
052: .getMonitoringContext());
053: }
054:
055: public SubsystemImpl() {
056: this (new MonitoringConfiguration(new PropertyHelper(
057: new Properties())));
058: }
059:
060: public SubsystemImpl(Properties properties) {
061: this (
062: new MonitoringConfiguration(new PropertyHelper(
063: properties)));
064: }
065:
066: private void setMonitoringContext(
067: MonitoringContext monitoringContext) {
068: this .monitoringContext = monitoringContext;
069: }
070:
071: public Boolean isDisabled() {
072: return getMonitoringContext().getMonitoringDisable();
073: }
074:
075: public String getNamingDomain() {
076: return getMonitoringContext().getNamingDomain();
077: }
078:
079: public Map getRegistry() {
080: return registry;
081: }
082:
083: public void setRegistry(Map registry) {
084: this .registry = registry;
085: }
086:
087: public void registerMBean(Object mBeanObject, String nameProperties)
088: throws MonitoringException {
089: ObjectName on = null;
090: try {
091: on = new ObjectName(nameProperties);
092: getMBeanServer().registerMBean(mBeanObject, on);
093: } catch (InstanceAlreadyExistsException e) {
094: throw new MonitoringException(e);
095: } catch (MBeanRegistrationException e) {
096: throw new MonitoringException(e);
097: } catch (NotCompliantMBeanException e) {
098: throw new MonitoringException(e);
099: } catch (MalformedObjectNameException e) {
100: throw new MonitoringException(e);
101: }
102: }
103:
104: public Boolean isRegistered(String objectName)
105: throws MonitoringException {
106: try {
107: return getMBeanServer().isRegistered(
108: new ObjectName(objectName)) ? Boolean.TRUE
109: : Boolean.FALSE;
110: } catch (MalformedObjectNameException e) {
111: throw new MonitoringException(e);
112: }
113: }
114:
115: public void unregisterMBean(String objectName)
116: throws MonitoringException {
117: try {
118: getMBeanServer()
119: .unregisterMBean(new ObjectName(objectName));
120: } catch (InstanceNotFoundException e) {
121: throw new MonitoringException(e);
122: } catch (MBeanRegistrationException e) {
123: throw new MonitoringException(e);
124: } catch (MalformedObjectNameException e) {
125: throw new MonitoringException(e);
126: }
127: }
128:
129: private MonitoringContext getMonitoringContext() {
130: return monitoringContext;
131: }
132:
133: public MBeanServer getMBeanServer() throws MonitoringException {
134: if (mBeanServer == null) {
135: if (getMonitoringContext().getUseJavaPlatformMBeanServer()
136: .booleanValue()) {
137: mBeanServer = SubsystemHelper
138: .getJavaPlatformMBeanServer();
139: }
140: }
141:
142: if (mBeanServer == null) {
143: ServerFactory serverFactory = new SunServerFactory();
144: mBeanServer = serverFactory
145: .getMBeanServer(getMonitoringContext());
146: }
147:
148: return mBeanServer;
149: }
150:
151: private void setMBeanServer(MBeanServer mBeanServer) {
152: this .mBeanServer = mBeanServer;
153: }
154:
155: public Client getClient() throws MonitoringException {
156: if (client == null) {
157: ClientFactory clientFactory = new SunClientFactory();
158: client = clientFactory.getClient(getMonitoringContext(),
159: immortalMBeanObjectNames);
160: }
161:
162: return client;
163: }
164:
165: private void setClient(Client client) {
166: this .client = client;
167: }
168:
169: public void start() throws MonitoringException {
170: try {
171: setImmortalMBeanObjectNames(SubsystemHelper
172: .getImmortalMBeanObjectNames(getMonitoringContext()));
173:
174: if (getMonitoringContext().getSubsystemMode().equals(
175: MonitoringContext.SUBSYSTEM_MODE)) {
176: getMBeanServer();
177:
178: try {
179: SubsystemHelper.gearUpMonitoringSubsystem(this ,
180: getMonitoringContext());
181: } catch (MonitoringException e) {
182: if (logger.isLoggable(Level.INFO)) {
183: logger.log(getLogRecord(Level.INFO,
184: "PSMN_CSPM0001", new Object[] { e
185: .getLocalizedMessage() }, e));
186: }
187: }
188:
189: try {
190: SubsystemHelper.gearUpConnectorServer(
191: getMonitoringContext(), getMBeanServer());
192: } catch (MonitoringException e) {
193: if (logger.isLoggable(Level.INFO)) {
194: logger.log(getLogRecord(Level.INFO,
195: "PSMN_CSPM0001", new Object[] { e
196: .getLocalizedMessage() }, e));
197: }
198: }
199:
200: try {
201: SubsystemHelper.gearUpHtmlAdaptor(
202: getMonitoringContext(), getMBeanServer());
203: } catch (MonitoringException e) {
204: if (logger.isLoggable(Level.INFO)) {
205: logger.log(getLogRecord(Level.INFO,
206: "PSMN_CSPM0001", new Object[] { e
207: .getLocalizedMessage() }, e));
208: }
209: }
210: } else {
211: getClient();
212: }
213: } finally {
214: getMonitoringContext().getPropertyHelper()
215: .printProperties();
216: }
217: }
218:
219: public void stop(Boolean unregisterMBeans)
220: throws MonitoringException {
221: if (getMonitoringContext().getSubsystemMode().equals(
222: MonitoringContext.SUBSYSTEM_MODE)) {
223: if (unregisterMBeans.booleanValue()) {
224: try {
225: Set mBeanNames = getMBeanServer().queryNames(
226: new ObjectName(monitoringContext
227: .getNamingDomain()
228: + ":*"), null);
229: ObjectName[] objectNames = (ObjectName[]) mBeanNames
230: .toArray(new ObjectName[mBeanNames.size()]);
231: for (int i = 0; i < objectNames.length; i++) {
232: if (!immortalMBeanObjectNames
233: .contains(objectNames[i])) {
234: try {
235: getMBeanServer().unregisterMBean(
236: objectNames[i]);
237: } catch (InstanceNotFoundException e) {
238: if (logger.isLoggable(Level.SEVERE)) {
239: logger
240: .log(getLogRecord(
241: Level.SEVERE,
242: "PSMN_CSPM0001",
243: new Object[] { e
244: .getLocalizedMessage() },
245: e));
246: }
247: } catch (MBeanRegistrationException e) {
248: if (logger.isLoggable(Level.SEVERE)) {
249: logger
250: .log(getLogRecord(
251: Level.SEVERE,
252: "PSMN_CSPM0001",
253: new Object[] { e
254: .getLocalizedMessage() },
255: e));
256: }
257: }
258: }
259: }
260: } catch (MalformedObjectNameException mone) {
261: throw new MonitoringException(mone);
262: }
263: }
264: } else {
265: try {
266: getClient().disconnect();
267: } catch (IOException ioe) {
268: throw new MonitoringException(ioe);
269: } finally {
270: setClient(null);
271: }
272: }
273: }
274:
275: public void destroy() throws MonitoringException {
276: if (getMonitoringContext().getSubsystemMode().equals(
277: MonitoringContext.SUBSYSTEM_MODE)) {
278: try {
279: Set mBeanNames = getMBeanServer().queryNames(
280: new ObjectName(monitoringContext
281: .getNamingDomain()
282: + ":*"), null);
283: ObjectName[] objectNames = (ObjectName[]) mBeanNames
284: .toArray(new ObjectName[mBeanNames.size()]);
285: for (int i = 0; i < objectNames.length; i++) {
286: try {
287: getMBeanServer()
288: .unregisterMBean(objectNames[i]);
289: } catch (InstanceNotFoundException e) {
290: if (logger.isLoggable(Level.SEVERE)) {
291: logger
292: .log(getLogRecord(
293: Level.SEVERE,
294: "PSMN_CSPM0001",
295: new Object[] { e
296: .getLocalizedMessage() },
297: e));
298: }
299: } catch (MBeanRegistrationException e) {
300: if (logger.isLoggable(Level.SEVERE)) {
301: logger
302: .log(getLogRecord(
303: Level.SEVERE,
304: "PSMN_CSPM0001",
305: new Object[] { e
306: .getLocalizedMessage() },
307: e));
308: }
309: }
310: }
311: } catch (MalformedObjectNameException mone) {
312: throw new MonitoringException(mone);
313: }
314:
315: if (getMBeanServer().getDefaultDomain().equals(
316: getMonitoringContext().getNamingDomain())) {
317: MBeanServerFactory.releaseMBeanServer(getMBeanServer());
318: }
319: setMBeanServer(null);
320: } else {
321: try {
322: getClient().disconnect();
323: } catch (IOException ioe) {
324: throw new MonitoringException(ioe);
325: } finally {
326: setClient(null);
327: }
328: }
329: }
330:
331: public List getImmortalMBeanObjectNames() {
332: return immortalMBeanObjectNames;
333: }
334:
335: public void setImmortalMBeanObjectNames(
336: List immortalMBeanObjectNames) {
337: this .immortalMBeanObjectNames = immortalMBeanObjectNames;
338: }
339:
340: public String getResourceBundleBaseName() {
341: return resourceBundleBaseName;
342: }
343:
344: public void setResourceBundleBaseName(String rbBaseName) {
345: this .resourceBundleBaseName = rbBaseName;
346: }
347:
348: private MonitoringContext monitoringContext;
349: private MBeanServer mBeanServer;
350: private Client client;
351: private List immortalMBeanObjectNames = new ArrayList();
352: private String resourceBundleBaseName;
353: private Map registry = Collections.synchronizedMap(new HashMap());
354:
355: public static String[] NOTIFICATION_ATTRIBUTE_NAMES = new String[] {
356: "Disable", "ConnectorServerPort", "HtmlAdaptorPort",
357: "UseJavaPlatformMBeanServer" };
358:
359: public NotificationFilter getNotificationFilter() {
360: SubsystemNotificationFilter subsystemNotificationFilter = new SubsystemNotificationFilter();
361: subsystemNotificationFilter
362: .setEnabledAttributes(NOTIFICATION_ATTRIBUTE_NAMES);
363: return subsystemNotificationFilter;
364: }
365:
366: public void handleNotification(Notification notification,
367: Object handback) {
368: if (notification.getType().equals(
369: AttributeChangeNotification.ATTRIBUTE_CHANGE)) {
370: AttributeChangeNotification attributeChangeNotification = (AttributeChangeNotification) notification;
371: String name = attributeChangeNotification
372: .getAttributeName();
373:
374: if (name.equals(NOTIFICATION_ATTRIBUTE_NAMES[0])) {
375: Boolean newValue = (Boolean) attributeChangeNotification
376: .getNewValue();
377: Boolean oldValue = (Boolean) attributeChangeNotification
378: .getOldValue();
379: if (oldValue.booleanValue() != newValue.booleanValue()) {
380: if (!newValue.booleanValue()) {
381: try {
382: stop(Boolean.FALSE);
383: } catch (MonitoringException e) {
384: if (logger.isLoggable(Level.SEVERE)) {
385: logger
386: .log(getLogRecord(
387: Level.SEVERE,
388: "PSMN_CSPM0001",
389: new Object[] { e
390: .getLocalizedMessage() },
391: e));
392: }
393: }
394: }
395: }
396: } else if (name.equals(NOTIFICATION_ATTRIBUTE_NAMES[1])) {
397: Integer newValue = (Integer) attributeChangeNotification
398: .getNewValue();
399: Integer oldValue = (Integer) attributeChangeNotification
400: .getOldValue();
401: if (oldValue.intValue() != newValue.intValue()) {
402: try {
403: SubsystemHelper.windDownConnectorServer(
404: getMonitoringContext(),
405: getMBeanServer());
406: } catch (MonitoringException e) {
407: if (logger.isLoggable(Level.SEVERE)) {
408: logger
409: .log(getLogRecord(
410: Level.SEVERE,
411: "PSMN_CSPM0001",
412: new Object[] { e
413: .getLocalizedMessage() },
414: e));
415: }
416: }
417:
418: try {
419: SubsystemHelper.gearUpConnectorServer(
420: getMonitoringContext(),
421: getMBeanServer());
422: } catch (MonitoringException e) {
423: if (logger.isLoggable(Level.SEVERE)) {
424: logger
425: .log(getLogRecord(
426: Level.SEVERE,
427: "PSMN_CSPM0001",
428: new Object[] { e
429: .getLocalizedMessage() },
430: e));
431: }
432: }
433: }
434: } else if (name.equals(NOTIFICATION_ATTRIBUTE_NAMES[2])) {
435: Integer newValue = (Integer) attributeChangeNotification
436: .getNewValue();
437: Integer oldValue = (Integer) attributeChangeNotification
438: .getOldValue();
439: if (oldValue.intValue() != newValue.intValue()) {
440: try {
441: SubsystemHelper.windDownHtmlAdaptor(
442: getMonitoringContext(),
443: getMBeanServer());
444: } catch (MonitoringException e) {
445: if (logger.isLoggable(Level.SEVERE)) {
446: logger
447: .log(getLogRecord(
448: Level.SEVERE,
449: "PSMN_CSPM0001",
450: new Object[] { e
451: .getLocalizedMessage() },
452: e));
453: }
454: }
455:
456: try {
457: SubsystemHelper.gearUpHtmlAdaptor(
458: getMonitoringContext(),
459: getMBeanServer());
460: } catch (MonitoringException e) {
461: if (logger.isLoggable(Level.SEVERE)) {
462: logger
463: .log(getLogRecord(
464: Level.SEVERE,
465: "PSMN_CSPM0001",
466: new Object[] { e
467: .getLocalizedMessage() },
468: e));
469: }
470: }
471: }
472: } else if (name.equals(NOTIFICATION_ATTRIBUTE_NAMES[3])) {
473: Boolean newValue = (Boolean) attributeChangeNotification
474: .getNewValue();
475: Boolean oldValue = (Boolean) attributeChangeNotification
476: .getOldValue();
477: if (oldValue.booleanValue() != newValue.booleanValue()) {
478: try {
479: destroy();
480: } catch (MonitoringException e) {
481: if (logger.isLoggable(Level.SEVERE)) {
482: logger
483: .log(getLogRecord(
484: Level.SEVERE,
485: "PSMN_CSPM0001",
486: new Object[] { e
487: .getLocalizedMessage() },
488: e));
489: }
490: }
491: try {
492: start();
493: } catch (MonitoringException e) {
494: if (logger.isLoggable(Level.SEVERE)) {
495: logger
496: .log(getLogRecord(
497: Level.SEVERE,
498: "PSMN_CSPM0001",
499: new Object[] { e
500: .getLocalizedMessage() },
501: e));
502: }
503: }
504: }
505: }
506: }
507: }
508: }
|