001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMXConnectorSourceImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * JMXConnectorSourceImpl.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on June 13, 2005, 12:02 PM
037: */package com.sun.jbi.util.jmx;
038:
039: import java.io.File;
040:
041: import com.sun.jbi.util.StringTranslator;
042: import javax.management.remote.JMXConnector;
043: import javax.management.remote.JMXConnectorFactory;
044: import javax.management.remote.JMXServiceURL;
045:
046: import java.util.Map;
047:
048: /**
049: * This class is to be used by jbi components / services to get a JMX Client Connector.
050: * This is a wrapper around the JMXConnectorFactory and allows one to connect to
051: * a Connector Server, which may have proprietary extensions for Security. The
052: * corresponding client Connector Source would be made available through this interface.
053: *
054: * @author Sun Microsystems, Inc.
055: */
056: public class JMXConnectorSourceImpl implements JMXConnectorSource {
057:
058: /** Username. */
059: private String mUser;
060:
061: /** Password. */
062: private String mPwd;
063:
064: /** JMXServiceURL. */
065: private JMXServiceURL mJmxServiceUrl;
066:
067: /** JMXCOnnector. */
068: private JMXConnector mJmxConnector;
069:
070: /** JMX Creds. */
071: private static final String CREDENTIALS_KEY = "jmx.remote.credentials";
072:
073: /** NULL_OBJ Key. */
074: private static final String NULL_OBJECT = "NULL_OBJECT";
075:
076: /**
077: * Ctor.
078: */
079: public JMXConnectorSourceImpl() {
080: mJmxConnector = null;
081: mJmxServiceUrl = null;
082: mPwd = null;
083: mUser = null;
084: }
085:
086: /**
087: * @param username is the username to be used for the Connection.
088: * @param password is the user password.
089: */
090: public void setCredentials(String username, String password) {
091: mUser = username;
092: mPwd = password;
093: }
094:
095: /**
096: * This operation is not supported by this implementation of the Connector source and
097: * is a no-op.
098: *
099: * @param secureFlag indicates whether the connection is to be secured (ex. use SSL)
100: */
101: public void setIsSecure(boolean secureFlag) {
102: // -- no-op
103: }
104:
105: /**
106: * This operation is not supported by this implementation of the Connector source
107: * and is a no-op.
108: *
109: * @param truststore path to the JKS truststore file.
110: * @param type is the type of the Keystore ( JKS, JCEKS etc)
111: * @param passwd - the truststore password
112: */
113: public void setTrustStore(File truststore, String type,
114: char[] passwd) {
115: // -- no-op
116: }
117:
118: /**
119: * @param jmxServiceURL - JMX Service URL
120: */
121: public void setJMXServiceURL(JMXServiceURL jmxServiceURL) {
122: mJmxServiceUrl = jmxServiceURL;
123: }
124:
125: /**
126: * If the JMX Connector Server's host and port cannot be identified from the
127: * JMX Service URL or if the JMXServiceURL is not set, then this is the process
128: * of indicating this information. This is a no-op in this implementation.
129: *
130: * @param host - hostname
131: * @param port - port
132: */
133: public void setHostAndPort(String host, int port) {
134: // -- no-op
135: }
136:
137: /**
138: * If the connection has already been created, return the existing
139: * JMXConnector unless 'forceNew' is true or the connection is currently null.
140: *
141: * @param forceNew - create a new connection
142: * @param environment - a set of attributes to determine how the connection is made.
143: * This parameter can be null. Keys in this map must be Strings. The appropriate type
144: * of each associated value depends on the attribute. The contents of environment are
145: * not changed by this call
146: * @return the JMX Connector.
147: * @throws java.io.IOException if a connection cannot be established.
148: */
149: public javax.management.remote.JMXConnector getJMXConnector(
150: boolean forceNew, Map environment)
151: throws java.io.IOException {
152: StringTranslator translator = new StringTranslator(
153: "com.sun.jbi.util", null);
154: if (mJmxServiceUrl == null) {
155: throw new java.io.IOException(translator.getString(
156: NULL_OBJECT, "JMXServiceURL"));
157: }
158:
159: if ((mUser == null) || (mPwd == null)) {
160: throw new java.io.IOException(translator.getString(
161: NULL_OBJECT, "Username/Password"));
162: }
163:
164: if ((mJmxConnector == null) || (forceNew)) {
165: // -- Create a new connection.
166:
167: java.util.HashMap env = new java.util.HashMap();
168: if (environment != null) {
169: env.putAll(environment);
170: }
171:
172: if (!env.containsKey(CREDENTIALS_KEY)) {
173: String[] credentials = new String[] { mUser, mPwd };
174: env.put(CREDENTIALS_KEY, credentials);
175: }
176:
177: mJmxConnector = JMXConnectorFactory.connect(mJmxServiceUrl,
178: env);
179: }
180:
181: return mJmxConnector;
182:
183: }
184:
185: }
|