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

示例1
private DefaultConnectionFactory buildDefaultConnectionFactory() {
    return new DefaultConnectionFactory(getOperationQueueLength(), getReadBufferSize(), getHashAlgorithm()) {
        @Override
        public long getOperationTimeout() {
            return getOperationTimeoutMillis();
        }

        @Override
        public boolean isDaemon() {
            return isDaemonMode();
        }

        @Override
        public AuthDescriptor getAuthDescriptor() {
            return createAuthDescriptor();
        }
    };
}
 
示例2
private KetamaConnectionFactory buildKetamaConnectionFactory() {
    return new KetamaConnectionFactory() {
        @Override
        public long getOperationTimeout() {
            return getOperationTimeoutMillis();
        }

        @Override
        public boolean isDaemon() {
            return isDaemonMode();
        }

        @Override
        public AuthDescriptor getAuthDescriptor() {
            return createAuthDescriptor();
        }
    };
}
 
示例3
private BinaryConnectionFactory buildBinaryConnectionFactory() {
    return new BinaryConnectionFactory(getOperationQueueLength(), getReadBufferSize(), getHashAlgorithm()) {
        @Override
        public long getOperationTimeout() {
            return getOperationTimeoutMillis();
        }

        @Override
        public boolean isDaemon() {
            return isDaemonMode();
        }

        @Override
        public AuthDescriptor getAuthDescriptor() {
            return createAuthDescriptor();
        }
    };
}
 
示例4
@Test
public void createConnectionFactoryBuilder() throws Exception {
    Properties props = new Properties();
    props.setProperty(HASH_ALGORITHM_PROPERTY_KEY, DefaultHashAlgorithm.NATIVE_HASH.name());
    props.setProperty(OPERATION_TIMEOUT_MILLIS_PROPERTY_KEY, String.valueOf(13579));
    props.setProperty(TRANSCODER_PROPERTY_KEY, FakeTranscoder.class.getName());
    props.setProperty(AUTH_GENERATOR_PROPERTY_KEY, FakeAuthDescriptorGenerator.class.getName());

    ConnectionFactoryBuilder builder = spyMemcachedAdapter.createConnectionFactoryBuilder(new OverridableReadOnlyPropertiesImpl(props));

    ConnectionFactory connectionFactory = builder.build();
    assertThat(connectionFactory.getHashAlg()).isEqualTo(DefaultHashAlgorithm.NATIVE_HASH);
    assertThat(connectionFactory.getOperationTimeout()).isEqualTo(13579);

    Transcoder<Object> transcoder = connectionFactory.getDefaultTranscoder();
    assertThat(transcoder).isExactlyInstanceOf(FakeTranscoder.class);
    FakeTranscoder fakeTranscoder = (FakeTranscoder) transcoder;
    assertThat(fakeTranscoder.isInitialized()).isTrue();

    AuthDescriptor authDescriptor = connectionFactory.getAuthDescriptor();
    assertThat(authDescriptor.getMechs()).isEqualTo(FakeAuthDescriptorGenerator.FAKE_MECHS);
}
 
示例5
@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 ) );
}
 
示例6
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));
}
 
示例7
@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;
}
 
示例8
@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);
}
 
示例9
@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);
    }
}
 
示例10
public ApiMemcached() {
	AppConstants ac = AppConstants.getInstance();
	String address = ac.getProperty("etag.cache.server", "localhost:11211");
	String username = ac.getProperty("etag.cache.username", "");
	String password = ac.getProperty("etag.cache.password", "");
	int timeout = ac.getInt("etag.cache.timeout", 10);

	List<InetSocketAddress> addresses = AddrUtil.getAddresses(address);

	ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder()
		.setProtocol(Protocol.BINARY)
		.setOpTimeout(timeout)
		.setInitialObservers(Collections.singleton(obs));

	if(addresses.size()  > 1)
		connectionFactoryBuilder.setFailureMode(FailureMode.Redistribute);
	else
		connectionFactoryBuilder.setFailureMode(FailureMode.Retry);

	if(!username.isEmpty())
		connectionFactoryBuilder.setAuthDescriptor(AuthDescriptor.typical(username, password));

	ConnectionFactory cf = connectionFactoryBuilder.build();

	try {
		client = new MemcachedClient(cf, addresses);
		//Fetching a none-existing key to test the connection
		Future<Object> future = client.asyncGet("test-connection");
		future.get(timeout, TimeUnit.MILLISECONDS);
	} catch (Exception e) {
		ConfigurationWarnings.add(log, "Unable to connect to one or more memcached servers.");
	}
}
 
示例11
@Override
public AuthDescriptor getAuthDescriptor() {
    return underlying.getAuthDescriptor();
}
 
示例12
@Override
public AuthDescriptor getAuthDescriptor() {
    return underlying.getAuthDescriptor();
}
 
示例13
@Override
public AuthDescriptor generate(OverridableReadOnlyProperties properties) {
    return new AuthDescriptor(FAKE_MECHS, null);
}
 
示例14
AuthDescriptor generate(OverridableReadOnlyProperties properties);