1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.common;
21
22 import java.nio.charset.Charset;
23 import java.sql.*;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.xml.datatype.DatatypeFactory;
27 import org.miloss.fgsms.services.interfaces.policyconfiguration.KeyNameValue;
28 import org.miloss.fgsms.services.interfaces.policyconfiguration.KeyNameValueEnc;
29 import org.miloss.fgsms.services.interfaces.policyconfiguration.TransportAuthenticationStyle;
30 import org.apache.log4j.Level;
31
32 import org.miloss.fgsms.services.interfaces.common.SecurityWrapper;
33 import org.miloss.fgsms.services.interfaces.policyconfiguration.GlobalPolicy;
34 import us.gov.ic.ism.v2.ClassificationType;
35
36
37
38
39
40
41
42
43
44
45 public class DBSettingsLoader {
46
47
48
49
50
51
52
53
54
55
56 public static List<KeyNameValueEnc> GetPropertiesFromDB(boolean isPooled) {
57 Connection con = null;
58 PreparedStatement com = null;
59 ResultSet rs = null;
60 List<KeyNameValueEnc> items = new ArrayList<KeyNameValueEnc>();
61 try {
62 if (isPooled) {
63 con = Utility.getConfigurationDBConnection();
64 } else {
65 con = Utility.getConfigurationDB_NONPOOLED_Connection();
66 }
67 com = con.prepareStatement("select * from settings;");
68 rs = com.executeQuery();
69 while (rs.next()) {
70 KeyNameValueEnc k = new KeyNameValueEnc();
71 k.setShouldEncrypt(rs.getBoolean("isencrypted"));
72 KeyNameValue p = new KeyNameValue();
73 p.setPropertyKey(rs.getString("keycol"));
74 p.setPropertyName(rs.getString("namecol"));
75 p.setPropertyValue(new String(rs.getBytes("valuecol"), Charset.forName("UTF-8")));
76 k.setKeyNameValue(p);
77 items.add(k);
78 }
79
80 } catch (Exception ex) {
81 Logger.getLogger(Utility.logname).log(Level.ERROR, "problem loading settings from the database", ex);
82 } finally {
83 DBUtils.safeClose(rs);
84 DBUtils.safeClose(com);
85 DBUtils.safeClose(con);
86 }
87
88 return items;
89 }
90
91
92
93
94
95
96
97
98 public static List<KeyNameValueEnc> GetPropertiesFromDB(boolean isPooled, String keyfilter) {
99 Connection con = null;
100 PreparedStatement com = null;
101 ResultSet rs = null;
102 List<KeyNameValueEnc> items = new ArrayList<KeyNameValueEnc>();
103
104 try {
105
106 if (isPooled) {
107 con = Utility.getConfigurationDBConnection();
108 } else {
109 con = Utility.getConfigurationDB_NONPOOLED_Connection();
110 }
111 com = con.prepareStatement("select * from settings where keycol=?;");
112 com.setString(1, keyfilter);
113 rs = com.executeQuery();
114 while (rs.next()) {
115 KeyNameValueEnc k = new KeyNameValueEnc();
116 k.setShouldEncrypt(rs.getBoolean("isencrypted"));
117 KeyNameValue p = new KeyNameValue();
118 p.setPropertyKey(rs.getString("keycol"));
119 p.setPropertyName(rs.getString("namecol"));
120 p.setPropertyValue(new String(rs.getBytes("valuecol"), Constants.CHARSET));
121 k.setKeyNameValue(p);
122 items.add(k);
123 }
124
125 } catch (Exception ex) {
126 Logger.getLogger(Utility.logname).log(Level.ERROR, "problem loading " + keyfilter + " settings from the database", ex);
127 } finally {
128 DBUtils.safeClose(rs);
129 DBUtils.safeClose(com);
130 DBUtils.safeClose(con);
131 }
132 return items;
133 }
134
135
136
137
138
139
140
141
142 public static KeyNameValueEnc GetPropertiesFromDB(boolean isPooled, String keyfilter, String namefilter) {
143 Connection con = null;
144 PreparedStatement com = null;
145 ResultSet rs = null;
146 KeyNameValueEnc k = new KeyNameValueEnc();
147 try {
148
149 if (isPooled) {
150 con = Utility.getConfigurationDBConnection();
151 } else {
152 con = Utility.getConfigurationDB_NONPOOLED_Connection();
153 }
154 com = con.prepareStatement("select * from settings where keycol=? and namecol=?;");
155 com.setString(1, keyfilter);
156 com.setString(2, namefilter);
157 rs = com.executeQuery();
158 if (rs.next()) {
159 k.setShouldEncrypt(rs.getBoolean("isencrypted"));
160 KeyNameValue p = new KeyNameValue();
161 p.setPropertyKey(rs.getString("keycol"));
162 p.setPropertyName(rs.getString("namecol"));
163 p.setPropertyValue(new String(rs.getBytes("valuecol"), Constants.CHARSET));
164 k.setKeyNameValue(p);
165 }
166
167 } catch (Exception ex) {
168 Logger.getLogger(Utility.logname).log(Level.ERROR, "problem loading " + keyfilter + " " + namefilter + " settings from the database", ex);
169 } finally {
170 DBUtils.safeClose(rs);
171 DBUtils.safeClose(com);
172 DBUtils.safeClose(con);
173 }
174 return k;
175
176 }
177
178
179
180
181
182
183
184
185
186 public static String[] GetDefaultBuellerCredentials(boolean isPooled) {
187 Connection con = null;
188 PreparedStatement com = null;
189 ResultSet rs = null;
190 String[] data = new String[2];
191 try {
192
193 if (isPooled) {
194 con = Utility.getConfigurationDBConnection();
195 } else {
196 con = Utility.getConfigurationDB_NONPOOLED_Connection();
197 }
198
199 com = con.prepareStatement("select * from settings where keycol=?;");
200 com.setString(1, "Bueller");
201 rs = com.executeQuery();
202
203
204 while (rs.next()) {
205 if (rs.getString("namecol").equalsIgnoreCase("defaultUser")) {
206 data[0] = new String(rs.getBytes("valuecol"), Constants.CHARSET);
207 }
208 if (rs.getString("namecol").equalsIgnoreCase("defaultPassword")) {
209 data[1] = new String(rs.getBytes("valuecol"), Constants.CHARSET);
210 }
211 }
212
213 } catch (Exception ex) {
214 Logger.getLogger(Utility.logname).log(Level.ERROR, "problem loading default bueller credentials from the database", ex);
215 } finally {
216 DBUtils.safeClose(rs);
217 DBUtils.safeClose(com);
218 DBUtils.safeClose(con);
219 }
220
221 if (Utility.stringIsNullOrEmpty(data[0]) || Utility.stringIsNullOrEmpty(data[1])) {
222 return null;
223 }
224 return data;
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 public static String[] GetCredentials(boolean isPooled, final String url) {
241 Connection con = null;
242 PreparedStatement com = null;
243 ResultSet rs = null;
244 String[] data = null;
245 try {
246
247 if (isPooled) {
248 con = Utility.getConfigurationDBConnection();
249 } else {
250 con = Utility.getConfigurationDB_NONPOOLED_Connection();
251 }
252 com = con.prepareStatement("select * from bueller where uri=?;");
253 com.setString(1, url);
254 rs = com.executeQuery();
255 if (rs.next()) {
256 data = new String[3];
257 data[0] = rs.getString("username");
258 data[1] = new String(rs.getBytes("pwdcol"), Constants.CHARSET);
259 try {
260 int x = rs.getInt("authtype");
261 TransportAuthenticationStyle tas = TransportAuthenticationStyle.values()[x];
262 data[2] = tas.value();
263 } catch (Exception e) {
264 data[2] = TransportAuthenticationStyle.NA.value();
265 }
266 }
267 } catch (Exception ex) {
268 Logger.getLogger(Utility.logname).log(Level.ERROR, "problem loading credentials for " + url + " from the database", ex);
269 } finally {
270 DBUtils.safeClose(rs);
271 DBUtils.safeClose(com);
272 DBUtils.safeClose(con);
273 }
274 return data;
275 }
276
277 public static GlobalPolicy GetGlobalPolicy(boolean pooled) {
278
279 PreparedStatement comm = null;
280 ResultSet results = null;
281 GlobalPolicy ret = new GlobalPolicy();
282 Connection con;
283 if (pooled) {
284 con = Utility.getConfigurationDBConnection();
285 } else {
286 con = Utility.getConfigurationDB_NONPOOLED_Connection();
287 }
288 try {
289 DatatypeFactory df = DatatypeFactory.newInstance();
290
291 comm = con.prepareStatement("Select * from GlobalPolicies;");
292
293
294
295
296 results = comm.executeQuery();
297 boolean foundPolicy = false;
298 if (results.next()) {
299 ret.setPolicyRefreshRate(df.newDuration(results.getLong("PolicyRefreshRate")));
300
301
302
303 ret.setRecordedMessageCap(results.getInt("RecordedMessageCap"));
304 SecurityWrapper wrap = new SecurityWrapper(ClassificationType.fromValue(results.getString("classification")),
305 results.getString("caveat"));
306 ret.setClassification(wrap);
307 ret.setAgentsEnabled(results.getBoolean("agentsenable"));
308
309 foundPolicy = true;
310 }
311 results.close();
312 comm.close();
313
314 if (!foundPolicy) {
315 try {
316 comm = con.prepareStatement("INSERT INTO GlobalPolicies (PolicyRefreshRate, RecordedMessageCap, classification, agentsenable, caveat) "
317 + " VALUES (?, ?, ?, true, ?);");
318 comm.setLong(1, 30 * 60 * 100);
319 comm.setLong(2, 1024000);
320
321 comm.setString(3, "U");
322 comm.setString(4, "");
323 comm.execute();
324 ret.setRecordedMessageCap(1024000);
325 ret.setClassification(new SecurityWrapper(ClassificationType.U, "None"));
326 ret.setAgentsEnabled(true);
327 } catch (SQLException ex) {
328 Logger.getLogger(Utility.logname).log(Level.ERROR, "error setting global policy", ex);
329 }
330 }
331 comm.close();
332 con.close();
333
334 KeyNameValueEnc d = DBSettingsLoader.GetPropertiesFromDB(true, "UddiPublisher", "Interval");
335 if (d != null && d.getKeyNameValue() != null) {
336 try {
337 ret.setUDDIPublishRate(df.newDuration(Long.parseLong(d.getKeyNameValue().getPropertyValue())));
338 } catch (Exception ex) {
339 ret.setUDDIPublishRate(df.newDuration(30 * 60 * 100));
340 }
341 } else {
342 ret.setUDDIPublishRate(df.newDuration(30 * 60 * 100));
343 }
344
345 } catch (Exception ex) {
346 Logger.getLogger(Utility.logname).log(Level.ERROR, "error getting global policy", ex);
347 } finally {
348 DBUtils.safeClose(results);
349 DBUtils.safeClose(comm);
350 DBUtils.safeClose(con);
351 }
352 return ret;
353 }
354 }