|
31 | 31 | import java.beans.PropertyChangeListener;
|
32 | 32 | import java.beans.PropertyChangeSupport;
|
33 | 33 | import java.util.Properties;
|
| 34 | +import java.util.regex.Pattern; |
34 | 35 | import javax.management.MBeanServerConnection;
|
35 | 36 | import javax.management.remote.JMXServiceURL;
|
36 | 37 |
|
|
71 | 72 | */
|
72 | 73 | public abstract class JmxModel extends Model {
|
73 | 74 |
|
| 75 | + private static final String JAR_SUFFIX = ".jar"; // NOI18N |
| 76 | + private static final Pattern MODULE_MAIN_CLASS_PATTERN = Pattern.compile("^(\\w+\\.)*\\w+/(\\w+\\.)+\\w+$"); |
| 77 | + |
74 | 78 | protected PropertyChangeSupport propertyChangeSupport =
|
75 | 79 | new AsyncPropertyChangeSupport(this);
|
76 | 80 | /**
|
@@ -236,4 +240,108 @@ public void removePropertyChangeListener(PropertyChangeListener listener) {
|
236 | 240 | * @since VisualVM 1.3
|
237 | 241 | */
|
238 | 242 | public abstract void setFlagValue(String name,String value);
|
| 243 | + |
| 244 | + /** |
| 245 | + * Returns the Java virtual machine command line. |
| 246 | + * |
| 247 | + * @return String - contains the command line of the target Java |
| 248 | + * application or <CODE>NULL</CODE> if the |
| 249 | + * command line cannot be determined. |
| 250 | + */ |
| 251 | + public abstract String getCommandLine(); |
| 252 | + |
| 253 | + /** |
| 254 | + * Return the arguments to the main class for the target Java application. |
| 255 | + * Returns the arguments to the main class. If the arguments can't be |
| 256 | + * found, <code>null</code> is returned. |
| 257 | + * |
| 258 | + * @return String - contains the arguments to the main class for the |
| 259 | + * target Java application or the <code>null</code> |
| 260 | + * if the command line cannot be determined. |
| 261 | + */ |
| 262 | + |
| 263 | + public String getMainArgs() { |
| 264 | + String commandLine = getCommandLine(); |
| 265 | + |
| 266 | + if (commandLine != null) { |
| 267 | + String arg0 = getFirstArgument(commandLine); |
| 268 | + |
| 269 | + int firstSpace = arg0.length(); |
| 270 | + if (firstSpace < commandLine.length()) { |
| 271 | + return commandLine.substring(firstSpace); |
| 272 | + } |
| 273 | + } |
| 274 | + return null; |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Return the main class for the target Java application. |
| 279 | + * Returns the main class, if the application started with the <em>-jar</em> option, |
| 280 | + * it tries to determine main class from the jar file. If |
| 281 | + * the jar file is not accessible, the main class is simple |
| 282 | + * name of the jar file. |
| 283 | + * @return String - the main class of the target Java |
| 284 | + * application. |
| 285 | + */ |
| 286 | + public String getMainClass() { |
| 287 | + String commandLine = getCommandLine(); |
| 288 | + |
| 289 | + if (commandLine != null) { |
| 290 | + String mainClassName = getFirstArgument(commandLine); |
| 291 | + |
| 292 | + if (mainClassName.endsWith(JAR_SUFFIX)) { |
| 293 | + mainClassName = mainClassName.replace('\\', '/'); |
| 294 | + int index = mainClassName.lastIndexOf('/'); |
| 295 | + if (index != -1) { |
| 296 | + mainClassName = mainClassName.substring(index + 1); |
| 297 | + } |
| 298 | + } else if (MODULE_MAIN_CLASS_PATTERN.matcher(mainClassName).find()) { |
| 299 | + return mainClassName.substring(mainClassName.indexOf('/')+1); |
| 300 | + } |
| 301 | + mainClassName = mainClassName.replace('\\', '/').replace('/', '.'); |
| 302 | + return mainClassName; |
| 303 | + } |
| 304 | + return null; |
| 305 | + } |
| 306 | + |
| 307 | + private String getFirstArgument(String commandLine) { |
| 308 | + String mainClassName = null; |
| 309 | + |
| 310 | + // search for jar file |
| 311 | + int jarIndex = commandLine.indexOf(JAR_SUFFIX); |
| 312 | + if (jarIndex != -1) { |
| 313 | + String jarFile = commandLine.substring(0,jarIndex+JAR_SUFFIX.length()); |
| 314 | + // if this is not end of commandLine check that jar file is separated by space from other arguments |
| 315 | + if (jarFile.length() == commandLine.length() || commandLine.charAt(jarFile.length()) == ' ') { |
| 316 | + // jarFile must be on classpath |
| 317 | + String classPath = getClassPath(); |
| 318 | + if (classPath != null && classPath.indexOf(jarFile) != -1) { |
| 319 | + mainClassName = jarFile; |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + // it looks like ordinary commandline with main class |
| 324 | + if (mainClassName == null) { |
| 325 | + int firstSpace = commandLine.indexOf(' '); |
| 326 | + if (firstSpace > 0) { |
| 327 | + mainClassName = commandLine.substring(0, firstSpace); |
| 328 | + } else { |
| 329 | + mainClassName = commandLine; |
| 330 | + } |
| 331 | + } |
| 332 | + return mainClassName; |
| 333 | + } |
| 334 | + |
| 335 | + /** |
| 336 | + * Returns the Java virtual machine implementation version. |
| 337 | + * This method is equivalent to {@link System#getProperty |
| 338 | + * System.getProperty("java.class.path")}. |
| 339 | + * |
| 340 | + * @return the Java virtual machine classpath. |
| 341 | + * |
| 342 | + * @see java.lang.System#getProperty |
| 343 | + */ |
| 344 | + private String getClassPath() { |
| 345 | + return getSystemProperties().getProperty("java.class.path"); // NOI18N |
| 346 | + } |
239 | 347 | }
|
0 commit comments