001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.jmx.server.weblogic;
019:
020: import javax.management.MBeanServer;
021: import javax.naming.AuthenticationException;
022: import javax.naming.CommunicationException;
023: import javax.naming.Context;
024: import javax.naming.NamingException;
025:
026: import org.sape.carbon.core.component.ComponentConfiguration;
027: import org.sape.carbon.core.component.lifecycle.Configurable;
028: import org.sape.carbon.core.exception.ExceptionUtility;
029: import org.sape.carbon.services.jmx.server.MBeanServerService;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import weblogic.jndi.Environment;
034: import weblogic.management.MBeanHome;
035:
036: /**
037: * <P>Weblogic specific implementation of the MBeanServerProvider interface.</p>
038: *
039: * Copyright 2002 Sapient
040: * @author Greg Hinkle
041: * @version $Revision: 1.8 $ ($Author: dvoet $)
042: */
043: public class WebLogicMBeanServerServiceImpl implements
044: MBeanServerService, Configurable {
045:
046: /**
047: * Provides a handle to Apache-commons logger
048: */
049: private Log log = LogFactory.getLog(this .getClass());
050:
051: /** Configuration for the service. */
052: protected WebLogicMBeanServerServiceConfiguration config;
053:
054: /**
055: * Gets the instance of the Weblogic MBean Server.
056: *
057: * @return instance of the Weblogic MBean server
058: */
059: public MBeanServer getMBeanServer() {
060: MBeanServer mbeanServer = null;
061: MBeanHome home = null;
062: try {
063: Environment env = new Environment();
064: if (config != null) {
065: if (config.getInitialContextFactory() != null) {
066: env.setInitialContextFactory(config
067: .getInitialContextFactory());
068: }
069:
070: if (config.getPrincipal() != null) {
071: env.setSecurityPrincipal(config.getPrincipal());
072: }
073:
074: if (config.getCredentials() != null) {
075: env.setSecurityCredentials(config.getCredentials());
076: }
077: }
078:
079: Context ctx = env.getInitialContext();
080: home = (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
081: mbeanServer = home.getMBeanServer();
082:
083: if (mbeanServer != null) {
084: if (log.isTraceEnabled()) {
085: log.trace("Got back MBeanServer of type ["
086: + mbeanServer.getClass().getName() + "]");
087: }
088: }
089: } catch (AuthenticationException e) {
090: if (log.isTraceEnabled()) {
091: log.trace("Caught exception: " + e
092: + ExceptionUtility.captureStackTrace(e));
093: }
094: } catch (CommunicationException e) {
095: if (log.isTraceEnabled()) {
096: log.trace("Caught exception: " + e
097: + ExceptionUtility.captureStackTrace(e));
098: }
099: } catch (NamingException e) {
100: if (log.isTraceEnabled()) {
101: log.trace("Caught exception: " + e
102: + ExceptionUtility.captureStackTrace(e));
103: }
104: }
105: return mbeanServer;
106: }
107:
108: /**
109: * Configures the service.
110: *
111: * @param configuration <code>WebLogicMBeanServerServiceConfiguration</code>
112: * configuration object for the service
113: */
114: public void configure(ComponentConfiguration configuration) {
115: try {
116: this .config = (WebLogicMBeanServerServiceConfiguration) configuration;
117:
118: if (log.isTraceEnabled()) {
119: log
120: .trace("Specifed proper WebLogicMBeanServerServiceConfiguration."
121: + "Using values from configuration file.");
122: }
123:
124: } catch (ClassCastException cce) {
125: this .config = null;
126: if (log.isTraceEnabled()) {
127: log
128: .trace("Specifed WebLogicMBeanServerServiceConfiguration does not "
129: + "implement correct interface. Empty InitialContext "
130: + "settings will be used.");
131: }
132: }
133: }
134: }
|