Project

General

Profile

1
package eu.dnetlib.services;
2

    
3
import java.util.HashMap;
4
import java.util.Map;
5

    
6
import javax.annotation.PostConstruct;
7
import javax.servlet.http.HttpServletRequest;
8

    
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.context.Lifecycle;
13
import org.springframework.web.bind.annotation.RequestMapping;
14

    
15
import eu.dnetlib.enabling.annotations.DnetService;
16
import eu.dnetlib.enabling.annotations.DnetServiceType;
17
import eu.dnetlib.exceptions.DnetGenericException;
18

    
19
public abstract class BaseService implements Lifecycle {
20

    
21
	private static final Log log = LogFactory.getLog(BaseService.class);
22

    
23
	private boolean started = false;
24

    
25
	private String profileId;
26

    
27
	private Map<String, String> serviceProperties = new HashMap<>();
28

    
29
	private Map<String, String> extraProtocols = new HashMap<>();
30

    
31
	private DnetServiceType serviceType;
32

    
33
	@PostConstruct
34
	public void init() throws DnetGenericException {
35
		if (getClass().isAnnotationPresent(DnetService.class)) {
36
			serviceType = getClass().getAnnotation(DnetService.class).value();
37
		} else {
38
			throw new DnetGenericException("A required annotation is missing (@DnetService) in class " + getClass());
39
		}
40
	}
41

    
42
	@Override
43
	public void start() {
44
		log.info("Starting service " + serviceType);
45
		if (started) {
46
			log.warn("Service " + this + "already started, check bean initializations!");
47
		}
48
		started = true;
49
	}
50

    
51
	@Override
52
	public boolean isRunning() {
53
		log.debug("called isRunning " + this);
54
		return false;
55
	}
56

    
57
	@Override
58
	public void stop() {
59
		log.info("Stopping service " + this);
60
	}
61

    
62
	@RequestMapping("identify")
63
	public ServiceRunningInstance identify(final HttpServletRequest req) {
64
		return new ServiceRunningInstance(getProfileId(), StringUtils.substringBeforeLast(req.getRequestURI(), "/identify"));
65
	}
66

    
67
	public String getProfileId() {
68
		return profileId;
69
	}
70

    
71
	public void setProfileId(final String profileId) {
72
		this.profileId = profileId;
73
	}
74

    
75
	public Map<String, String> getServiceProperties() {
76
		return serviceProperties;
77
	}
78

    
79
	public void setServiceProperties(final Map<String, String> serviceProperties) {
80
		this.serviceProperties = serviceProperties;
81
	}
82

    
83
	public Map<String, String> getExtraProtocols() {
84
		return extraProtocols;
85
	}
86

    
87
	public void setExtraProtocols(final Map<String, String> extraProtocols) {
88
		this.extraProtocols = extraProtocols;
89
	}
90

    
91
	public DnetServiceType getServiceType() {
92
		return serviceType;
93
	}
94

    
95
	public void setServiceType(final DnetServiceType serviceType) {
96
		this.serviceType = serviceType;
97
	}
98
}
(1-1/2)