View Javadoc
1   package edu.internet2.middleware.grouper.misc;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import edu.internet2.middleware.grouper.internal.util.GrouperUuid;
7   import edu.internet2.middleware.grouper.util.GrouperUtil;
8   import edu.internet2.middleware.grouperClient.jdbc.GcDbAccess;
9   
10  public class GrouperFailsafe {
11  
12    public static void main(String[] args) {
13  
14    }
15  
16    /**
17     * see if this failsafe is approved
18     * @param name
19     * @return true if this failsafe is approved
20     */
21    public static boolean isApproved(String name) {
22      
23      int count = new GcDbAccess().sql("select count(1) from grouper_failsafe where name = ? and (approved_once = 'T' or approved_until >= ?)")
24          .addBindVar(name).addBindVar(System.currentTimeMillis()).select(int.class);
25      
26      return count > 0;
27    }
28    
29    /**
30     * see if there is a failsafe issue last run (might be approved)
31     * @param name
32     * @return true if this failsafe is approved
33     */
34    public static boolean isFailsafeIssue(String name) {
35      
36      int count = new GcDbAccess().sql("select count(1) from grouper_failsafe where name = ? "
37          + "and ((last_failsafe_issue is not null and last_success is null) "
38          + "or (last_failsafe_issue is not null and last_success is not null and last_failsafe_issue >= last_success ))")
39          .addBindVar(name).select(int.class);
40      
41      return count > 0;
42    }
43    
44    /**
45     * make sure a row exists for this job name
46     * @param name
47     */
48    public static void insertRow(String name) {
49      
50      // try multiple times in case another process adds the row for the name
51      GrouperUtil.tryMultipleTimes(5, new Runnable() {
52  
53        @Override
54        public void run() {
55          
56          int count = new GcDbAccess().sql("select count(1) from grouper_failsafe where name = ?")
57              .addBindVar(name).select(int.class);
58          if (count == 0) {
59            new GcDbAccess().sql("insert into grouper_failsafe (id, name, approved_once, last_updated) values (?, ?, ?, ?)")
60              .addBindVar(GrouperUuid.getUuid()).addBindVar(name).addBindVar("F").addBindVar(System.currentTimeMillis()).executeSql();
61          }
62        }
63        
64      });
65    }
66    
67    /**
68     * 
69     * @param name
70     */
71    public static void assignApproveNextRun(String name) {
72      insertRow(name);
73      long now = System.currentTimeMillis();
74      new GcDbAccess().sql("update grouper_failsafe set approved_once = ?, last_updated = ? where name = ?")
75        .addBindVar("T").addBindVar(now).addBindVar(name).executeSql();
76    }
77  
78    /**
79     * assign a success to this job
80     * @param jobName
81     */
82    public static void assignSuccess(String jobName) {
83      insertRow(jobName);
84      long now = System.currentTimeMillis();
85      new GcDbAccess().sql("update grouper_failsafe set approved_once = 'F', last_run = ?, last_failsafe_issue_started = null, last_failsafe_issue = null, "
86          + "last_success = ?, last_updated = ? where name = ?").addBindVar(now).addBindVar(now).addBindVar(now).addBindVar(jobName).executeSql();
87      
88    }
89  
90    /**
91     * remove failure from this job
92     * @param jobName
93     */
94    public static void removeFailure(String jobName) {
95      insertRow(jobName);
96      long now = System.currentTimeMillis();
97      new GcDbAccess().sql("update grouper_failsafe set approved_once = 'F', last_failsafe_issue_started = null, last_failsafe_issue = null, "
98          + "last_updated = ? where name = ?").addBindVar(now).addBindVar(jobName).executeSql();
99      
100   }
101 
102   /**
103    * assign an failure to this job
104    * @param jobName
105    */
106   public static void assignFailed(String jobName) {
107     insertRow(jobName);
108     long now = System.currentTimeMillis();
109     new GcDbAccess().sql("update grouper_failsafe set last_failsafe_issue_started = ?, last_updated = ? where name = ? and last_failsafe_issue_started is null")
110       .addBindVar(now).addBindVar(now).addBindVar(jobName).executeSql();
111     new GcDbAccess().sql("update grouper_failsafe set last_run = ?, last_failsafe_issue = ?, "
112         + "last_updated = ? where name = ?").addBindVar(now).addBindVar(now).addBindVar(now).addBindVar(jobName).executeSql();
113     
114   }
115 
116   /**
117    * get all job names where not approved and needs approval
118    * @return job names
119    */
120   public static Set<String> retrieveJobNamesNeedApprovalNotApproved() {
121     Set<String> jobNames = new HashSet<String>(new GcDbAccess().sql("select name from grouper_failsafe "
122         + " where (approved_once is null or approved_once != 'T') and (approved_until is null or approved_until < ?) "
123         + " and ((last_failsafe_issue is not null and last_success is null) "
124         + "or (last_failsafe_issue is not null and last_success is not null and last_failsafe_issue >= last_success )) ")
125         .addBindVar(System.currentTimeMillis()).selectList(String.class));
126     
127     return jobNames;
128   }
129   
130 }