public <ReturnType, CreateArgType, ConsumedType> void registerConsumerFactory( AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> factory ) throws AuthenticationFactoryException { if ( !factory.getConsumedType().isInterface() && !AuthenticationProvider.class.isAssignableFrom( factory.getConsumedType() ) ) { throw new AuthenticationFactoryException( BaseMessages.getString( PKG, "AuthenticationManager.ConsumedTypeError", factory ) ); } Map<Class<?>, AuthenticationConsumerFactory<?, ?, ?>> createTypeMap = getRelevantConsumerFactoryMap( factory.getReturnType(), factory.getCreateArgType() ); synchronized ( createTypeMap ) { createTypeMap.put( factory.getConsumedType(), factory ); } }
@SuppressWarnings( { "rawtypes", "unchecked" } ) @Override public <ReturnType, CreateArgType, ConsumedType> AuthenticationPerformer<ReturnType, CreateArgType> create( AuthenticationProvider authenticationProvider, AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> authenticationConsumerFactory ) { if ( authenticationConsumerFactory.getConsumedType().isInstance( authenticationProvider ) ) { return new DefaultAuthenticationPerformer( authenticationProvider, authenticationConsumerFactory ); } else if ( AuthenticationConsumerInvocationHandler.isCompatible( authenticationConsumerFactory.getConsumedType(), authenticationProvider ) ) { return new ClassloaderBridgingAuthenticationPerformer<ReturnType, CreateArgType, ConsumedType>( authenticationProvider, authenticationConsumerFactory ); } return null; } }
@SuppressWarnings( "unchecked" ) @Override public ReturnType perform( CreateArgType consumerCreateArg ) throws AuthenticationConsumptionException { AuthenticationConsumer<ReturnType, ConsumedType> consumer = authenticationConsumerFactory.create( consumerCreateArg ); ConsumedType providerProxy = (ConsumedType) Proxy.newProxyInstance( consumer.getClass().getClassLoader(), new Class[] { authenticationConsumerFactory.getConsumedType() }, new AuthenticationConsumerInvocationHandler( provider ) ); return consumer.consume( providerProxy ); }
@Override public <ReturnType, CreateArgType, ConsumedType> AuthenticationPerformer<ReturnType, CreateArgType> create( AuthenticationProvider authenticationProvider, AuthenticationConsumerFactory<ReturnType, CreateArgType, ConsumedType> authenticationConsumer ) { if ( AuthenticationConsumerInvocationHandler.isCompatible( authenticationConsumer.getConsumedType(), authenticationProvider ) ) { return new ClassloaderBridgingAuthenticationPerformer<ReturnType, CreateArgType, ConsumedType>( authenticationProvider, authenticationConsumer ); } return null; } } );
@SuppressWarnings( { "rawtypes", "unchecked" } ) @Test public void testRegisterConsumerFactory() throws AuthenticationConsumptionException, AuthenticationFactoryException { AuthenticationConsumer<Object, KerberosAuthenticationProvider> authConsumer = mock( AuthenticationConsumer.class ); AuthenticationConsumerFactory<Object, AuthenticationConsumer, KerberosAuthenticationProvider> factory = mock( AuthenticationConsumerFactory.class ); when( factory.getReturnType() ).thenReturn( Object.class ); when( factory.getCreateArgType() ).thenReturn( AuthenticationConsumer.class ); when( factory.getConsumedType() ).thenReturn( KerberosAuthenticationProvider.class ); when( factory.create( authConsumer ) ).thenReturn( authConsumer ); KerberosAuthenticationProvider kerberosAuthenticationProvider = new KerberosAuthenticationProvider( "kerb", "kerb", true, "pass", true, "none" ); manager.registerAuthenticationProvider( kerberosAuthenticationProvider ); manager.registerConsumerFactory( factory ); manager.getAuthenticationPerformer( Object.class, AuthenticationConsumer.class, kerberosAuthenticationProvider.getId() ).perform( authConsumer ); verify( authConsumer ).consume( kerberosAuthenticationProvider ); }