ConnectionManager.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9. import com.cognos.CAM_AAA.authentication.INamespaceConfiguration;
  10. public class ConnectionManager
  11. {
  12. // A singleton instance of this class
  13. private final static ConnectionManager INSTANCE = new ConnectionManager();
  14. private ConnectionManager()
  15. {
  16. }
  17. /**
  18. * Returns the instance of the connection manager
  19. */
  20. public final static ConnectionManager get()
  21. {
  22. return INSTANCE;
  23. }
  24. public static String getSqlExceptionDetails(final SQLException e)
  25. {
  26. final StringBuffer buffer = new StringBuffer();
  27. for (SQLException se = e; null != se; se = se.getNextException())
  28. {
  29. buffer.append("SQL STATE: " + se.getSQLState());
  30. buffer.append("ERROR CODE: " + se.getErrorCode());
  31. buffer.append("MESSAGE: " + se.getMessage());
  32. buffer.append("\n");
  33. }
  34. return buffer.toString();
  35. }
  36. private AccountCache accountCache;
  37. private final ThreadLocal< Connection > connections = new ThreadLocal< Connection >()
  38. {
  39. @Override
  40. protected Connection initialValue()
  41. {
  42. return ConnectionManager.this.openConnection();
  43. }
  44. };
  45. private String connectionString;
  46. private GroupCache groupCache;
  47. private String password;
  48. private String username;
  49. private String singleSignon;
  50. public void init(final INamespaceConfiguration configuration) throws IOException, ClassNotFoundException
  51. {
  52. this.loadProperties(configuration);
  53. }
  54. private void createAccountCache()
  55. {
  56. this.accountCache = new AccountCache(this);
  57. }
  58. private void createGroupCache()
  59. {
  60. this.groupCache = new GroupCache(this);
  61. }
  62. public AccountCache getAccountCache()
  63. {
  64. if (null == this.accountCache)
  65. this.createAccountCache();
  66. return this.accountCache;
  67. }
  68. public Connection getConnection()
  69. {
  70. return this.connections.get();
  71. }
  72. public GroupCache getGroupCache()
  73. {
  74. if (null == this.groupCache)
  75. this.createGroupCache();
  76. return this.groupCache;
  77. }
  78. public boolean singleSignOnEnabled()
  79. {
  80. if (singleSignon != null && singleSignon.equalsIgnoreCase("true"))
  81. return true;
  82. else
  83. return false;
  84. }
  85. protected void loadDriver(final String driverClass) throws ClassNotFoundException
  86. {
  87. Class.forName(driverClass);
  88. }
  89. private void loadProperties(final INamespaceConfiguration configuration) throws IOException, ClassNotFoundException
  90. {
  91. final Properties props = new Properties();
  92. final String installLocation = configuration.getInstallLocation();
  93. final File file =
  94. new File(installLocation + File.separator + "configuration" + File.separator + "JDBC_Config_"
  95. + configuration.getID() + ".properties");
  96. if (!file.exists()) {
  97. throw new FileNotFoundException(file.toString());
  98. }
  99. props.load(new FileInputStream(file));
  100. this.connectionString = props.getProperty("connectionString");
  101. this.loadDriver(props.getProperty("driverClass"));
  102. this.username = props.getProperty("username");
  103. this.password = props.getProperty("password");
  104. this.singleSignon = props.getProperty("singleSignon");
  105. }
  106. private Connection openConnection()
  107. {
  108. try
  109. {
  110. return DriverManager.getConnection(this.connectionString, this.username, this.password);
  111. }
  112. catch (final SQLException ex)
  113. {
  114. throw new RuntimeException("DriverManager.getConnection()", ex);
  115. }
  116. }
  117. }