You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by "clebertsuconic (via GitHub)" <gi...@apache.org> on 2023/08/01 02:15:36 UTC

[GitHub] [activemq-artemis] clebertsuconic commented on a diff in pull request #4565: ARTEMIS-4372 Implement Pico-cli and script auto-complete

clebertsuconic commented on code in PR #4565:
URL: https://github.com/apache/activemq-artemis/pull/4565#discussion_r1280044335


##########
artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java:
##########
@@ -157,68 +174,132 @@ private ConnectionFactory createAMQPConnectionFactory(String brokerURL,
       }
 
       try {
-         Connection connection = cf.createConnection();
-         connection.close();
+         tryConnect(brokerURL, user, password, cf);
          return cf;
       } catch (JMSSecurityException e) {
          // if a security exception will get the user and password through an input
          getActionContext().err.println("Connection failed::" + e.getMessage());
-         cf = new JmsConnectionFactory(inputUser(user), inputPassword(password), brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new JmsConnectionFactory(user, password, brokerURL);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+            e.printStackTrace();
+         }
          return cf;
       } catch (JMSException e) {
          // if a connection exception will ask for the URL, user and password
          getActionContext().err.println("Connection failed::" + e.getMessage());
-         cf = new JmsConnectionFactory(inputUser(user), inputPassword(password), inputBrokerURL(brokerURL));
+         brokerURL = inputBrokerURL(brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new JmsConnectionFactory(user, password, brokerURL);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+            e2.printStackTrace();
+         }
          return cf;
       }
    }
 
    protected ActiveMQConnectionFactory createCoreConnectionFactory() {
+      recoverConnectionInformation();
       return createCoreConnectionFactory(brokerURL, user, password, clientID);
    }
 
+   private void recoverConnectionInformation() {
+      if (CONNECTION_INFORMATION.get() != null) {
+         ConnectionInformation connectionInfo = CONNECTION_INFORMATION.get();
+         if (this.user == null) {
+            this.user  = connectionInfo.user;
+         }
+         if (this.password == null) {
+            this.password  = connectionInfo.password;
+         }
+         if (this.brokerURL == null) {
+            this.brokerURL  = connectionInfo.uri;
+         }
+
+      }
+   }
+
+   void saveConnectionInfo(String brokerURL, String user, String password) {
+      if (Shell.inShell() && CONNECTION_INFORMATION.get() == null) {
+         CONNECTION_INFORMATION.set(new ConnectionInformation(brokerURL, user, password));
+         System.out.println("CLI connected to broker " + brokerURL + ", user:" + user);
+      }
+   }
+
    protected ActiveMQConnectionFactory createCoreConnectionFactory(String brokerURL,
                                                                    String user,
                                                                    String password,
                                                                    String clientID) {
+      if (brokerURL.startsWith("amqp://")) {
+         // replacing amqp:// by tcp://
+         brokerURL = "tcp" + brokerURL.substring(4);
+      }
+
       ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL, user, password);
       if (clientID != null) {
          getActionContext().out.println("Consumer:: clientID = " + clientID);
          cf.setClientID(clientID);
       }
       try {
-         Connection connection = cf.createConnection();
-         connection.close();
+         tryConnect(brokerURL, user, password, cf);
          return cf;
       } catch (JMSSecurityException e) {
          // if a security exception will get the user and password through an input
          if (getActionContext() != null) {
             getActionContext().err.println("Connection failed::" + e.getMessage());
          }
-         cf = new ActiveMQConnectionFactory(brokerURL, inputUser(user), inputPassword(password));
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+         }
          return cf;
       } catch (JMSException e) {
          // if a connection exception will ask for the URL, user and password
          if (getActionContext() != null) {
             getActionContext().err.println("Connection failed::" + e.getMessage());
          }
-         cf = new ActiveMQConnectionFactory(inputBrokerURL(brokerURL), inputUser(user), inputPassword(password));
+         brokerURL = inputBrokerURL(brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+         }

Review Comment:
   I see what you mean now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org