794e2b486d5e82dddf06cc8b5b5f26726be71b5e
[clamp.git] / src / main / java / org / onap / clamp / clds / util / ClampTimer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23 package org.onap.clamp.clds.util;
24
25 import java.util.Timer;
26 import java.util.TimerTask;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import org.springframework.security.core.context.SecurityContextHolder;
31
32 /**
33  * Define the ClampTimer and CleanupTask, to clear up the Spring Authenticataion info when time is up.
34  */
35
36 public class ClampTimer {
37     protected static final EELFLogger logger          = EELFManager.getInstance().getLogger(ClampTimer.class);
38     Timer timer;
39
40     public ClampTimer(int seconds) {
41         timer = new Timer();
42         timer.schedule(new CleanupTask(), seconds*1000);
43     }
44
45     class CleanupTask extends TimerTask {
46         public void run() {
47             logger.debug("Time is up, clear the Spring authenticataion settings");
48             //Clear up the spring authentication
49             SecurityContextHolder.getContext().setAuthentication(null);
50             //Terminate the timer thread
51             timer.cancel(); 
52         }
53     }
54 }