summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-music/src/main/java/org/onap/portalapp/music/conf/MusicSessionRepositoryHandler.java
blob: 1b0dff94a530d78d9d32079c1c9a5208b49312d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.onap.portalapp.music.conf;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.exceptions.MusicLockingException;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.portalapp.music.service.MusicService;
import org.onap.portalapp.music.util.MusicUtil;
import org.springframework.session.Session;

public class MusicSessionRepositoryHandler {
	
	private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicSessionRepositoryHandler.class);
	private final Map<String, Session> sessions = new ConcurrentHashMap<>();
	private boolean musicCache = MusicUtil.isCached();
	
	
	public Session get(String id) {
		if(!musicCache){
			try {
				Session session = MusicService.getMetaAttribute(id);
				return session;
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "get failed with id " + id, e);
			}
		}
		return this.sessions.get(id);
	}



	public void remove(String id) {
		 sessions.remove(id);
		 try {
			MusicService.removeSession(id);
		} catch (MusicLockingException e) {
			logger.error(EELFLoggerDelegate.errorLogger, "removeSession locking failed with id " + id, e);
		} catch (MusicServiceException e) {
			logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed with id " + id, e);
		}
	}



	public void put(String id, MusicSession musicSession) {
		sessions.put(id, musicSession);
		try {
			MusicService.setMetaAttribute(musicSession);
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "setMetaAttribute failed with id " + id, e);
		}
	}

}