AT&T 2.0.19 Code drop, stage 2
[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*1000; // 15 mins
36         private static final long START = 3000; // Start in 3 seconds
37         private Deque<Registrant<ENV>> registrants;
38         private Timer timer, erroringTimer;
39
40         public Registrar(final ENV env, boolean shutdownHook) {
41                 registrants = new ConcurrentLinkedDeque<Registrant<ENV>>();
42
43                 erroringTimer = null;
44                 timer = new Timer(REGISTRAR,true);
45                 timer.schedule(new RegistrationTimerTask(env), START, INTERVAL); 
46                 
47                 if(shutdownHook) {
48                         Runtime.getRuntime().addShutdownHook(new Thread() {
49                                 public void run() {
50                                         close(env);
51                                 }
52                         });
53                 }
54         }
55         
56         private class RegistrationTimerTask extends TimerTask {
57                 private final ENV env;
58                 public RegistrationTimerTask(ENV env) {
59                         this.env = env;
60                 }
61                 @Override
62                 public void run() {
63                         for(Iterator<Registrant<ENV>> iter = registrants.iterator(); iter.hasNext();) {
64                                 Registrant<ENV> reg = iter.next();
65                                 Result<Void> rv = reg.update(env);
66                                 synchronized(REGISTRAR) {
67                                         if(rv.isOK()) {
68                                                 if(erroringTimer!=null) {
69                                                         erroringTimer.cancel();
70                                                         erroringTimer = null;
71                                                 }
72                                         } else {
73                                                 // Account for different Registrations not being to same place
74                                                 if(erroringTimer==null) {
75                                                         erroringTimer =  new Timer(REGISTRAR + " error re-check ",true);
76                                                         erroringTimer.schedule(new RegistrationTimerTask(env),20000,20000);
77                                                 }
78                                         }
79                                 }
80                         }
81                 }
82         }
83         
84         public void register(Registrant<ENV> r) {
85                 registrants.addLast(r);
86         }
87         
88         public void deregister(Registrant<ENV> r) {
89                 registrants.remove(r);
90         }
91
92         public void close(ENV env) {
93                 timer.cancel();
94
95                 Registrant<ENV> r;
96                 while(registrants.peek()!=null) {
97                         r = registrants.pop();
98                         r.cancel(env);
99                 }
100         }
101 }