EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.util]

COVERAGE SUMMARY FOR SOURCE FILE [Throwables2.java]

nameclass, %method, %block, %line, %
Throwables2.java100% (1/1)57%  (4/7)68%  (130/190)61%  (19.4/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Throwables2100% (1/1)57%  (4/7)68%  (130/190)61%  (19.4/32)
Throwables2 (): void 0%   (0/1)0%   (0/3)0%   (0/1)
propagateAuthorizationOrOriginalException (Exception): Object 0%   (0/1)0%   (0/28)0%   (0/6)
propagateOrNull (Exception): Object 0%   (0/1)0%   (0/12)0%   (0/3)
<static initializer> 100% (1/1)75%  (6/8)75%  (0.8/1)
getFirstThrowableOfType (ProvisionException, Class): Throwable 100% (1/1)79%  (27/34)86%  (6/7)
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause (Class [], Except... 100% (1/1)91%  (77/85)79%  (7.9/10)
getFirstThrowableOfType (Throwable, Class): Throwable 100% (1/1)100% (20/20)100% (5/5)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.util;
20 
21import static com.google.common.base.Predicates.instanceOf;
22import static com.google.common.base.Throwables.getCausalChain;
23import static com.google.common.base.Throwables.propagate;
24import static com.google.common.collect.Iterables.find;
25 
26import java.util.NoSuchElementException;
27 
28import org.jclouds.http.HttpResponseException;
29import org.jclouds.rest.AuthorizationException;
30import org.jclouds.rest.InsufficientResourcesException;
31import org.jclouds.rest.ResourceNotFoundException;
32 
33import com.google.common.base.Throwables;
34import com.google.inject.ProvisionException;
35import com.google.inject.spi.Message;
36 
37/**
38 * General utilities used in jclouds code.
39 * 
40 * @author Adrian Cole
41 */
42public class Throwables2 {
43 
44   @SuppressWarnings("unchecked")
45   public static <T extends Throwable> T getFirstThrowableOfType(Throwable from, Class<T> clazz) {
46      if (from instanceof ProvisionException)
47         return getFirstThrowableOfType(ProvisionException.class.cast(from), clazz);
48      try {
49         return (T) find(getCausalChain(from), instanceOf(clazz));
50      } catch (NoSuchElementException e) {
51         return null;
52      }
53   }
54 
55   public static <T extends Throwable> T getFirstThrowableOfType(ProvisionException e, Class<T> clazz) {
56      for (Message message : e.getErrorMessages()) {
57         if (message.getCause() != null) {
58            T cause = getFirstThrowableOfType(message.getCause(), clazz);
59            if (cause instanceof ProvisionException)
60               return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz);
61            return cause;
62         }
63      }
64      return null;
65   }
66 
67   public static <T> T propagateOrNull(Exception from) {
68      propagate(from);
69      assert false : "exception should have propogated";
70      return null;
71   }
72 
73   // Note this needs to be kept up-to-date with all top-level exceptions jclouds works against
74   @SuppressWarnings( { "unchecked", "rawtypes" })
75   public static Exception returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(Class[] exceptionTypes,
76            Exception exception) throws Exception {
77      for (Class type : exceptionTypes) {
78         Throwable throwable = getFirstThrowableOfType(exception, type);
79         if (throwable != null) {
80            return (Exception) throwable;
81         }
82      }
83      for (Class<Exception> propagatableExceptionType : new Class[] { IllegalStateException.class,
84               UnsupportedOperationException.class, IllegalArgumentException.class, AuthorizationException.class,
85               ResourceNotFoundException.class, InsufficientResourcesException.class, HttpResponseException.class }) {
86         Throwable throwable = getFirstThrowableOfType(exception, propagatableExceptionType);
87         if (throwable != null) {
88            throw (Exception) throwable;
89         }
90      }
91      Throwables.throwCause(exception, true);
92      return exception;
93   }
94 
95   public static <T> T propagateAuthorizationOrOriginalException(Exception e) {
96      AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class);
97      if (aex != null)
98         throw aex;
99      propagate(e);
100      assert false : "exception should have propogated " + e;
101      return null;
102   }
103 
104}

[all classes][org.jclouds.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov