Phillip Huff
Phillip Huff

Reputation: 565

Accessing ManagedBean method from EJB

I have an EJB that handles creation of customer user accounts that needs access to a managed bean (account scoped) which manages user verification keys for user accounts (the keys are transient, so they don't need to be handled by database calls). However, I can't figure out a way to send the verification key to the EJB (which generates the verification email that is send to a user).

AccountVerifierBean.java

@ManagedBean(name = "accountVerifierBean", eager = true)
@ApplicationScoped
public class AccountVerifierBean implements Serializable {
    private HashMap<String, String> verificationKeyMapping;

    public AccountVerifierBean() {}

    public boolean verifyKey(String username, String key) {
        return key.equals(verificationKeyMapping.get(username));
    }
    public String generateKey(String username) {
        Date time = new Date();
        String key = username + time.toString();
        key = Encryption.hashSHA(key);
        verificationKeyMapping.put(username, key);
        return key;
    }
}

CustomerService.java

@Named
@Stateless
@LocalBean
public class CustomerService {
    @PersistenceContext(unitName = "MovieProject-ejbPU")
    private EntityManager em;

    private String username;
    private String password;

    //getters and setters

    public void validateEmail() {
        Properties serverConfig = new Properties();
        serverConfig.put("mail.smtp.host", "localhost");
        serverConfig.put("mail.smtp.auth", "true");
        serverConfig.put("mail.smtp.port", "25");

        try {
            Session session = Session.getInstance(serverConfig, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("<ACCOUNT>","<PASSWORD>");
                }
            });
            MimeMessage message = new MimeMessage( session );
            message.setFrom(new InternetAddress("[email protected]","VideoPile"));
            message.addRecipient(
                Message.RecipientType.TO, new InternetAddress(username)
            );
            message.setSubject("Welcome to VideoPile!");
            message.setContent("<p>Welcome to VideoPile, Please verify your email.</p><p>" + verifierKey + "</p>", "text/html; charset=utf-8"); //verifierKey is what I'm trying to get from AccountVerifierBean.
            Transport.send( message );
        }
        catch (MessagingException ex){
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception e) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public String encrypt(String password) {
        try {
            return new     String(Base64.encode(MessageDigest.getInstance("SHA").digest(password.getBytes())));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

I've tried @Inject, @ManagedProperty, using the Application map, and using the ELContext. Nothing seems to work.

EDIT: I think there is something wrong with the bean. Any methods called from bean don't seem to do anything (EL is resolved, no bean method calls though).

I've tested the annotations that it uses (both are javax.faces.bean.*)

Upvotes: 1

Views: 1090

Answers (1)

Phillip Huff
Phillip Huff

Reputation: 565

So, the issue was in the AccountVerifierBean.

I added the following lines to faces-config.xml and it is now working.

<managed-bean eager="true">
  <managed-bean-name>accountVerifierBean</managed-bean-name>
  <managed-bean-class>org.Videotheque.beans.AccountVerifierBean</managed-bean-class>
  <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

I'm fairly certain that the problem was because my bean needed to be in the EJB package instead of the WAR so that the EJBs can access it, but because of that, the WAR didn't know that the bean existed.

Upvotes: 1

Related Questions