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