EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.aws.ec2.predicates]

COVERAGE SUMMARY FOR SOURCE FILE [SpotInstanceRequestActive.java]

nameclass, %method, %block, %line, %
SpotInstanceRequestActive.java0%   (0/1)0%   (0/3)0%   (0/125)0%   (0/17)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SpotInstanceRequestActive0%   (0/1)0%   (0/3)0%   (0/125)0%   (0/17)
SpotInstanceRequestActive (AWSEC2Client): void 0%   (0/1)0%   (0/9)0%   (0/4)
apply (SpotInstanceRequest): boolean 0%   (0/1)0%   (0/100)0%   (0/12)
refresh (SpotInstanceRequest): SpotInstanceRequest 0%   (0/1)0%   (0/16)0%   (0/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.aws.ec2.predicates;
20 
21import java.util.NoSuchElementException;
22 
23import javax.annotation.Resource;
24import javax.inject.Singleton;
25 
26import org.jclouds.aws.ec2.AWSEC2Client;
27import org.jclouds.aws.ec2.domain.SpotInstanceRequest;
28import org.jclouds.logging.Logger;
29import org.jclouds.rest.ResourceNotFoundException;
30 
31import com.google.common.base.Predicate;
32import com.google.common.collect.Iterables;
33import com.google.inject.Inject;
34 
35/**
36 * 
37 * 
38 * @author Adrian Cole
39 */
40@Singleton
41public class SpotInstanceRequestActive implements Predicate<SpotInstanceRequest> {
42 
43   private final AWSEC2Client client;
44 
45   @Resource
46   protected Logger logger = Logger.NULL;
47 
48   @Inject
49   public SpotInstanceRequestActive(AWSEC2Client client) {
50      this.client = client;
51   }
52 
53   public boolean apply(SpotInstanceRequest spot) {
54      logger.trace("looking for state on spot %s", spot);
55      try {
56         spot = refresh(spot);
57         logger.trace("%s: looking for spot state %s: currently: %s", spot.getId(), SpotInstanceRequest.State.ACTIVE,
58                  spot.getState());
59         if (spot.getState() == SpotInstanceRequest.State.CANCELLED
60                  || spot.getState() == SpotInstanceRequest.State.CLOSED)
61            throw new IllegalStateException(String.format("spot request %s %s", spot.getId(), spot.getState()));
62         if (spot.getFaultCode() != null)
63            throw new IllegalStateException(String.format("spot request %s fault code(%s) message(%s)", spot.getId(),
64                     spot.getFaultCode(), spot.getFaultMessage()));
65         return spot.getState() == SpotInstanceRequest.State.ACTIVE;
66      } catch (ResourceNotFoundException e) {
67         return false;
68      } catch (NoSuchElementException e) {
69         return false;
70      }
71   }
72 
73   private SpotInstanceRequest refresh(SpotInstanceRequest spot) {
74      return Iterables.getOnlyElement(client.getSpotInstanceServices().describeSpotInstanceRequestsInRegion(
75               spot.getRegion(), spot.getId()));
76   }
77}

[all classes][org.jclouds.aws.ec2.predicates]
EMMA 2.0.5312 (C) Vladimir Roubtsov