ConnectionFactoryFactoryBean.getObjectType() does not conform to contract at all

View: New views
1 Messages — Rating Filter:   Alert me  

ConnectionFactoryFactoryBean.getObjectType() does not conform to contract at all

by fdurden :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,

Here's the current implementation of ConnectionFactoryFactoryBean.getObjectType() (for Jencks 2.1):

    public Class<?> getObjectType() {
        try {
            Object connectionFactory = getConnectionFactory();
            if (connectionFactory != null) {
                return connectionFactory.getClass();
            }
        } catch (ResourceException e) {
        }
        return null;
    }

    public Object getConnectionFactory() throws ResourceException {
        // we must initialize the connection factory outside of the getObject method since spring needs the
        // connetion factory type for autowiring before we have created the bean
        if (connectionFactory == null) {
            // START SNIPPET: cf
            if (managedConnectionFactory == null) {
                throw new NullPointerException("managedConnectionFactory is null");
            }
            if (connectionManager != null) {
                connectionFactory = managedConnectionFactory.createConnectionFactory(connectionManager);
            } else {
                connectionFactory = managedConnectionFactory.createConnectionFactory();
            }
            // END SNIPPET: cf
        }
        return connectionFactory;
    }

Now if managedConnectionFactory hasn't been set, getObjectType throws a NPE. But the contract for Spring's FactoryBean.getObjectType says, among other things, the following:

1) Return the type of object that this FactoryBean creates, or null if not known in advance.

2) In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; [...]

3) This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available."


1) is not respected, since it throws a NPE instead of returning null. 2) is not respected since singleton creation is not avoided as far as possible. The getConnectionFactory actually tries to create the singleton. 3) is not respected since the implementation expects managedConnectionFactory to be set.

I propose the following implementation:

    public Class<?> getObjectType() {
        if(connectionFactory != null)
        {
            return connectionFactory.getClass();
        }
        return null;
    }

I would've raised a JIRA issue, but the JIRA site seems not to be working.

Ref: org.springframework.beans.factory.FactoryBean.getObjectType()