1 | package de.uni_leipzig.asv.clarin.webservices.pidservices2; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.IOException; |
---|
6 | import java.util.Properties; |
---|
7 | import org.slf4j.Logger; |
---|
8 | import org.slf4j.LoggerFactory; |
---|
9 | |
---|
10 | /** |
---|
11 | * Stores some information needed for establishing connection to resolver server |
---|
12 | * |
---|
13 | * @author Thomas Eckart |
---|
14 | * @author Twan Goosen |
---|
15 | * |
---|
16 | */ |
---|
17 | public class Configuration { |
---|
18 | |
---|
19 | private final static Logger LOG = LoggerFactory.getLogger(Configuration.class); |
---|
20 | |
---|
21 | private final String serviceBaseURL; |
---|
22 | private final String handlePrefix; |
---|
23 | private final String user; |
---|
24 | private final String password; |
---|
25 | |
---|
26 | /** |
---|
27 | * Creates a configuration from the <em>config.properties</em> |
---|
28 | * file. Expecting the following properties in the file: |
---|
29 | * <ul> |
---|
30 | * <li>SERVICE_BASE_URL</li> |
---|
31 | * <li>HANDLE_PREFIX</li> |
---|
32 | * <li>USER</li> |
---|
33 | * <li>PASSWORD</li> |
---|
34 | * </ul> |
---|
35 | * |
---|
36 | * A missing property will result in a runtime exception |
---|
37 | * |
---|
38 | * @throws IOException if the properties file could not be read |
---|
39 | */ |
---|
40 | public Configuration() throws IOException { |
---|
41 | this(readProperties("config.properties")); |
---|
42 | } |
---|
43 | |
---|
44 | public static Properties readProperties(String file) throws IOException { |
---|
45 | final Properties properties = new Properties(); |
---|
46 | try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) { |
---|
47 | properties.load(stream); |
---|
48 | } |
---|
49 | return properties; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Creates a configuration from properties. Expecting the following |
---|
54 | * properties: |
---|
55 | * <ul> |
---|
56 | * <li>SERVICE_BASE_URL</li> |
---|
57 | * <li>HANDLE_PREFIX</li> |
---|
58 | * <li>USER</li> |
---|
59 | * <li>PASSWORD</li> |
---|
60 | * </ul> |
---|
61 | * A missing property will result in a runtime exception |
---|
62 | * |
---|
63 | * |
---|
64 | * @param properties |
---|
65 | */ |
---|
66 | public Configuration(Properties properties) { |
---|
67 | this(getRequiredProperty(properties, "SERVICE_BASE_URL"), |
---|
68 | getRequiredProperty(properties, "HANDLE_PREFIX"), |
---|
69 | getRequiredProperty(properties, "USER"), |
---|
70 | getRequiredProperty(properties, "PASSWORD")); |
---|
71 | } |
---|
72 | |
---|
73 | private static String getRequiredProperty(Properties properties, String name) { |
---|
74 | final String value = properties.getProperty(name); |
---|
75 | if (value == null) { |
---|
76 | throw new RuntimeException("Required property " + name + " missing!"); |
---|
77 | } |
---|
78 | LOG.debug("Read PID client configuration parameter {}: '{}'", name, value); |
---|
79 | return value; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * |
---|
84 | * @param serviceBaseURL |
---|
85 | * @param handlePrefix |
---|
86 | * @param user |
---|
87 | * @param password |
---|
88 | */ |
---|
89 | public Configuration(final String serviceBaseURL, final String handlePrefix, final String user, |
---|
90 | final String password) { |
---|
91 | this.serviceBaseURL = serviceBaseURL; |
---|
92 | this.handlePrefix = handlePrefix; |
---|
93 | this.user = user; |
---|
94 | this.password = password; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * @return serviceBaseURL (e.g. http://handle.gwdg.de:8080/pidservice/) |
---|
99 | */ |
---|
100 | public String getServiceBaseURL() { |
---|
101 | return serviceBaseURL; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @return handle prefix (e.g. 11022) |
---|
106 | */ |
---|
107 | public String getHandlePrefix() { |
---|
108 | return handlePrefix; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * @return resolver account name |
---|
113 | */ |
---|
114 | public String getUser() { |
---|
115 | return user; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * |
---|
120 | * @return resolver password |
---|
121 | */ |
---|
122 | public String getPassword() { |
---|
123 | return password; |
---|
124 | } |
---|
125 | } |
---|