1
|
package eu.dnetlib.utils;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Properties;
|
5
|
import java.util.regex.Matcher;
|
6
|
import java.util.regex.Pattern;
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
|
10
|
import eu.dnetlib.domain.ServiceIdentity;
|
11
|
|
12
|
public class ServiceIdentityFactory {
|
13
|
private static Logger logger = Logger.getLogger(ServiceIdentityFactory.class);
|
14
|
|
15
|
/**
|
16
|
* Reads a properties file with the name <serviceName>.properties from the
|
17
|
* classpath and returns a ServiceIdentity object that describes a service's
|
18
|
* version.
|
19
|
* @param serviceName the serviceName
|
20
|
* @return the service version.
|
21
|
*/
|
22
|
public static ServiceIdentity createIdentity(String serviceName) {
|
23
|
try {
|
24
|
Properties properties = new Properties();
|
25
|
|
26
|
properties.load(ServiceIdentityFactory.class.getClassLoader()
|
27
|
.getResourceAsStream(serviceName + ".properties"));
|
28
|
|
29
|
String name = properties.getProperty("name");
|
30
|
String label = properties.getProperty("label");
|
31
|
String version[] = properties.getProperty("version").split("\\.");
|
32
|
String major = version.length > 0?version[0]:null;
|
33
|
String minor = version.length > 1?version[1]:null;
|
34
|
String micro = version.length > 2?version[2]:null;
|
35
|
|
36
|
return new ServiceIdentity(name, major, minor, micro, label);
|
37
|
} catch (IOException e) {
|
38
|
logger.error("Error creating service identity", e);
|
39
|
|
40
|
return null;
|
41
|
}
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Parse a service version. Version syntax:
|
46
|
* ${NAME}-${MAJOR}.${MINOR}.${MICRO}[-${LABEL}]
|
47
|
* @param identity The string representation
|
48
|
* @return the ServiceIdentity object
|
49
|
*/
|
50
|
public static ServiceIdentity parseIdentity(String identity) {
|
51
|
Pattern p = Pattern.compile("(.+)-(\\d)\\.(\\d)\\.(\\d)(-(.*))?");
|
52
|
Matcher m = p.matcher(identity);
|
53
|
|
54
|
if (m.find()) {
|
55
|
String name = m.group(1);
|
56
|
String major = m.group(2);
|
57
|
String minor = m.group(3);
|
58
|
String micro = m.group(4);
|
59
|
String label = m.group(6);
|
60
|
|
61
|
return new ServiceIdentity(name, major, minor, micro, label);
|
62
|
} else {
|
63
|
return null;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
}
|