@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.");
    }
  }
  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);
      }
    }
  }
  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;
    }
  }
  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);
    }
  }