Issue 298: fixed regression on authorizationexception not propagating, and also missing gogrid file

This commit is contained in:
Adrian Cole 2010-07-02 12:05:41 -07:00
parent 32e7ca0a1f
commit 9bac52d315
10 changed files with 397 additions and 310 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.blobstore;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import java.util.Properties;
@ -63,21 +64,31 @@ public class BlobStoreContextFactory {
this.contextFactory = restContextFactory;
}
public static <S, A> BlobStoreContext buildContextUnwrappingExceptions(
BlobStoreContextBuilder<S, A> builder) {
try {
return builder.buildBlobStoreContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
}
/**
* @see RestContextFactory#createContextBuilder(String, String, String)
*/
public BlobStoreContext createContext(String provider, String identity, String credential) {
return BlobStoreContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, identity, credential))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, Properties)
*/
public BlobStoreContext createContext(String provider, Properties overrides) {
return BlobStoreContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, overrides)).buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -85,9 +96,10 @@ public class BlobStoreContextFactory {
*/
public BlobStoreContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) {
return BlobStoreContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, modules, overrides))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -95,27 +107,29 @@ public class BlobStoreContextFactory {
*/
public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) {
return BlobStoreContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, identity, credential, modules))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/
public BlobStoreContext createContext(String providerName, @Nullable String identity,
public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return BlobStoreContextBuilder.class.cast(
contextFactory.createContextBuilder(providerName, identity, credential, modules,
overrides)).buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -123,16 +137,18 @@ public class BlobStoreContextFactory {
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, modules))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) {
return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, overrides))
.buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -140,7 +156,8 @@ public class BlobStoreContextFactory {
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) {
return BlobStoreContextBuilder.class.cast(
createContextBuilder(contextSpec, modules, overrides)).buildBlobStoreContext();
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -19,6 +19,7 @@
package org.jclouds.compute;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import java.util.Properties;
@ -63,22 +64,31 @@ public class ComputeServiceContextFactory {
this.contextFactory = restContextFactory;
}
public static <S, A> ComputeServiceContext buildContextUnwrappingExceptions(
ComputeServiceContextBuilder<S, A> builder) {
try {
return builder.buildComputeServiceContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
}
/**
* @see RestContextFactory#createContextBuilder(String, String, String)
*/
public ComputeServiceContext createContext(String provider, String identity, String credential) {
return ComputeServiceContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, identity, credential))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, Properties)
*/
public ComputeServiceContext createContext(String provider, Properties overrides) {
return ComputeServiceContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, overrides))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -86,9 +96,10 @@ public class ComputeServiceContextFactory {
*/
public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) {
return ComputeServiceContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, modules, overrides))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -96,27 +107,30 @@ public class ComputeServiceContextFactory {
*/
public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) {
return ComputeServiceContextBuilder.class.cast(
contextFactory.createContextBuilder(provider, identity, credential, modules))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/
public ComputeServiceContext createContext(String providerName, @Nullable String identity,
public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return ComputeServiceContextBuilder.class.cast(
contextFactory.createContextBuilder(providerName, identity, credential, modules,
overrides)).buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules,
overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec)
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -124,8 +138,9 @@ public class ComputeServiceContextFactory {
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, modules))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -133,8 +148,9 @@ public class ComputeServiceContextFactory {
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Properties overrides) {
return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, overrides))
.buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
@ -142,7 +158,8 @@ public class ComputeServiceContextFactory {
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) {
return ComputeServiceContextBuilder.class.cast(
createContextBuilder(contextSpec, modules, overrides)).buildComputeServiceContext();
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -398,7 +398,7 @@ public class StubComputeServiceContextModule extends AbstractModule {
@Singleton
Location getLocation(@org.jclouds.rest.annotations.Provider String providerName) {
Location provider = new LocationImpl(LocationScope.PROVIDER, providerName, providerName, null);
return new LocationImpl(LocationScope.ZONE, "memory", "memory", provider);
return new LocationImpl(LocationScope.ZONE, providerName, providerName, provider);
}
@Provides

View File

@ -18,14 +18,12 @@
*/
package org.jclouds.compute.util;
import java.util.Formatter;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import org.jclouds.compute.ComputeServiceContextBuilder;
import org.jclouds.compute.domain.Architecture;
import org.jclouds.compute.domain.ComputeMetadata;
@ -145,7 +143,8 @@ public class ComputeServiceUtils {
public static boolean isKeyAuth(NodeMetadata createdNode) {
return createdNode.getCredentials().credential != null
&& createdNode.getCredentials().credential.startsWith("-----BEGIN RSA PRIVATE KEY-----");
&& createdNode.getCredentials().credential
.startsWith("-----BEGIN RSA PRIVATE KEY-----");
}
/**

View File

@ -99,8 +99,8 @@ public abstract class BaseComputeServiceLiveTest {
protected Map<String, String> keyPair;
@BeforeGroups(groups = { "integration", "live" })
public void setupClient() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
public void setupClient() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
if (tag == null)
tag = checkNotNull(provider, "provider");
setupCredentials();
@ -110,31 +110,27 @@ public abstract class BaseComputeServiceLiveTest {
Injector injector = createSshClientInjector();
sshFactory = injector.getInstance(SshClient.Factory.class);
SocketOpen socketOpen = injector.getInstance(SocketOpen.class);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1,
TimeUnit.SECONDS);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
injector.injectMembers(socketOpen); // add logger
}
protected void setupKeyPair() throws FileNotFoundException, IOException {
String secretKeyFile;
try {
secretKeyFile = checkNotNull(System
.getProperty("jclouds.test.ssh.keyfile"),
secretKeyFile = checkNotNull(System.getProperty("jclouds.test.ssh.keyfile"),
"jclouds.test.ssh.keyfile");
} catch (NullPointerException e) {
secretKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa";
}
checkSecretKeyFile(secretKeyFile);
String secret = Files.toString(new File(secretKeyFile), Charsets.UTF_8);
assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n"
+ secret;
keyPair = ImmutableMap.<String, String> of("private", secret, "public",
Files.toString(new File(secretKeyFile + ".pub"), Charsets.UTF_8));
assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" + secret;
keyPair = ImmutableMap.<String, String> of("private", secret, "public", Files.toString(
new File(secretKeyFile + ".pub"), Charsets.UTF_8));
}
protected void setupCredentials() {
identity = checkNotNull(System.getProperty("jclouds.test.identity"),
"jclouds.test.identity");
identity = checkNotNull(System.getProperty("jclouds.test.identity"), "jclouds.test.identity");
credential = checkNotNull(System.getProperty("jclouds.test.credential"),
"jclouds.test.credential");
}
@ -146,20 +142,16 @@ public abstract class BaseComputeServiceLiveTest {
private void initializeContextAndClient() throws IOException {
if (context != null)
context.close();
context = new ComputeServiceContextFactory()
.createContext(provider, identity, credential, ImmutableSet.of(
new Log4JLoggingModule(), getSshModule()));
context = new ComputeServiceContextFactory().createContext(provider, identity, credential,
ImmutableSet.of(new Log4JLoggingModule(), getSshModule()));
client = context.getComputeService();
}
private void checkSecretKeyFile(String secretKeyFile)
throws FileNotFoundException {
Utils
.checkNotEmpty(secretKeyFile,
private void checkSecretKeyFile(String secretKeyFile) throws FileNotFoundException {
Utils.checkNotEmpty(secretKeyFile,
"System property: [jclouds.test.ssh.keyfile] set to an empty string");
if (!new File(secretKeyFile).exists()) {
throw new FileNotFoundException("secretKeyFile not found at: "
+ secretKeyFile);
throw new FileNotFoundException("secretKeyFile not found at: " + secretKeyFile);
}
}
@ -172,7 +164,7 @@ public abstract class BaseComputeServiceLiveTest {
ImmutableSet.<Module> of(new Log4JLoggingModule())).close();
}
@Test(enabled = true)
@Test(enabled = true, dependsOnMethods = "testCorrectAuthException")
public void testImagesCache() throws Exception {
client.listImages();
long time = System.currentTimeMillis();
@ -183,9 +175,8 @@ public abstract class BaseComputeServiceLiveTest {
// since surefire and eclipse don't otherwise guarantee the order, we are
// starting this one alphabetically before create2nodes..
@Test(enabled = true, dependsOnMethods = "testImagesCache")
public void testAScriptExecutionAfterBootWithBasicTemplate()
throws Exception {
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
String tag = this.tag + "run";
try {
client.destroyNodesMatching(NodePredicates.withTag(tag));
@ -195,21 +186,18 @@ public abstract class BaseComputeServiceLiveTest {
TemplateOptions options = client.templateOptions().blockOnPort(22, 120);
try {
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1,
options);
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, options);
Credentials good = nodes.iterator().next().getCredentials();
assert good.identity != null;
assert good.credential != null;
Image image = Iterables.get(nodes, 0).getImage();
try {
Map<? extends NodeMetadata, ExecResponse> responses = runScriptWithCreds(
tag, image.getOsFamily(), new Credentials(good.identity,
"romeo"));
Map<? extends NodeMetadata, ExecResponse> responses = runScriptWithCreds(tag, image
.getOsFamily(), new Credentials(good.identity, "romeo"));
assert false : "shouldn't pass with a bad password\n" + responses;
} catch (RunScriptOnNodesException e) {
assert Throwables.getRootCause(e).getMessage()
.contains("Auth fail") : e;
assert Throwables.getRootCause(e).getMessage().contains("Auth fail") : e;
}
runScriptWithCreds(tag, image.getOsFamily(), good);
@ -221,11 +209,10 @@ public abstract class BaseComputeServiceLiveTest {
}
}
@Test(enabled = true, dependsOnMethods = "testImagesCache")
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testTemplateMatch() throws Exception {
template = buildTemplate(client.templateBuilder());
Template toMatch = client.templateBuilder().imageId(
template.getImage().getId()).build();
Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build();
assertEquals(toMatch.getImage(), template.getImage());
}
@ -240,14 +227,14 @@ public abstract class BaseComputeServiceLiveTest {
}
template = buildTemplate(client.templateBuilder());
template.getOptions().installPrivateKey(keyPair.get("private"))
.authorizePublicKey(keyPair.get("public")).runScript(
template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(
keyPair.get("public")).runScript(
buildScript(template.getImage().getOsFamily()).getBytes());
try {
nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template));
} catch (RunNodesException e) {
nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e
.getNodeErrors().keySet()));
nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e.getNodeErrors()
.keySet()));
throw e;
}
assertEquals(nodes.size(), 2);
@ -274,11 +261,9 @@ public abstract class BaseComputeServiceLiveTest {
}
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired()
throws Exception {
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
initializeContextAndClient();
TreeSet<NodeMetadata> nodes = Sets.newTreeSet(client.runNodesWithTag(tag,
1, template));
TreeSet<NodeMetadata> nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 1, template));
checkNodes(nodes, tag);
NodeMetadata node = nodes.first();
this.nodes.add(node);
@ -287,13 +272,11 @@ public abstract class BaseComputeServiceLiveTest {
assertEquals(node.getImage(), template.getImage());
}
protected Map<? extends NodeMetadata, ExecResponse> runScriptWithCreds(
final String tag, OsFamily osFamily, Credentials creds)
throws RunScriptOnNodesException {
protected Map<? extends NodeMetadata, ExecResponse> runScriptWithCreds(final String tag,
OsFamily osFamily, Credentials creds) throws RunScriptOnNodesException {
try {
return client.runScriptOnNodesMatching(NodePredicates
.runningWithTag(tag), buildScript(osFamily).getBytes(),
RunScriptOptions.Builder.overrideCredentialsWith(creds));
return client.runScriptOnNodesMatching(NodePredicates.runningWithTag(tag), buildScript(
osFamily).getBytes(), RunScriptOptions.Builder.overrideCredentialsWith(creds));
} catch (SshException e) {
if (Throwables.getRootCause(e).getMessage().contains("Auth fail")) {
// System.err.printf("bad credentials: %s:%s for %s%n",
@ -304,15 +287,14 @@ public abstract class BaseComputeServiceLiveTest {
}
}
protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag)
throws IOException {
protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag) throws IOException {
for (NodeMetadata node : nodes) {
assertNotNull(node.getProviderId());
assertNotNull(node.getTag());
assertEquals(node.getTag(), tag);
assertEquals(node.getState(), NodeState.RUNNING);
assert node.getPublicAddresses().size() >= 1
|| node.getPrivateAddresses().size() >= 1 : "no ips in" + node;
assert node.getPublicAddresses().size() >= 1 || node.getPrivateAddresses().size() >= 1 : "no ips in"
+ node;
assertNotNull(node.getCredentials());
if (node.getCredentials().identity != null) {
assertNotNull(node.getCredentials().identity);
@ -343,14 +325,11 @@ public abstract class BaseComputeServiceLiveTest {
case RHEL:
return new StringBuilder()
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")
.append(
"echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append(
"echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append("echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append("echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append(
"echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append(
"echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append("echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
.append("yum --nogpgcheck -y install java-1.6.0-openjdk\n")
.append(
"echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n")
@ -362,21 +341,18 @@ public abstract class BaseComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception {
Set<? extends NodeMetadata> metadataSet = Sets.newHashSet(Iterables
.filter(client.listNodesDetailsMatching(NodePredicates.all()),
Predicates.and(NodePredicates.withTag(tag), Predicates
.not(NodePredicates.TERMINATED))));
Set<? extends NodeMetadata> metadataSet = Sets.newHashSet(Iterables.filter(client
.listNodesDetailsMatching(NodePredicates.all()), Predicates.and(NodePredicates
.withTag(tag), Predicates.not(NodePredicates.TERMINATED))));
for (NodeMetadata node : nodes) {
metadataSet.remove(node);
NodeMetadata metadata = client.getNodeMetadata(node.getId());
assertEquals(metadata.getProviderId(), node.getProviderId());
assertEquals(metadata.getTag(), node.getTag());
assertLocationSameOrChild(metadata.getLocation(), template
.getLocation());
assertLocationSameOrChild(metadata.getLocation(), template.getLocation());
assertEquals(metadata.getImage(), template.getImage());
assertEquals(metadata.getState(), NodeState.RUNNING);
assertEquals(metadata.getPrivateAddresses(), node
.getPrivateAddresses());
assertEquals(metadata.getPrivateAddresses(), node.getPrivateAddresses());
assertEquals(metadata.getPublicAddresses(), node.getPublicAddresses());
}
assertNodeZero(metadataSet);
@ -384,8 +360,7 @@ public abstract class BaseComputeServiceLiveTest {
protected void assertNodeZero(Set<? extends NodeMetadata> metadataSet) {
assert metadataSet.size() == 0 : String.format(
"nodes left in set: [%s] which didn't match set: [%s]",
metadataSet, nodes);
"nodes left in set: [%s] which didn't match set: [%s]", metadataSet, nodes);
}
@Test(enabled = true, dependsOnMethods = "testGet")
@ -412,8 +387,7 @@ public abstract class BaseComputeServiceLiveTest {
}
public void testGetNodesWithDetails() throws Exception {
for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates
.all())) {
for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates.all())) {
assert node.getProviderId() != null : node;
assert node.getLocation() != null : node;
assertEquals(node.getType(), ComputeType.NODE);
@ -482,18 +456,14 @@ public abstract class BaseComputeServiceLiveTest {
}
// no inbound ports
TemplateOptions options = client.templateOptions().blockUntilRunning(
false).inboundPorts();
TemplateOptions options = client.templateOptions().blockUntilRunning(false).inboundPorts();
try {
long time = System.currentTimeMillis();
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1,
options);
Set<? extends NodeMetadata> nodes = client.runNodesWithTag(tag, 1, options);
NodeMetadata node = Iterables.getOnlyElement(nodes);
assertEquals(node.getState(), NodeState.PENDING);
assert node.getState() != NodeState.RUNNING;
long duration = System.currentTimeMillis() - time;
assert duration < 30 * 1000 : "duration longer than 30 seconds!: "
+ duration / 1000;
assert duration < 30 * 1000 : "duration longer than 30 seconds!: " + duration / 1000;
} finally {
client.destroyNodesMatching(NodePredicates.withTag(tag));
}
@ -529,15 +499,13 @@ public abstract class BaseComputeServiceLiveTest {
}
}
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node)
throws IOException {
IPSocket socket = new IPSocket(Iterables
.get(node.getPublicAddresses(), 0), 22);
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException {
IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), 22);
socketTester.apply(socket); // TODO add transitionTo option that accepts
// a socket conection
// state.
SshClient ssh = sshFactory.create(socket, node.getCredentials().identity,
keyPair.get("private").getBytes());
SshClient ssh = sshFactory.create(socket, node.getCredentials().identity, keyPair.get(
"private").getBytes());
try {
ssh.connect();
ExecResponse hello = ssh.exec("echo hello");
@ -551,13 +519,11 @@ public abstract class BaseComputeServiceLiveTest {
}
@AfterTest
protected void cleanup() throws InterruptedException, ExecutionException,
TimeoutException {
protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException {
if (nodes != null) {
client.destroyNodesMatching(NodePredicates.withTag(tag));
for (NodeMetadata node : Iterables.filter(client
.listNodesDetailsMatching(NodePredicates.all()), NodePredicates
.withTag(tag))) {
for (NodeMetadata node : Iterables.filter(client.listNodesDetailsMatching(NodePredicates
.all()), NodePredicates.withTag(tag))) {
assert node.getState() == NodeState.TERMINATED : node;
}
}

View File

@ -66,8 +66,7 @@ import com.google.inject.Module;
* @author Adrian Cole
*/
@Test(groups = "live", enabled = true, sequential = true, testName = "stub.StubComputeServiceIntegrationTest")
public class StubComputeServiceIntegrationTest extends
BaseComputeServiceLiveTest {
public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTest {
private static final ExecResponse EXEC_GOOD = new ExecResponse("", "", 0);
private static final ExecResponse EXEC_BAD = new ExecResponse("", "", 1);
@ -85,10 +84,9 @@ public class StubComputeServiceIntegrationTest extends
@Test
public void testTemplateBuilder() {
Template defaultTemplate = client.templateBuilder().build();
assertEquals(defaultTemplate.getImage().getArchitecture(),
Architecture.X86_64);
assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64);
assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU);
assertEquals(defaultTemplate.getLocation().getId(), "memory");
assertEquals(defaultTemplate.getLocation().getId(), provider);
assertEquals(defaultTemplate.getSize().getCores(), 4.0d);
}
@ -111,21 +109,17 @@ public class StubComputeServiceIntegrationTest extends
expect(open.apply(new IPSocket("144.175.1.4", 22))).andReturn(true);
expect(
factory.create(eq(new IPSocket("144.175.1.1", 22)),
eq("root"), aryEq(keyPair.get("private").getBytes())))
.andReturn(client1).atLeastOnce();
factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair
.get("private").getBytes()))).andReturn(client1).atLeastOnce();
expect(
factory.create(eq(new IPSocket("144.175.1.2", 22)),
eq("root"), aryEq(keyPair.get("private").getBytes())))
.andReturn(client2).atLeastOnce();
factory.create(eq(new IPSocket("144.175.1.2", 22)), eq("root"), aryEq(keyPair
.get("private").getBytes()))).andReturn(client2).atLeastOnce();
expect(
factory.create(eq(new IPSocket("144.175.1.3", 22)),
eq("root"), aryEq(keyPair.get("private").getBytes())))
.andReturn(client3).atLeastOnce();
factory.create(eq(new IPSocket("144.175.1.3", 22)), eq("root"), aryEq(keyPair
.get("private").getBytes()))).andReturn(client3).atLeastOnce();
expect(
factory.create(eq(new IPSocket("144.175.1.4", 22)),
eq("root"), aryEq(keyPair.get("private").getBytes())))
.andReturn(client4).atLeastOnce();
factory.create(eq(new IPSocket("144.175.1.4", 22)), eq("root"), aryEq(keyPair
.get("private").getBytes()))).andReturn(client4).atLeastOnce();
helloAndJava(client1);
helloAndJava(client2);
@ -147,10 +141,8 @@ public class StubComputeServiceIntegrationTest extends
private void helloAndJava(SshClient client) {
client.connect();
expect(client.exec("echo hello")).andReturn(
new ExecResponse("hello", "", 0));
expect(client.exec("java -version")).andReturn(
new ExecResponse("", "OpenJDK", 0));
expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0));
expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0));
client.disconnect();
}
@ -170,26 +162,21 @@ public class StubComputeServiceIntegrationTest extends
SshClient client3 = createMock(SshClient.class);
SshClient client4 = createMock(SshClient.class);
expect(
factory.create(new IPSocket("144.175.1.1", 22), "root",
"romeo")).andThrow(new SshException("Auth fail"));
expect(
factory.create(new IPSocket("144.175.1.1", 22), "root",
"password1")).andReturn(client1).atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "romeo")).andThrow(
new SshException("Auth fail"));
expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "password1")).andReturn(
client1).atLeastOnce();
client1.connect();
runScript(client1, "computeserv", 1);
client1.disconnect();
expect(
factory.create(new IPSocket("144.175.1.2", 22), "root",
"password2")).andReturn(client2).atLeastOnce();
expect(
factory.create(new IPSocket("144.175.1.3", 22), "root",
"password3")).andReturn(client3).atLeastOnce();
expect(
factory.create(new IPSocket("144.175.1.4", 22), "root",
"password4")).andReturn(client4).atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn(
client2).atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.3", 22), "root", "password3")).andReturn(
client3).atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn(
client4).atLeastOnce();
runScriptAndInstallSsh(client2, "runscript", 2);
runScriptAndInstallSsh(client3, "runscript", 3);
@ -204,17 +191,14 @@ public class StubComputeServiceIntegrationTest extends
bind(SshClient.Factory.class).toInstance(factory);
}
private void runScriptAndInstallSsh(SshClient client,
String scriptName, int nodeId) {
private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
client.connect();
runScript(client, scriptName, nodeId);
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys"))
.andReturn(EXEC_GOOD);
expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(
EXEC_GOOD);
expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")).andReturn(EXEC_GOOD);
expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(EXEC_GOOD);
client.put(eq(".ssh/id_rsa.pub"), isEq(keyPair.get("public")));
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
@ -230,24 +214,16 @@ public class StubComputeServiceIntegrationTest extends
client.put(eq("" + scriptName + ""), isEq(initScript(scriptName,
buildScript(OsFamily.UBUNTU))));
expect(client.exec("chmod 755 " + scriptName + "")).andReturn(
EXEC_GOOD);
expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn(nodeId + "")
.atLeastOnce();
expect(client.exec("./" + scriptName + " init")).andReturn(
EXEC_GOOD);
expect(client.exec("./" + scriptName + " start")).andReturn(
EXEC_GOOD);
expect(client.exec("./" + scriptName + " status")).andReturn(
EXEC_GOOD);
expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce();
expect(client.exec("./" + scriptName + " init")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " start")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_GOOD);
// next status says the script is done, since not found.
expect(client.exec("./" + scriptName + " status")).andReturn(
EXEC_BAD);
expect(client.exec("./" + scriptName + " tail")).andReturn(
EXEC_GOOD);
expect(client.exec("./" + scriptName + " tailerr")).andReturn(
EXEC_GOOD);
expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_BAD);
expect(client.exec("./" + scriptName + " tail")).andReturn(EXEC_GOOD);
expect(client.exec("./" + scriptName + " tailerr")).andReturn(EXEC_GOOD);
}
};
@ -264,9 +240,8 @@ public class StubComputeServiceIntegrationTest extends
}
public static String initScript(String scriptName, String script) {
return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/"
+ scriptName, ImmutableMap.<String, String> of(), Iterables
.toArray(Splitter.on("\n").split(
return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" + scriptName, ImmutableMap
.<String, String> of(), Iterables.toArray(Splitter.on("\n").split(
new String(checkNotNull(script, "script"))), String.class))
.build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX);
}
@ -279,12 +254,10 @@ public class StubComputeServiceIntegrationTest extends
public void testAssignability() throws Exception {
@SuppressWarnings("unused")
RestContext<ConcurrentMap<Integer, StubNodeMetadata>, ConcurrentMap<Integer, StubNodeMetadata>> stubContext = new ComputeServiceContextFactory()
.createContext(provider, identity, credential)
.getProviderSpecificContext();
.createContext(provider, identity, credential).getProviderSpecificContext();
}
private static class InputStreamEquals implements IArgumentMatcher,
Serializable {
private static class InputStreamEquals implements IArgumentMatcher, Serializable {
private static final long serialVersionUID = 583055160049982067L;
@ -330,8 +303,8 @@ public class StubComputeServiceIntegrationTest extends
if (o == null || !this.getClass().equals(o.getClass()))
return false;
InputStreamEquals other = (InputStreamEquals) o;
return this.expected == null && other.expected == null
|| this.expected != null && this.expected.equals(other.expected);
return this.expected == null && other.expected == null || this.expected != null
&& this.expected.equals(other.expected);
}
@Override
@ -343,8 +316,50 @@ public class StubComputeServiceIntegrationTest extends
@Override
protected void setupKeyPair() throws FileNotFoundException, IOException {
keyPair = ImmutableMap.<String, String> of("public", "ssh-rsa",
"private", "-----BEGIN RSA PRIVATE KEY-----");
keyPair = ImmutableMap.<String, String> of("public", "ssh-rsa", "private",
"-----BEGIN RSA PRIVATE KEY-----");
}
// TODO: I have absolutely no idea why I have to redeclare all this cruft. If I don't, then we
// get all sorts of not allowed to depend on errors.
@Override
public void testImagesCache() throws Exception {
super.testImagesCache();
}
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
super.testAScriptExecutionAfterBootWithBasicTemplate();
}
@Test(enabled = true, dependsOnMethods = "testTemplateMatch")
public void testCreateTwoNodesWithRunScript() throws Exception {
super.testCreateTwoNodesWithRunScript();
}
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
super.testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired();
}
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception {
super.testGet();
}
@Test(enabled = true, dependsOnMethods = "testGet")
public void testOptionToNotBlock() throws Exception {
super.testOptionToNotBlock();
}
@Test(enabled = true, dependsOnMethods = "testGet")
public void testReboot() throws Exception {
super.testReboot();
}
@Test(enabled = true, dependsOnMethods = { "testImagesCache" })
public void testTemplateMatch() throws Exception {
super.testTemplateMatch();
}
}

View File

@ -19,6 +19,11 @@
package org.jclouds.rest;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.toArray;
import static org.jclouds.util.Utils.initContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import static org.jclouds.util.Utils.resolveContextBuilderClass;
import static org.jclouds.util.Utils.resolvePropertiesBuilderClass;
import java.io.IOException;
import java.util.Properties;
@ -27,11 +32,9 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import org.jclouds.PropertiesBuilder;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
/**
@ -239,8 +242,8 @@ public class RestContextFactory {
Class<S> sync;
Class<A> async;
try {
contextBuilderClass = Utils.resolveContextBuilderClass(providerName, props);
propertiesBuilderClass = Utils.resolvePropertiesBuilderClass(providerName, props);
contextBuilderClass = resolveContextBuilderClass(providerName, props);
propertiesBuilderClass = resolvePropertiesBuilderClass(providerName, props);
sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null);
async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null);
} catch (Exception e) {
@ -271,7 +274,6 @@ public class RestContextFactory {
public static <S, A> RestContextBuilder<S, A> createContextBuilder(
ContextSpec<S, A> contextSpec, Iterable<? extends Module> modules, Properties overrides) {
try {
PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor(
Properties.class).newInstance(overrides);
@ -283,69 +285,70 @@ public class RestContextFactory {
if (contextSpec.endpoint != null)
builder.endpoint(contextSpec.endpoint);
RestContextBuilder<S, A> contextBuilder = Utils.initContextBuilder(
RestContextBuilder<S, A> contextBuilder = initContextBuilder(
contextSpec.contextBuilderClass, contextSpec.sync, contextSpec.async, builder
.build());
contextBuilder.withModules(Iterables.toArray(modules, Module.class));
contextBuilder.withModules(toArray(modules, Module.class));
return contextBuilder;
} catch (Exception e) {
AuthorizationException aex = Utils
.getFirstThrowableOfType(e, AuthorizationException.class);
if (aex != null)
throw aex;
Throwables.propagate(e);
assert false : "exception should have propogated " + e;
return null;
return propagateAuthorizationOrOriginalException(e);
}
}
/**
* @see RestContextFactory#createContextBuilder(String, String, String)
*/
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, String identity,
String credential) {
return (RestContext<S, A>) createContextBuilder(provider, identity, credential)
.buildContext();
public <S, A> RestContext<S, A> createContext(String provider, String identity, String credential) {
RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential);
return buildContextUnwrappingExceptions(builder);
}
public static <S, A> RestContext<S, A> buildContextUnwrappingExceptions(
RestContextBuilder<S, A> builder) {
try {
return builder.buildContext();
} catch (Exception e) {
return propagateAuthorizationOrOriginalException(e);
}
}
/**
* @see RestContextFactory#createContextBuilder(String, Properties)
*/
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(provider, overrides).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(provider, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, Iterable)
*/
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider,
Iterable<? extends Module> modules, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(provider, modules, overrides).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(provider, modules, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable)
*/
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) {
return (RestContext<S, A>) createContextBuilder(provider, identity, credential, modules)
.buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential,
modules);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
*/
@SuppressWarnings("unchecked")
public <S, A> RestContext<S, A> createContext(String providerName, @Nullable String identity,
public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
return (RestContext<S, A>) createContextBuilder(providerName, identity, credential, modules,
overrides).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential,
modules, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
@ -353,14 +356,16 @@ public class RestContextFactory {
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) {
return createContextBuilder(contextSpec, modules, overrides).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec)
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec) {
return createContextBuilder(contextSpec).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec);
return buildContextUnwrappingExceptions(builder);
}
/**
@ -368,7 +373,8 @@ public class RestContextFactory {
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) {
return createContextBuilder(contextSpec, modules).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules);
return buildContextUnwrappingExceptions(builder);
}
/**
@ -376,6 +382,7 @@ public class RestContextFactory {
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Properties overrides) {
return createContextBuilder(contextSpec, overrides).buildContext();
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, overrides);
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -20,6 +20,14 @@ package org.jclouds.util;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.util.Patterns.CHAR_TO_PATTERN;
import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN;
@ -44,17 +52,16 @@ import javax.annotation.Resource;
import org.jclouds.PropertiesBuilder;
import org.jclouds.logging.Logger;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.RestContextBuilder;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
@ -69,6 +76,15 @@ import com.google.inject.spi.Message;
*/
public class Utils {
public static <T> T propagateAuthorizationOrOriginalException(Exception e) {
AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class);
if (aex != null)
throw aex;
Throwables.propagate(e);
assert false : "exception should have propogated " + e;
return null;
}
/**
* Like Ordering, but handle the case where there are multiple valid maximums
*/
@ -100,28 +116,31 @@ public class Utils {
}
public static Object propagateOrNull(Exception from) {
Throwables.propagate(from);
propagate(from);
assert false : "exception should have propogated";
return null;
}
@SuppressWarnings("unchecked")
public static <T extends Throwable> T getFirstThrowableOfType(Throwable from, Class<T> clazz) {
if (from instanceof ProvisionException)
return getFirstThrowableOfType(ProvisionException.class.cast(from), clazz);
try {
return (T) Iterables.find(Throwables.getCausalChain(from), Predicates.instanceOf(clazz));
return (T) find(getCausalChain(from), instanceOf(clazz));
} catch (NoSuchElementException e) {
return null;
}
}
public static Throwable firstRootCauseOrOriginalException(ProvisionException e) {
public static <T extends Throwable> T getFirstThrowableOfType(ProvisionException e,
Class<T> clazz) {
for (Message message : e.getErrorMessages()) {
Throwable cause = Throwables.getRootCause(message.getCause());
T cause = getFirstThrowableOfType(message.getCause(), clazz);
if (cause instanceof ProvisionException)
return firstRootCauseOrOriginalException(ProvisionException.class.cast(cause));
return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz);
return cause;
}
return e;
return null;
}
public static String replaceTokens(String value, Iterable<Entry<String, String>> tokenValues) {
@ -319,7 +338,7 @@ public class Utils {
@SuppressWarnings("unchecked")
public static Iterable<String> getSupportedProvidersOfTypeInProperties(
final Class<? extends RestContextBuilder> type, final Properties properties) {
return Iterables.filter(Iterables.transform(Iterables.filter(properties.entrySet(),
return filter(transform(filter(properties.entrySet(),
new Predicate<Map.Entry<Object, Object>>() {
@Override
@ -334,19 +353,19 @@ public class Utils {
public String apply(Entry<Object, Object> from) {
String keyString = from.getKey().toString();
try {
String provider = Iterables.get(Splitter.on('.').split(keyString), 0);
String provider = get(Splitter.on('.').split(keyString), 0);
Class<RestContextBuilder<Object, Object>> clazz = resolveContextBuilderClass(
provider, properties);
if (type.isAssignableFrom(clazz))
return provider;
} catch (ClassNotFoundException e) {
} catch (Exception e) {
Throwables.propagate(e);
propagate(e);
}
return null;
}
}), Predicates.notNull());
}), notNull());
}
@SuppressWarnings("unchecked")

View File

@ -45,7 +45,7 @@ public class UtilsTest {
AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
assertEquals(Utils.firstRootCauseOrOriginalException(pex), aex);
assertEquals(Utils.getFirstThrowableOfType(pex, AuthorizationException.class), aex);
}
public void testGetFirstThrowableOfTypeOuter() {

View File

@ -0,0 +1,47 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.gogrid.functions;
import static org.jclouds.util.Utils.propagateOrNull;
import java.util.Set;
import javax.inject.Singleton;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ReturnEmptySetOnNotFound implements Function<Exception, Set<Server>> {
@SuppressWarnings("unchecked")
public Set<Server> apply(Exception from) {
if (from instanceof ResourceNotFoundException) {
return ImmutableSet.<Server> of();
}
return Set.class.cast(propagateOrNull(from));
}
}