View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package org.miloss.fgsms.tomcat.jdbcp;
7   
8   import java.util.Hashtable;
9   
10  import javax.naming.Context;
11  import javax.naming.Name;
12  import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
13  
14  import org.miloss.fgsms.common.AES;
15  
16  /**
17   * Automagically decrypts passwords for JDBC connections
18   * @author alex.oree
19   */
20  public class EncryptedConnectionFactory extends org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory {
21  
22      @Override
23      public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
24          Object o = super.getObjectInstance(obj, name, nameCtx, environment);
25          if (o != null) {
26              BasicDataSource ds = (BasicDataSource) o;
27              if (ds.getPassword() != null && ds.getPassword().length() > 0) {
28                  ds.setPassword(AES.DE(ds.getPassword()));
29              }
30              return ds;
31          } else {
32              return null;
33          }
34      }
35  }