Ejemplo n.º 1
0
    private void tryConnect() throws IOException {
      if (mode == MODE_SELF) {
        jmxc = null;
        conn = ManagementFactory.getPlatformMBeanServer();
      } else {
        if (mode == MODE_LOCAL) {
          if (!lvm.isManageable()) {
            lvm.startManagementAgent();
            if (!lvm.isManageable()) {
              // FIXME: what to throw
              throw new IOException(lvm + " not manageable"); // NOI18N
            }
          }
          if (jmxUrl == null) {
            jmxUrl = new JMXServiceURL(lvm.connectorAddress());
          }
        }

        Map<String, Object> env = new HashMap();
        if (envProvider != null) env.putAll(envProvider.getEnvironment(app, app.getStorage()));
        if (userName != null || password != null)
          env.put(JMXConnector.CREDENTIALS, new String[] {userName, password});

        if (!insecure && mode != MODE_LOCAL && env.get(JMXConnector.CREDENTIALS) != null) {
          env.put("jmx.remote.x.check.stub", "true"); // NOI18N
          checkSSLStub = true;
        } else {
          checkSSLStub = false;
        }

        jmxc = JMXConnectorFactory.newJMXConnector(jmxUrl, env);
        jmxc.addConnectionNotificationListener(this, null, null);
        try {
          jmxc.connect(env);
        } catch (java.io.IOException e) {
          // Likely a SSL-protected RMI registry
          if ("rmi".equals(jmxUrl.getProtocol())) { // NOI18N
            env.put("com.sun.jndi.rmi.factory.socket", sslRMIClientSocketFactory); // NOI18N
            jmxc.connect(env);
          } else {
            throw e;
          }
        }

        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        conn = Checker.newChecker(this, mbsc);
      }
      isDead = false;
    }
Ejemplo n.º 2
0
 void connect() {
   setConnectionState(ConnectionState.CONNECTING);
   try {
     tryConnect();
     setConnectionState(ConnectionState.CONNECTED);
   } catch (SecurityException e) {
     setConnectionState(ConnectionState.DISCONNECTED);
     throw e;
   } catch (SaslException e) {
     // Workaround for JBoss/WildFly authentication failed exception
     throw new SecurityException(e);
   } catch (Exception e) {
     setConnectionState(ConnectionState.DISCONNECTED);
     // Workaround for GlassFish's LoginException class not found
     if (e.toString().contains("com.sun.enterprise.security.LoginException")) { // NOI18N
       throw new SecurityException(
           "Authentication failed! Invalid username or password"); // NOI18N
     }
     if (LOGGER.isLoggable(Level.INFO)) {
       // Try to provide info on the target
       //    Use PID when attach was used to connect,
       //    Use JMXServiceURL otherwise...
       final String param =
           (lvm != null)
               ? String.valueOf(lvm.vmid())
               : ((jmxUrl != null) ? jmxUrl.toString() : ""); // NOI18N
       LOGGER.log(Level.INFO, "connect(" + param + ")", e); // NOI18N
     }
   }
 }
Ejemplo n.º 3
0
  /**
   * Returns all the target VMs that are running at this launcher's target address. Note that these
   * target VMs may or may not have been launched by this launcher. Note also that if the list of
   * running VMs doesn't change on the target, two calls to this method return VMs that are equal.
   *
   * @return the list of running target VMs
   */
  public LocalVirtualMachine[] getRunningVirtualMachines() {
    // Select the VMs that are actually running
    Vector actuallyRunning = new Vector();
    Enumeration en = this.runningVMs.elements();
    while (en.hasMoreElements()) {
      LocalVirtualMachine vm = (LocalVirtualMachine) en.nextElement();
      if (vm.isRunning()) actuallyRunning.addElement(vm);
    }
    this.runningVMs = actuallyRunning;

    // Return the running VMs
    int size = actuallyRunning.size();
    LocalVirtualMachine[] result = new LocalVirtualMachine[size];
    for (int i = 0; i < size; i++) result[i] = (LocalVirtualMachine) actuallyRunning.elementAt(i);
    return result;
  }
Ejemplo n.º 4
0
 /**
  * Creates an instance of {@code JmxModel} for a {@link JvmstatApplication}.
  *
  * @param application the {@link JvmstatApplication}.
  */
 public JmxModelImpl(Application application, JvmstatModel jvmstat) {
   try {
     JvmJvmstatModel jvmstatModel = JvmJvmstatModelFactory.getJvmstatModelFor(application);
     // Create ProxyClient (i.e. create the JMX connection to the JMX agent)
     ProxyClient proxyClient = null;
     if (Application.CURRENT_APPLICATION.equals(application)) {
       // Monitor self
       proxyClient = new ProxyClient(this);
     } else if (application.isLocalApplication()) {
       // Create a ProxyClient from local pid
       String connectorAddress =
           jvmstat.findByName("sun.management.JMXConnectorServer.address"); // NOI18N
       String javaHome = jvmstat.findByName("java.property.java.home"); // NOI18N
       LocalVirtualMachine lvm =
           new LocalVirtualMachine(
               application.getPid(),
               AttachModelFactory.getAttachFor(application) != null,
               connectorAddress,
               javaHome);
       if (!lvm.isManageable()) {
         if (lvm.isAttachable()) {
           proxyClient = new ProxyClient(this, lvm);
         } else {
           if (LOGGER.isLoggable(Level.WARNING)) {
             LOGGER.warning(
                 "The JMX management agent "
                     + // NOI18N
                     "cannot be enabled in this application (pid "
                     + // NOI18N
                     application.getPid()
                     + ")"); // NOI18N
           }
         }
       } else {
         proxyClient = new ProxyClient(this, lvm);
       }
     }
     if (proxyClient == null) {
       // Create a ProxyClient for the remote out-of-the-box
       // JMX management agent using the port and security
       // related information retrieved through jvmstat.
       List<String> urls =
           jvmstat.findByPattern(
               "sun.management.JMXConnectorServer.[0-9]+.remoteAddress"); // NOI18N
       if (urls.size() != 0) {
         List<String> auths =
             jvmstat.findByPattern(
                 "sun.management.JMXConnectorServer.[0-9]+.authenticate"); // NOI18N
         proxyClient = new ProxyClient(this, urls.get(0));
         if ("true".equals(auths.get(0))) { // NOI18N
           supplyCredentials(application, proxyClient);
         }
       } else {
         // Create a ProxyClient for the remote out-of-the-box
         // JMX management agent using the port specified in
         // the -Dcom.sun.management.jmxremote.port=<port>
         // system property
         String jvmArgs = jvmstatModel.getJvmArgs();
         StringTokenizer st = new StringTokenizer(jvmArgs);
         int port = -1;
         boolean authenticate = false;
         while (st.hasMoreTokens()) {
           String token = st.nextToken();
           if (token.startsWith("-Dcom.sun.management.jmxremote.port=")) { // NOI18N
             port = Integer.parseInt(token.substring(token.indexOf("=") + 1)); // NOI18N
           } else if (token.equals("-Dcom.sun.management.jmxremote.authenticate=true")) { // NOI18N
             authenticate = true;
           }
         }
         if (port != -1) {
           proxyClient = new ProxyClient(this, application.getHost(), port);
           if (authenticate) {
             supplyCredentials(application, proxyClient);
           }
         }
       }
     }
     if (proxyClient != null) {
       client = proxyClient;
       removedListener = new ApplicationRemovedListener();
       availabilityListener = new ApplicationAvailabilityListener();
       connect(application, proxyClient, removedListener, availabilityListener);
     }
   } catch (Exception e) {
     LOGGER.throwing(JmxModelImpl.class.getName(), "<init>", e); // NOI18N
     client = null;
   }
 }
Ejemplo n.º 5
0
 public int getVmid() {
   return (lvm != null) ? lvm.vmid() : 0;
 }
Ejemplo n.º 6
0
 private static String getCacheKey(LocalVirtualMachine lvm) {
   return Integer.toString(lvm.vmid());
 }
Ejemplo n.º 7
0
 public static String getConnectionName(LocalVirtualMachine lvm) {
   return Integer.toString(lvm.vmid());
 }
Ejemplo n.º 8
0
  private void tryConnect(boolean requireRemoteSSL) throws IOException {
    if (jmxUrl == null && "localhost".equals(hostName) && port == 0) {
      // Monitor self
      this.jmxc = null;
      this.mbsc = ManagementFactory.getPlatformMBeanServer();
      this.server = Snapshot.newSnapshot(mbsc);
    } else {
      // Monitor another process
      if (lvm != null) {
        if (!lvm.isManageable()) {
          lvm.startManagementAgent();
          if (!lvm.isManageable()) {
            // FIXME: what to throw
            throw new IOException(lvm + "not manageable");
          }
        }
        if (this.jmxUrl == null) {
          this.jmxUrl = new JMXServiceURL(lvm.connectorAddress());
        }
      }
      Map<String, Object> env = new HashMap<String, Object>();
      if (requireRemoteSSL) {
        env.put("jmx.remote.x.check.stub", "true");
      }
      // Need to pass in credentials ?
      if (userName == null && password == null) {
        if (isVmConnector()) {
          // Check for SSL config on reconnection only
          if (stub == null) {
            checkSslConfig();
          }
          this.jmxc = new RMIConnector(stub, null);
          jmxc.connect(env);
        } else {
          this.jmxc = JMXConnectorFactory.connect(jmxUrl, env);
        }
      } else {
        env.put(JMXConnector.CREDENTIALS, new String[] {userName, password});
        if (isVmConnector()) {
          // Check for SSL config on reconnection only
          if (stub == null) {
            checkSslConfig();
          }
          this.jmxc = new RMIConnector(stub, null);
          jmxc.connect(env);
        } else {
          this.jmxc = JMXConnectorFactory.connect(jmxUrl, env);
        }
      }
      this.mbsc = jmxc.getMBeanServerConnection();
      this.server = Snapshot.newSnapshot(mbsc);
    }
    this.isDead = false;

    try {
      ObjectName on = new ObjectName(THREAD_MXBEAN_NAME);
      this.hasPlatformMXBeans = server.isRegistered(on);
      this.hasHotSpotDiagnosticMXBean =
          server.isRegistered(new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME));
      // check if it has 6.0 new APIs
      if (this.hasPlatformMXBeans) {
        MBeanOperationInfo[] mopis = server.getMBeanInfo(on).getOperations();
        // look for findDeadlockedThreads operations;
        for (MBeanOperationInfo op : mopis) {
          if (op.getName().equals("findDeadlockedThreads")) {
            this.supportsLockUsage = true;
            break;
          }
        }

        on = new ObjectName(COMPILATION_MXBEAN_NAME);
        this.hasCompilationMXBean = server.isRegistered(on);
      }
    } catch (MalformedObjectNameException e) {
      // should not reach here
      throw new InternalError(e.getMessage());
    } catch (IntrospectionException e) {
      InternalError ie = new InternalError(e.getMessage());
      ie.initCause(e);
      throw ie;
    } catch (InstanceNotFoundException e) {
      InternalError ie = new InternalError(e.getMessage());
      ie.initCause(e);
      throw ie;
    } catch (ReflectionException e) {
      InternalError ie = new InternalError(e.getMessage());
      ie.initCause(e);
      throw ie;
    }

    if (hasPlatformMXBeans) {
      // WORKAROUND for bug 5056632
      // Check if the access role is correct by getting a RuntimeMXBean
      getRuntimeMXBean();
    }
  }
Ejemplo n.º 9
0
 private ProxyClient(LocalVirtualMachine lvm) throws IOException {
   this.lvm = lvm;
   this.connectionName = getConnectionName(lvm);
   this.displayName = "pid: " + lvm.vmid() + " " + lvm.displayName();
 }