Java源码示例:net.spy.memcached.auth.PlainCallbackHandler

示例1
@Override
public void activateService()
    throws Exception
{
    MemcacheConfiguration config = configuration.get();
    expiration = ( config.expiration().get() == null )
                 ? 3600
                 : config.expiration().get();
    String addresses = ( config.addresses().get() == null )
                       ? "localhost:11211"
                       : config.addresses().get();
    Protocol protocol = ( config.protocol().get() == null )
                        ? Protocol.TEXT
                        : Protocol.valueOf( config.protocol().get().toUpperCase() );
    String username = config.username().get();
    String password = config.password().get();
    String authMech = config.authMechanism().get() == null
                      ? "PLAIN"
                      : config.authMechanism().get();

    ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
    builder.setProtocol( protocol );
    if( username != null && !username.isEmpty() )
    {
        String[] authType = { authMech };
        AuthDescriptor to = new AuthDescriptor( authType, new PlainCallbackHandler( username, password ) );
        builder.setAuthDescriptor( to );
    }

    client = new MemcachedClient( builder.build(), AddrUtil.getAddresses( addresses ) );
}
 
示例2
protected AuthDescriptor createAuthDescriptor() {
    String username = properties.get(PROP_USERNAME);
    String password = properties.get(PROP_PASSWORD);
    if (username == null || password == null) {
        return null;
    }
    return new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
}
 
示例3
@Override
public AuthDescriptor generate(OverridableReadOnlyProperties properties) {
    String username = properties.getRequiredProperty(USERNAME_PROPERTY_KEY);
    String password = properties.getRequiredProperty(PASSWORD_PROPERTY_KEY);

    AuthDescriptor authDescriptor = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
    return authDescriptor;
}
 
示例4
@Test
public void generate() throws Exception {
    Properties props = new Properties();
    props.setProperty(PlainAuthDescriptorGenerator.USERNAME_PROPERTY_KEY, "username");
    props.setProperty(PlainAuthDescriptorGenerator.PASSWORD_PROPERTY_KEY, "password");

    AuthDescriptor authDescriptor = generator.generate(new OverridableReadOnlyPropertiesImpl(props));

    assertThat(authDescriptor).isNotNull();
    assertThat(authDescriptor.getMechs()).isEqualTo(new String[]{"PLAIN"});
    assertThat(authDescriptor.getCallback()).isExactlyInstanceOf(PlainCallbackHandler.class);
}
 
示例5
@Override
public void startVendorInstance() throws Exception {
    String[] nodes = get("nodes").split(",");
    List<InetSocketAddress> addresses = new ArrayList<>();
    for (String node : nodes) {
        String[] addressParts = node.split(":");
        if (addressParts.length == 0 || addressParts.length > 2) {
            throw new IllegalArgumentException("Invalid node address. Example: localhost:11211");
        }

        int port = 11211; //default memcached port
        if (addressParts.length == 2) {
            port = Integer.parseInt(addressParts[1]);
        }
        addresses.add(new InetSocketAddress(addressParts[0], port));
    }

    if (get("MEMCACHED_USERNAME") != null && get("MEMCACHED_PASSWORD") != null) {
        AuthDescriptor authDescriptor =
                new AuthDescriptor(new String[]{"PLAIN"},
                        new PlainCallbackHandler(get("MEMCACHED_USERNAME"), get("MEMCACHED_PASSWORD")));
        this.client = new MemcachedClient(new ConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(authDescriptor).build(), addresses);
    } else {
        this.client = new MemcachedClient(addresses);
    }
}