Switched from a Provider to a Supplier as requested

This commit is contained in:
Andrei Savu 2012-09-16 23:08:17 +03:00
parent 3898f1eee2
commit 1e50fbeb69
4 changed files with 76 additions and 23 deletions

View File

@ -30,6 +30,7 @@ import java.security.InvalidKeyException;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.inject.Provider;
import org.jclouds.blobstore.BlobRequestSigner;
@ -57,7 +58,7 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner {
private final Crypto crypto;
private final Provider<Long> unixEpochTimestampProvider;
private final Provider<String> temporaryUrlKeyProvider;
private final Supplier<String> temporaryUrlKeySupplier;
private final BlobToObject blobToObject;
private final BlobToHttpGetOptions blob2HttpGetOptions;
@ -69,12 +70,12 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner {
@Inject
public SwiftBlobRequestSigner(RestAnnotationProcessor<CommonSwiftAsyncClient> processor, BlobToObject blobToObject,
BlobToHttpGetOptions blob2HttpGetOptions, Crypto crypto, @TimeStamp Provider<Long> unixEpochTimestampProvider,
@TemporaryUrlKey Provider<String> temporaryUrlKeyProvider) throws SecurityException, NoSuchMethodException {
@TemporaryUrlKey Supplier<String> temporaryUrlKeySupplier) throws SecurityException, NoSuchMethodException {
this.processor = checkNotNull(processor, "processor");
this.crypto = checkNotNull(crypto, "crypto");
this.unixEpochTimestampProvider = checkNotNull(unixEpochTimestampProvider, "unixEpochTimestampProvider");
this.temporaryUrlKeyProvider = checkNotNull(temporaryUrlKeyProvider, "temporaryUrlKeyProvider");
this.temporaryUrlKeySupplier = checkNotNull(temporaryUrlKeySupplier, "temporaryUrlKeyProvider");
this.blobToObject = checkNotNull(blobToObject, "blobToObject");
this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
@ -121,7 +122,7 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner {
HttpRequest.Builder builder = request.toBuilder();
builder.filters(filter(request.getFilters(), instanceOf(AuthenticateRequest.class)));
String key = temporaryUrlKeyProvider.get();
String key = temporaryUrlKeySupplier.get();
if (key == null) {
throw new UnsupportedOperationException();
}

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.openstack.swift.blobstore.config;
import com.google.common.base.Supplier;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import org.jclouds.blobstore.AsyncBlobStore;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.BlobStore;
@ -34,6 +36,7 @@ import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi;
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyAsyncApi;
import org.jclouds.openstack.swift.suppliers.ReturnOrFetchTemporaryUrlKey;
import java.util.UUID;
@ -53,18 +56,6 @@ public class SwiftBlobStoreContextModule extends AbstractModule {
return System.currentTimeMillis() / 1000; /* convert to seconds */
}
@Provides
@TemporaryUrlKey
protected String temporaryUrlKeyProvider(TemporaryUrlKeyApi client) {
String key = client.getTemporaryUrlKey();
if (key == null) {
client.setTemporaryUrlKey(UUID.randomUUID().toString());
return client.getTemporaryUrlKey();
}
return key;
}
@Override
protected void configure() {
install(new BlobStoreMapModule());
@ -72,6 +63,12 @@ public class SwiftBlobStoreContextModule extends AbstractModule {
bind(AsyncBlobStore.class).to(SwiftAsyncBlobStore.class).in(Scopes.SINGLETON);
bind(BlobStore.class).to(SwiftBlobStore.class).in(Scopes.SINGLETON);
bind(BlobRequestSigner.class).to(SwiftBlobRequestSigner.class);
configureTemporaryUrlExtension();
}
protected void configureTemporaryUrlExtension() {
bindClientAndAsyncClient(binder(), TemporaryUrlKeyApi.class, TemporaryUrlKeyAsyncApi.class);
bind(new TypeLiteral<Supplier<String>>() {
}).annotatedWith(TemporaryUrlKey.class).to(ReturnOrFetchTemporaryUrlKey.class);
}
}

View File

@ -0,0 +1,49 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.swift.suppliers;
import com.google.common.base.Supplier;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkNotNull;
@Singleton
public class ReturnOrFetchTemporaryUrlKey implements Supplier<String> {
private TemporaryUrlKeyApi client;
@Inject
public ReturnOrFetchTemporaryUrlKey(TemporaryUrlKeyApi client) {
this.client = checkNotNull(client, "client");
}
@Override
public String get() {
String key = client.getTemporaryUrlKey();
if (key == null) {
client.setTemporaryUrlKey(UUID.randomUUID().toString());
return client.getTemporaryUrlKey();
}
return key;
}
}

View File

@ -21,13 +21,18 @@ package org.jclouds.openstack.swift;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import static org.jclouds.rest.config.BinderUtils.bindClientAndAsyncClient;
import java.net.URI;
import java.util.Properties;
import javax.inject.Singleton;
import com.google.common.base.Suppliers;
import com.google.inject.*;
import com.google.inject.util.Modules;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.functions.URIFromAuthenticationResponseForService;
import org.jclouds.openstack.internal.TestOpenStackAuthenticationModule;
@ -35,15 +40,14 @@ import org.jclouds.openstack.reference.AuthHeaders;
import org.jclouds.openstack.swift.blobstore.config.SwiftBlobStoreContextModule;
import org.jclouds.openstack.swift.config.SwiftRestClientModule;
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi;
import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyAsyncApi;
import org.jclouds.openstack.swift.suppliers.ReturnOrFetchTemporaryUrlKey;
import org.jclouds.rest.internal.BaseAsyncClientTest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
/**
* Tests behavior of {@code BindSwiftObjectMetadataToRequest}
@ -79,15 +83,17 @@ public abstract class CommonSwiftClientTest extends BaseAsyncClientTest<SwiftAsy
}
}
public static class FixedSwiftBlobStoreContextModule extends SwiftBlobStoreContextModule {
public static class StaticTimeAndTemporaryUrlKeyModule extends SwiftBlobStoreContextModule {
@Override
protected Long unixEpochTimestampProvider() {
return UNIX_EPOCH_TIMESTAMP;
}
@Override
protected String temporaryUrlKeyProvider(TemporaryUrlKeyApi client) {
return TEMPORARY_URL_KEY;
protected void configureTemporaryUrlExtension() {
bindClientAndAsyncClient(binder(), TemporaryUrlKeyApi.class, TemporaryUrlKeyAsyncApi.class);
bind(new TypeLiteral<Supplier<String>>() {
}).annotatedWith(TemporaryUrlKey.class).toInstance(Suppliers.ofInstance(TEMPORARY_URL_KEY));
}
}
@ -95,7 +101,7 @@ public abstract class CommonSwiftClientTest extends BaseAsyncClientTest<SwiftAsy
protected ApiMetadata createApiMetadata() {
return new SwiftApiMetadata().toBuilder().defaultModules(
ImmutableSet.<Class<? extends Module>>of(StorageEndpointModule.class, SwiftRestClientModule.class,
FixedSwiftBlobStoreContextModule.class)).build();
StaticTimeAndTemporaryUrlKeyModule.class)).build();
}
@Override