Remove Tabs, per Jococo
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / register / Registrar.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.register;
23
24 import java.util.Deque;
25 import java.util.Iterator;
26 import java.util.Timer;
27 import java.util.TimerTask;
28 import java.util.concurrent.ConcurrentLinkedDeque;
29
30 import org.onap.aaf.cadi.client.Result;
31 import org.onap.aaf.misc.env.impl.BasicEnv;
32
33 public class Registrar<ENV extends BasicEnv> {
34     private static final String REGISTRAR = "Registrar";
35     private static final long INTERVAL = 15*60*1000L; // 15 mins
36     private static final long START = 3000; // Start in 3 seconds
37     private static final Object LOCK = new Object();
38     private Deque<Registrant<ENV>> registrants;
39     private Timer timer, erroringTimer;
40
41     public Registrar(final ENV env, boolean shutdownHook) {
42         registrants = new ConcurrentLinkedDeque<Registrant<ENV>>();
43
44         erroringTimer = null;
45         timer = new Timer(REGISTRAR,true);
46         timer.schedule(new RegistrationTimerTask(env), START, INTERVAL); 
47         
48         if (shutdownHook) {
49             Runtime.getRuntime().addShutdownHook(new Thread() {
50                 public void run() {
51                     close(env);
52                 }
53             });
54         }
55     }
56     
57     private class RegistrationTimerTask extends TimerTask {
58         private final ENV env;
59         public RegistrationTimerTask(ENV env) {
60             this.env = env;
61         }
62         @Override
63         public void run() {
64             for (Iterator<Registrant<ENV>> iter = registrants.iterator(); iter.hasNext();) {
65                 Registrant<ENV> reg = iter.next();
66                 Result<Void> rv = reg.update(env);
67                 synchronized(LOCK) {
68                     if (rv.isOK()) {
69                         if (erroringTimer!=null) {
70                             erroringTimer.cancel();
71                             erroringTimer = null;
72                         }
73                     } else {
74                         env.error().log(rv.toString());
75                         // Account for different Registrations not being to same place
76                         if (erroringTimer==null) {
77                             erroringTimer =  new Timer(REGISTRAR + " error re-check ",true);
78                             erroringTimer.schedule(new RegistrationTimerTask(env),20000,20000);
79                         }
80                     }
81                 }
82             }
83         }
84     }
85     
86     public void register(Registrant<ENV> r) {
87         registrants.addLast(r);
88     }
89     
90     public void deregister(Registrant<ENV> r) {
91         registrants.remove(r);
92     }
93
94     public void close(ENV env) {
95         timer.cancel();
96
97         Registrant<ENV> r;
98         while (registrants.peek()!=null) {
99             r = registrants.pop();
100             r.cancel(env);
101         }
102     }
103 }