001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS
026 */
027package org.opends.server.protocols.jmx;
028
029import java.io.IOException;
030import java.util.Map;
031
032import javax.management.ListenerNotFoundException;
033import javax.management.MBeanServerConnection;
034import javax.management.NotificationFilter;
035import javax.management.NotificationListener;
036import javax.management.remote.JMXConnector;
037import javax.management.remote.JMXConnectorFactory;
038import javax.management.remote.JMXServiceURL;
039import javax.security.auth.Subject;
040
041/**
042 * Wrapper class for the JMX's RMI connector. This class has the exact same
043 * functionalities but maintain inner variables which are used during the
044 * connection phase.
045 * <p>
046 * Note that the javadoc has been copied from the
047 * javax.management.remote.JMXConnector interface.
048 */
049public class OpendsJmxConnector implements JMXConnector
050{
051
052  /** The wrapped JMX RMI connector. */
053  private JMXConnector jmxc;
054
055  /** The connection environment set at creation. */
056  private Map<String,Object> environment;
057
058  /** The JMX Service URL. */
059  private JMXServiceURL serviceURL;
060
061  /**
062   * Creates a connector client for the connector server at the
063   * given host and port.  The resultant client is not connected until its
064   *  connect method is called.
065   *
066   * @param serverHostname the target server hostname
067   *
068   * @param serverPort the target server port
069   *
070   * @param environment a set of attributes to determine how the
071   * connection is made.  This parameter can be null.  Keys in this
072   * map must be Strings.  The appropriate type of each associated
073   * value depends on the attribute.  The contents of
074   * <code>environment</code> are not changed by this call.
075   *
076   * @exception IOException if the connector client cannot be made
077   * because of a communication problem.
078   */
079  public OpendsJmxConnector(String serverHostname, int serverPort,
080      Map<String,Object> environment) throws IOException
081  {
082    serviceURL = new JMXServiceURL(
083        "service:jmx:rmi:///jndi/rmi://" + serverHostname + ":" + serverPort
084            + "/org.opends.server.protocols.jmx.client-unknown");
085
086    this.jmxc = JMXConnectorFactory.newJMXConnector(serviceURL, environment);
087    this.environment = environment ;
088  }
089
090  /**
091   * Returns the connection environment.
092   *
093   * @return Map the connection environment used by new connections
094   */
095  public Map<String, Object> getConnectionEnv()
096  {
097    return environment;
098  }
099
100  /** {@inheritDoc} */
101  @Override
102  public void connect() throws IOException, SecurityException
103  {
104    connect(null);
105  }
106
107  /** {@inheritDoc} */
108  @Override
109  public void connect(Map<String,?> env) throws IOException, SecurityException
110  {
111    jmxc.connect(env);
112  }
113
114  /** {@inheritDoc} */
115  @Override
116  public MBeanServerConnection getMBeanServerConnection() throws IOException
117  {
118    return jmxc.getMBeanServerConnection();
119  }
120
121  /** {@inheritDoc} */
122  @Override
123  public MBeanServerConnection getMBeanServerConnection(
124      Subject delegationSubject) throws IOException
125  {
126    return jmxc.getMBeanServerConnection(delegationSubject);
127  }
128
129  /** {@inheritDoc} */
130  @Override
131  public void close() throws IOException
132  {
133    jmxc.close();
134  }
135
136  /** {@inheritDoc} */
137  @Override
138  public void addConnectionNotificationListener(
139      NotificationListener listener, NotificationFilter filter,
140      Object handback) throws NullPointerException
141  {
142    jmxc.addConnectionNotificationListener(listener, filter, handback);
143  }
144
145  /** {@inheritDoc} */
146  @Override
147  public void removeConnectionNotificationListener(
148      NotificationListener listener) throws ListenerNotFoundException,
149      NullPointerException
150  {
151    jmxc.removeConnectionNotificationListener(listener);
152  }
153
154  /** {@inheritDoc} */
155  @Override
156  public void removeConnectionNotificationListener(
157      NotificationListener l, NotificationFilter f, Object handback)
158      throws ListenerNotFoundException
159  {
160    jmxc.removeConnectionNotificationListener(l, f, handback);
161  }
162
163  /** {@inheritDoc} */
164  @Override
165  public String getConnectionId() throws IOException
166  {
167    return jmxc.getConnectionId();
168  }
169}