5652N/A/*
5744N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
5652N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5652N/A *
5652N/A * This code is free software; you can redistribute it and/or modify it
5652N/A * under the terms of the GNU General Public License version 2 only, as
5744N/A * published by the Free Software Foundation. Oracle designates this
5744N/A * particular file as subject to the "Classpath" exception as provided
5744N/A * by Oracle in the LICENSE file that accompanied this code.
5652N/A *
5652N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5652N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5652N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5652N/A * version 2 for more details (a copy is included in the LICENSE file that
5652N/A * accompanied this code).
5652N/A *
5652N/A * You should have received a copy of the GNU General Public License version
5652N/A * 2 along with this work; if not, write to the Free Software Foundation,
5652N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5652N/A *
5652N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5652N/A * or visit www.oracle.com if you need additional information or have any
5652N/A * questions.
5652N/A */
5652N/Apackage sun.management.jdp;
5652N/A
5652N/Aimport java.io.IOException;
5652N/Aimport java.util.HashMap;
5652N/Aimport java.util.Map;
5652N/Aimport java.util.Objects;
5652N/Aimport java.util.UUID;
5652N/A
5652N/A/**
5652N/A * A packet to broadcasts JMX URL
5652N/A *
5652N/A * Fields:
5652N/A *
5652N/A * <ul>
5652N/A * <li>UUID - broadcast session ID, changed every time when we start/stop
5652N/A * discovery service</li>
5652N/A * <li>JMX_URL - URL to connect to JMX service</li>
5652N/A * <li>MAIN_CLASS - optional name of main class, filled from sun.java.command stripped for
5652N/A * security reason to first space</li>
5652N/A * <li>INSTANCE_NAME - optional custom name of particular instance as provided by customer</li>
5652N/A * </ul>
5652N/A */
5652N/Apublic final class JdpJmxPacket
5652N/A extends JdpGenericPacket
5652N/A implements JdpPacket {
5652N/A
5652N/A /**
5652N/A * Session ID
5652N/A */
5652N/A public final static String UUID_KEY = "DISCOVERABLE_SESSION_UUID";
5652N/A /**
5652N/A * Name of main class
5652N/A */
5652N/A public final static String MAIN_CLASS_KEY = "MAIN_CLASS";
5652N/A /**
5652N/A * JMX service URL
5652N/A */
5652N/A public final static String JMX_SERVICE_URL_KEY = "JMX_SERVICE_URL";
5652N/A /**
5652N/A * Name of Java instance
5652N/A */
5652N/A public final static String INSTANCE_NAME_KEY = "INSTANCE_NAME";
5652N/A
5652N/A private UUID id;
5652N/A private String mainClass;
5652N/A private String jmxServiceUrl;
5652N/A private String instanceName;
5652N/A
5652N/A /**
5652N/A * Create new instance from user provided data. Set mandatory fields
5652N/A *
5652N/A * @param id - java instance id
5652N/A * @param jmxServiceUrl - JMX service url
5652N/A */
5652N/A public JdpJmxPacket(UUID id, String jmxServiceUrl) {
5652N/A this.id = id;
5652N/A this.jmxServiceUrl = jmxServiceUrl;
5652N/A }
5652N/A
5652N/A /**
5652N/A * Create new instance from network data Parse packet and set fields.
5652N/A *
5652N/A * @param data - raw packet data as it came from a Net
5652N/A * @throws JdpException
5652N/A */
5652N/A public JdpJmxPacket(byte[] data)
5652N/A throws JdpException {
5652N/A JdpPacketReader reader;
5652N/A
5652N/A reader = new JdpPacketReader(data);
5652N/A Map<String, String> p = reader.getDiscoveryDataAsMap();
5652N/A
5652N/A String sId = p.get(UUID_KEY);
5652N/A this.id = (sId == null) ? null : UUID.fromString(sId);
5652N/A this.jmxServiceUrl = p.get(JMX_SERVICE_URL_KEY);
5652N/A this.mainClass = p.get(MAIN_CLASS_KEY);
5652N/A this.instanceName = p.get(INSTANCE_NAME_KEY);
5652N/A }
5652N/A
5652N/A /**
5652N/A * Set main class field
5652N/A *
5652N/A * @param mainClass - main class of running app
5652N/A */
5652N/A public void setMainClass(String mainClass) {
5652N/A this.mainClass = mainClass;
5652N/A }
5652N/A
5652N/A /**
5652N/A * Set instance name field
5652N/A *
5652N/A * @param instanceName - name of instance as provided by customer
5652N/A */
5652N/A public void setInstanceName(String instanceName) {
5652N/A this.instanceName = instanceName;
5652N/A }
5652N/A
5652N/A /**
5652N/A * @return id of discovery session
5652N/A */
5652N/A public UUID getId() {
5652N/A return id;
5652N/A }
5652N/A
5652N/A /**
5652N/A *
5652N/A * @return main class field
5652N/A */
5652N/A public String getMainClass() {
5652N/A return mainClass;
5652N/A }
5652N/A
5652N/A /**
5652N/A *
5652N/A * @return JMX service URL
5652N/A */
5652N/A public String getJmxServiceUrl() {
5652N/A return jmxServiceUrl;
5652N/A }
5652N/A
5652N/A /**
5652N/A *
5652N/A * @return instance name
5652N/A */
5652N/A public String getInstanceName() {
5652N/A return instanceName;
5652N/A }
5652N/A
5652N/A /**
5652N/A *
5652N/A * @return assembled packet ready to be sent across a Net
5652N/A * @throws IOException
5652N/A */
5652N/A @Override
5652N/A public byte[] getPacketData() throws IOException {
5652N/A // Assemble packet from fields to byte array
5652N/A JdpPacketWriter writer;
5652N/A writer = new JdpPacketWriter();
5652N/A writer.addEntry(UUID_KEY, (id == null) ? null : id.toString());
5652N/A writer.addEntry(MAIN_CLASS_KEY, mainClass);
5652N/A writer.addEntry(JMX_SERVICE_URL_KEY, jmxServiceUrl);
5652N/A writer.addEntry(INSTANCE_NAME_KEY, instanceName);
5652N/A return writer.getPacketBytes();
5652N/A }
5652N/A
5652N/A /**
5652N/A *
5652N/A * @return packet hash code
5652N/A */
5652N/A @Override
5652N/A public int hashCode() {
5652N/A int hash = 1;
5652N/A hash = hash * 31 + id.hashCode();
5652N/A hash = hash * 31 + jmxServiceUrl.hashCode();
5652N/A return hash;
5652N/A }
5652N/A
5652N/A /**
5652N/A * Compare two packets
5652N/A *
5652N/A * @param o - packet to compare
5652N/A * @return either packet equals or not
5652N/A */
5652N/A @Override
5652N/A public boolean equals(Object o) {
5652N/A
5652N/A if (o == null || ! (o instanceof JdpJmxPacket) ){
5652N/A return false;
5652N/A }
5652N/A
5652N/A JdpJmxPacket p = (JdpJmxPacket) o;
5652N/A return Objects.equals(id, p.getId()) && Objects.equals(jmxServiceUrl, p.getJmxServiceUrl());
5652N/A }
5652N/A}