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 | |
---|
8 | import org.slf4j.Logger; |
---|
9 | import org.slf4j.LoggerFactory; |
---|
10 | |
---|
11 | /** |
---|
12 | * Stores some information needed for establishing connection to resolver server |
---|
13 | * |
---|
14 | * @author Thomas Eckart |
---|
15 | * @author Twan Goosen |
---|
16 | * |
---|
17 | */ |
---|
18 | public class Configuration { |
---|
19 | private final static Logger LOG = LoggerFactory.getLogger(Configuration.class); |
---|
20 | |
---|
21 | private String serviceBaseURL; |
---|
22 | private String handlePrefix; |
---|
23 | private String user; |
---|
24 | private String password; |
---|
25 | |
---|
26 | /** |
---|
27 | * Creates a configuration from the <em>config.properties</em> file. Expecting the following properties in the file: |
---|
28 | * <ul> |
---|
29 | * <li>SERVICE_BASE_URL</li> |
---|
30 | * <li>HANDLE_PREFIX</li> |
---|
31 | * <li>USER</li> |
---|
32 | * <li>PASSWORD</li> |
---|
33 | * </ul> |
---|
34 | * |
---|
35 | * A missing property will result in a runtime exception |
---|
36 | * |
---|
37 | * @throws IOException |
---|
38 | * 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 properties: |
---|
54 | * <ul> |
---|
55 | * <li>SERVICE_BASE_URL</li> |
---|
56 | * <li>HANDLE_PREFIX</li> |
---|
57 | * <li>USER</li> |
---|
58 | * <li>PASSWORD</li> |
---|
59 | * </ul> |
---|
60 | * A missing property will result in a runtime exception |
---|
61 | * |
---|
62 | * |
---|
63 | * @param properties |
---|
64 | */ |
---|
65 | public Configuration(Properties properties) { |
---|
66 | this(getRequiredProperty(properties, "SERVICE_BASE_URL"), getRequiredProperty(properties, "HANDLE_PREFIX"), |
---|
67 | getRequiredProperty(properties, "USER"), getRequiredProperty(properties, "PASSWORD")); |
---|
68 | } |
---|
69 | |
---|
70 | private static String getRequiredProperty(Properties properties, String name) { |
---|
71 | final String value = properties.getProperty(name); |
---|
72 | if (value == null) { |
---|
73 | throw new RuntimeException("Required property " + name + " missing!"); |
---|
74 | } |
---|
75 | LOG.debug("Read PID client configuration parameter {}: '{}'", name, value); |
---|
76 | return value; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * |
---|
81 | * @param serviceBaseURL |
---|
82 | * @param handlePrefix |
---|
83 | * @param user |
---|
84 | * @param password |
---|
85 | */ |
---|
86 | public Configuration(final String serviceBaseURL, final String handlePrefix, final String user, |
---|
87 | final String password) { |
---|
88 | this.serviceBaseURL = serviceBaseURL; |
---|
89 | this.handlePrefix = handlePrefix; |
---|
90 | this.user = user; |
---|
91 | this.password = password; |
---|
92 | } |
---|
93 | |
---|
94 | public void setServiceBaseURL(final String serviceBaseURL) { |
---|
95 | this.serviceBaseURL = serviceBaseURL; |
---|
96 | } |
---|
97 | |
---|
98 | public void setHandlePrefix(final String handlePrefix) { |
---|
99 | this.handlePrefix = handlePrefix; |
---|
100 | } |
---|
101 | |
---|
102 | public void setUser(final String user) { |
---|
103 | this.user = user; |
---|
104 | } |
---|
105 | |
---|
106 | public void setPassword(final String password) { |
---|
107 | this.password = password; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * @return serviceBaseURL (e.g. http://handle.gwdg.de:8080/pidservice/) |
---|
112 | */ |
---|
113 | public String getServiceBaseURL() { |
---|
114 | return serviceBaseURL; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * @return handle prefix (e.g. 11022) |
---|
119 | */ |
---|
120 | public String getHandlePrefix() { |
---|
121 | return handlePrefix; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * @return resolver account name |
---|
126 | */ |
---|
127 | public String getUser() { |
---|
128 | return user; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * |
---|
133 | * @return resolver password |
---|
134 | */ |
---|
135 | public String getPassword() { |
---|
136 | return password; |
---|
137 | } |
---|
138 | } |
---|