~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/authentication/src/main/java/com/eucalyptus/auth/policy/key/KeepAlive.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.auth.policy.key;
 
2
 
 
3
import java.util.Date;
 
4
import org.apache.log4j.Logger;
 
5
import net.sf.json.JSONException;
 
6
import com.eucalyptus.auth.Contract;
 
7
import com.eucalyptus.auth.policy.PolicySpec;
 
8
import com.eucalyptus.auth.policy.condition.ConditionOp;
 
9
import com.eucalyptus.auth.policy.condition.NumericEquals;
 
10
 
 
11
@PolicyKey( Keys.EC2_KEEPALIVE )
 
12
public class KeepAlive extends ContractKey<Date> {
 
13
  
 
14
  private static final Logger LOG = Logger.getLogger( KeepAlive.class );
 
15
  
 
16
  private static final String KEY = Keys.EC2_KEEPALIVE;
 
17
  
 
18
  private static final String ACTION_RUNINSTANCES = PolicySpec.VENDOR_EC2 + ":" + PolicySpec.EC2_RUNINSTANCES;
 
19
  
 
20
  private static final long YEAR = 1000 * 60 * 60 * 24 * 365; // one year by default
 
21
  private static final long MINUTE = 1000 * 60; // minute
 
22
  
 
23
  @Override
 
24
  public void validateConditionType( Class<? extends ConditionOp> conditionClass ) throws JSONException {
 
25
    if ( NumericEquals.class != conditionClass ) {
 
26
      throw new JSONException( KEY + " is not allowed in condition " + conditionClass.getName( ) + ". NumericEquals is required." );
 
27
    }
 
28
  }
 
29
 
 
30
  @Override
 
31
  public void validateValueType( String value ) throws JSONException {
 
32
    KeyUtils.validateIntegerValue( value, KEY );
 
33
  }
 
34
 
 
35
  @Override
 
36
  public Contract<Date> getContract( final String[] values ) {
 
37
    return new Contract<Date>( ) {
 
38
      @Override
 
39
      public Contract.Type getType( ) {
 
40
        return Contract.Type.EXPIRATION;
 
41
      }
 
42
      @Override
 
43
      public Date getValue( ) {
 
44
        return getExpiration( values[0] );
 
45
      }
 
46
    };
 
47
  }
 
48
 
 
49
  @Override
 
50
  public boolean canApply( String action, String resourceType ) {
 
51
    return ACTION_RUNINSTANCES.equals( action );
 
52
  }
 
53
 
 
54
  @Override
 
55
  public boolean isBetter( Contract<Date> current, Contract<Date> update ) {
 
56
    return update.getValue( ).after( current.getValue( ) );
 
57
  }
 
58
  
 
59
  /**
 
60
   * @param keepAlive In minute.
 
61
   * @return
 
62
   */
 
63
  private static Date getExpiration( String keepAlive ) {
 
64
    try {
 
65
      return new Date( System.currentTimeMillis( ) + Long.valueOf( keepAlive ) * MINUTE );
 
66
    } catch ( Exception e ) {
 
67
      LOG.debug( e, e );
 
68
      return new Date( System.currentTimeMillis( ) + YEAR );
 
69
    }
 
70
  }
 
71
  
 
72
}