Issue 820:Move off PropertiesBuilder for rocoto

This commit is contained in:
Adrian Cole 2012-01-29 20:29:33 +08:00
parent bf4b6e8c95
commit 55975af60d
30 changed files with 511 additions and 317 deletions

View File

@ -18,7 +18,6 @@
*/ */
package org.jclouds.cloudsigma; package org.jclouds.cloudsigma;
import static com.google.common.base.Preconditions.checkArgument;
import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.cloudsigma.reference.CloudSigmaConstants.PROPERTY_VNC_PASSWORD; import static org.jclouds.cloudsigma.reference.CloudSigmaConstants.PROPERTY_VNC_PASSWORD;
@ -48,11 +47,4 @@ public class CloudSigmaPropertiesBuilder extends PropertiesBuilder {
super(properties); super(properties);
} }
@Override
public Properties build() {
Properties props = super.build();
checkArgument(props.getProperty(PROPERTY_VNC_PASSWORD).length() <= 8,
"vnc passwords should be less that 8 characters!");
return props;
}
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.cloudsigma.compute; package org.jclouds.cloudsigma.compute;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static org.jclouds.concurrent.FutureIterables.transformParallel; import static org.jclouds.concurrent.FutureIterables.transformParallel;
@ -104,6 +105,7 @@ public class CloudSigmaComputeServiceAdapter implements
this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed"); this.driveNotClaimed = checkNotNull(driveNotClaimed, "driveNotClaimed");
this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier"); this.locationSupplier = checkNotNull(locationSupplier, "locationSupplier");
this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword"); this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
checkArgument(defaultVncPassword.length() <= 8, "vnc passwords should be less that 8 characters!");
this.cache = checkNotNull(cache, "cache"); this.cache = checkNotNull(cache, "cache");
this.executor = checkNotNull(executor, "executor"); this.executor = checkNotNull(executor, "executor");
} }

View File

@ -18,7 +18,6 @@
*/ */
package org.jclouds.elasticstack; package org.jclouds.elasticstack;
import static com.google.common.base.Preconditions.checkArgument;
import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.elasticstack.reference.ElasticStackConstants.PROPERTY_VNC_PASSWORD; import static org.jclouds.elasticstack.reference.ElasticStackConstants.PROPERTY_VNC_PASSWORD;
@ -44,14 +43,6 @@ public class ElasticStackPropertiesBuilder extends PropertiesBuilder {
return properties; return properties;
} }
@Override
public Properties build() {
Properties props = super.build();
checkArgument(props.getProperty(PROPERTY_VNC_PASSWORD).length() <= 8,
"vnc passwords should be less that 8 characters!");
return props;
}
public ElasticStackPropertiesBuilder(Properties properties) { public ElasticStackPropertiesBuilder(Properties properties) {
super(properties); super(properties);
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.elasticstack.compute; package org.jclouds.elasticstack.compute;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.notNull; import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
@ -100,6 +101,7 @@ public class ElasticStackComputeServiceAdapter implements
this.preinstalledImages = checkNotNull(preinstalledImages, "preinstalledImages"); this.preinstalledImages = checkNotNull(preinstalledImages, "preinstalledImages");
this.cache = checkNotNull(cache, "cache"); this.cache = checkNotNull(cache, "cache");
this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword"); this.defaultVncPassword = checkNotNull(defaultVncPassword, "defaultVncPassword");
checkArgument(defaultVncPassword.length() <= 8, "vnc passwords should be less that 8 characters!");
this.executor = checkNotNull(executor, "executor"); this.executor = checkNotNull(executor, "executor");
} }

View File

@ -49,6 +49,7 @@ public class S3PropertiesBuilder extends PropertiesBuilder {
properties.setProperty(PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "true"); properties.setProperty(PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "true");
properties.setProperty(PROPERTY_RELAX_HOSTNAME, "true"); properties.setProperty(PROPERTY_RELAX_HOSTNAME, "true");
properties.setProperty(PROPERTY_BLOBSTORE_DIRECTORY_SUFFIX, DIRECTORY_SUFFIX_FOLDER); properties.setProperty(PROPERTY_BLOBSTORE_DIRECTORY_SUFFIX, DIRECTORY_SUFFIX_FOLDER);
properties.setProperty(PROPERTY_USER_METADATA_PREFIX, String.format("x-${%s}-meta-", PROPERTY_HEADER_TAG));
return properties; return properties;
} }
@ -60,21 +61,4 @@ public class S3PropertiesBuilder extends PropertiesBuilder {
super(); super();
} }
protected S3PropertiesBuilder withMetaPrefix(String prefix) {
properties.setProperty(PROPERTY_USER_METADATA_PREFIX, prefix);
return this;
}
protected void setMetaPrefix() {
if (properties.getProperty(PROPERTY_USER_METADATA_PREFIX) == null) {
properties.setProperty(PROPERTY_USER_METADATA_PREFIX, String.format("x-%s-meta-", properties
.getProperty(PROPERTY_HEADER_TAG)));
}
}
@Override
public Properties build() {
setMetaPrefix();
return super.build();
}
} }

View File

@ -42,6 +42,8 @@ public class VCloudPropertiesBuilder extends PropertiesBuilder {
Properties properties = super.defaultProperties(); Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_API_VERSION, "1.0"); properties.setProperty(PROPERTY_API_VERSION, "1.0");
properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "1"); properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "1");
properties.setProperty(PROPERTY_VCLOUD_XML_NAMESPACE,
String.format("http://www.vmware.com/vcloud/v${%s}", PROPERTY_VCLOUD_VERSION_SCHEMA));
properties.setProperty(PROPERTY_SESSION_INTERVAL, 8 * 60 + ""); properties.setProperty(PROPERTY_SESSION_INTERVAL, 8 * 60 + "");
properties.setProperty(PROPERTY_VCLOUD_XML_SCHEMA, "http://vcloud.safesecureweb.com/ns/vcloud.xsd"); properties.setProperty(PROPERTY_VCLOUD_XML_SCHEMA, "http://vcloud.safesecureweb.com/ns/vcloud.xsd");
properties.setProperty("jclouds.dns_name_length_min", "1"); properties.setProperty("jclouds.dns_name_length_min", "1");
@ -58,25 +60,4 @@ public class VCloudPropertiesBuilder extends PropertiesBuilder {
super(properties); super(properties);
} }
protected void setNs() {
if (properties.getProperty(PROPERTY_VCLOUD_XML_NAMESPACE) == null)
properties.setProperty(PROPERTY_VCLOUD_XML_NAMESPACE, "http://www.vmware.com/vcloud/v"
+ properties.getProperty(PROPERTY_VCLOUD_VERSION_SCHEMA));
}
public VCloudPropertiesBuilder withApiVersion(String version) {
properties.setProperty(PROPERTY_API_VERSION, "1.0");
return this;
}
public VCloudPropertiesBuilder withSchemaVersion(String version) {
properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "1.0");
return this;
}
@Override
public Properties build() {
setNs();
return super.build();
}
} }

View File

@ -1,57 +0,0 @@
/**
* 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.azure.storage;
import static org.jclouds.Constants.PROPERTY_ENDPOINT;
import static org.jclouds.Constants.PROPERTY_IDENTITY;
import java.util.Properties;
import java.util.regex.Pattern;
import org.jclouds.PropertiesBuilder;
import org.jclouds.util.Strings2;
/**
* Builds properties used in Azure Connections
*
* @author Adrian Cole
*/
public class AzureStoragePropertiesBuilder extends PropertiesBuilder {
public AzureStoragePropertiesBuilder() {
this(new Properties());
}
public AzureStoragePropertiesBuilder(Properties properties) {
super(properties);
}
public static final Pattern IDENTITY_PATTERN = Pattern.compile("\\{identity\\}");
@Override
public Properties build() {
String endpoint = properties.getProperty(PROPERTY_ENDPOINT);
String identity = properties.getProperty(PROPERTY_IDENTITY);
properties.setProperty(PROPERTY_ENDPOINT, Strings2.replaceAll(endpoint, IDENTITY_PATTERN,
identity));
return super.build();
}
}

View File

@ -33,7 +33,6 @@ import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jclouds.PropertiesBuilder; import org.jclouds.PropertiesBuilder;
import org.jclouds.trmk.vcloud_0_8.domain.FenceMode;
/** /**
* Builds properties used in Terremark VCloud Clients * Builds properties used in Terremark VCloud Clients
@ -48,10 +47,16 @@ public class TerremarkVCloudPropertiesBuilder extends PropertiesBuilder {
properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "0.8"); properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "0.8");
properties.setProperty(PROPERTY_SESSION_INTERVAL, 8 * 60 + ""); properties.setProperty(PROPERTY_SESSION_INTERVAL, 8 * 60 + "");
properties.setProperty(PROPERTY_VCLOUD_XML_SCHEMA, "http://vcloud.safesecureweb.com/ns/vcloud.xsd"); properties.setProperty(PROPERTY_VCLOUD_XML_SCHEMA, "http://vcloud.safesecureweb.com/ns/vcloud.xsd");
properties.setProperty(PROPERTY_VCLOUD_DEFAULT_FENCEMODE, "allowInOut");
properties.setProperty(PROPERTY_TERREMARK_EXTENSION_NS, String.format("urn:tmrk:${%s}-${%s}",
PROPERTY_TERREMARK_EXTENSION_NAME, PROPERTY_TERREMARK_EXTENSION_VERSION));
properties.setProperty(PROPERTY_VCLOUD_XML_NAMESPACE,
String.format("http://www.vmware.com/vcloud/v${%s}", PROPERTY_VCLOUD_VERSION_SCHEMA));
properties.setProperty("jclouds.dns_name_length_min", "1"); properties.setProperty("jclouds.dns_name_length_min", "1");
properties.setProperty("jclouds.dns_name_length_max", "15"); properties.setProperty("jclouds.dns_name_length_max", "15");
// terremark can sometimes block extremely long times // terremark can sometimes block extremely long times
properties.setProperty(PROPERTY_VCLOUD_TIMEOUT_TASK_COMPLETED, TimeUnit.MINUTES.toMillis(20) + ""); properties.setProperty(PROPERTY_VCLOUD_TIMEOUT_TASK_COMPLETED, TimeUnit.MINUTES.toMillis(20) + "");
return properties; return properties;
} }
@ -59,45 +64,4 @@ public class TerremarkVCloudPropertiesBuilder extends PropertiesBuilder {
super(properties); super(properties);
} }
void setExtensions() {
if (properties.getProperty(PROPERTY_TERREMARK_EXTENSION_NS) == null) {
properties.setProperty(
PROPERTY_TERREMARK_EXTENSION_NS,
String.format("urn:tmrk:%s-%s", properties.getProperty(PROPERTY_TERREMARK_EXTENSION_NAME),
properties.getProperty(PROPERTY_TERREMARK_EXTENSION_VERSION)));
}
}
protected void setNs() {
if (properties.getProperty(PROPERTY_VCLOUD_XML_NAMESPACE) == null)
properties.setProperty(PROPERTY_VCLOUD_XML_NAMESPACE,
"http://www.vmware.com/vcloud/v" + properties.getProperty(PROPERTY_VCLOUD_VERSION_SCHEMA));
}
protected void setFenceMode() {
if (properties.getProperty(PROPERTY_VCLOUD_DEFAULT_FENCEMODE) == null) {
if (properties.getProperty(PROPERTY_VCLOUD_VERSION_SCHEMA).startsWith("0.8"))
properties.setProperty(PROPERTY_VCLOUD_DEFAULT_FENCEMODE, "allowInOut");
else
properties.setProperty(PROPERTY_VCLOUD_DEFAULT_FENCEMODE, FenceMode.ALLOW_IN_OUT.toString());
}
}
public TerremarkVCloudPropertiesBuilder withApiVersion(String version) {
properties.setProperty(PROPERTY_API_VERSION, "0.8");
return this;
}
public TerremarkVCloudPropertiesBuilder withSchemaVersion(String version) {
properties.setProperty(PROPERTY_VCLOUD_VERSION_SCHEMA, "0.8");
return this;
}
@Override
public Properties build() {
setNs();
setFenceMode();
setExtensions();
return super.build();
}
} }

View File

@ -71,6 +71,11 @@
<artifactId>guice</artifactId> <artifactId>guice</artifactId>
<version>3.0</version> <version>3.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.99soft.guice</groupId>
<artifactId>rocoto</artifactId>
<version>6.0</version>
</dependency>
<dependency> <dependency>
<groupId>javax.inject</groupId> <groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId> <artifactId>javax.inject</artifactId>

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds; package org.jclouds;
import static org.jclouds.Constants.PROPERTY_API;
import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_BUILD_VERSION; import static org.jclouds.Constants.PROPERTY_BUILD_VERSION;
import static org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT; import static org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT;
@ -219,6 +220,8 @@ public class PropertiesBuilder {
props.setProperty(PROPERTY_CONNECTION_TIMEOUT, 60000 + ""); props.setProperty(PROPERTY_CONNECTION_TIMEOUT, 60000 + "");
props.setProperty(PROPERTY_IO_WORKER_THREADS, 20 + ""); props.setProperty(PROPERTY_IO_WORKER_THREADS, 20 + "");
props.setProperty(PROPERTY_USER_THREADS, 0 + ""); props.setProperty(PROPERTY_USER_THREADS, 0 + "");
props.setProperty(PROPERTY_API, "");
props.setProperty(PROPERTY_API_VERSION, "");
props.setProperty(PROPERTY_BUILD_VERSION, ""); props.setProperty(PROPERTY_BUILD_VERSION, "");
props.setProperty(PROPERTY_MAX_CONNECTION_REUSE, 75 + ""); props.setProperty(PROPERTY_MAX_CONNECTION_REUSE, 75 + "");
props.setProperty(PROPERTY_MAX_SESSION_FAILURES, 2 + ""); props.setProperty(PROPERTY_MAX_SESSION_FAILURES, 2 + "");

View File

@ -0,0 +1,95 @@
/**
* 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.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
/**
* finds all the named string bindings who's name annotation matches the filter.
* for convenience, the bindings are returned as a map.
*
* @author Adrian Cole
*/
@Singleton
public class FilterStringsBoundToInjectorByName implements Function<Predicate<String>, Map<String, String>> {
private final Injector injector;
@Inject
public FilterStringsBoundToInjectorByName(Injector injector) {
this.injector = checkNotNull(injector, "injector");
}
@Override
public Map<String, String> apply(Predicate<String> filter) {
List<Binding<String>> stringBindings = injector.findBindingsByType(new TypeLiteral<String>() {
});
Iterable<Binding<String>> annotatedWithName = Iterables.filter(stringBindings, new Predicate<Binding<String>>() {
@Override
public boolean apply(Binding<String> input) {
Annotation annotation = input.getKey().getAnnotation();
if (annotation == null)
return false;
return (annotation instanceof javax.inject.Named) || (annotation instanceof com.google.inject.name.Named);
}
});
Map<String, Binding<String>> bindingsByName = Maps.uniqueIndex(annotatedWithName,
new Function<Binding<String>, String>() {
@Override
public String apply(Binding<String> input) {
Annotation annotation = input.getKey().getAnnotation();
return ((annotation instanceof javax.inject.Named) ? javax.inject.Named.class.cast(annotation)
.value() : com.google.inject.name.Named.class.cast(annotation).value());
}
});
Map<String, Binding<String>> filteredBindingsByName = Maps.filterKeys(bindingsByName, filter);
Map<String, String> stringBoundByName = Maps.transformValues(filteredBindingsByName,
new Function<Binding<String>, String>() {
@Override
public String apply(Binding<String> input) {
return input.getProvider().get();
}
});
return stringBoundByName;
}
}

View File

@ -19,7 +19,6 @@
package org.jclouds.location.config; package org.jclouds.location.config;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.inject.name.Names.named;
import static org.jclouds.location.reference.LocationConstants.ISO3166_CODES; import static org.jclouds.location.reference.LocationConstants.ISO3166_CODES;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGION; import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGION;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_ZONE; import static org.jclouds.location.reference.LocationConstants.PROPERTY_ZONE;
@ -32,54 +31,54 @@ import javax.inject.Singleton;
import org.jclouds.location.Iso3166; import org.jclouds.location.Iso3166;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.inject.ConfigurationException; import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
import com.google.inject.Key;
/** /**
* *
* looks for properties bound to the naming conventions jclouds.region.{@code regionId} * looks for properties bound to the naming conventions jclouds.region.
* .iso3166-codes and jclouds.zone.{@code zoneId}.iso3166-codes * {@code regionId} .iso3166-codes and jclouds.zone.{@code zoneId}.iso3166-codes
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Singleton @Singleton
public class ProvideIso3166CodesByLocationIdViaProperties implements javax.inject.Provider<Map<String, Set<String>>> { public class ProvideIso3166CodesByLocationIdViaProperties implements javax.inject.Provider<Map<String, Set<String>>> {
private final Injector injector; private final Function<Predicate<String>, Map<String, String>> filterStringsBoundByName;
@Inject @Inject
ProvideIso3166CodesByLocationIdViaProperties(Injector injector) { ProvideIso3166CodesByLocationIdViaProperties(
this.injector = checkNotNull(injector, "injector"); Function<Predicate<String>, Map<String, String>> filterStringsBoundByName) {
this.filterStringsBoundByName = checkNotNull(filterStringsBoundByName, "filterStringsBoundByName");
} }
@Singleton @Singleton
@Iso3166 @Iso3166
@Override @Override
public Map<String, Set<String>> get() { public Map<String, Set<String>> get() {
Builder<String, Set<String>> codes = ImmutableMap.<String, Set<String>> builder(); Map<String, String> stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(new Predicate<String>() {
for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE))
try { @Override
String regionString = injector.getInstance(Key.get(String.class, named(key + "s"))); public boolean apply(String input) {
for (String region : Splitter.on(',').split(regionString)) { return input.startsWith(PROPERTY_REGION) || input.startsWith(PROPERTY_ZONE);
try {
codes.put(region, ImmutableSet.copyOf(Splitter.on(',')
.split(
injector.getInstance(Key.get(String.class, named(key + "." + region + "."
+ ISO3166_CODES))))));
} catch (ConfigurationException e) {
// this happens if regions property isn't set
// services not run by AWS may not have regions, so this is ok.
}
}
} catch (ConfigurationException e) {
// this happens if regions property isn't set
// services not run by AWS may not have regions, so this is ok.
} }
});
Builder<String, Set<String>> codes = ImmutableMap.<String, Set<String>> builder();
for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) {
String regionOrZoneString = stringsBoundWithRegionOrZonePrefix.get(key + "s");
if (regionOrZoneString == null)
continue;
for (String region : Splitter.on(',').split(regionOrZoneString)) {
String isoCodes = stringsBoundWithRegionOrZonePrefix.get(key + "." + region + "." + ISO3166_CODES);
if (isoCodes != null)
codes.put(region, ImmutableSet.copyOf(Splitter.on(',').split(isoCodes)));
}
}
return codes.build(); return codes.build();
} }
} }

View File

@ -24,11 +24,9 @@ import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.location.Region; import org.jclouds.location.Region;
@ -36,7 +34,6 @@ import org.jclouds.logging.Logger;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.inject.ConfigurationException; import com.google.inject.ConfigurationException;
import com.google.inject.Injector; import com.google.inject.Injector;
@ -53,15 +50,13 @@ import com.google.inject.name.Names;
public class ProvideRegionToURIViaProperties implements javax.inject.Provider<Map<String, URI>> { public class ProvideRegionToURIViaProperties implements javax.inject.Provider<Map<String, URI>> {
private final Injector injector; private final Injector injector;
private final Multimap<String, String> constants;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@Inject @Inject
protected ProvideRegionToURIViaProperties(Injector injector, @Named("CONSTANTS") Multimap<String, String> constants) { protected ProvideRegionToURIViaProperties(Injector injector) {
this.injector = injector; this.injector = injector;
this.constants = constants;
} }
@Singleton @Singleton
@ -74,10 +69,6 @@ public class ProvideRegionToURIViaProperties implements javax.inject.Provider<Ma
for (String region : Splitter.on(',').split(regionString)) { for (String region : Splitter.on(',').split(regionString)) {
String regionUri = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGION + "." + region String regionUri = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGION + "." + region
+ "." + ENDPOINT))); + "." + ENDPOINT)));
for (Entry<String, String> entry : constants.entries()) {
regionUri = regionUri.replace(new StringBuilder().append('{').append(entry.getKey()).append('}').toString(), entry
.getValue());
}
regions.put(region, URI.create(regionUri)); regions.put(region, URI.create(regionUri));
} }
return regions.build(); return regions.build();

View File

@ -27,7 +27,6 @@ import static com.google.common.collect.Iterables.addAll;
import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static com.google.inject.Scopes.SINGLETON; import static com.google.inject.Scopes.SINGLETON;
import static com.google.inject.name.Names.bindProperties;
import static com.google.inject.util.Types.newParameterizedType; import static com.google.inject.util.Types.newParameterizedType;
import static org.jclouds.Constants.PROPERTY_API; import static org.jclouds.Constants.PROPERTY_API;
import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_API_VERSION;
@ -45,7 +44,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.Map.Entry;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -55,9 +53,12 @@ import org.jclouds.concurrent.MoreExecutors;
import org.jclouds.concurrent.SingleThreaded; import org.jclouds.concurrent.SingleThreaded;
import org.jclouds.concurrent.config.ConfiguresExecutorService; import org.jclouds.concurrent.config.ConfiguresExecutorService;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService; import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.internal.FilterStringsBoundToInjectorByName;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.lifecycle.config.LifeCycleModule; import org.jclouds.lifecycle.config.LifeCycleModule;
import org.jclouds.location.Iso3166; import org.jclouds.location.Iso3166;
import org.jclouds.location.Provider; import org.jclouds.location.Provider;
@ -73,14 +74,15 @@ import org.jclouds.rest.config.CredentialStoreModule;
import org.jclouds.rest.config.RestClientModule; import org.jclouds.rest.config.RestClientModule;
import org.jclouds.rest.config.RestModule; import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.internal.RestContextImpl; import org.jclouds.rest.internal.RestContextImpl;
import org.jclouds.util.Maps2;
import org.nnsoft.guice.rocoto.Rocoto;
import org.nnsoft.guice.rocoto.configuration.ConfigurationModule;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.ExecutionList; import com.google.common.util.concurrent.ExecutionList;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
@ -106,69 +108,105 @@ import com.google.inject.TypeLiteral;
*/ */
public class RestContextBuilder<S, A> { public class RestContextBuilder<S, A> {
static class BindPropertiesAndPrincipalContext extends AbstractModule { static class BindPropertiesToAnnotations extends ConfigurationModule {
protected Properties properties;
protected BindPropertiesAndPrincipalContext(Properties properties) {
this.properties = checkNotNull(properties, "properties");
}
@Provides
@Singleton
@Named("CONSTANTS")
protected Multimap<String, String> constants() {
ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.<String, String> builder();
for (Entry<Object, Object> entry : properties.entrySet())
if (entry.getValue() != null)
builder.put(entry.getKey().toString(), entry.getValue().toString());
return LinkedHashMultimap.create(builder.build());
}
@Provides @Provides
@Singleton @Singleton
@Named("TIMEOUTS") @Named("TIMEOUTS")
protected Map<String, Long> timeouts() { protected Map<String, Long> timeouts(Function<Predicate<String>, Map<String, String>> filterStringsBoundByName) {
final ImmutableMap.Builder<String, Long> builder = ImmutableMap.<String, Long> builder(); Map<String, String> stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(new Predicate<String>() {
for (final Entry<Object, Object> entry : properties.entrySet()) {
final String key = String.valueOf(entry.getKey()); @Override
if (key.startsWith(PROPERTY_TIMEOUTS_PREFIX) && entry.getValue() != null) { public boolean apply(String input) {
try { return input.startsWith(PROPERTY_TIMEOUTS_PREFIX);
final Long val = Long.valueOf(String.valueOf(entry.getValue()));
builder.put(key.replaceFirst(PROPERTY_TIMEOUTS_PREFIX, ""), val);
} catch (final Throwable t) {}
} }
}
return builder.build(); });
Map<String, Long> longsByName = Maps.transformValues(stringBoundWithTimeoutPrefix, new Function<String, Long>() {
@Override
public Long apply(String input) {
return Long.valueOf(String.valueOf(input));
}
});
return Maps2.transformKeys(longsByName, new Function<String, String>() {
@Override
public String apply(String input) {
return input.replaceFirst(PROPERTY_TIMEOUTS_PREFIX, "");
}
});
} }
@Provides
@Singleton
@Provider
protected String bindProvider(@Named(PROPERTY_PROVIDER) String in){
return in;
}
@Provides
@Singleton
@Provider
protected URI bindProviderEndpoint(@Named(PROPERTY_ENDPOINT) String in){
return URI.create(in);
}
@Provides
@Singleton
@Iso3166
protected Set<String> bindIsoCodes(@Named(PROPERTY_ISO3166_CODES) String in){
return ImmutableSet.copyOf(filter(on(',').split( in),
not(equalTo(""))));
}
@Provides
@Singleton
@Api
protected String bindApi(@Named(PROPERTY_API) String in){
return in;
}
@Provides
@Singleton
@ApiVersion
protected String bindApiVersion(@Named(PROPERTY_API_VERSION) String in){
return in;
}
@Provides
@Singleton
@BuildVersion
protected String bindBuildVersion(@Named(PROPERTY_BUILD_VERSION) String in){
return in;
}
@Provides
@Singleton
@Identity
protected String bindIdentity(@Named(PROPERTY_IDENTITY) String in){
return in;
}
@Provides
@Singleton
@Credential
@Nullable
protected String bindCredential(ValueOfConfigurationKeyOrNull config){
return config.apply(PROPERTY_CREDENTIAL);
}
@Override @Override
protected void configure() { protected void bindConfigurations() {
Properties toBind = new Properties(); bind(new TypeLiteral<Function<Predicate<String>, Map<String, String>>>() {
toBind.putAll(checkNotNull(properties, "properties")); }).to(FilterStringsBoundToInjectorByName.class);
toBind.putAll(System.getProperties());
bindProperties(binder(), toBind);
bind(String.class).annotatedWith(Provider.class).toInstance(
checkNotNull(toBind.getProperty(PROPERTY_PROVIDER), PROPERTY_PROVIDER));
bind(URI.class).annotatedWith(Provider.class).toInstance(
URI.create(checkNotNull(toBind.getProperty(PROPERTY_ENDPOINT), PROPERTY_ENDPOINT)));
bind(new TypeLiteral<Set<String>>() {
}).annotatedWith(Iso3166.class).toInstance(
ImmutableSet.copyOf(filter(on(',').split(
checkNotNull(toBind.getProperty(PROPERTY_ISO3166_CODES), PROPERTY_ISO3166_CODES)),
not(equalTo("")))));
bind(new TypeLiteral<Map<String, Set<String>>>() { bind(new TypeLiteral<Map<String, Set<String>>>() {
}).annotatedWith(Iso3166.class).toProvider(ProvideIso3166CodesByLocationIdViaProperties.class); }).annotatedWith(Iso3166.class).toProvider(ProvideIso3166CodesByLocationIdViaProperties.class);
if (toBind.containsKey(PROPERTY_API))
bind(String.class).annotatedWith(Api.class).toInstance(toBind.getProperty(PROPERTY_API));
if (toBind.containsKey(PROPERTY_API_VERSION))
bind(String.class).annotatedWith(ApiVersion.class).toInstance(toBind.getProperty(PROPERTY_API_VERSION));
if (toBind.containsKey(PROPERTY_BUILD_VERSION))
bind(String.class).annotatedWith(BuildVersion.class).toInstance(toBind.getProperty(PROPERTY_BUILD_VERSION));
if (toBind.containsKey(PROPERTY_IDENTITY))
bind(String.class).annotatedWith(Identity.class).toInstance(
checkNotNull(toBind.getProperty(PROPERTY_IDENTITY), PROPERTY_IDENTITY));
if (toBind.containsKey(PROPERTY_CREDENTIAL))
bind(String.class).annotatedWith(Credential.class).toInstance(toBind.getProperty(PROPERTY_CREDENTIAL));
} }
} }
@ -190,6 +228,17 @@ public class RestContextBuilder<S, A> {
} }
public Injector buildInjector() { public Injector buildInjector() {
modules.add(Rocoto.expandVariables(new ConfigurationModule(){
@Override
protected void bindConfigurations() {
Properties toBind = new Properties();
toBind.putAll(checkNotNull(properties, "properties"));
toBind.putAll(System.getProperties());
bindProperties(toBind);
}
}));
addContextModule(modules); addContextModule(modules);
addClientModuleIfNotPresent(modules); addClientModuleIfNotPresent(modules);
addLoggingModuleIfNotPresent(modules); addLoggingModuleIfNotPresent(modules);
@ -198,7 +247,7 @@ public class RestContextBuilder<S, A> {
addExecutorServiceIfNotPresent(modules); addExecutorServiceIfNotPresent(modules);
addCredentialStoreIfNotPresent(modules); addCredentialStoreIfNotPresent(modules);
modules.add(new LifeCycleModule()); modules.add(new LifeCycleModule());
modules.add(new BindPropertiesAndPrincipalContext(properties)); modules.add(new BindPropertiesToAnnotations());
Injector returnVal = Guice.createInjector(Stage.PRODUCTION, modules); Injector returnVal = Guice.createInjector(Stage.PRODUCTION, modules);
returnVal.getInstance(ExecutionList.class).execute(); returnVal.getInstance(ExecutionList.class).execute();
return returnVal; return returnVal;
@ -334,8 +383,8 @@ public class RestContextBuilder<S, A> {
} }
} }
@VisibleForTesting @VisibleForTesting
public Properties getProperties() { protected Properties getProperties() {
return properties; return properties;
} }

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.annotations;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -33,7 +34,7 @@ import javax.inject.Qualifier;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Target( { ANNOTATION_TYPE, FIELD, PARAMETER }) @Target( { ANNOTATION_TYPE, FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
@Qualifier @Qualifier
public @interface Api { public @interface Api {

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.annotations;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -36,7 +37,7 @@ import org.jclouds.Constants;
* @author Adrian Cole * @author Adrian Cole
* @see Constants#PROPERTY_API_VERSION * @see Constants#PROPERTY_API_VERSION
*/ */
@Target( { ANNOTATION_TYPE, FIELD, PARAMETER }) @Target( { ANNOTATION_TYPE, FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
@Qualifier @Qualifier
public @interface ApiVersion { public @interface ApiVersion {

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.annotations;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -36,7 +37,7 @@ import org.jclouds.Constants;
* @author Adrian Cole * @author Adrian Cole
* @see Constants#PROPERTY_BUILD_VERSION * @see Constants#PROPERTY_BUILD_VERSION
*/ */
@Target( { ANNOTATION_TYPE, FIELD, PARAMETER }) @Target( { ANNOTATION_TYPE, FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
@Qualifier @Qualifier
public @interface BuildVersion { public @interface BuildVersion {

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.annotations;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -33,7 +34,7 @@ import javax.inject.Qualifier;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Target( { ANNOTATION_TYPE, FIELD, PARAMETER }) @Target( { ANNOTATION_TYPE, FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
@Qualifier @Qualifier
public @interface Credential { public @interface Credential {

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.annotations;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -33,7 +34,7 @@ import javax.inject.Qualifier;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Target( { ANNOTATION_TYPE, FIELD, PARAMETER }) @Target( { ANNOTATION_TYPE, FIELD, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
@Qualifier @Qualifier
public @interface Identity { public @interface Identity {

View File

@ -29,6 +29,7 @@ import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Qualifier; import javax.inject.Qualifier;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.ws.rs.Path;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.concurrent.ExceptionParsingListenableFuture; import org.jclouds.concurrent.ExceptionParsingListenableFuture;
@ -132,7 +133,7 @@ public class AsyncRestClientProxy<T> implements InvocationHandler {
} else if (isRestCall(method)) { } else if (isRestCall(method)) {
return createListenableFutureForHttpRequestMappedToMethodAndArgs(method, args); return createListenableFutureForHttpRequestMappedToMethodAndArgs(method, args);
} else { } else {
throw new RuntimeException("method is intended solely to set constants: " + method); throw new RuntimeException(String.format("Method is not annotated as either http or provider method: %s", method));
} }
} }

View File

@ -39,22 +39,19 @@ import java.lang.reflect.Method;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.rest.RestContext;
import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.internal.RestAnnotationProcessor.MethodKey; import org.jclouds.rest.internal.RestAnnotationProcessor.MethodKey;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides; import com.google.inject.Provides;
/** /**
@ -70,19 +67,11 @@ public class SeedAnnotationCache extends CacheLoader<Class<?>, Boolean> {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
protected final Multimap<String, String> constants;
protected final Injector injector; protected final Injector injector;
@Inject @Inject
public SeedAnnotationCache(Injector injector, @Named("CONSTANTS") Multimap<String, String> constants) { public SeedAnnotationCache(Injector injector) {
this.injector = injector; this.injector = injector;
this.constants = constants;
}
public void bindConstant(Method method) {
String key = method.getAnnotation(PathParam.class).value();
String value = injector.getInstance(Key.get(String.class, method.getAnnotation(Named.class)));
constants.put(key, value);
} }
@Override @Override
@ -106,14 +95,12 @@ public class SeedAnnotationCache extends CacheLoader<Class<?>, Boolean> {
methodToIndexesOfOptions.get(method); methodToIndexesOfOptions.get(method);
} }
delegationMap.put(new MethodKey(method), method); delegationMap.put(new MethodKey(method), method);
} else if (isConstantDeclaration(method)) {
bindConstant(method);
} else if (!method.getDeclaringClass().equals(declaring)) { } else if (!method.getDeclaringClass().equals(declaring)) {
logger.trace("skipping potentially overridden method %s", method); logger.trace("skipping potentially overridden method %s", method);
} else if (method.isAnnotationPresent(Provides.class)) { } else if (method.isAnnotationPresent(Provides.class)) {
logger.trace("skipping provider method %s", method); logger.trace("skipping provider method %s", method);
} else { } else {
logger.trace("Method is not annotated as either http or constant: %s", method); logger.trace("Method is not annotated as either http or provider method: %s", method);
} }
} }
return true; return true;
@ -124,7 +111,4 @@ public class SeedAnnotationCache extends CacheLoader<Class<?>, Boolean> {
|| ImmutableSet.copyOf(method.getParameterTypes()).contains(HttpRequest.class); || ImmutableSet.copyOf(method.getParameterTypes()).contains(HttpRequest.class);
} }
public static boolean isConstantDeclaration(Method method) {
return method.isAnnotationPresent(PathParam.class) && method.isAnnotationPresent(Named.class);
}
} }

View File

@ -5,8 +5,7 @@ azurequeue.contextbuilder=org.jclouds.azure.storage.AzureStorageContextBuilder
azurequeue.sync=org.jclouds.azurequeue.AzureQueueClient azurequeue.sync=org.jclouds.azurequeue.AzureQueueClient
azurequeue.async=org.jclouds.azurequeue.AzureQueueAsyncClient azurequeue.async=org.jclouds.azurequeue.AzureQueueAsyncClient
azurequeue.api-version=2009-09-19 azurequeue.api-version=2009-09-19
azurequeue.propertiesbuilder=org.jclouds.azure.storage.AzureStoragePropertiesBuilder azurequeue.endpoint=https://${jclouds.identity}.queue.core.windows.net
azurequeue.endpoint=https://{identity}.queue.core.windows.net
azureblob.contextbuilder=org.jclouds.azureblob.AzureBlobContextBuilder azureblob.contextbuilder=org.jclouds.azureblob.AzureBlobContextBuilder
azureblob.propertiesbuilder=org.jclouds.azureblob.AzureBlobPropertiesBuilder azureblob.propertiesbuilder=org.jclouds.azureblob.AzureBlobPropertiesBuilder

View File

@ -0,0 +1,104 @@
/**
* 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.internal;
import static org.testng.Assert.assertEquals;
import javax.inject.Named;
import org.testng.annotations.Test;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Provides;
import com.google.inject.name.Names;
/**
*
* @author Adrian Cole
*/
@Test(testName = "FilterStringsBoundToInjectorByNameTest")
public class FilterStringsBoundToInjectorByNameTest {
public void testEmptyWhenNoStringsBound() {
FilterStringsBoundToInjectorByName fn = Guice.createInjector().getInstance(FilterStringsBoundToInjectorByName.class);
assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of());
}
public void testEmptyWhenNotStringsBound() {
FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named("foo")).to(1l);
}
}).getInstance(FilterStringsBoundToInjectorByName.class);
assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of());
}
public void testReturnsGuiceNamedString() {
FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named("foo")).to("bar");
}
}).getInstance(FilterStringsBoundToInjectorByName.class);
assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of("foo", "bar"));
}
public void testReturnsJavaNamedString() {
FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
@SuppressWarnings("unused")
@Named("foo")
@Provides
String provideFoo() {
return "bar";
}
@Override
protected void configure() {
}
}).getInstance(FilterStringsBoundToInjectorByName.class);
assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of("foo", "bar"));
}
public void testFilterWorks() {
FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named("foo")).to("bar");
bindConstant().annotatedWith(Names.named("bing")).to("bong");
}
}).getInstance(FilterStringsBoundToInjectorByName.class);
assertEquals(fn.apply(Predicates.<String> equalTo("bing")), ImmutableMap.<String, String> of("bing", "bong"));
}
}

View File

@ -0,0 +1,94 @@
/**
* 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.location.config;
import static org.testng.Assert.assertEquals;
import java.util.Map;
import java.util.Set;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Provides;
/**
*
* @author Adrian Cole
*/
@Test(testName = "ProvideIso3166CodesByLocationIdViaPropertiesTest")
public class ProvideIso3166CodesByLocationIdViaPropertiesTest {
public void testEmptyWhenNoLocationsBound() {
ProvideIso3166CodesByLocationIdViaProperties fn = createWithValue(ImmutableMap.<String, String> of());
assertEquals(fn.get(), ImmutableMap.<String, Set<String>> of());
}
public void testEmptyWhenRegionsAndZonesBoundButNoIsoCodes() {
ProvideIso3166CodesByLocationIdViaProperties fn = createWithValue(ImmutableMap.<String, String> of(
"jclouds.regions", "us-east", "jclouds.zones", "us-easta"));
assertEquals(fn.get(), ImmutableMap.<String, Set<String>> of());
}
public void testIsoCodesWhenRegionsAndZonesBoundWithIsoCodes() {
ProvideIso3166CodesByLocationIdViaProperties fn = createWithValue(ImmutableMap.<String, String> of(
"jclouds.regions", "us-east", "jclouds.region.us-east.iso3166-codes", "US", "jclouds.zones", "us-easta",
"jclouds.zone.us-easta.iso3166-codes", "US-CA"));
assertEquals(
fn.get(),
ImmutableMap.<String, Set<String>> of("us-east", ImmutableSet.of("US"), "us-easta",
ImmutableSet.of("US-CA")));
}
//
private ProvideIso3166CodesByLocationIdViaProperties createWithValue(final ImmutableMap<String, String> value) {
ProvideIso3166CodesByLocationIdViaProperties fn = Guice.createInjector(new AbstractModule() {
@SuppressWarnings("unused")
@Provides
Function<Predicate<String>, Map<String, String>> provide() {
return new Function<Predicate<String>, Map<String, String>>() {
@Override
public Map<String, String> apply(Predicate<String> input) {
return Maps.filterKeys(value, input);
};
};
}
@Override
protected void configure() {
}
}).getInstance(ProvideIso3166CodesByLocationIdViaProperties.class);
return fn;
}
}

View File

@ -18,13 +18,18 @@
*/ */
package org.jclouds.aws.ec2; package org.jclouds.aws.ec2;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Logger;
import org.jclouds.aws.ec2.compute.config.AWSEC2ComputeServiceContextModule; import org.jclouds.aws.ec2.compute.config.AWSEC2ComputeServiceContextModule;
import org.jclouds.aws.ec2.config.AWSEC2RestClientModule; import org.jclouds.aws.ec2.config.AWSEC2RestClientModule;
import org.jclouds.ec2.EC2ContextBuilder; import org.jclouds.ec2.EC2ContextBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
@ -34,9 +39,34 @@ import com.google.inject.Module;
public class AWSEC2ContextBuilder extends EC2ContextBuilder { public class AWSEC2ContextBuilder extends EC2ContextBuilder {
public AWSEC2ContextBuilder(Properties props) { public AWSEC2ContextBuilder(Properties props) {
super(props); super(warnAndReplaceIfUsingOldImageKey(props));
} }
@VisibleForTesting
@Override
public Properties getProperties(){
return properties;
}
//TODO: determine how to do conditional manipulation w/rocoto
static Properties warnAndReplaceIfUsingOldImageKey(Properties props) {
if (props.containsKey(PROPERTY_EC2_AMI_OWNERS)) {
StringBuilder query = new StringBuilder();
String owners = props.remove(PROPERTY_EC2_AMI_OWNERS).toString();
if ("*".equals(owners))
query.append("state=available;image-type=machine");
else if (!"".equals(owners))
query.append("owner-id=").append(owners).append(";state=available;image-type=machine");
else if ("".equals(owners))
query = new StringBuilder();
props.setProperty(PROPERTY_EC2_AMI_QUERY, query.toString());
Logger.getAnonymousLogger().warning(
String.format("Property %s is deprecated, please use new syntax: %s=%s", PROPERTY_EC2_AMI_OWNERS,
PROPERTY_EC2_AMI_QUERY, query.toString()));
}
return props;
}
@Override @Override
protected void addClientModule(List<Module> modules) { protected void addClientModule(List<Module> modules) {
modules.add(new AWSEC2RestClientModule()); modules.add(new AWSEC2RestClientModule());

View File

@ -27,7 +27,6 @@ import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIM
import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS; import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Logger;
import org.jclouds.aws.domain.Region; import org.jclouds.aws.domain.Region;
@ -72,28 +71,5 @@ public class AWSEC2PropertiesBuilder extends org.jclouds.ec2.EC2PropertiesBuilde
super(properties); super(properties);
} }
@Override
public Properties build() {
Properties props = super.build();
warnAndReplaceIfUsingOldImageKey(props);
return props;
}
protected void warnAndReplaceIfUsingOldImageKey(Properties props) {
if (props.containsKey(PROPERTY_EC2_AMI_OWNERS)) {
StringBuilder query = new StringBuilder();
String owners = properties.remove(PROPERTY_EC2_AMI_OWNERS).toString();
if ("*".equals(owners))
query.append("state=available;image-type=machine");
else if (!"".equals(owners))
query.append("owner-id=").append(owners).append(";state=available;image-type=machine");
else if ("".equals(owners))
query = new StringBuilder();
props.setProperty(PROPERTY_EC2_AMI_QUERY, query.toString());
Logger.getAnonymousLogger().warning(
String.format("Property %s is deprecated, please use new syntax: %s=%s", PROPERTY_EC2_AMI_OWNERS,
PROPERTY_EC2_AMI_QUERY, query.toString()));
}
}
} }

View File

@ -29,13 +29,13 @@ import org.testng.annotations.Test;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "unit", testName = "AWSEC2PropertiesBuilderTest") @Test(groups = "unit", testName = "AWSEC2ContextBuilderTest")
public class AWSEC2PropertiesBuilderTest { public class AWSEC2ContextBuilderTest {
public void testConvertImageSyntax() { public void testConvertImageSyntax() {
Properties input = new Properties(); Properties input = new Properties();
input.setProperty(PROPERTY_EC2_AMI_OWNERS, "137112412989,063491364108,099720109477,411009282317"); input.setProperty(PROPERTY_EC2_AMI_OWNERS, "137112412989,063491364108,099720109477,411009282317");
Properties props = new AWSEC2PropertiesBuilder(input).build(); Properties props = new AWSEC2ContextBuilder(input).getProperties();
assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null); assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null);
assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY), assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY),
"owner-id=137112412989,063491364108,099720109477,411009282317;state=available;image-type=machine"); "owner-id=137112412989,063491364108,099720109477,411009282317;state=available;image-type=machine");
@ -44,7 +44,7 @@ public class AWSEC2PropertiesBuilderTest {
public void testConvertImageSyntaxWhenStar() { public void testConvertImageSyntaxWhenStar() {
Properties input = new Properties(); Properties input = new Properties();
input.setProperty(PROPERTY_EC2_AMI_OWNERS, "*"); input.setProperty(PROPERTY_EC2_AMI_OWNERS, "*");
Properties props = new AWSEC2PropertiesBuilder(input).build(); Properties props = new AWSEC2ContextBuilder(input).getProperties();
assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null); assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null);
assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY), "state=available;image-type=machine"); assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY), "state=available;image-type=machine");
} }
@ -52,7 +52,7 @@ public class AWSEC2PropertiesBuilderTest {
public void testConvertImageSyntaxWhenBlank() { public void testConvertImageSyntaxWhenBlank() {
Properties input = new Properties(); Properties input = new Properties();
input.setProperty(PROPERTY_EC2_AMI_OWNERS, ""); input.setProperty(PROPERTY_EC2_AMI_OWNERS, "");
Properties props = new AWSEC2PropertiesBuilder(input).build(); Properties props = new AWSEC2ContextBuilder(input).getProperties();
assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null); assertEquals(props.getProperty(PROPERTY_EC2_AMI_OWNERS), null);
assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY), ""); assertEquals(props.getProperty(PROPERTY_EC2_AMI_QUERY), "");
} }

View File

@ -35,7 +35,7 @@
<properties> <properties>
<test.initializer>org.jclouds.azureblob.blobstore.integration.AzureBlobTestInitializer</test.initializer> <test.initializer>org.jclouds.azureblob.blobstore.integration.AzureBlobTestInitializer</test.initializer>
<test.azureblob.endpoint>https://{identity}.blob.core.windows.net</test.azureblob.endpoint> <test.azureblob.endpoint>https://${jclouds.identity}.blob.core.windows.net</test.azureblob.endpoint>
<test.azureblob.api-version>2009-09-19</test.azureblob.api-version> <test.azureblob.api-version>2009-09-19</test.azureblob.api-version>
<test.azureblob.build-version></test.azureblob.build-version> <test.azureblob.build-version></test.azureblob.build-version>
<test.azureblob.identity>${test.azure.identity}</test.azureblob.identity> <test.azureblob.identity>${test.azure.identity}</test.azureblob.identity>

View File

@ -25,20 +25,20 @@ import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_M
import java.util.Properties; import java.util.Properties;
import org.jclouds.azure.storage.AzureStoragePropertiesBuilder; import org.jclouds.PropertiesBuilder;
/** /**
* Builds properties used in AzureBlob Connections * Builds properties used in AzureBlob Connections
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class AzureBlobPropertiesBuilder extends AzureStoragePropertiesBuilder { public class AzureBlobPropertiesBuilder extends PropertiesBuilder {
@Override @Override
protected Properties defaultProperties() { protected Properties defaultProperties() {
Properties properties = super.defaultProperties(); Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_USER_METADATA_PREFIX, "x-ms-meta-"); properties.setProperty(PROPERTY_USER_METADATA_PREFIX, "x-ms-meta-");
properties.setProperty(PROPERTY_API_VERSION, "2009-09-19"); properties.setProperty(PROPERTY_API_VERSION, "2009-09-19");
properties.setProperty(PROPERTY_ENDPOINT, "https://{identity}.blob.core.windows.net"); properties.setProperty(PROPERTY_ENDPOINT, "https://${jclouds.identity}.blob.core.windows.net");
properties.setProperty(PROPERTY_ISO3166_CODES, "US-TX,US-IL,IE-D,SG,NL-NH,HK"); properties.setProperty(PROPERTY_ISO3166_CODES, "US-TX,US-IL,IE-D,SG,NL-NH,HK");
return properties; return properties;
} }

View File

@ -34,7 +34,7 @@
<packaging>bundle</packaging> <packaging>bundle</packaging>
<properties> <properties>
<test.azurequeue.endpoint>https://{identity}.queue.core.windows.net</test.azurequeue.endpoint> <test.azurequeue.endpoint>https://${jclouds.identity}.queue.core.windows.net</test.azurequeue.endpoint>
<test.azurequeue.api-version>2009-09-19</test.azurequeue.api-version> <test.azurequeue.api-version>2009-09-19</test.azurequeue.api-version>
<test.azurequeue.build-version></test.azurequeue.build-version> <test.azurequeue.build-version></test.azurequeue.build-version>
<test.azurequeue.identity>${test.azure.identity}</test.azurequeue.identity> <test.azurequeue.identity>${test.azure.identity}</test.azurequeue.identity>