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: * @(#)AppSrvJMXConnectorSource.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * AppSrvJMXConnectorSource.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: import java.util.Map;
041:
042: /**
043: * Note that this is a Sun One Application Server specific class. For connecting to a
044: * Standard JMX Connector Server use the JMXCOnnectorSourceImpl class.
045: */
046: import com.sun.appserv.management.client.AppserverConnectionSource;
047: import com.sun.appserv.management.client.TrustStoreTrustManager;
048: import com.sun.appserv.management.client.TLSParams;
049: import com.sun.appserv.management.client.HandshakeCompletedListenerImpl;
050:
051: import com.sun.jbi.util.StringTranslator;
052:
053: import javax.management.remote.JMXConnector;
054: import javax.management.remote.JMXServiceURL;
055:
056: import javax.net.ssl.HandshakeCompletedListener;
057: import javax.net.ssl.X509TrustManager;
058:
059: /**
060: * This class is to be used by jbi components / services to get a JMX Client Connector.
061: * This is a wrapper around the JMXConnectorFactory and allows one to connect to
062: * a Connector Server, which may have proprietary extensions for Security. The
063: * corresponding client Connector Source would be made available through this interface.
064: *
065: * @author Sun Microsystems, Inc.
066: */
067: public class AppSrvJMXConnectorSource implements JMXConnectorSource {
068:
069: /** Username. */
070: private String mUser;
071:
072: /** Password. */
073: private String mPwd;
074:
075: /** JMXServiceURL. */
076: private JMXServiceURL mJmxServiceUrl;
077:
078: /** JMXCOnnector. */
079: private JMXConnector mJmxConnector;
080:
081: /** SSL Flag. */
082: private boolean mIsSecure;
083:
084: /** TLS Params. */
085: private TLSParams mTlsParams;
086:
087: /** Host. */
088: private String mHost;
089:
090: /** Port. */
091: private int mPort;
092:
093: /** Null Obj. */
094: private static final String NULL_OBJECT = "NULL_OBJECT";
095:
096: /** Default Store Type. */
097: private static final String DEFAULT_STORE_TYPE = "JKS";
098:
099: /**
100: * Ctor.
101: */
102: public AppSrvJMXConnectorSource() {
103: mJmxConnector = null;
104: mJmxServiceUrl = null;
105: mPwd = null;
106: mUser = null;
107: mIsSecure = false;
108: mHost = null;
109: mPort = 0;
110: }
111:
112: /**
113: * @param username is the username to be used for the Connection.
114: * @param password is the user password.
115: */
116: public void setCredentials(String username, String password) {
117: mUser = username;
118: mPwd = password;
119: }
120:
121: /**
122: * This operation is not supported by this implementation of the Connector source and
123: * is a no-op.
124: *
125: * @param secureFlag indicates whether the connection is to be secured (ex. use SSL)
126: */
127: public void setIsSecure(boolean secureFlag) {
128: mIsSecure = secureFlag;
129: }
130:
131: /**
132: * This operation is not supported by this implementation of the Connector source
133: * and is a no-op.
134: *
135: * @param truststore path to the JKS truststore file.
136: * @param type is the type of the Keystore ( JKS, JCEKS etc)
137: * @param passwd - the truststore password
138: */
139: public void setTrustStore(File truststore, String type,
140: char[] passwd) {
141: if (type == null) {
142: type = DEFAULT_STORE_TYPE;
143: }
144: TrustStoreTrustManager trustMgr = new TrustStoreTrustManager(
145: truststore, type, passwd);
146: trustMgr.setPrompt(true);
147:
148: HandshakeCompletedListener listener = new HandshakeCompletedListenerImpl();
149: mTlsParams = new TLSParams(new X509TrustManager[] { trustMgr },
150: listener);
151: }
152:
153: /**
154: * @param jmxServiceURL - JMX Service URL
155: */
156: public void setJMXServiceURL(JMXServiceURL jmxServiceURL) {
157: mJmxServiceUrl = jmxServiceURL;
158:
159: if (mJmxServiceUrl != null) {
160: if (mJmxServiceUrl.getHost() != null
161: && mJmxServiceUrl.getPort() != 0) {
162: mHost = mJmxServiceUrl.getHost();
163: mPort = mJmxServiceUrl.getPort();
164: }
165: }
166: }
167:
168: /**
169: * If the JMX Connector Server's host and port cannot be identified from the
170: * JMX Service URL or if the JMXServiceURL is not set, then this is the process
171: * of indicating this information. This is a no-op in this implementation.
172: *
173: * @param host - hostname
174: * @param port - port
175: */
176: public void setHostAndPort(String host, int port) {
177: mHost = host;
178: mPort = port;
179: }
180:
181: /**
182: * If the connection has already been created, return the existing
183: * JMXConnector unless 'forceNew' is true or the connection is currently null.
184: *
185: * @param forceNew - create a new connection
186: * @param environment - a set of attributes to determine how the connection is made.
187: * This parameter can be null. Keys in this map must be Strings. The appropriate type
188: * of each associated value depends on the attribute. The contents of environment are
189: * not changed by this call
190: * @return the JMX Connector.
191: * @throws java.io.IOException if a connection cannot be established.
192: */
193: public javax.management.remote.JMXConnector getJMXConnector(
194: boolean forceNew, Map environment)
195: throws java.io.IOException {
196:
197: StringTranslator translator = new StringTranslator(
198: "com.sun.jbi.util", null);
199: if (mHost == null || mPort == 0) {
200: throw new java.io.IOException(translator.getString(
201: NULL_OBJECT, "JMX Connector Server Host/Port"));
202: }
203:
204: if ((mUser == null) || (mPwd == null)) {
205: throw new java.io.IOException(translator.getString(
206: NULL_OBJECT, "Username/Password"));
207: }
208:
209: if ((mJmxConnector == null) || (forceNew)) {
210: // -- Create a new connection.
211: AppserverConnectionSource src = null;
212: if (mIsSecure) {
213: System.out.println("Connecting using TLS Params.");
214: src = new AppserverConnectionSource(
215: AppserverConnectionSource.PROTOCOL_RMI, mHost,
216: mPort, mUser, mPwd, mTlsParams, environment);
217: } else {
218: src = new AppserverConnectionSource(
219: AppserverConnectionSource.PROTOCOL_RMI, mHost,
220: mPort, mUser, mPwd, null, environment);
221:
222: }
223:
224: mJmxConnector = src.getJMXConnector(forceNew);
225: }
226:
227: return mJmxConnector;
228:
229: }
230:
231: }
|