diff --git a/make/CompileNativeLibraries.gmk b/make/CompileNativeLibraries.gmk index 57ef12e32cb15e4e151bcf6de49aa74f2e1d83cf..c23b958b066ca325ab0d98fbe69223ee6ee2e9a1 100644 --- a/make/CompileNativeLibraries.gmk +++ b/make/CompileNativeLibraries.gmk @@ -87,6 +87,9 @@ include lib/Awt2dLibraries.gmk include lib/SoundLibraries.gmk +# Include the corresponding custom file, if present. +-include $(CUSTOM_MAKE_DIR)/CompileNativeLibraries.gmk + ########################################################################################## all: $(COPY_FILES) $(BUILD_LIBRARIES) diff --git a/src/share/classes/com/sun/tools/attach/AttachOperationFailedException.java b/src/share/classes/com/sun/tools/attach/AttachOperationFailedException.java new file mode 100644 index 0000000000000000000000000000000000000000..7f676ee49934ecbdb60428badf33edd6d7c3c645 --- /dev/null +++ b/src/share/classes/com/sun/tools/attach/AttachOperationFailedException.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.sun.tools.attach; + +import java.io.IOException; + +/** + * Exception type to signal that an attach operation failed in the target VM. + * + *

This exception can be thrown by the various operations of + * {@link com.sun.tools.attach.VirtualMachine} when the operation + * fails in the target VM. If there is a communication error, + * a regular IOException will be thrown. + * + * @since 1.9 + */ +@jdk.Exported +public class AttachOperationFailedException extends IOException { + + private static final long serialVersionUID = 2140308168167478043L; + + /** + * Constructs an AttachOperationFailedException with + * the specified detail message. + * + * @param s the detail message. + */ + public AttachOperationFailedException(String message) { + super(message); + } +} diff --git a/src/share/classes/com/sun/tools/attach/VirtualMachine.java b/src/share/classes/com/sun/tools/attach/VirtualMachine.java index 1bea751214769958d6cc6969a0e6f68695b2a821..d5e934a6e2d2acbd5524ed35fbee9ef748518c35 100644 --- a/src/share/classes/com/sun/tools/attach/VirtualMachine.java +++ b/src/share/classes/com/sun/tools/attach/VirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -76,16 +76,10 @@ import java.io.IOException; * // attach to target VM * VirtualMachine vm = VirtualMachine.attach("2177"); * - * // get system properties in target VM - * Properties props = vm.getSystemProperties(); - * - * // construct path to management agent - * String home = props.getProperty("java.home"); - * String agent = home + File.separator + "lib" + File.separator - * + "management-agent.jar"; - * - * // load agent into target VM - * vm.loadAgent(agent, "com.sun.management.jmxremote.port=5000"); + * // start management agent + * Properties props = new Properties(); + * props.put("com.sun.management.jmxremote.port", "5000"); + * vm.startManagementAgent(props); * * // detach * vm.detach(); @@ -93,9 +87,9 @@ import java.io.IOException; * * *

In this example we attach to a Java virtual machine that is identified by - * the process identifier 2177. The system properties from the target - * VM are then used to construct the path to a management agent which is then - * loaded into the target VM. Once loaded the client detaches from the target VM.

+ * the process identifier 2177. Then the JMX management agent is + * started in the target process using the supplied arguments. Finally, the + * client detaches from the target VM.

* *

A VirtualMachine is safe for use by multiple concurrent threads.

* @@ -564,8 +558,15 @@ public abstract class VirtualMachine { * * @return The system properties * + * @throws AttachOperationFailedException + * If the target virtual machine is unable to complete the + * attach operation. A more specific error message will be + * given by {@link AttachOperationFailedException#getMessage()}. + * * @throws IOException - * If an I/O error occurs + * If an I/O error occurs, a communication error for example, + * that cannot be identified as an error to indicate that the + * operation failed in the target VM. * * @see java.lang.System#getProperties * @see #loadAgentLibrary @@ -591,11 +592,80 @@ public abstract class VirtualMachine { * * @return The agent properties * + * @throws AttachOperationFailedException + * If the target virtual machine is unable to complete the + * attach operation. A more specific error message will be + * given by {@link AttachOperationFailedException#getMessage()}. + * * @throws IOException - * If an I/O error occurs + * If an I/O error occurs, a communication error for example, + * that cannot be identified as an error to indicate that the + * operation failed in the target VM. */ public abstract Properties getAgentProperties() throws IOException; + /** + * Starts the JMX management agent in the target virtual machine. + * + *

The configuration properties are the same as those specified on + * the command line when starting the JMX management agent. In the same + * way as on the command line, you need to specify at least the + * {@code com.sun.management.jmxremote.port} property. + * + *

See the online documentation for + * Monitoring and Management Using JMX Technology for further details. + * + * @param agentProperties + * A Properties object containing the configuration properties + * for the agent. + * + * @throws AttachOperationFailedException + * If the target virtual machine is unable to complete the + * attach operation. A more specific error message will be + * given by {@link AttachOperationFailedException#getMessage()}. + * + * @throws IOException + * If an I/O error occurs, a communication error for example, + * that cannot be identified as an error to indicate that the + * operation failed in the target VM. + * + * @throws IllegalArgumentException + * If keys or values in agentProperties are invalid. + * + * @throws NullPointerException + * If agentProperties is null. + * + * @since 1.8 + */ + public abstract void startManagementAgent(Properties agentProperties) throws IOException; + + /** + * Starts the local JMX management agent in the target virtual machine. + * + *

See the online documentation for + * Monitoring and Management Using JMX Technology for further details. + * + * @return The String representation of the local connector's service address. + * The value can be parsed by the + * {@link javax.management.remote.JMXServiceURL#JMXServiceURL(String)} + * constructor. + * + * @throws AttachOperationFailedException + * If the target virtual machine is unable to complete the + * attach operation. A more specific error message will be + * given by {@link AttachOperationFailedException#getMessage()}. + * + * @throws IOException + * If an I/O error occurs, a communication error for example, + * that cannot be identified as an error to indicate that the + * operation failed in the target VM. + * + * @since 1.8 + */ + public abstract String startLocalManagementAgent() throws IOException; + /** * Returns a hash-code value for this VirtualMachine. The hash * code is based upon the VirtualMachine's components, and satifies diff --git a/src/share/classes/java/lang/Class.java b/src/share/classes/java/lang/Class.java index eb2b8b80a76d79098a645657d34d1f9d8fec1e65..2440baaaba545a6775c64f0a65635389641bb0eb 100644 --- a/src/share/classes/java/lang/Class.java +++ b/src/share/classes/java/lang/Class.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -130,11 +130,15 @@ public final class Class implements java.io.Serializable, } /* - * Constructor. Only the Java Virtual Machine creates Class - * objects. + * Private constructor. Only the Java Virtual Machine creates Class objects. + * This constructor is not used and prevents the default constructor being + * generated. */ - private Class() {} - + private Class(ClassLoader loader) { + // Initialize final field for classLoader. The initialization value of non-null + // prevents future JIT optimizations from assuming this final field is null. + classLoader = loader; + } /** * Converts the object to a string. The string representation is the @@ -677,8 +681,10 @@ public final class Class implements java.io.Serializable, } // Package-private to allow ClassLoader access - native ClassLoader getClassLoader0(); + ClassLoader getClassLoader0() { return classLoader; } + // Initialized in JVM not by private constructor + private final ClassLoader classLoader; /** * Returns an array of {@code TypeVariable} objects that represent the diff --git a/src/share/classes/java/lang/reflect/AccessibleObject.java b/src/share/classes/java/lang/reflect/AccessibleObject.java index f98aed5db25e3f29e19df4e8d2314fbd92a719a9..0bf5e0eea417d12b264bb493258acecb535917b0 100644 --- a/src/share/classes/java/lang/reflect/AccessibleObject.java +++ b/src/share/classes/java/lang/reflect/AccessibleObject.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -129,16 +129,24 @@ public class AccessibleObject implements AnnotatedElement { setAccessible0(this, flag); } - /* Check that you aren't exposing java.lang.Class.. */ + /* Check that you aren't exposing java.lang.Class. or sensitive + fields in java.lang.Class. */ private static void setAccessible0(AccessibleObject obj, boolean flag) throws SecurityException { if (obj instanceof Constructor && flag == true) { Constructor c = (Constructor)obj; if (c.getDeclaringClass() == Class.class) { - throw new SecurityException("Can not make a java.lang.Class" + + throw new SecurityException("Cannot make a java.lang.Class" + " constructor accessible"); } + } else if (obj instanceof Field && flag == true) { + Field f = (Field)obj; + if (f.getDeclaringClass() == Class.class && + f.getName().equals("classLoader")) { + throw new SecurityException("Cannot make java.lang.Class.classLoader" + + " accessible"); + } } obj.override = flag; } diff --git a/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java b/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java index 4b858bbb82cc36dccce1f5e2231e20a37f04df2f..bf2e9516525aa16bf78361fe7dd6925bae73f68c 100644 --- a/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java +++ b/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,7 +33,7 @@ import com.sun.tools.attach.spi.AttachProvider; import java.io.InputStream; import java.io.IOException; import java.util.Properties; -import java.util.Map; +import java.util.stream.Collectors; /* * The HotSpot implementation of com.sun.tools.attach.VirtualMachine. @@ -161,6 +161,50 @@ public abstract class HotSpotVirtualMachine extends VirtualMachine { return props; } + private static final String MANAGMENT_PREFIX = "com.sun.management."; + + private static boolean checkedKeyName(Object key) { + if (!(key instanceof String)) { + throw new IllegalArgumentException("Invalid option (not a String): "+key); + } + if (!((String)key).startsWith(MANAGMENT_PREFIX)) { + throw new IllegalArgumentException("Invalid option: "+key); + } + return true; + } + + private static String stripKeyName(Object key) { + return ((String)key).substring(MANAGMENT_PREFIX.length()); + } + + @Override + public void startManagementAgent(Properties agentProperties) throws IOException { + if (agentProperties == null) { + throw new NullPointerException("agentProperties cannot be null"); + } + // Convert the arguments into arguments suitable for the Diagnostic Command: + // "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false" + String args = agentProperties.entrySet().stream() + .filter(entry -> checkedKeyName(entry.getKey())) + .map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue())) + .collect(Collectors.joining(" ")); + executeJCmd("ManagementAgent.start " + args); + } + + private String escape(Object arg) { + String value = arg.toString(); + if (value.contains(" ")) { + return "'" + value + "'"; + } + return value; + } + + @Override + public String startLocalManagementAgent() throws IOException { + executeJCmd("ManagementAgent.start_local"); + return getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress"); + } + // --- HotSpot specific methods --- // same as SIGQUIT @@ -257,6 +301,20 @@ public abstract class HotSpotVirtualMachine extends VirtualMachine { return value; } + /* + * Utility method to read data into a String. + */ + String readErrorMessage(InputStream sis) throws IOException { + byte b[] = new byte[1024]; + int n; + StringBuffer message = new StringBuffer(); + while ((n = sis.read(b)) != -1) { + message.append(new String(b, 0, n, "UTF-8")); + } + return message.toString(); + } + + // -- attach timeout support private static long defaultAttachTimeout = 5000; diff --git a/src/share/classes/sun/tools/jcmd/JCmd.java b/src/share/classes/sun/tools/jcmd/JCmd.java index 8c5a2c50274c798a421a3cdffc9aba8420497b4f..0ff46323ec6ff828e9488c2a5c2db553ac822fc1 100644 --- a/src/share/classes/sun/tools/jcmd/JCmd.java +++ b/src/share/classes/sun/tools/jcmd/JCmd.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,10 +33,12 @@ import java.util.ArrayList; import java.util.Comparator; import java.net.URISyntaxException; +import com.sun.tools.attach.AttachOperationFailedException; import com.sun.tools.attach.VirtualMachine; import com.sun.tools.attach.VirtualMachineDescriptor; import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; + import sun.tools.attach.HotSpotVirtualMachine; import sun.tools.jstat.JStatLogger; import sun.jvmstat.monitor.Monitor; @@ -119,6 +121,7 @@ public class JCmd { pids.add(arg.getPid() + ""); } + boolean success = true; for (String pid : pids) { System.out.println(pid + ":"); if (arg.isListCounters()) { @@ -126,11 +129,16 @@ public class JCmd { } else { try { executeCommandForPid(pid, arg.getCommand()); + } catch(AttachOperationFailedException ex) { + System.err.println(ex.getMessage()); + success = false; } catch(Exception ex) { ex.printStackTrace(); + success = false; } } } + System.exit(success ? 0 : 1); } private static void executeCommandForPid(String pid, String command) @@ -150,13 +158,18 @@ public class JCmd { // read to EOF and just print output byte b[] = new byte[256]; int n; + boolean messagePrinted = false; do { n = in.read(b); if (n > 0) { String s = new String(b, 0, n, "UTF-8"); System.out.print(s); + messagePrinted = true; } } while (n > 0); + if (!messagePrinted) { + System.out.println("Command executed successfully"); + } } } vm.detach(); diff --git a/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java b/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java index 65b03e0fac0ccb9d0ebedfda3b6fb5fe6c229064..e6c827b70ce4202543fce1e9c2196038f25bfaae 100644 --- a/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java +++ b/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,8 +32,6 @@ import java.io.File; // Sun specific import com.sun.tools.attach.VirtualMachine; import com.sun.tools.attach.VirtualMachineDescriptor; -import com.sun.tools.attach.AgentInitializationException; -import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; // Sun private @@ -238,35 +236,7 @@ public class LocalVirtualMachine { throw ioe; } - String home = vm.getSystemProperties().getProperty("java.home"); - - // Normally in ${java.home}/jre/lib/management-agent.jar but might - // be in ${java.home}/lib in build environments. - - String agent = home + File.separator + "jre" + File.separator + - "lib" + File.separator + "management-agent.jar"; - File f = new File(agent); - if (!f.exists()) { - agent = home + File.separator + "lib" + File.separator + - "management-agent.jar"; - f = new File(agent); - if (!f.exists()) { - throw new IOException("Management agent not found"); - } - } - - agent = f.getCanonicalPath(); - try { - vm.loadAgent(agent, "com.sun.management.jmxremote"); - } catch (AgentLoadException x) { - IOException ioe = new IOException(x.getMessage()); - ioe.initCause(x); - throw ioe; - } catch (AgentInitializationException x) { - IOException ioe = new IOException(x.getMessage()); - ioe.initCause(x); - throw ioe; - } + vm.startLocalManagementAgent(); // get the connector address Properties agentProps = vm.getAgentProperties(); diff --git a/src/share/javavm/export/jvm.h b/src/share/javavm/export/jvm.h index 2bef0812054f6a71e90f00e1273a1684750deb51..396ca68558e20b3023db6c55809fa23cbe0db769 100644 --- a/src/share/javavm/export/jvm.h +++ b/src/share/javavm/export/jvm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -426,9 +426,6 @@ JVM_GetClassName(JNIEnv *env, jclass cls); JNIEXPORT jobjectArray JNICALL JVM_GetClassInterfaces(JNIEnv *env, jclass cls); -JNIEXPORT jobject JNICALL -JVM_GetClassLoader(JNIEnv *env, jclass cls); - JNIEXPORT jboolean JNICALL JVM_IsInterface(JNIEnv *env, jclass cls); diff --git a/src/share/native/common/check_code.c b/src/share/native/common/check_code.c index 92d96b19675c1a852e27b672811876d2b2b0d44f..4ffbd67eaf246a6c68ec4a68365c9a7d82f5cb06 100644 --- a/src/share/native/common/check_code.c +++ b/src/share/native/common/check_code.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1357,16 +1357,9 @@ verify_opcode_operands(context_type *context, unsigned int inumber, int offset) } (*env)->DeleteLocalRef(env, super); - /* The optimizer make cause this to happen on local code */ + /* The optimizer may cause this to happen on local code */ if (not_found) { -#ifdef BROKEN_JAVAC - jobject loader = JVM_GetClassLoader(env, context->class); - int has_loader = (loader != 0); - (*env)->DeleteLocalRef(env, loader); - if (has_loader) -#endif /* BROKEN_JAVAC */ - CCerror(context, - "Illegal use of nonvirtual function call"); + CCerror(context, "Illegal use of nonvirtual function call"); } } } diff --git a/src/share/native/java/lang/Class.c b/src/share/native/java/lang/Class.c index b0ba34349d8d19ebcaffc1d77cfabd5e57c01124..c5e717fe0df57c6736376189aa5c8d980ae16ba3 100644 --- a/src/share/native/java/lang/Class.c +++ b/src/share/native/java/lang/Class.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -45,7 +45,6 @@ extern jboolean VerifyFixClassname(char *utf_name); #define CLS "Ljava/lang/Class;" #define CPL "Lsun/reflect/ConstantPool;" #define STR "Ljava/lang/String;" -#define JCL "Ljava/lang/ClassLoader;" #define FLD "Ljava/lang/reflect/Field;" #define MHD "Ljava/lang/reflect/Method;" #define CTR "Ljava/lang/reflect/Constructor;" @@ -56,7 +55,6 @@ static JNINativeMethod methods[] = { {"getName0", "()" STR, (void *)&JVM_GetClassName}, {"getSuperclass", "()" CLS, NULL}, {"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces}, - {"getClassLoader0", "()" JCL, (void *)&JVM_GetClassLoader}, {"isInterface", "()Z", (void *)&JVM_IsInterface}, {"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners}, {"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners}, @@ -81,7 +79,6 @@ static JNINativeMethod methods[] = { #undef OBJ #undef CLS #undef STR -#undef JCL #undef FLD #undef MHD #undef CTR diff --git a/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java b/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java index d58a4c100f67eb81c28aca1c09b46b81087fad2e..ad344f1411eb09fb6dcd73dcb65856218ffbaa91 100644 --- a/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java +++ b/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,14 +24,14 @@ */ package sun.tools.attach; -import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.AttachOperationFailedException; import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.spi.AttachProvider; + import java.io.InputStream; import java.io.IOException; import java.io.File; -import java.util.Properties; /* * Bsd implementation of HotSpotVirtualMachine @@ -191,6 +191,8 @@ public class BsdVirtualMachine extends HotSpotVirtualMachine { } if (completionStatus != 0) { + // read from the stream and use that as the error message + String message = readErrorMessage(sis); sis.close(); // In the event of a protocol mismatch then the target VM @@ -205,7 +207,11 @@ public class BsdVirtualMachine extends HotSpotVirtualMachine { if (cmd.equals("load")) { throw new AgentLoadException("Failed to load agent library"); } else { - throw new IOException("Command failed in target VM"); + if (message == null) { + throw new AttachOperationFailedException("Command failed in target VM"); + } else { + throw new AttachOperationFailedException(message); + } } } @@ -237,8 +243,9 @@ public class BsdVirtualMachine extends HotSpotVirtualMachine { if ((off < 0) || (off > bs.length) || (len < 0) || ((off + len) > bs.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); - } else if (len == 0) + } else if (len == 0) { return 0; + } return BsdVirtualMachine.read(s, bs, off, len); } diff --git a/src/solaris/classes/sun/tools/attach/LinuxVirtualMachine.java b/src/solaris/classes/sun/tools/attach/LinuxVirtualMachine.java index 1e08d70446e4516deceaa45afcd585dd85f0fdff..9dec425da0ddcc5668f149a45be04f972fe775c9 100644 --- a/src/solaris/classes/sun/tools/attach/LinuxVirtualMachine.java +++ b/src/solaris/classes/sun/tools/attach/LinuxVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,14 +24,14 @@ */ package sun.tools.attach; -import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.AttachOperationFailedException; import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.spi.AttachProvider; + import java.io.InputStream; import java.io.IOException; import java.io.File; -import java.util.Properties; /* * Linux implementation of HotSpotVirtualMachine @@ -207,6 +207,8 @@ public class LinuxVirtualMachine extends HotSpotVirtualMachine { } if (completionStatus != 0) { + // read from the stream and use that as the error message + String message = readErrorMessage(sis); sis.close(); // In the event of a protocol mismatch then the target VM @@ -221,7 +223,11 @@ public class LinuxVirtualMachine extends HotSpotVirtualMachine { if (cmd.equals("load")) { throw new AgentLoadException("Failed to load agent library"); } else { - throw new IOException("Command failed in target VM"); + if (message == null) { + throw new AttachOperationFailedException("Command failed in target VM"); + } else { + throw new AttachOperationFailedException(message); + } } } diff --git a/src/solaris/classes/sun/tools/attach/SolarisVirtualMachine.java b/src/solaris/classes/sun/tools/attach/SolarisVirtualMachine.java index c7dcf21cf03c5c3f7db111bc13e0bfb8fc1c9713..3c57e4bda6ad24f198a81ab339ad44fac4691d39 100644 --- a/src/solaris/classes/sun/tools/attach/SolarisVirtualMachine.java +++ b/src/solaris/classes/sun/tools/attach/SolarisVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,15 +24,15 @@ */ package sun.tools.attach; -import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.AttachOperationFailedException; import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.spi.AttachProvider; + import java.io.InputStream; import java.io.IOException; import java.io.File; import java.io.FileNotFoundException; -import java.util.Properties; /* * Solaris implementation of HotSpotVirtualMachine. @@ -147,11 +147,17 @@ public class SolarisVirtualMachine extends HotSpotVirtualMachine { // If non-0 it means an error but we need to special-case the // "load" command to ensure that the right exception is thrown. if (completionStatus != 0) { + // read from the stream and use that as the error message + String message = readErrorMessage(sis); sis.close(); if (cmd.equals("load")) { throw new AgentLoadException("Failed to load agent library"); } else { - throw new IOException("Command failed in target VM"); + if (message == null) { + throw new AttachOperationFailedException("Command failed in target VM"); + } else { + throw new AttachOperationFailedException(message); + } } } diff --git a/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c b/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c index cf3ff20300f02c79ab48b104a3484c1451899a2e..f8283ab1a830f11df79373d92105b577707dbec8 100644 --- a/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c +++ b/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -198,14 +198,14 @@ JNIEXPORT jint JNICALL Java_sun_tools_attach_BsdVirtualMachine_read len = remaining; } - RESTARTABLE(read(fd, buf+off, len), n); + RESTARTABLE(read(fd, buf, len), n); if (n == -1) { JNU_ThrowIOExceptionWithLastError(env, "read"); } else { if (n == 0) { n = -1; // EOF } else { - (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf+off)); + (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf)); } } return n; diff --git a/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c b/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c index 8c8c4eb1d69ef9be967fb67f3889b4696c4762b7..beb57648eeccf5e76ebd671f7eff7b685f326dab 100644 --- a/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c +++ b/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c @@ -416,14 +416,14 @@ JNIEXPORT jint JNICALL Java_sun_tools_attach_LinuxVirtualMachine_read len = remaining; } - RESTARTABLE(read(fd, buf+off, len), n); + RESTARTABLE(read(fd, buf, len), n); if (n == -1) { JNU_ThrowIOExceptionWithLastError(env, "read"); } else { if (n == 0) { n = -1; // EOF } else { - (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf+off)); + (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf)); } } return n; diff --git a/src/solaris/native/sun/tools/attach/SolarisVirtualMachine.c b/src/solaris/native/sun/tools/attach/SolarisVirtualMachine.c index 45b38eafe5eb8e073db249fa0ef1dc7e5b14aed1..32dd249f321b93619e2cdab83e6244626f31b467 100644 --- a/src/solaris/native/sun/tools/attach/SolarisVirtualMachine.c +++ b/src/solaris/native/sun/tools/attach/SolarisVirtualMachine.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -161,14 +161,14 @@ JNIEXPORT jint JNICALL Java_sun_tools_attach_SolarisVirtualMachine_read len = remaining; } - RESTARTABLE(read(fd, buf+off, len), n); + RESTARTABLE(read(fd, buf, len), n); if (n == -1) { JNU_ThrowIOExceptionWithLastError(env, "read"); } else { if (n == 0) { n = -1; // EOF } else { - (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf+off)); + (*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf)); } } return n; diff --git a/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java b/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java index 6ea66773f1d82a81972873266779574ccaac9b35..3b34c0d181a3968ada615618142da57cc55b7f77 100644 --- a/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java +++ b/src/windows/classes/sun/tools/attach/WindowsVirtualMachine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,15 +24,15 @@ */ package sun.tools.attach; -import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.AttachOperationFailedException; import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.spi.AttachProvider; + import sun.tools.attach.HotSpotVirtualMachine; + import java.io.IOException; -import java.io.File; import java.io.InputStream; -import java.util.Properties; import java.util.Random; public class WindowsVirtualMachine extends HotSpotVirtualMachine { @@ -105,11 +105,17 @@ public class WindowsVirtualMachine extends HotSpotVirtualMachine { // read completion status int status = readInt(is); if (status != 0) { + // read from the stream and use that as the error message + String message = readErrorMessage(is); // special case the load command so that the right exception is thrown if (cmd.equals("load")) { throw new AgentLoadException("Failed to load agent library"); } else { - throw new IOException("Command failed in target VM"); + if (message == null) { + throw new AttachOperationFailedException("Command failed in target VM"); + } else { + throw new AttachOperationFailedException(message); + } } } diff --git a/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c b/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c index d7016723c3b093303909c02c88806ffe23d83ea7..82628e17e4cc67676b95daafb858021745af99ad 100644 --- a/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c +++ b/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c @@ -341,7 +341,7 @@ JNIEXPORT jint JNICALL Java_sun_tools_attach_WindowsVirtualMachine_readPipe if (nread == 0) { return (jint)-1; // EOF } else { - (*env)->SetByteArrayRegion(env, ba, off, (jint)nread, (jbyte *)(buf+off)); + (*env)->SetByteArrayRegion(env, ba, off, (jint)nread, (jbyte *)(buf)); } } diff --git a/test/com/sun/tools/attach/SimpleProvider.java b/test/com/sun/tools/attach/SimpleProvider.java index 2a380f56215e49b0b5dc51dc3736071c809289c9..54f3dadf040971ccdffd17823eea67454f804081 100644 --- a/test/com/sun/tools/attach/SimpleProvider.java +++ b/test/com/sun/tools/attach/SimpleProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -100,4 +100,12 @@ class SimpleVirtualMachine extends VirtualMachine { public void dataDumpRequest() throws IOException { } + + public String startLocalManagementAgent() { + return null; + } + + public void startManagementAgent(Properties agentProperties) { + } + } diff --git a/test/com/sun/tools/attach/StartManagementAgent.java b/test/com/sun/tools/attach/StartManagementAgent.java new file mode 100644 index 0000000000000000000000000000000000000000..16e701ede37c6d7b3f214c7cf01261730691a394 --- /dev/null +++ b/test/com/sun/tools/attach/StartManagementAgent.java @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import com.sun.tools.attach.AttachOperationFailedException; +import com.sun.tools.attach.VirtualMachine; + +import java.io.File; +import java.io.FileWriter; +import java.util.Properties; +import java.util.HashMap; + +import javax.management.remote.JMXServiceURL; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; + +import jdk.testlibrary.ProcessThread; +import jdk.testlibrary.Utils; + +/* + * @test + * @summary Test for VirtualMachine.startManagementAgent and VirtualMachine.startLocalManagementAgent + * @library /lib/testlibrary + * @run build Application Shutdown SimpleProvider jdk.testlibrary.* + * @run main StartManagementAgent + */ + +/* + * This test is not meant to test all possible configuration parameters to + * the JMX agent, there are other tests for that. This test makes sure it is + * possible to start the agent via attach. + */ +public class StartManagementAgent { + public static void main(String[] args) throws Throwable { + final String pidFile = "StartManagementAgent.Application.pid"; + ProcessThread processThread = null; + RunnerUtil.ProcessInfo info = null; + try { + processThread = RunnerUtil.startApplication(pidFile); + info = RunnerUtil.readProcessInfo(pidFile); + runTests(info.pid); + } catch (Throwable t) { + System.out.println("StartManagementAgent got unexpected exception: " + t); + t.printStackTrace(); + throw t; + } finally { + // Make sure the Application process is stopped. + RunnerUtil.stopApplication(info.shutdownPort, processThread); + } + } + + private static void basicTests(VirtualMachine vm) throws Exception { + + // Try calling with null argument + boolean exception = false; + try { + vm.startManagementAgent(null); + } catch (NullPointerException e) { + exception = true; + } + if (!exception) { + throw new Exception("startManagementAgent(null) should throw NPE"); + } + + // Try calling with a property value with a space in it + Properties p = new Properties(); + File f = new File("file with space"); + try (FileWriter fw = new FileWriter(f)) { + fw.write("com.sun.management.jmxremote.port=apa"); + } + p.put("com.sun.management.config.file", f.getAbsolutePath()); + try { + vm.startManagementAgent(p); + } catch(AttachOperationFailedException ex) { + // We expect parsing of "apa" above to fail, but if the file path + // can't be read we get a different exception message + if (!ex.getMessage().contains("java.lang.NumberFormatException")) { + throw ex; + } + } + } + + private static final String LOCAL_CONNECTOR_ADDRESS_PROP = + "com.sun.management.jmxremote.localConnectorAddress"; + + private static final int MAX_RETRIES = 10; + + public static void runTests(int pid) throws Exception { + VirtualMachine vm = VirtualMachine.attach(""+pid); + try { + + basicTests(vm); + + testLocalAgent(vm); + + // we retry the remote case several times in case the error + // was caused by a port conflict + int i = 0; + boolean success = false; + do { + try { + System.err.println("Trying remote agent. Try #" + i); + testRemoteAgent(vm); + success = true; + } catch(Exception ex) { + System.err.println("testRemoteAgent failed with exception:"); + ex.printStackTrace(); + System.err.println("Retrying."); + } + i++; + } while(!success && i < MAX_RETRIES); + if (!success) { + throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries"); + } + } finally { + vm.detach(); + } + } + + public static void testLocalAgent(VirtualMachine vm) throws Exception { + Properties agentProps = vm.getAgentProperties(); + String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); + if (address != null) { + throw new Exception("Local management agent already started"); + } + + String result = vm.startLocalManagementAgent(); + + // try to parse the return value as a JMXServiceURL + new JMXServiceURL(result); + + agentProps = vm.getAgentProperties(); + address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); + if (address == null) { + throw new Exception("Local management agent could not be started"); + } + } + + public static void testRemoteAgent(VirtualMachine vm) throws Exception { + int port = Utils.getFreePort(); + + // try to connect - should fail + tryConnect(port, false); + + // start agent + System.out.println("Starting agent on port: " + port); + Properties mgmtProps = new Properties(); + mgmtProps.put("com.sun.management.jmxremote.port", port); + mgmtProps.put("com.sun.management.jmxremote.authenticate", "false"); + mgmtProps.put("com.sun.management.jmxremote.ssl", "false"); + vm.startManagementAgent(mgmtProps); + + // try to connect - should work + tryConnect(port, true); + + // try to start again - should fail + boolean exception = false; + try { + vm.startManagementAgent(mgmtProps); + } catch(AttachOperationFailedException ex) { + // expected + exception = true; + } + if (!exception) { + throw new Exception("Expected the second call to vm.startManagementAgent() to fail"); + } + } + + private static void tryConnect(int port, boolean shouldSucceed) throws Exception { + String jmxUrlStr = + String.format( + "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", + port); + JMXServiceURL url = new JMXServiceURL(jmxUrlStr); + HashMap env = new HashMap<>(); + + boolean succeeded; + try { + JMXConnector c = JMXConnectorFactory.connect(url, env); + c.getMBeanServerConnection(); + succeeded = true; + } catch(Exception ex) { + succeeded = false; + } + if (succeeded && !shouldSucceed) { + throw new Exception("Could connect to agent, but should not have been possible"); + } + if (!succeeded && shouldSucceed) { + throw new Exception("Could not connect to agent"); + } + } +} diff --git a/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java b/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java index 24397656a906c26949efe6fba5780f942d196fe3..558b2dabb041de17b537ec51f9c43cdebd8c7271 100644 --- a/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java +++ b/test/sun/management/jmxremote/bootstrap/JvmstatCountersTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -143,9 +143,12 @@ public class JvmstatCountersTest { String vmid = name.substring(0, name.indexOf("@")); System.out.println("vmid = " + vmid); VirtualMachine vm = VirtualMachine.attach(vmid); - String agent = vm.getSystemProperties().getProperty("java.home") + - File.separator + "lib" + File.separator + "management-agent.jar"; - vm.loadAgent(agent, "com.sun.management.jmxremote.port=0,com.sun.management.jmxremote.authenticate=false,com.sun.management.jmxremote.ssl=false"); + Properties p = new Properties(); + p.put("com.sun.management.jmxremote.port", "0"); + p.put("com.sun.management.jmxremote.authenticate", "false"); + p.put("com.sun.management.jmxremote.ssl", "false"); + vm.startManagementAgent(p); + vm.startLocalManagementAgent(); vm.detach(); String localAddress2 = ConnectorAddressLink.importFrom(0); if (localAddress2 == null) { diff --git a/test/sun/management/jmxremote/bootstrap/LocalManagementTest.java b/test/sun/management/jmxremote/bootstrap/LocalManagementTest.java index 6eecbda4d2291aa43b4d18e90570a5a80b216169..54173e6eff9ecf3c42dd6b223689c09c5455acc8 100644 --- a/test/sun/management/jmxremote/bootstrap/LocalManagementTest.java +++ b/test/sun/management/jmxremote/bootstrap/LocalManagementTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -79,16 +79,6 @@ public class LocalManagementTest { return doTest("1", "-Dcom.sun.management.jmxremote"); } - private static boolean test2() throws Exception { - Path agentPath = findAgent(); - if (agentPath != null) { - String agent = agentPath.toString(); - return doTest("2", "-javaagent:" + agent); - } else { - return false; - } - } - /** * no args (blank) - manager should attach and start agent */ @@ -96,41 +86,6 @@ public class LocalManagementTest { return doTest("3", null); } - /** - * sanity check arguments to management-agent.jar - */ - private static boolean test4() throws Exception { - Path agentPath = findAgent(); - if (agentPath != null) { - ProcessBuilder builder = ProcessTools.createJavaProcessBuilder( - "-javaagent:" + agentPath.toString() + - "=com.sun.management.jmxremote.port=7775," + - "com.sun.management.jmxremote.authenticate=false," + - "com.sun.management.jmxremote.ssl=false", - "-cp", - TEST_CLASSPATH, - "TestApplication", - "-exit" - ); - - Process prc = null; - try { - prc = ProcessTools.startProcess( - "TestApplication", - builder - ); - int exitCode = prc.waitFor(); - return exitCode == 0; - } finally { - if (prc != null) { - prc.destroy(); - prc.waitFor(); - } - } - } - return false; - } - /** * use DNS-only name service */ @@ -138,27 +93,6 @@ public class LocalManagementTest { return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\""); } - private static Path findAgent() { - FileSystem FS = FileSystems.getDefault(); - Path agentPath = FS.getPath( - TEST_JDK, "jre", "lib", "management-agent.jar" - ); - if (!isFileOk(agentPath)) { - agentPath = FS.getPath( - TEST_JDK, "lib", "management-agent.jar" - ); - } - if (!isFileOk(agentPath)) { - System.err.println("Can not locate management-agent.jar"); - return null; - } - return agentPath; - } - - private static boolean isFileOk(Path path) { - return Files.isRegularFile(path) && Files.isReadable(path); - } - private static boolean doTest(String testId, String arg) throws Exception { List args = new ArrayList<>(); args.add("-cp"); diff --git a/test/sun/management/jmxremote/bootstrap/TestManager.java b/test/sun/management/jmxremote/bootstrap/TestManager.java index 4495c2d07fe60e9b567dd1677b0650568171dfb5..76b43249df80480fb6057517d0cd389db833e0dc 100644 --- a/test/sun/management/jmxremote/bootstrap/TestManager.java +++ b/test/sun/management/jmxremote/bootstrap/TestManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,7 +40,6 @@ import java.lang.management.RuntimeMXBean; import static java.lang.management.ManagementFactory.*; import java.net.Socket; import java.net.InetSocketAddress; -import java.io.File; import java.io.IOException; // Sun specific @@ -55,28 +54,8 @@ public class TestManager { * Starts the management agent in the target VM */ private static void startManagementAgent(String pid) throws IOException { - /* - * JAR file normally in ${java.home}/jre/lib but may be in ${java.home}/lib - * with development/non-images builds - */ - String home = System.getProperty("java.home"); - String agent = home + File.separator + "jre" + File.separator + "lib" - + File.separator + "management-agent.jar"; - File f = new File(agent); - if (!f.exists()) { - agent = home + File.separator + "lib" + File.separator + - "management-agent.jar"; - f = new File(agent); - if (!f.exists()) { - throw new RuntimeException("management-agent.jar missing"); - } - } - agent = f.getCanonicalPath(); - - System.out.println("Loading " + agent + " into target VM ..."); - try { - VirtualMachine.attach(pid).loadAgent(agent); + VirtualMachine.attach(pid).startLocalManagementAgent(); } catch (Exception x) { throw new IOException(x.getMessage()); } @@ -122,8 +101,7 @@ public class TestManager { if (agentPropLocalConnectorAddress == null && jvmstatLocalConnectorAddress == null) { - // No JMX Connector address so attach to VM, and load - // management-agent.jar + // No JMX Connector address so attach to VM, and start local agent startManagementAgent(pid); agentPropLocalConnectorAddress = (String) vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP); diff --git a/test/sun/management/jmxremote/startstop/JMXStartStopTest.java b/test/sun/management/jmxremote/startstop/JMXStartStopTest.java index cacb7087c27bb3c56d207b6769d47d399ab1c1a0..d9372448c500886242fa6b24fbed8e2b566cbc8d 100644 --- a/test/sun/management/jmxremote/startstop/JMXStartStopTest.java +++ b/test/sun/management/jmxremote/startstop/JMXStartStopTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,16 +31,13 @@ import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import javax.management.*; @@ -60,7 +57,6 @@ import jdk.testlibrary.JDKToolLauncher; * JCMD achieves the desired results */ public class JMXStartStopTest { - private static final String TEST_JDK = System.getProperty("test.jdk"); private static final String TEST_SRC = System.getProperty("test.src"); private static final boolean verbose = false; @@ -76,8 +72,8 @@ public class JMXStartStopTest { QueryExp query) throws Exception { - Set names = server.queryNames(pattern,query); - for (Iterator i=names.iterator(); i.hasNext(); ) { + Set names = server.queryNames(pattern,query); + for (Iterator i = names.iterator(); i.hasNext(); ) { ObjectName name = (ObjectName)i.next(); MBeanInfo info = server.getMBeanInfo(name); dbg_print("Got MBean: " + name); @@ -87,7 +83,7 @@ public class JMXStartStopTest { continue; for (MBeanAttributeInfo attr : attrs) { if (attr.isReadable()) { - Object o = server.getAttribute(name, attr.getName()); + server.getAttribute(name, attr.getName()); } } } @@ -108,9 +104,8 @@ public class JMXStartStopTest { } JMXServiceURL url = new JMXServiceURL(jmxUrlStr); - Map m = new HashMap(); - JMXConnector c = JMXConnectorFactory.connect(url,m); + JMXConnector c = JMXConnectorFactory.connect(url, null); MBeanServerConnection conn = c.getMBeanServerConnection(); ObjectName pattern = new ObjectName("java.lang:type=Memory,*"); @@ -180,9 +175,8 @@ public class JMXStartStopTest { port); JMXServiceURL url = new JMXServiceURL(jmxUrlStr); - Map m = new HashMap(); - JMXConnector c = JMXConnectorFactory.connect(url,m); + JMXConnector c = JMXConnectorFactory.connect(url, null); MBeanServerConnection conn = c.getMBeanServerConnection(); ObjectName pattern = new ObjectName("java.lang:type=Memory,*"); @@ -273,25 +267,6 @@ public class JMXStartStopTest { } } - /** - * Retrieves the PID of the test application using JCMD - * @return The PID of the test application - * @throws InterruptedException - * @throws IOException - */ - private static String getPID() throws InterruptedException, IOException { - final AtomicReference pid = new AtomicReference<>(); - jcmd( - null, - line -> { - if (line.endsWith("JMXStartStopDoSomething")) { - pid.set(line.split(" ")[0]); - } - } - ); - return pid.get(); - } - private static class Something { private Process p; private final ProcessBuilder pb; @@ -434,7 +409,7 @@ public class JMXStartStopTest { private static final int port1 = 50234; private static final int port2 = 50235; - private static void test_01() throws Exception { + static void test_01() throws Exception { // Run an app with JMX enabled stop it and // restart on other port @@ -459,7 +434,7 @@ public class JMXStartStopTest { } } - private static void test_02() throws Exception { + static void test_02() throws Exception { // Run an app without JMX enabled // start JMX by jcmd @@ -478,7 +453,7 @@ public class JMXStartStopTest { } } - private static void test_03() throws Exception { + static void test_03() throws Exception { // Run an app without JMX enabled // start JMX by jcmd on one port than on other one @@ -507,7 +482,7 @@ public class JMXStartStopTest { } } - private static void test_04() throws Exception { + static void test_04() throws Exception { // Run an app without JMX enabled // start JMX by jcmd on one port, specify rmi port explicitly @@ -528,7 +503,7 @@ public class JMXStartStopTest { } } - private static void test_05() throws Exception { + static void test_05() throws Exception { // Run an app without JMX enabled, it will enable local server // but should leave remote server disabled @@ -545,7 +520,7 @@ public class JMXStartStopTest { } } - private static void test_06() throws Exception { + static void test_06() throws Exception { // Run an app without JMX enabled // start JMX by jcmd on one port, specify rmi port explicitly // attempt to start it again @@ -569,7 +544,7 @@ public class JMXStartStopTest { final boolean[] checks = new boolean[3]; jcmd( line -> { - if (line.equals("java.lang.RuntimeException: Invalid agent state")) { + if (line.contains("java.lang.RuntimeException: Invalid agent state")) { checks[0] = true; } }, @@ -580,7 +555,7 @@ public class JMXStartStopTest { jcmd( line -> { - if (line.equals("java.lang.RuntimeException: Invalid agent state")) { + if (line.contains("java.lang.RuntimeException: Invalid agent state")) { checks[1] = true; } }, @@ -648,7 +623,7 @@ public class JMXStartStopTest { } } - private static void test_08() throws Exception { + static void test_08() throws Exception { // Run an app with JMX enabled and with some properties set // in command line. // stop JMX agent and then start it again with different property values @@ -682,7 +657,7 @@ public class JMXStartStopTest { } } - private static void test_09() throws Exception { + static void test_09() throws Exception { // Run an app with JMX enabled and with some properties set // in command line. // stop JMX agent and then start it again with different property values @@ -718,7 +693,7 @@ public class JMXStartStopTest { } } - private static void test_10() throws Exception { + static void test_10() throws Exception { // Run an app with JMX enabled and with some properties set // in command line. // stop JMX agent and then start it again with different property values @@ -754,7 +729,7 @@ public class JMXStartStopTest { } } - private static void test_11() throws Exception { + static void test_11() throws Exception { // Run an app with JMX enabled // stop remote agent // make sure local agent is not affected @@ -775,7 +750,7 @@ public class JMXStartStopTest { } } - private static void test_12() throws Exception { + static void test_12() throws Exception { // Run an app with JMX disabled // start local agent only @@ -793,28 +768,4 @@ public class JMXStartStopTest { s.stop(); } } - - private static void test_13() throws Exception { - // Run an app with -javaagent make sure it works as expected - - // system properties are ignored - - System.out.println("**** Test thirteen ****"); - - String agent = TEST_JDK + "/jre/lib/management-agent.jar"; - if (!new File(agent).exists()) { - agent = TEST_JDK + "/lib/management-agent.jar"; - } - - Something s = doSomething("test_14", - "-javaagent:" + agent + "=com.sun.management.jmxremote.port=" + - port1 + ",com.sun.management.jmxremote.authenticate=false", - "-Dcom.sun.management.jmxremote.ssl=false" - ); - - try { - testNoConnect(port1); - } finally { - s.stop(); - } - } }