private void connect(
     Application application,
     ProxyClient proxyClient,
     ApplicationRemovedListener listener,
     ApplicationAvailabilityListener aListener) {
   while (true) {
     try {
       proxyClient.connect();
       application.notifyWhenRemoved(listener);
       application.addPropertyChangeListener(Stateful.PROPERTY_STATE, aListener);
       break;
     } catch (SecurityException e) {
       LOGGER.log(Level.INFO, "connect", e); // NOI18N
       if (proxyClient.hasSSLStubCheck()) {
         Storage storage = application.getStorage();
         String noSSLProp = JmxApplicationProvider.PROPERTY_RETRY_WITHOUT_SSL;
         String noSSL = storage.getCustomProperty(noSSLProp);
         if (noSSL != null && Boolean.parseBoolean(noSSL)) { // NOI18N
           proxyClient.setInsecure();
           continue;
         } else {
           String conn = storage.getCustomProperty(DataSourceDescriptor.PROPERTY_NAME);
           if (conn == null)
             conn = storage.getCustomProperty(ApplicationType.PROPERTY_SUGGESTED_NAME);
           if (conn == null) conn = proxyClient.getUrl().toString();
           String msg =
               NbBundle.getMessage(JmxModelImpl.class, "MSG_Insecure_SSL", conn); // NOI18N
           String title = NbBundle.getMessage(JmxModelImpl.class, "Title_Insecure_SSL"); // NOI18N
           String retry = NbBundle.getMessage(JmxModelImpl.class, "Retry_Insecure_SSL"); // NOI18N
           JLabel l = new JLabel(msg);
           JCheckBox c = new JCheckBox();
           Mnemonics.setLocalizedText(c, retry);
           c.setSelected(noSSL == null);
           JPanel p = new JPanel(new BorderLayout(0, 20));
           p.add(l, BorderLayout.CENTER);
           p.add(c, BorderLayout.SOUTH);
           NotifyDescriptor dd =
               new NotifyDescriptor.Confirmation(p, title, NotifyDescriptor.YES_NO_OPTION);
           if (DialogDisplayer.getDefault().notify(dd) == NotifyDescriptor.YES_OPTION) {
             storage.setCustomProperty(noSSLProp, Boolean.toString(c.isSelected()));
             proxyClient.setInsecure();
             continue;
           } else {
             break;
           }
         }
       }
       if (supplyCredentials(application, proxyClient) == null) {
         break;
       }
     }
   }
 }
 /** Ask for security credentials. */
 private CredentialsConfigurator supplyCredentials(
     Application application, ProxyClient proxyClient) {
   String displayName =
       application.getStorage().getCustomProperty(DataSourceDescriptor.PROPERTY_NAME);
   if (displayName == null) displayName = proxyClient.getUrl().toString();
   CredentialsConfigurator jsc = CredentialsConfigurator.supplyCredentials(displayName);
   if (jsc != null) proxyClient.setCredentials(jsc.getUsername(), jsc.getPassword());
   return jsc;
 }
    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;
    }
 /**
  * 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;
   }
 }