Example #1
0
  @Create
  public void create() {
    if (entityManager == null) {
      entityManager =
          Expressions.instance().createValueExpression("#{entityManager}", EntityManager.class);
    }

    tokenUsernameProperty =
        new AnnotatedBeanProperty<TokenUsername>(tokenClass, TokenUsername.class);
    tokenValueProperty = new AnnotatedBeanProperty<TokenValue>(tokenClass, TokenValue.class);

    if (!tokenUsernameProperty.isSet()) {
      throw new IllegalStateException(
          "Invalid tokenClass "
              + tokenClass.getName()
              + " - required annotation @TokenUsername not found on any Field or Method.");
    }

    if (!tokenValueProperty.isSet()) {
      throw new IllegalStateException(
          "Invalid tokenClass "
              + tokenClass.getName()
              + " - required annotation @TokenValue not found on any Field or Method.");
    }
  }
Example #2
0
  public void createToken(String username, String value) {
    if (tokenClass == null) {
      throw new IllegalStateException("Could not create token, tokenClass not set");
    }

    try {
      Object token = tokenClass.newInstance();

      tokenUsernameProperty.setValue(token, username);
      tokenValueProperty.setValue(token, value);

      lookupEntityManager().persist(token);
    } catch (Exception ex) {
      if (ex instanceof IdentityManagementException) {
        throw (IdentityManagementException) ex;
      } else {
        throw new IdentityManagementException("Could not create account", ex);
      }
    }
  }
Example #3
0
  public Object lookupToken(String username, String value) {
    try {
      Object token =
          lookupEntityManager()
              .createQuery(
                  "select t from "
                      + tokenClass.getName()
                      + " t where "
                      + tokenUsernameProperty.getName()
                      + " = :username and "
                      + tokenValueProperty.getName()
                      + " = :value")
              .setParameter("username", username)
              .setParameter("value", value)
              .getSingleResult();

      return token;
    } catch (NoResultException ex) {
      return null;
    }
  }
Example #4
0
  public void invalidateAll(String username) {
    Query query =
        lookupEntityManager()
            .createQuery(
                "select t from "
                    + tokenClass.getName()
                    + " t where "
                    + tokenUsernameProperty.getName()
                    + " = :username")
            .setParameter("username", username);

    for (Object token : query.getResultList()) {
      lookupEntityManager().remove(token);
    }
  }