users@glassfish.java.net

_at_Resource Dependency Injection: Resource won't inject

From: <glassfish_at_javadesktop.org>
Date: Thu, 31 Jan 2008 13:21:36 PST

I am trying to get a DataSource injected into my bean.
I have clientDataSource defined with a resource-ref in my sun-web.xml file, but clientDataSource is not being populated in instances of this bean.

How do I make this injection happen?

[code]
public class TokenHandlerBean {

    private final static String salt = "Some secret";
    @Resource
    private DataSource clientDataSource;
    private Map<String, String> tokenMap;

    @PostConstruct
    public void initialize() {
        try {
            Map<String, String> tempTokenMap = new HashMap<String, String>();
            final Connection connection = clientDataSource.getConnection();
            PreparedStatement stmt = connection.prepareStatement("SELECT DISTINCT clientName FROM client_table");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                String clientName = rs.getString(1);
                tempTokenMap.put(TokenGenerator.generate(clientName, salt), clientName);
            }
            tokenMap = tempTokenMap;
            connection.close();
        } catch (SQLException ex) {
            throw new IllegalStateException(ex);
        }
    }
    
    public boolean isValid(String token) {
        return getClientName(token) != null;
    }

    public boolean isValid(String token, boolean reinit) {
        return getClientName(token, reinit) != null;
    }

    public String getClientName(String token) {
        return getClientName(token, false);
    }
    
    public Map<String,String> getTokenMap() {
        return tokenMap;
    }
    
    public String getClientName(String token, boolean reinit) {
        String clientName = tokenMap.get(token);
        if (clientName == null && reinit) {
            initialize();
            clientName = tokenMap.get(token);
        }
        return clientName;
    }
    
    @PreDestroy
    private void clear() {
        tokenMap = null;
    }
    
}
[/code]

I reference this bean in my jsp with:
[code]
<jsp:useBean id="tokenHandler" class="com.ancerno.TokenHandlerBean" scope="application" />
[/code]
[Message sent by forum member 'datasurfer' (datasurfer)]

http://forums.java.net/jive/thread.jspa?messageID=256847