Exemplo n.º 1
0
  @Override
  public void connectionFailed(
      final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
    ActiveMQServerLogger.LOGGER.bridgeConnectionFailed(failedOver);

    synchronized (connectionGuard) {
      keepConnecting = true;
    }

    try {
      if (producer != null) {
        producer.close();
      }

      cleanUpSessionFactory(csf);
    } catch (Throwable dontCare) {
    }

    try {
      session.cleanUp(false);
    } catch (Throwable dontCare) {
    }

    if (scaleDownTargetNodeID != null && !scaleDownTargetNodeID.equals(nodeUUID.toString())) {
      synchronized (this) {
        try {
          logger.debug(
              "Moving "
                  + queue.getMessageCount()
                  + " messages from "
                  + queue.getName()
                  + " to "
                  + scaleDownTargetNodeID);
          ((QueueImpl) queue)
              .moveReferencesBetweenSnFQueues(SimpleString.toSimpleString(scaleDownTargetNodeID));

          // stop the bridge from trying to reconnect and clean up all the bindings
          fail(true);
        } catch (Exception e) {
          ActiveMQServerLogger.LOGGER.warn(e.getMessage(), e);
        }
      }
    } else if (scaleDownTargetNodeID != null) {
      // the disconnected node is scaling down to me, no need to reconnect to it
      logger.debug(
          "Received scaleDownTargetNodeID: " + scaleDownTargetNodeID + "; cancelling reconnect.");
      fail(true);
    } else {
      logger.debug("Received invalid scaleDownTargetNodeID: " + scaleDownTargetNodeID);

      fail(me.getType() == ActiveMQExceptionType.DISCONNECTED);
    }

    tryScheduleRetryReconnect(me.getType());
  }
Exemplo n.º 2
0
  protected boolean authenticate(String username, String password) throws LoginException {

    MessageFormat userSearchMatchingFormat;
    boolean userSearchSubtreeBool;

    DirContext context = null;

    if (ActiveMQServerLogger.LOGGER.isDebugEnabled()) {
      ActiveMQServerLogger.LOGGER.debug("Create the LDAP initial context.");
    }
    try {
      context = open();
    } catch (NamingException ne) {
      FailedLoginException ex = new FailedLoginException("Error opening LDAP connection");
      ex.initCause(ne);
      throw ex;
    }

    if (!isLoginPropertySet(USER_SEARCH_MATCHING)) return false;

    userSearchMatchingFormat = new MessageFormat(getLDAPPropertyValue(USER_SEARCH_MATCHING));
    userSearchSubtreeBool =
        Boolean.valueOf(getLDAPPropertyValue(USER_SEARCH_SUBTREE)).booleanValue();

    try {

      String filter = userSearchMatchingFormat.format(new String[] {doRFC2254Encoding(username)});
      SearchControls constraints = new SearchControls();
      if (userSearchSubtreeBool) {
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
      } else {
        constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
      }

      // setup attributes
      List<String> list = new ArrayList<String>();
      if (isLoginPropertySet(USER_ROLE_NAME)) {
        list.add(getLDAPPropertyValue(USER_ROLE_NAME));
      }
      String[] attribs = new String[list.size()];
      list.toArray(attribs);
      constraints.setReturningAttributes(attribs);

      if (ActiveMQServerLogger.LOGGER.isDebugEnabled()) {
        ActiveMQServerLogger.LOGGER.debug("Get the user DN.");
        ActiveMQServerLogger.LOGGER.debug("Looking for the user in LDAP with ");
        ActiveMQServerLogger.LOGGER.debug("  base DN: " + getLDAPPropertyValue(USER_BASE));
        ActiveMQServerLogger.LOGGER.debug("  filter: " + filter);
      }

      NamingEnumeration<SearchResult> results =
          context.search(getLDAPPropertyValue(USER_BASE), filter, constraints);

      if (results == null || !results.hasMore()) {
        ActiveMQServerLogger.LOGGER.warn("User " + username + " not found in LDAP.");
        throw new FailedLoginException("User " + username + " not found in LDAP.");
      }

      SearchResult result = results.next();

      if (results.hasMore()) {
        // ignore for now
      }

      String dn;
      if (result.isRelative()) {
        ActiveMQServerLogger.LOGGER.debug("LDAP returned a relative name: " + result.getName());

        NameParser parser = context.getNameParser("");
        Name contextName = parser.parse(context.getNameInNamespace());
        Name baseName = parser.parse(getLDAPPropertyValue(USER_BASE));
        Name entryName = parser.parse(result.getName());
        Name name = contextName.addAll(baseName);
        name = name.addAll(entryName);
        dn = name.toString();
      } else {
        ActiveMQServerLogger.LOGGER.debug("LDAP returned an absolute name: " + result.getName());

        try {
          URI uri = new URI(result.getName());
          String path = uri.getPath();

          if (path.startsWith("/")) {
            dn = path.substring(1);
          } else {
            dn = path;
          }
        } catch (URISyntaxException e) {
          if (context != null) {
            close(context);
          }
          FailedLoginException ex = new FailedLoginException("Error parsing absolute name as URI.");
          ex.initCause(e);
          throw ex;
        }
      }

      if (ActiveMQServerLogger.LOGGER.isDebugEnabled()) {
        ActiveMQServerLogger.LOGGER.debug("Using DN [" + dn + "] for binding.");
      }

      Attributes attrs = result.getAttributes();
      if (attrs == null) {
        throw new FailedLoginException("User found, but LDAP entry malformed: " + username);
      }
      List<String> roles = null;
      if (isLoginPropertySet(USER_ROLE_NAME)) {
        roles = addAttributeValues(getLDAPPropertyValue(USER_ROLE_NAME), attrs, roles);
      }

      // check the credentials by binding to server
      if (bindUser(context, dn, password)) {
        // if authenticated add more roles
        roles = getRoles(context, dn, username, roles);
        if (ActiveMQServerLogger.LOGGER.isDebugEnabled()) {
          ActiveMQServerLogger.LOGGER.debug("Roles " + roles + " for user " + username);
        }
        for (int i = 0; i < roles.size(); i++) {
          groups.add(new RolePrincipal(roles.get(i)));
        }
      } else {
        throw new FailedLoginException("Password does not match for user: "******"Error contacting LDAP");
      ex.initCause(e);
      throw ex;
    } catch (NamingException e) {
      if (context != null) {
        close(context);
      }
      FailedLoginException ex = new FailedLoginException("Error contacting LDAP");
      ex.initCause(e);
      throw ex;
    }

    return true;
  }