View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10   *
11   * 
12   */
13  /*  ---------------------------------------------------------------------------
14   *  U.S. Government, Department of the Army
15   *  Army Materiel Command
16   *  Research Development Engineering Command
17   *  Communications Electronics Research Development and Engineering Center
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   * Provides a simple mechanism for loading configuration information from the
38   * fgsms Config Database, table: settings. Primarily used for agents that run on
39   * the server, SLA plugins and or federation jobs that run on the fgsms server.
40   *
41   * This is only usable on the FGSMS server.
42   *
43   * @author AO
44   */
45  public class DBSettingsLoader {
46  
47      /**
48       * Gets the General Settings from the config database for fgsms Aux Services
49       * and some agents
50       *
51       * if isShouldEncrypt is true, then the value is encrypted
52       *
53       * @param isPooled
54       * @return key.name = value * returns null if if an error occurs
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       * Gets the General Settings from the config database for fgsms Aux Services
93       * and some agents for items matching the specified key
94       *
95       * @param isPooled
96       * @return key.name = value returns null if if an error occurs
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      * Gets a specific General Settings from the config database for fgsms Aux
137      * Services and some agents for items matching the specified key and name
138      *
139      * @param isPooled
140      * @return key.name = value returns null if if an error occurs
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      * returns a default Buller username and an encrypted password or NULL if
180      * not defined Items are returned from the bueller table. As of RC6
181      *
182      * @param isPooled
183      * @param url
184      * @return
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             //data[0] = rs.getString("keycol");
203             //data[1] = rs.getString("valuecol");
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      * returns a username and an encrypted password for a specific url or NULL
229      * if not credentials are available. this is typically a lowest permissions
230      * account to ascertain status of http or jms urls if not defined. Items are
231      * returned from the bueller table. As of RC6
232      *
233      * 6.2 added a third parameter equal to the value of the authentication
234      * mechanism specified
235      *
236      * @param isPooled
237      * @param url
238      * @return
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             //get the global policy for data retension
295             /////////////////////////////////////////////
296             results = comm.executeQuery();
297             boolean foundPolicy = false;
298             if (results.next()) {
299                 ret.setPolicyRefreshRate(df.newDuration(results.getLong("PolicyRefreshRate")));
300                 //moved this to settings list
301 
302                 //response.getPolicy().setUDDIPublishRate(fac.newDuration(results.getLong("UDDIPublishRate")));
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                     //comm.setLong(3, 1024000);
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 }